Skip to content

docs: add custom object patch example - #4868

Open
YuqiGuo105 wants to merge 1 commit into
kubernetes-client:masterfrom
YuqiGuo105:docs/issue-3106-custom-object-patch
Open

docs: add custom object patch example#4868
YuqiGuo105 wants to merge 1 commit into
kubernetes-client:masterfrom
YuqiGuo105:docs/issue-3106-custom-object-patch

Conversation

@YuqiGuo105

Copy link
Copy Markdown
Contributor

Summary

  • add a runnable example for patching namespaced custom objects through PatchUtils
  • document why generated custom-object PATCH requests should not be executed directly
  • accept the JSON Patch document as a file instead of hard-coding a resource-specific operation
  • add HTTP-level tests for namespaced and cluster-scoped custom objects
  • cover both JSON Patch and JSON Merge Patch media types

Problem

The generated CustomObjectsApi PATCH request defaults to Content-Type: application/json. Kubernetes PATCH endpoints require a patch-specific media type such as:

  • application/json-patch+json
  • application/merge-patch+json

Calling the generated request's execute() method directly can therefore result in 415 Unsupported Media Type, even when the patch document itself is valid.

Solution

The new PatchCustomObjectExample builds the generated custom-object request with buildCall(null) and executes it through PatchUtils.patch(...):

PatchUtils.patch(
    Object.class,
    () ->
        api.patchNamespacedCustomObject(
                group,
                version,
                namespace,
                plural,
                name,
                new V1Patch(patchJson))
            .buildCall(null),
    V1Patch.PATCH_FORMAT_JSON_PATCH,
    client);

The example reads the patch document from a user-provided file, so it does not assume a particular custom-resource schema or that optional metadata fields already exist.

The generated CustomObjectsApi.java and generated API documentation are intentionally left unchanged because those files would be overwritten during OpenAPI regeneration.

Tests

PatchUtilsTest now verifies that custom-object PATCH requests:

  • use the namespaced custom-object URL
  • preserve the original JSON Patch and JSON Merge Patch bodies
  • send the requested patch-specific Content-Type
  • deserialize the returned custom object
  • use the correct cluster-scoped custom-object URL

Validation performed:

  • ./mvnw -pl util,examples/examples-release-latest -am -Dtest=PatchUtilsTest -Dsurefire.failIfNoSpecifiedTests=false test
  • ./mvnw spotless:check
  • ./mvnw -pl util,examples/examples-release-latest -am clean test

Closes #3106.

@kubernetes-prow kubernetes-prow Bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jul 29, 2026
@kubernetes-prow
kubernetes-prow Bot requested a review from brendandburns July 29, 2026 15:54
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: YuqiGuo105
Once this PR has been reviewed and has the lgtm label, please assign yue9944882 for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubernetes-prow
kubernetes-prow Bot requested a review from yue9944882 July 29, 2026 15:54
@kubernetes-prow kubernetes-prow Bot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 29, 2026
@YuqiGuo105
YuqiGuo105 marked this pull request as ready for review July 29, 2026 15:57
@kubernetes-prow kubernetes-prow Bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to patch custom object with CustomObjectsApi.patchNamespacedCustomObject

1 participant