You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2022/07/20 06:08:51 UTC

[GitHub] [apisix-ingress-controller] AlinsRan commented on a diff in pull request #1152: fix: trigger ApisixRoute event when service is created

AlinsRan commented on code in PR #1152:
URL: https://github.com/apache/apisix-ingress-controller/pull/1152#discussion_r925205372


##########
pkg/ingress/apisix_route.go:
##########
@@ -41,13 +42,19 @@ type apisixRouteController struct {
 	controller *Controller
 	workqueue  workqueue.RateLimitingInterface
 	workers    int
+
+	// svc meta key -> ApisixRoute name -> empty
+	svcLock sync.RWMutex

Review Comment:
   No read lock。



##########
pkg/ingress/apisix_route.go:
##########
@@ -82,9 +95,114 @@ func (c *apisixRouteController) runWorker(ctx context.Context) {
 		if quit {
 			return
 		}
-		err := c.sync(ctx, obj.(*types.Event))
-		c.workqueue.Done(obj)
-		c.handleSyncErr(obj, err)
+
+		switch val := obj.(type) {
+		case *types.Event:
+			err := c.sync(ctx, val)
+			c.workqueue.Done(obj)
+			c.handleSyncErr(obj, err)
+		case string:
+			err := c.handleSvcAdd(val)
+			c.workqueue.Done(obj)
+			c.handleSvcErr(val, err)
+		}
+	}
+}
+
+func (c *apisixRouteController) syncServiceRelationship(ev *types.Event, name string, ar kube.ApisixRoute) {
+	obj := ev.Object.(kube.ApisixRouteEvent)
+
+	var (
+		oldBackends []string
+		newBackends []string
+	)
+	switch obj.GroupVersion {
+	case config.ApisixV2beta3:
+		var (
+			old    *v2beta3.ApisixRoute
+			newObj *v2beta3.ApisixRoute
+		)
+
+		if ev.Type == types.EventUpdate {
+			old = obj.OldObject.V2beta3()
+		} else if ev.Type == types.EventDelete {
+			old = ev.Tombstone.(kube.ApisixRoute).V2beta3()
+		}
+
+		if ev.Type != types.EventDelete {
+			newObj = ar.V2beta3()
+		}
+
+		// calculate diff, so we don't need to care about the event order
+		if old != nil {
+			for _, rule := range old.Spec.HTTP {
+				for _, backend := range rule.Backends {
+					oldBackends = append(oldBackends, backend.ServiceName)
+
+					delete(c.svcMap[old.Namespace+"/"+backend.ServiceName], old.Name)
+				}
+			}
+		}
+		if newObj != nil {
+			for _, rule := range newObj.Spec.HTTP {
+				for _, backend := range rule.Backends {
+					newBackends = append(newBackends, newObj.Namespace+"/"+backend.ServiceName)
+				}
+			}
+		}
+	case config.ApisixV2:
+		var (
+			old    *v2.ApisixRoute
+			newObj *v2.ApisixRoute
+		)
+
+		if ev.Type == types.EventUpdate {
+			old = obj.OldObject.V2()
+		} else if ev.Type == types.EventDelete {
+			old = ev.Tombstone.(kube.ApisixRoute).V2()
+		}
+
+		if ev.Type != types.EventDelete {
+			newObj = ar.V2()
+		}
+
+		// calculate diff, so we don't need to care about the event order
+		if old != nil {
+			for _, rule := range old.Spec.HTTP {
+				for _, backend := range rule.Backends {
+					oldBackends = append(oldBackends, backend.ServiceName)
+
+					delete(c.svcMap[old.Namespace+"/"+backend.ServiceName], old.Name)

Review Comment:
   Does this conflict with line 198? There is no lock here.



##########
test/e2e/suite-ingress/suite-ingress-resource/resourcepushing.go:
##########
@@ -576,6 +578,113 @@ spec:
 			resp = s.NewAPISIXClient().GET("/status/200").WithHeader("Host", "httpbin.com").Expect()
 			resp.Status(http.StatusNotFound)
 		})
+
+		ginkgo.It("k8s service is created later than ApisixRoute", func() {
+			createSvc := func() {
+				_httpbinDeploymentTemplate := `
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+  name: httpbin-temp
+spec:
+  replicas: 1
+  selector:
+    matchLabels:
+      app: httpbin-temp
+  strategy:
+    rollingUpdate:
+      maxSurge: 50%
+      maxUnavailable: 1
+    type: RollingUpdate
+  template:
+    metadata:
+      labels:
+        app: httpbin-temp
+    spec:
+      terminationGracePeriodSeconds: 0
+      containers:
+        - livenessProbe:
+            failureThreshold: 3
+            initialDelaySeconds: 2
+            periodSeconds: 5
+            successThreshold: 1
+            tcpSocket:
+              port: 80
+            timeoutSeconds: 2
+          readinessProbe:
+            failureThreshold: 3
+            initialDelaySeconds: 2
+            periodSeconds: 5
+            successThreshold: 1
+            tcpSocket:
+              port: 80
+            timeoutSeconds: 2
+          image: "localhost:5000/kennethreitz/httpbin"

Review Comment:
   localhost:5000/kennethreitz/httpbin:dev



##########
pkg/ingress/apisix_route.go:
##########
@@ -41,13 +42,19 @@ type apisixRouteController struct {
 	controller *Controller
 	workqueue  workqueue.RateLimitingInterface
 	workers    int
+
+	// svc meta key -> ApisixRoute name -> empty

Review Comment:
   we should only maintain the `map` when the service is not exist? @tao12345666333 



##########
pkg/ingress/apisix_route.go:
##########
@@ -82,9 +95,114 @@ func (c *apisixRouteController) runWorker(ctx context.Context) {
 		if quit {
 			return
 		}
-		err := c.sync(ctx, obj.(*types.Event))
-		c.workqueue.Done(obj)
-		c.handleSyncErr(obj, err)
+
+		switch val := obj.(type) {
+		case *types.Event:
+			err := c.sync(ctx, val)
+			c.workqueue.Done(obj)
+			c.handleSyncErr(obj, err)
+		case string:
+			err := c.handleSvcAdd(val)
+			c.workqueue.Done(obj)
+			c.handleSvcErr(val, err)
+		}
+	}
+}
+
+func (c *apisixRouteController) syncServiceRelationship(ev *types.Event, name string, ar kube.ApisixRoute) {
+	obj := ev.Object.(kube.ApisixRouteEvent)
+
+	var (
+		oldBackends []string
+		newBackends []string
+	)
+	switch obj.GroupVersion {
+	case config.ApisixV2beta3:
+		var (
+			old    *v2beta3.ApisixRoute
+			newObj *v2beta3.ApisixRoute
+		)
+
+		if ev.Type == types.EventUpdate {
+			old = obj.OldObject.V2beta3()
+		} else if ev.Type == types.EventDelete {
+			old = ev.Tombstone.(kube.ApisixRoute).V2beta3()
+		}
+
+		if ev.Type != types.EventDelete {
+			newObj = ar.V2beta3()
+		}
+
+		// calculate diff, so we don't need to care about the event order
+		if old != nil {
+			for _, rule := range old.Spec.HTTP {
+				for _, backend := range rule.Backends {
+					oldBackends = append(oldBackends, backend.ServiceName)
+
+					delete(c.svcMap[old.Namespace+"/"+backend.ServiceName], old.Name)

Review Comment:
   Does this conflict with line 198? There is no lock here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org