Greg
Greg

Reputation: 1367

how to edit custom resource removing just one finalizer

I'm using fabric8 and java operator sdk. I'd like to remove one custom finalizer in the deleteResource handler of the ResourceController. What is the suggested way to remove the finalizer and save the custom resource?

Upvotes: 1

Views: 1206

Answers (1)

Rohan Kumar
Rohan Kumar

Reputation: 5882

I think you should be able to add finalizers using Fabric8 Kubernetes Client. For example, if you want to add/remove finalizers like these in your Custom Resource's metadata:

  finalizers:
  - finalizer.stable.example.com

You can use either our typed API or typeless API for achieving this. I'm adding examples of both the approaches below using a Dummy Custom Resource provided in our CRDExample:

Typed API (Requires POJOs of Custom Resources)

MixedOperation<Dummy, DummyList, DoneableDummy, Resource<Dummy, DoneableDummy>> dummyCRClient = client
  .customResources(dummyCRD, Dummy.class, DummyList.class, DoneableDummy.class);
// Fetch resource fom Kubernetes API server
Dummy dummy2 = dummyCRClient.inNamespace("default").withName("second-dummy").get();
// Get metadata
ObjectMeta objectMeta = dummy2.getMetadata();
// Modify metadata
objectMeta.setFinalizers(Collections.singletonList("finalizer.stable.example.com"));
// Patch it back in Custom Resource
dummy2.setMetadata(objectMeta);

// Patch to Kubernetes
dummyCRClient.inNamespace("default").withName("second-dummy").patch(dummy2);

Typeless API (Provides way to access raw JSON Custom Resources)

try (KubernetesClient client = new DefaultKubernetesClient()) {
    CustomResourceDefinitionContext crdContext = new CustomResourceDefinitionContext.Builder()
            .withGroup("demo.fabric8.io")
            .withPlural("dummies")
            .withScope("Namespaced")
            .withVersion("v1")
            .withName("dummies.demo.fabric8.io")
            .build();
    // Fetch resource fom Kubernetes API server
    Map<String, Object> dummy2  = client.customResource(crdContext).get("default", "second-dummy");
    JSONObject dummy2JsonObj = new JSONObject(dummy2);
    // Get metadata
    JSONObject dummy2ObjectMeta = dummy2JsonObj.getJSONObject("metadata");
    // Modify metadata
    dummy2ObjectMeta.put("finalizers", new String[] { "finalizer.stable.example.com"});
    // Patch it back in Custom Resource
    dummy2JsonObj.put("metadata", dummy2ObjectMeta);

    // Patch to Kubernetes
    client.customResource(crdContext).edit("default", "second-dummy", dummy2JsonObj.toString());
}

Upvotes: 1

Related Questions