snowdrop / istio-java-api

A Java API to generate Istio descriptors, inspired by Fabric8's kubernetes-model.
Apache License 2.0
112 stars 33 forks source link

editMatchingHttp() is not able to find a matching http #66

Closed naitikdani closed 5 years ago

naitikdani commented 5 years ago

@metacosm , editMatchingHttp() is throwing the following error when trying to remove a specific match from virtualservice yaml file.

java.lang.RuntimeException: Can't edit matching http. No match found. at me.snowdrop.istio.api.networking.v1alpha3.VirtualServiceSpecFluentImpl.editMatchingHttp(VirtualServiceSpecFluentImpl.java

Here is the sample of yaml file and the code:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews-route
  namespace: default
  resourceVersion: "2418174"
spec:
  gateways:
  - in-gateway
  hosts:
  - reviews.prod.svc.cluster.local
  http:
  - match:
    - uri:
        exact: /onboard
    - uri:
        exact: /offboard
    route:
    - destination:
        host: reviews.prod.svc.cluster.local
        port:
          number: 9080
  - match:
    - headers:
        tenantid:
          exact: coke
    route:
    - destination:
        host: service-coke
        port:
          number: 8080

In the above yaml file I am trying to remove match with headers "coke" and destionation host as "service-coke":

           Map<String, StringMatch> matchMap = new HashMap<String, StringMatch>();
            matchMap.put("tenantid", new StringMatch(new ExactMatchType("coke")));
            HTTPMatchRequest req = new HTTPMatchRequestBuilder().withHeaders(matchMap)
                    .build();

            HTTPRouteDestination dst = new HTTPRouteDestinationBuilder()
                    .withNewDestination().withHost("service-coke").endDestination().build();

            HTTPRouteBuilder builder = new HTTPRouteBuilder(new HTTPRouteBuilder()
                    .withMatch(req).withRoute(dst).build());

            istioClient.virtualService().inNamespace("default").withName("reviews-route")
                    .edit().editSpec()
                    .editMatchingHttp(r -> (r.withRoute(dst).withMatch(req) == builder))
                    .removeFromMatch(req)
                    .endHttp().endSpec().done();

Am I doing it the wrong way? Please let me know. Thanks for your help.

metacosm commented 5 years ago

You need to use equals instead of == to compare complex objects. That said, there seems to be a bug in the code generation library that we're using and the generated equals method doesn't work properly either. See https://github.com/sundrio/sundrio/issues/142.

naitikdani commented 5 years ago

@metacosm, thanks for the update. When do you think the fix will be available?

metacosm commented 5 years ago

No idea yet.

metacosm commented 5 years ago

@naitikdani the initial issue that was found has been addressed in the underlying library. However, we're trying to get some more improvements that would make the API easier to use for your use case at the time. Hopefully, this will get done this week.

metacosm commented 5 years ago

@naitikdani to be more precise, the approach that you use wouldn't work even with the fix. The predicate needs to be something more like: h -> h.hasMatchingMatch(m -> m.hasHeaders() && m.getHeaders().equals(matchMap)) && h.hasMatchingRoute(r -> r.buildDestination().getHost().equals("service-coke")). Note that this predicate will still fail without https://github.com/sundrio/sundrio/pull/144.

metacosm commented 5 years ago

@naitikdani I will release a new beta version as soon as a new sundrio release is available.

metacosm commented 5 years ago

This should be addressed in https://github.com/snowdrop/istio-java-api/releases/tag/1.1.0-Beta5. Note that you can now do: istioClient.virtualService().withName("reviews-route") .edit().editSpec() .removeMatchingFromHttp(h -> h.hasMatchingMatch(m -> m.hasHeaders() && m.getHeaders().equals(matchMap)) && h.hasMatchingRoute(r -> r.buildDestination().getHost().equals("service-coke"))) .endSpec().done(). Let us know how it works for you!

naitikdani commented 5 years ago

Thanks @metacosm your fix works.