You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by zh...@apache.org on 2022/09/07 10:22:33 UTC

[apisix-ingress-controller] branch master updated: feat: support update and delete of HTTPRoute (#1315)

This is an automated email from the ASF dual-hosted git repository.

zhangjintao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-ingress-controller.git


The following commit(s) were added to refs/heads/master by this push:
     new 5c0ea2b4 feat: support update and delete of HTTPRoute (#1315)
5c0ea2b4 is described below

commit 5c0ea2b42138b0d0df59d85936d2b72feaa669c5
Author: Xin Rong <ro...@api7.ai>
AuthorDate: Wed Sep 7 18:22:25 2022 +0800

    feat: support update and delete of HTTPRoute (#1315)
---
 pkg/providers/gateway/gateway_httproute.go         | 55 ++++++++++++-
 test/e2e/scaffold/k8s.go                           |  8 +-
 test/e2e/suite-gateway/gateway_httproute.go        | 92 +++++++++++++++++++++-
 .../suite-plugins-other/mqtt-proxy.go              |  1 +
 4 files changed, 149 insertions(+), 7 deletions(-)

diff --git a/pkg/providers/gateway/gateway_httproute.go b/pkg/providers/gateway/gateway_httproute.go
index 92d06090..f0a901e2 100644
--- a/pkg/providers/gateway/gateway_httproute.go
+++ b/pkg/providers/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 23f16b76..947ad8de 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 9a1f1893..f69cf16c 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)
+	})
 })
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 b906a280..e9e0844b 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