From 1491b35dbfbd232050d97cd2f1334cc364fec2dd Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Fri, 2 Sep 2022 02:12:15 +0000 Subject: [PATCH 1/4] feat: support update and delete of HTTPRoute --- pkg/providers/gateway/gateway_httproute.go | 53 +++++++++++- test/e2e/scaffold/k8s.go | 8 +- test/e2e/suite-gateway/gateway_httproute.go | 92 ++++++++++++++++++++- 3 files changed, 147 insertions(+), 6 deletions(-) diff --git a/pkg/providers/gateway/gateway_httproute.go b/pkg/providers/gateway/gateway_httproute.go index 92d060907d..d4cc80303e 100644 --- a/pkg/providers/gateway/gateway_httproute.go +++ b/pkg/providers/gateway/gateway_httproute.go @@ -220,5 +220,54 @@ func (c *gatewayHTTPRouteController) onAdd(obj interface{}) { 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.Any("old object", oldObj), + zap.Any("new object", newObj), + ) + + log.Debugw("update HTTPRoute", zap.String("key", key)) + 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.Any("object", obj), + ) + + log.Debugw("delete HTTPRoute", zap.String("key", key)) + 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) + }) }) From ebc1fdd198d41d1e10bd7edd8328be3d30677d1f Mon Sep 17 00:00:00 2001 From: rongxin Date: Fri, 2 Sep 2022 13:57:44 +0800 Subject: [PATCH 2/4] update HTTPRoute log --- pkg/providers/gateway/gateway_httproute.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/providers/gateway/gateway_httproute.go b/pkg/providers/gateway/gateway_httproute.go index d4cc80303e..f0a901e260 100644 --- a/pkg/providers/gateway/gateway_httproute.go +++ b/pkg/providers/gateway/gateway_httproute.go @@ -211,10 +211,10 @@ 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, @@ -238,11 +238,11 @@ func (c *gatewayHTTPRouteController) onUpdate(oldObj, newObj interface{}) { return } log.Debugw("Gateway HTTPRoute update event arrived", + zap.String("key", key), zap.Any("old object", oldObj), zap.Any("new object", newObj), ) - log.Debugw("update HTTPRoute", zap.String("key", key)) c.workqueue.Add(&types.Event{ Type: types.EventUpdate, Object: key, @@ -261,10 +261,10 @@ func (c *gatewayHTTPRouteController) OnDelete(obj interface{}) { return } log.Debugw("Gateway HTTPRoute delete event arrived", + zap.String("key", key), zap.Any("object", obj), ) - log.Debugw("delete HTTPRoute", zap.String("key", key)) c.workqueue.Add(&types.Event{ Type: types.EventDelete, Object: key, From ac480179a05fdd98e0aa7dcd20b4d6ecdfe9af58 Mon Sep 17 00:00:00 2001 From: rongxin Date: Fri, 2 Sep 2022 14:24:17 +0800 Subject: [PATCH 3/4] mqtt failed --- test/e2e/suite-plugins/suite-plugins-other/mqtt-proxy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/suite-plugins/suite-plugins-other/mqtt-proxy.go b/test/e2e/suite-plugins/suite-plugins-other/mqtt-proxy.go index b906a280ef..ab9b661590 100644 --- a/test/e2e/suite-plugins/suite-plugins-other/mqtt-proxy.go +++ b/test/e2e/suite-plugins/suite-plugins-other/mqtt-proxy.go @@ -116,7 +116,7 @@ spec: // test mqtt protocol c := s.NewMQTTClient() token := c.Connect() - token.WaitTimeout(3 * time.Second) + token.WaitTimeout(6 * time.Second) assert.Nil(ginkgo.GinkgoT(), token.Error(), "Checking mqtt connection") }) }) From 95492a0c688a8475e0784b33433ce902639f51d8 Mon Sep 17 00:00:00 2001 From: rongxin Date: Tue, 6 Sep 2022 14:29:32 +0800 Subject: [PATCH 4/4] update mqtt wait time --- test/e2e/suite-plugins/suite-plugins-other/mqtt-proxy.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/e2e/suite-plugins/suite-plugins-other/mqtt-proxy.go b/test/e2e/suite-plugins/suite-plugins-other/mqtt-proxy.go index ab9b661590..e9e0844be0 100644 --- a/test/e2e/suite-plugins/suite-plugins-other/mqtt-proxy.go +++ b/test/e2e/suite-plugins/suite-plugins-other/mqtt-proxy.go @@ -81,6 +81,7 @@ spec: protocol: TCP `)) s.EnsureNumEndpointsReady(ginkgo.GinkgoT(), "mosquito", 1) + time.Sleep(30 * time.Second) // setup Apisix Route for mqtt proxy apisixRoute := ` apiVersion: apisix.apache.org/v2 @@ -116,7 +117,7 @@ spec: // test mqtt protocol c := s.NewMQTTClient() token := c.Connect() - token.WaitTimeout(6 * time.Second) + token.WaitTimeout(3 * time.Second) assert.Nil(ginkgo.GinkgoT(), token.Error(), "Checking mqtt connection") }) })