Deleting path from kubernetes ingress with java kubernetes-client

6/20/2018

I'm trying to delete a path with it's backend from an ingress rule but can't quite get it to work.

This is how I try to delete it:

// get path to delete
Optional<HTTPIngressPath> pathToDelete = kubernetesClient.extensions().ingresses().withName("my-ingress")
    .get()
    .getSpec()
    .getRules()
    .get(0)
    .getHttp()
    .getPaths().stream()
        .filter(path -> path.getBackend().getServiceName().equals(buildServiceNameById(id))).findFirst();

if(!pathToDelete.isPresent()) {
    LOGGER.warn("Couldn't determine ingress path for " + buildServiceNameById(id) + ".");
    return;
}

kubernetesClient.extensions().ingresses().withName("my-ingress")
    .edit()
    .editSpec()
    .editFirstRule()
    .editHttp()
        .removeFromPaths(pathToDelete.get())
    .endHttp()
    .endRule()
    .endSpec()
    .done();

LOGGER.info("Deleted ingress routing " + pathToDelete.toString());

When I look at the logs I can see that it finds the path and outputs this:

Deleted ingress routing Optional[HTTPIngressPath(backend=IngressBackend(serviceName=my-service-6, servicePort=IntOrString(IntVal=8080, Kind=null, StrVal=null, additionalProperties={}), additionalProperties={}), path=/myservice/6, additionalProperties={})]

But when I describe the ingress, I can see that the rule is still there:

Name:             my-ingress
Namespace:        default
Address:          someAdress
Default backend:  default-http-backend:80 (<none>)
Rules:
  Host                                                 Path  Backends
  ----                                                 ----  --------
  someHost  
                                                       /                                           admin:8888 (<none>)
                                                       /myservice/6   my-service-6:8080 (<none>)
Annotations:
Events:
  Type     Reason               Age   From                                                             Message
  ----     ------               ----  ----                                                             -------
  Normal   Success              21m   public  Successfully applied ingress resource.
  Warning  ConfigReloadFailure  21m   public  Failed to apply ingress resource.
  Warning  ConfigReloadFailure  21m   public  Failed to apply ingress resource.
  Warning  ConfigReloadFailure  20m   public  Failed to apply ingress resource.
  Normal   Success              18m   public  Successfully applied ingress resource.
  Normal   Success              18m   public  Successfully applied ingress resource.
  Normal   Success              17m   public  Successfully applied ingress resource.
  Normal   Success              16m   public  Successfully applied ingress resource.

So my question is: How do I use the kubernetes-client-java to dynamically delete a path in Ingress? I created the rule with the kubernetes-client, so I know it's working in theory, I just can't get it to delete it again.

-- mammago
fabric8
java
kubernetes
kubernetes-ingress

1 Answer

6/18/2019

You can delete igress like this:

k8sClient.extensions().ingresses().withName("my-ingress").delete();
-- Sipeng
Source: StackOverflow