'Unable to patch ingress using k8s Java api

I am trying to patch ingress running on my linux machine using k8s Java api. I wrote the following code for patching:

//annotationValue is a string of comma separated CIDR. ex: "172.17.56.44/30,11.0.0.0/30"
String yamlStr ="{\"metadata\":{\"annotations\":{\"nginx.ingress.kubernetes.io/whitelist-source-range\":\""+annotationValue+"\"}}}";
log.info("Patching ingress: {} with :{}", ingressName, yamlStr);
V1Patch body = new V1Patch(yamlStr);
networkingV1Api.patchNamespacedIngress(ingressName, namespace, body, null, null, null, null);

but I get the following error:

io.kubernetes.client.openapi.ApiException:
at io.kubernetes.client.openapi.ApiClient.handleResponse(ApiClient.java:973)
at io.kubernetes.client.openapi.ApiClient.execute(ApiClient.java:885)
at io.kubernetes.client.openapi.apis.NetworkingV1Api.patchNamespacedIngressWithHttpInfo(NetworkingV1Api.java:6474)
at io.kubernetes.client.openapi.apis.NetworkingV1Api.patchNamespacedIngress(NetworkingV1Api.java:6428)
UpdatePublicDeployment.updateIngress(UpdatePublicDeployment.java:58)

However when I try to patch it from terminal using the following command:

kubectl patch ingress ingressName -n nameSpace --patch "$(cat patch.yaml)"
cat patch.yaml

produces

{"metadata":{"annotations":{"nginx.ingress.kubernetes.io/whitelist-source-range":"172.17.56.44/30,11.0.0.0/30"}}}

the ingress gets patched successfully.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source