From d43fda7f73d597e73ab59ccc4f728377e150ef9c Mon Sep 17 00:00:00 2001 From: Xin Rong Date: Tue, 13 Sep 2022 17:02:39 +0800 Subject: [PATCH] feat: support update and delete of HTTPRoute (#1315) (#1329) --- pkg/ingress/gateway/gateway_httproute.go | 55 +++++++++++- test/e2e/scaffold/k8s.go | 8 +- test/e2e/suite-gateway/gateway_httproute.go | 92 ++++++++++++++++++++- 3 files changed, 148 insertions(+), 7 deletions(-) diff --git a/pkg/ingress/gateway/gateway_httproute.go b/pkg/ingress/gateway/gateway_httproute.go index 3fb60ba5f9..3d0c2f6eaa 100644 --- a/pkg/ingress/gateway/gateway_httproute.go +++ b/pkg/ingress/gateway/gateway_httproute.go @@ -211,14 +211,63 @@ func (c *gatewayHTTPRouteController) onAdd(obj interface{}) { return } log.Debugw("gateway HTTPRoute add event arrived", + zap.String("key", key), zap.Any("object", obj), ) - log.Debugw("add HTTPRoute", zap.String("key", key)) c.workqueue.Add(&types.Event{ Type: types.EventAdd, Object: key, }) } -func (c *gatewayHTTPRouteController) onUpdate(oldObj, newObj interface{}) {} -func (c *gatewayHTTPRouteController) OnDelete(obj interface{}) {} + +func (c *gatewayHTTPRouteController) onUpdate(oldObj, newObj interface{}) { + oldHTTPRoute := oldObj.(*gatewayv1alpha2.HTTPRoute) + newHTTPRoute := newObj.(*gatewayv1alpha2.HTTPRoute) + if oldHTTPRoute.ResourceVersion >= newHTTPRoute.ResourceVersion { + return + } + key, err := cache.MetaNamespaceKeyFunc(oldObj) + if err != nil { + log.Errorw("found gateway HTTPRoute resource with bad meta namespace key", + zap.Error(err), + ) + return + } + if !c.controller.NamespaceProvider.IsWatchingNamespace(key) { + return + } + log.Debugw("Gateway HTTPRoute update event arrived", + zap.String("key", key), + zap.Any("old object", oldObj), + zap.Any("new object", newObj), + ) + + c.workqueue.Add(&types.Event{ + Type: types.EventUpdate, + Object: key, + }) +} + +func (c *gatewayHTTPRouteController) OnDelete(obj interface{}) { + key, err := cache.MetaNamespaceKeyFunc(obj) + if err != nil { + log.Errorw("found Gateway HTTPRoute resource with bad meta namespace key", + zap.Error(err), + ) + return + } + if !c.controller.NamespaceProvider.IsWatchingNamespace(key) { + return + } + log.Debugw("Gateway HTTPRoute delete event arrived", + zap.String("key", key), + zap.Any("object", obj), + ) + + c.workqueue.Add(&types.Event{ + Type: types.EventDelete, + Object: key, + Tombstone: obj, + }) +} diff --git a/test/e2e/scaffold/k8s.go b/test/e2e/scaffold/k8s.go index 23f16b76f2..947ad8dec9 100644 --- a/test/e2e/scaffold/k8s.go +++ b/test/e2e/scaffold/k8s.go @@ -121,9 +121,11 @@ func (s *Scaffold) CreateApisixRoute(name string, rules []ApisixRouteRule) { // CreateResourceFromString creates resource from a loaded yaml string. func (s *Scaffold) CreateResourceFromString(yaml string) error { - err := k8s.KubectlApplyFromStringE(s.t, s.kubectlOptions, yaml) - time.Sleep(5 * time.Second) - return err + return k8s.KubectlApplyFromStringE(s.t, s.kubectlOptions, yaml) +} + +func (s *Scaffold) DeleteResourceFromString(yaml string) error { + return k8s.KubectlDeleteFromStringE(s.t, s.kubectlOptions, yaml) } func (s *Scaffold) GetOutputFromString(shell ...string) (string, error) { diff --git a/test/e2e/suite-gateway/gateway_httproute.go b/test/e2e/suite-gateway/gateway_httproute.go index 9a1f1893a2..f69cf16c4b 100644 --- a/test/e2e/suite-gateway/gateway_httproute.go +++ b/test/e2e/suite-gateway/gateway_httproute.go @@ -107,7 +107,6 @@ spec: ginkgo.It("Basic HTTPRoute with 1 Hosts 1 Rule 2 Match 1 BackendRef", func() { backendSvc, backendPorts := s.DefaultHTTPBackend() - time.Sleep(time.Second * 15) route := fmt.Sprintf(` apiVersion: gateway.networking.k8s.io/v1alpha2 kind: HTTPRoute @@ -146,4 +145,95 @@ spec: Expect(). Status(http.StatusNotFound) }) + + ginkgo.It("Update HTTPRoute", func() { + backendSvc, backendPorts := s.DefaultHTTPBackend() + route := fmt.Sprintf(` +apiVersion: gateway.networking.k8s.io/v1alpha2 +kind: HTTPRoute +metadata: + name: basic-http-route +spec: + hostnames: ["httpbin.org"] + rules: + - matches: + - path: + type: PathPrefix + value: /ip + backendRefs: + - name: %s + port: %d +`, backendSvc, backendPorts[0]) + assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(route), "creating HTTPRoute") + time.Sleep(time.Second * 6) + assert.Nil(ginkgo.GinkgoT(), s.EnsureNumApisixRoutesCreated(1), "Checking number of routes") + assert.Nil(ginkgo.GinkgoT(), s.EnsureNumApisixUpstreamsCreated(1), "Checking number of upstreams") + + route = fmt.Sprintf(` +apiVersion: gateway.networking.k8s.io/v1alpha2 +kind: HTTPRoute +metadata: + name: basic-http-route +spec: + hostnames: ["httpbin.org"] + rules: + - matches: + - path: + type: PathPrefix + value: /get + backendRefs: + - name: %s + port: %d +`, backendSvc, backendPorts[0]) + assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(route), "update HTTPRoute") + assert.Nil(ginkgo.GinkgoT(), s.EnsureNumApisixRoutesCreated(1), "Checking number of routes") + + time.Sleep(6 * time.Second) + + _ = s.NewAPISIXClient().GET("/get"). + WithHeader("Host", "httpbin.org"). + Expect(). + Status(http.StatusOK) + _ = s.NewAPISIXClient().GET("/ip"). + WithHeader("Host", "httpbin.org"). + Expect(). + Status(http.StatusNotFound) + }) + + ginkgo.It("Delete HTTPRoute", func() { + backendSvc, backendPorts := s.DefaultHTTPBackend() + route := fmt.Sprintf(` +apiVersion: gateway.networking.k8s.io/v1alpha2 +kind: HTTPRoute +metadata: + name: basic-http-route +spec: + hostnames: ["httpbin.org"] + rules: + - matches: + - path: + type: PathPrefix + value: /ip + backendRefs: + - name: %s + port: %d +`, backendSvc, backendPorts[0]) + assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(route), "creating HTTPRoute") + time.Sleep(time.Second * 6) + assert.Nil(ginkgo.GinkgoT(), s.EnsureNumApisixRoutesCreated(1), "Checking number of routes") + assert.Nil(ginkgo.GinkgoT(), s.EnsureNumApisixUpstreamsCreated(1), "Checking number of upstreams") + + _ = s.NewAPISIXClient().GET("/ip"). + WithHeader("Host", "httpbin.org"). + Expect(). + Status(http.StatusOK) + + assert.Nil(ginkgo.GinkgoT(), s.DeleteResourceFromString(route), "delete HTTPRoute") + assert.Nil(ginkgo.GinkgoT(), s.EnsureNumApisixRoutesCreated(0), "Checking number of routes") + + _ = s.NewAPISIXClient().GET("/ip"). + WithHeader("Host", "httpbin.org"). + Expect(). + Status(http.StatusNotFound) + }) })