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



##########
File path: pkg/ingress/controller.go
##########
@@ -148,12 +153,20 @@ func NewController(cfg *config.Config) (*Controller, 
error) {
        }
 
        var (
-               watchingNamespace map[string]struct{}
+               watchingNamespace = new(sync.Map)
+               watchingLabels    = make(map[string]string)
        )
        if len(cfg.Kubernetes.AppNamespaces) > 1 || 
cfg.Kubernetes.AppNamespaces[0] != v1.NamespaceAll {
-               watchingNamespace = make(map[string]struct{}, 
len(cfg.Kubernetes.AppNamespaces))
                for _, ns := range cfg.Kubernetes.AppNamespaces {
-                       watchingNamespace[ns] = struct{}{}
+                       watchingNamespace.Store(ns, struct{}{})
+               }
+       }
+
+       // support namespace label-selector
+       if len(cfg.Kubernetes.NamespaceSelector) > 1 {
+               for _, labels := range cfg.Kubernetes.NamespaceSelector {
+                       labelSlice := strings.Split(labels, "=")

Review comment:
       The input labels are not validated, so invalid strings like `foo`, will 
only generate one element after calling `strings.Split` and the blow reference 
will be out of bound.

##########
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:
       @gxthrj 

##########
File path: cmd/ingress/ingress.go
##########
@@ -143,7 +143,8 @@ the apisix cluster and others are created`,
        cmd.PersistentFlags().BoolVar(&cfg.EnableProfiling, "enable-profiling", 
true, "enable profiling via web interface host:port/debug/pprof")
        cmd.PersistentFlags().StringVar(&cfg.Kubernetes.Kubeconfig, 
"kubeconfig", "", "Kubernetes configuration file (by default in-cluster 
configuration will be used)")
        
cmd.PersistentFlags().DurationVar(&cfg.Kubernetes.ResyncInterval.Duration, 
"resync-interval", time.Minute, "the controller resync (with Kubernetes) 
interval, the minimum resync interval is 30s")
-       cmd.PersistentFlags().StringSliceVar(&cfg.Kubernetes.AppNamespaces, 
"app-namespace", []string{config.NamespaceAll}, "namespaces that controller 
will watch for resources")
+       cmd.PersistentFlags().StringSliceVar(&cfg.Kubernetes.AppNamespaces, 
"app-namespace", []string{config.NamespaceAll}, "namespaces that controller 
will watch for resources, this is deprecated.")

Review comment:
       I think the change can be reverted.




-- 
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