Skip to content

Commit

Permalink
MAISTRA-1724 Don't watch namespaces if MRC is used (istio#161)
Browse files Browse the repository at this point in the history
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
dgn and mergify[bot] committed Aug 13, 2020
1 parent 46f0f4a commit a161a53
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions pilot/pkg/serviceregistry/kube/controller/namespacecontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import (
"context"
"fmt"
"strings"
"sync"
"time"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/watch"
informer "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -63,6 +65,15 @@ type NamespaceController struct {
namespaceController cache.Controller
// Controller and store for ConfigMap objects
configMapController cache.Controller

// if this is true, we don't create a K8s controller, but only react on namespace changes
// coming from the MRC
usesMemberRollController bool

// only used if usesMemberRollController is true
started bool
mutex sync.Mutex
namespaces sets.String
}

// NewNamespaceController returns a pointer to a newly constructed NamespaceController instance.
Expand All @@ -89,10 +100,6 @@ func NewNamespaceController(data func() map[string]string, options Options, kube
}
})

if mrc != nil {
mrc.Register(mlw)
}

configmapInformer := cache.NewSharedIndexInformer(mlw, &v1.ConfigMap{}, options.ResyncPeriod,
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})

Expand Down Expand Up @@ -132,6 +139,13 @@ func NewNamespaceController(data func() map[string]string, options Options, kube
})
c.configMapController = configmapInformer

if mrc != nil {
mrc.Register(mlw)
mrc.Register(c)
c.usesMemberRollController = true
return c
}

namespaceInformer := informer.NewNamespaceInformer(kubeClient, options.ResyncPeriod, cache.Indexers{})
namespaceInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
Expand All @@ -152,6 +166,19 @@ func NewNamespaceController(data func() map[string]string, options Options, kube

// Run starts the NamespaceController until a value is sent to stopCh.
func (nc *NamespaceController) Run(stopCh <-chan struct{}) {
if nc.usesMemberRollController {
nc.mutex.Lock()
nc.started = true
nc.mutex.Unlock()
go func() {
<-stopCh
nc.mutex.Lock()
nc.started = false
nc.mutex.Unlock()
}()
log.Infof("Namespace controller (MRC) started")
return
}
go nc.namespaceController.Run(stopCh)
go nc.configMapController.Run(stopCh)
cache.WaitForCacheSync(stopCh, nc.namespaceController.HasSynced, nc.configMapController.HasSynced)
Expand Down Expand Up @@ -193,3 +220,16 @@ func (nc *NamespaceController) configMapChange(obj interface{}) error {
}
return nil
}

func (nc *NamespaceController) UpdateNamespaces(namespaces []string) {
nc.mutex.Lock()
defer nc.mutex.Unlock()
namespaceSet := sets.NewString(namespaces...)
for _, ns := range namespaceSet.Difference(nc.namespaces).List() {
err := nc.insertDataForNamespace(ns)
if err != nil {
log.Errorf("Failed to create configMap in namespace %s: %s", ns, err)
}
}
nc.namespaces = namespaceSet
}

0 comments on commit a161a53

Please sign in to comment.