A logo showing the text blog.marcnuri.com
Español
Home»Java»Patching Kubernetes Resources from Java with Fabric8 Kubernetes Client 7

Recent Posts

  • MCP Tool Annotations: Adding Metadata and Context to Your AI Tools
  • Fabric8 Kubernetes Client 7.2 is now available!
  • Connecting to an MCP Server from JavaScript using AI SDK
  • Connecting to an MCP Server from JavaScript using LangChain.js
  • The Future of Developer Tools: Adapting to Machine-Based Developers

Categories

  • Artificial Intelligence
  • Front-end
  • Go
  • Industry and business
  • Java
  • JavaScript
  • Legacy
  • Operations
  • Personal
  • Pet projects
  • Tools

Archives

  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • August 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • February 2020
  • January 2020
  • December 2019
  • October 2019
  • September 2019
  • July 2019
  • March 2019
  • November 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • December 2017
  • July 2017
  • January 2017
  • December 2015
  • November 2015
  • December 2014
  • March 2014
  • February 2011
  • November 2008
  • June 2008
  • May 2008
  • April 2008
  • January 2008
  • November 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007

Patching Kubernetes Resources from Java with Fabric8 Kubernetes Client 7

2025-02-12 in Java tagged Java / Kubernetes / Client / Fabric8 / Cloud / OpenShift by Marc Nuri | Last updated: 2025-02-12
Versión en Español

Introduction

When managing Kubernetes resources programmatically, you might need to update parts of an existing resource without replacing it entirely. Kubernetes controllers and operators often use patching to update resources in a more efficient and controlled way. With the Fabric8 Kubernetes Client 7, there are several patching strategies available that map directly to Kubernetes native patch mechanisms.

In this post, I'll show you the three main patch methods: Strategic Merge Patch, JSON Patch, and JSON Merge Patch, and code examples for each.

What are the different patch methods in Kubernetes?

Kubernetes provides three main patch methods to update resources:

Strategic Merge Patch

The Strategic Merge Patch is a Kubernetes-specific patch format that allows you to update specific fields in a resource. This patch method is designed to be more user-friendly and less error-prone than other patch formats. It's particularly useful for updating resources that have a well-defined schema, such as Custom Resource Definitions (CRDs).

JSON Patch (RFC 6902)

JSON Patch is a standard format defined in RFC 6902 that describes how to apply a list of operations to a JSON document. Each operation specifies a change to be applied to the target document.

The following snippet shows an HTTP transaction that applies a JSON Patch to an HTTP resource:

PATCH /api/data/my-resource HTTP/1.1
PATCH /api/data/my-resource HTTP/1.1
Host: example.com
Content-Length: 152
Content-Type: application/json-patch+json

[
  { "op": "add", "path": "/a/b/c", "value": "foo" },
  { "op": "remove", "path": "/a/b/c" },
  { "op": "replace", "path": "/a/b/c", "value": "bar" }
]

JSON Merge Patch (RFC 7396)

JSON Merge Patch is a simpler patch format that works like a "partial update" of a JSON document as defined in RFC 7396. You supply a JSON document with the fields to change. Setting a field to null can remove it.

The Fabric8 Kubernetes Client provides support for all these patch types, making it a versatile tool for Java developers working with Kubernetes and OpenShift.

Patching resources with Fabric8 Kubernetes Client 7

The Fabric8 Kubernetes Client 7 provides a straightforward way to patch resources using the three main patch methods in Kubernetes.

Strategic Merge Patch

By default, Fabric8 provides a simple method that accepts a raw JSON (or YAML) string for a strategic merge patch. For example, suppose you want to update the container image of a Deployment. You can write:

StrategicMergePatch.java
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;

public class StrategicMergePatch {
  public static void main(String[] args) {
    try (KubernetesClient client = new KubernetesClientBuilder().build()) {
      String patch = "{"
        + "  \"spec\": {"
        + "    \"template\": {"
        + "      \"spec\": {"
        + "        \"containers\": ["
        + "          {\"name\": \"my-container\", \"image\": \"nginx:latest\"}"
        + "        ]"
        + "      }"
        + "    }"
        + "  }"
        + "}";
      client.apps().deployments().inNamespace("default").withName("my-deployment")
        .patch(patch);
    }
  }
}

In this snippet the patch string updates the container's image in a Deployment. Because we use the plain patch(String) method, Fabric8 uses the strategic merge patch algorithm by default.

JSON Patch

If you require more granular control, for example, when you need to replace a field value using a precise path, you can use JSON Patch. With Fabric8, this is achieved by specifying a PatchContext with the patch type set to JSON.

Consider the following example that updates a container image:

JsonPatch.java
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.dsl.base.PatchContext;
import io.fabric8.kubernetes.client.dsl.base.PatchType;

public class JsonPatch {
  public static void main(String[] args) {
    try (KubernetesClient client = new KubernetesClientBuilder().build()) {
      String patch = "["
        + "{\"op\": \"replace\", \"path\": \"/spec/template/spec/containers/0/image\", \"value\": \"nginx:latest\"}"
        + "]";
      client.apps().deployments().inNamespace("default").withName("my-deployment")
        .patch(PatchContext.of(PatchType.JSON), patch);
    }
  }
}

The previous snippet illustrates a few key points:

  • The Fabric8 client provides a patch(PatchContext, String) method that accepts a PatchContext object and a JSON Patch string.
  • The PatchContext object specifies the patch type as JSON.
  • PatchContext.of(PatchType) is a convenient way to create a PatchContext object with the desired patch type.
  • Additionally, you can set options such as force, fieldManager, and fieldValidation in the PatchContext by using the PatchContext.Builder.
  • The JSON Patch string contains the operation to replace the container image in the Deployment as specified by RFC 6902.

JSON Merge Patch

For a simpler patch operation, you can use the JSON Merge Patch method. It is useful when you simply want to update or remove fields in a resource.

The following example demonstrates how to remove an annotation from a resource:

JsonMergePatch.java
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.dsl.base.PatchContext;
import io.fabric8.kubernetes.client.dsl.base.PatchType;

public class JsonMergePatch {
  public static void main(String[] args) {
    try (KubernetesClient client = new KubernetesClientBuilder().build()) {
      // In this patch, setting the "foo" annotation to null removes it.
      String jsonMergePatch = "{"
        + "\"metadata\": {"
        + "  \"annotations\": {"
        + "    \"foo\": null"
        + "  }"
        + "}"
        + "}";
      PatchContext patchContext = PatchContext.of(PatchType.JSON_MERGE);
      client.apps().deployments().inNamespace("default").withName("my-deployment")
        .patch(patchContext, jsonMergePatch);
    }
  }
}

In this example, the JSON Merge Patch string removes the foo annotation from the resource by setting it to null.

When to use each patch method

Each patch method has its own use cases and benefits:

  • Strategic Merge Patch: Use this method when you want to update specific fields in a standard Kubernetes resource. It intelligently merges lists and maps, without affecting other fields.
  • JSON Patch: Ideal for fine-grained operations when you need precise control over what to add, remove, or replace. It works well when you're comfortable specifying JSON pointer paths.
  • JSON Merge Patch: A simpler method for partial updates. It's useful when you want to update or remove fields without specifying the exact path.

Note

Remember that patching has some pitfalls. For example, updating immutable fields (such as certain selectors) will result in errors.

Always consult the Kubernetes API documentation and test your patch operations thoroughly.

Conclusion

Patching Kubernetes resources using the Fabric8 Kubernetes Client version 7 is a powerful way to manage updates programmatically. By leveraging Strategic Merge Patch, JSON Merge Patch, and JSON Patch, you can handle a variety of use cases with ease. Whether adding annotations, updating container images, or modifying configurations, Fabric8 provides a robust API for interacting with your cluster.

You can find the complete source code for this post on GitHub.

Happy patching!

Twitter iconFacebook iconLinkedIn iconPinterest iconEmail icon

Post navigation
Giving Superpowers to Small Language Models with Model Context Protocol (MCP)DeepSeek-R1 using Goose AI agent using Groq as a provider
© 2007 - 2025 Marc Nuri