tokers commented on a change in pull request #715:
URL: 
https://github.com/apache/apisix-ingress-controller/pull/715#discussion_r731427735



##########
File path: pkg/ingress/namespace.go
##########
@@ -0,0 +1,178 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package ingress
+
+import (
+       "context"
+       "time"
+
+       "go.uber.org/zap"
+       corev1 "k8s.io/api/core/v1"
+       metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+       "k8s.io/apimachinery/pkg/labels"
+       "k8s.io/client-go/tools/cache"
+       "k8s.io/client-go/util/workqueue"
+
+       "github.com/apache/apisix-ingress-controller/pkg/log"
+       "github.com/apache/apisix-ingress-controller/pkg/types"
+)
+
+type namespaceController struct {
+       controller *Controller
+       workqueue  workqueue.RateLimitingInterface
+       workers    int
+}
+
+func (c *Controller) newNamespaceController() *namespaceController {
+       ctl := &namespaceController{
+               controller: c,
+               workqueue:  
workqueue.NewNamedRateLimitingQueue(workqueue.NewItemFastSlowRateLimiter(1*time.Second,
 60*time.Second, 5), "Namespace"),
+               workers:    1,
+       }
+       ctl.controller.namespaceInformer.AddEventHandler(
+               cache.ResourceEventHandlerFuncs{
+                       AddFunc:    ctl.onAdd,
+                       UpdateFunc: ctl.onUpdate,
+                       DeleteFunc: ctl.onDelete,
+               },
+       )
+       return ctl
+}
+
+func (c *Controller) initWatchingNamespaceByLabels(ctx context.Context) error {
+       labelSelector := metav1.LabelSelector{MatchLabels: c.watchingLabels}
+       opts := metav1.ListOptions{
+               LabelSelector: labels.Set(labelSelector.MatchLabels).String(),
+       }
+       namespaces, err := c.kubeClient.Client.CoreV1().Namespaces().List(ctx, 
opts)
+       if err != nil {
+               return err
+       } else {
+               for _, ns := range namespaces.Items {
+                       c.watchingNamespace[ns.Name] = struct{}{}
+               }
+       }
+       return nil
+}
+
+func (c *namespaceController) run(ctx context.Context) {
+       log.Info("namespace controller started")
+       defer log.Info("namespace controller exited")
+
+       if ok := cache.WaitForCacheSync(ctx.Done(), 
c.controller.namespaceInformer.HasSynced); !ok {
+               log.Error("informers sync failed")
+               return
+       }
+       for i := 0; i < c.workers; i++ {
+               go c.runWorker(ctx)
+       }
+       <-ctx.Done()
+}
+
+func (c *namespaceController) runWorker(ctx context.Context) {
+       for {
+               obj, quit := c.workqueue.Get()
+               if quit {
+                       return
+               }
+               err := c.sync(ctx, obj.(*types.Event))
+               c.workqueue.Done(obj)
+               c.handleSyncErr(obj.(*types.Event), err)
+       }
+}
+
+func (c *namespaceController) sync(ctx context.Context, ev *types.Event) error 
{
+       if ev.Type != types.EventDelete {
+               // check the labels of specify namespace
+               namespace, err := 
c.controller.kubeClient.Client.CoreV1().Namespaces().Get(ctx, 
ev.Object.(string), metav1.GetOptions{})
+               if err != nil {
+                       return err
+               } else {
+                       // if labels of namespace contains the watchingLabels, 
the namespace should be set to controller.watchingNamespace
+                       if 
c.controller.watchingLabels.IsSubsetOf(namespace.Labels) {
+                               c.controller.watchingNamespace[namespace.Name] 
= struct{}{}
+                       }
+               }
+       } else { // type == types.EventDelete
+               namespace := ev.Tombstone.(*corev1.Namespace)
+               if _, ok := c.controller.watchingNamespace[namespace.Name]; ok {
+                       if namespace, err := 
c.controller.kubeClient.Client.CoreV1().Namespaces().Get(ctx, 
ev.Object.(string), metav1.GetOptions{}); err == nil {
+                               // if namespace is still in k8s cluster, the 
delete event is deprecated
+                               if 
c.controller.watchingLabels.IsSubsetOf(namespace.Labels) {
+                                       
c.controller.watchingNamespace[namespace.Name] = struct{}{}
+                               }
+                               return nil
+                       }
+                       delete(c.controller.watchingNamespace, namespace.Name)
+                       // need to compare
+                       err := c.controller.CompareResources(ctx)

Review comment:
       > If one namespace has been removed, the route in APISIX has not been 
deleted
   
   Namespace remove event will cause all the resources inside it also removed, 
so our `ApisixRoute`, `ApisixUpstream` controllers will detect these delete 
events.




-- 
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-unsubscr...@apisix.apache.org

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


Reply via email to