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

In Virtualservice how to append new match rule #63

Closed naitikdani closed 5 years ago

naitikdani commented 5 years ago

I have a requirement where if i had a new microservice i need to update virtualservice with match rule. I tried to append the new match rule with edit() api but it didn't work.

kubectl get virtualservice/reviews-route -o yaml apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: reviews-route namespace: default spec: gateways:

I want to append the text in bold with edit() api.

Pasting a code snippet: ` istioClient.virtualService().createOrReplaceWithNew() .withNewMetadata().withName("reviews-route").endMetadata() .withNewSpec() .addToHosts("reviews.prod.svc.cluster.local") .addToGateways("logforwarding-gateway") .addNewHttp() .addNewMatch().withNewUri().withNewExactMatchType("/onboard").endUri().endMatch() .addNewMatch().withNewUri().withNewExactMatchType("/offboard").endUri().endMatch() .addNewRoute() .withNewDestination().withHost(reviewsHost).withNewPort().withNewNumberPort(9080).endPort() .endDestination() .endRoute() .endHttp() .addNewHttp() .addNewMatch().withHeaders(matchMap).endMatch() .addNewRoute() .withNewDestination().withHost("service-hello-901").withNewPort().withNewNumberPort(8080).endPort() .endDestination() .endRoute() .endHttp() .endSpec() .done();

        Map<String, StringMatch> matchMap1 = new HashMap<String, StringMatch>();
        matchMap1.put("tenantid", new StringMatch(new ExactMatchType("coke")));
        istioClient.virtualService().withName("reviews-route").edit().editSpec()
                .addNewHttp()
                .addNewMatch().withHeaders(matchMap1).endMatch()
                .addNewRoute()
                .withNewDestination().withHost("service-"+"coke").withNewPort().withNewNumberPort(8080).endPort()
                .endDestination()
                .endRoute()
                .endHttp()
                .endSpec()
                .done();`
naitikdani commented 5 years ago

In fact, I get this error when I call edit() on already created virtualservice.

kubectl get virtualservice/reviews-route -o yaml apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: creationTimestamp: "2019-05-09T20:35:37Z" generation: 1 name: reviews-route namespace: default resourceVersion: "2243865" selfLink: /apis/networking.istio.io/v1alpha3/namespaces/default/virtualservices/reviews-route

I am seeing this error when I call edit():

io.fabric8.kubernetes.client.KubernetesClientException: Failure executing: GET at: https://xxxxxxxxx/apis/$apiGroupName/$apiGroupVersion/namespaces/default/virtualservices/reviews-route. Message: 404 page not found

Any help will be appreciated

geoand commented 5 years ago

@iocanel probably another one for you :)

iocanel commented 5 years ago

@naitikdani: Unfortunately, we don't have a workaround for this one (its currently broken). @metacosm is working on it and hopefully, we will have a fix.

iocanel commented 5 years ago

@metacosm: feel free to throw it back at me, if you are swarmed, I just assigned it to you as I know you were already working on it.

metacosm commented 5 years ago

@naitikdani hi, this issue has been fixed with 1.1.0-Beta3. Please give it a try and let us know how it goes!

naitikdani commented 5 years ago

@metacosm the new version worked like a charm. Thank you so much for fixing this. One more question, does edit() API also support deleting a match rule? If yes, can you share what api should I call to make it work.

Thanks for all your help.

metacosm commented 5 years ago

@naitikdani I'd assume that something like: istioClient.virtualService().withName("reviews-route").edit().editSpec().editMatchingHttp(<predicate identifying the http route you want to modify>).removeFromMatch(<match you want to remove>).endHttp().endSpec().done() should work…