Skip to content

Commit

Permalink
Merge branch 'main' into chore/update-readme-dates-and-badge
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Ryan authored Sep 12, 2024
2 parents da4741a + 0102bf5 commit 988bc8f
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 106 deletions.
72 changes: 0 additions & 72 deletions internal/k8s/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,18 +548,6 @@ func (nsi *namespacedInformer) addVirtualServerRouteHandler(handlers cache.Resou
nsi.cacheSyncs = append(nsi.cacheSyncs, informer.HasSynced)
}

func (lbc *LoadBalancerController) addNamespaceHandler(handlers cache.ResourceEventHandlerFuncs, nsLabel string) {
optionsModifier := func(options *meta_v1.ListOptions) {
options.LabelSelector = nsLabel
}
nsInformer := informers.NewSharedInformerFactoryWithOptions(lbc.client, lbc.resync, informers.WithTweakListOptions(optionsModifier)).Core().V1().Namespaces().Informer()
nsInformer.AddEventHandler(handlers)
lbc.namespaceLabeledLister = nsInformer.GetStore()
lbc.namespaceWatcherController = nsInformer

lbc.cacheSyncs = append(lbc.cacheSyncs, nsInformer.HasSynced)
}

// Run starts the loadbalancer controller
func (lbc *LoadBalancerController) Run() {
lbc.ctx, lbc.cancel = context.WithCancel(context.Background())
Expand Down Expand Up @@ -1015,66 +1003,6 @@ func (lbc *LoadBalancerController) sync(task task) {
}
}

func (lbc *LoadBalancerController) syncNamespace(task task) {
key := task.Key
// process namespace and add to / remove from watched namespace list
_, exists, err := lbc.namespaceLabeledLister.GetByKey(key)
if err != nil {
lbc.syncQueue.Requeue(task, err)
return
}

if !exists {
// Check if change is because of a new label, or because of a deleted namespace
ns, _ := lbc.client.CoreV1().Namespaces().Get(context.TODO(), key, meta_v1.GetOptions{})

if ns != nil && ns.Status.Phase == api_v1.NamespaceActive {
// namespace still exists
glog.Infof("Removing Configuration for Unwatched Namespace: %v", key)
// Watched label for namespace was removed
// delete any now unwatched namespaced informer groups if required
nsi := lbc.getNamespacedInformer(key)
if nsi != nil {
lbc.cleanupUnwatchedNamespacedResources(nsi)
delete(lbc.namespacedInformers, key)
}
} else {
glog.Infof("Deleting Watchers for Deleted Namespace: %v", key)
nsi := lbc.getNamespacedInformer(key)
if nsi != nil {
lbc.removeNamespacedInformer(nsi, key)
}
}
if lbc.certManagerController != nil {
lbc.certManagerController.RemoveNamespacedInformer(key)
}
if lbc.externalDNSController != nil {
lbc.externalDNSController.RemoveNamespacedInformer(key)
}
} else {
// check if informer group already exists
// if not create new namespaced informer group
// update cert-manager informer group if required
// update external-dns informer group if required
glog.V(3).Infof("Adding or Updating Watched Namespace: %v", key)
nsi := lbc.getNamespacedInformer(key)
if nsi == nil {
glog.Infof("Adding New Watched Namespace: %v", key)
nsi = lbc.newNamespacedInformer(key)
nsi.start()
}
if lbc.certManagerController != nil {
lbc.certManagerController.AddNewNamespacedInformer(key)
}
if lbc.externalDNSController != nil {
lbc.externalDNSController.AddNewNamespacedInformer(key)
}
if !cache.WaitForCacheSync(nsi.stopCh, nsi.cacheSyncs...) {
return
}
}
}

func (lbc *LoadBalancerController) removeNamespacedInformer(nsi *namespacedInformer, key string) {
nsi.lock.Lock()
defer nsi.lock.Unlock()
Expand Down
34 changes: 0 additions & 34 deletions internal/k8s/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,40 +349,6 @@ func areResourcesDifferent(oldresource, resource *unstructured.Unstructured) (bo
return !eq, nil
}

// createNamespaceHandlers builds the handler funcs for namespaces
func createNamespaceHandlers(lbc *LoadBalancerController) cache.ResourceEventHandlerFuncs {
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
ns := obj.(*v1.Namespace)
glog.V(3).Infof("Adding Namespace to list of watched Namespaces: %v", ns.Name)
lbc.AddSyncQueue(obj)
},
DeleteFunc: func(obj interface{}) {
ns, isNs := obj.(*v1.Namespace)
if !isNs {
deletedState, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
glog.V(3).Infof("Error received unexpected object: %v", obj)
return
}
ns, ok = deletedState.Obj.(*v1.Namespace)
if !ok {
glog.V(3).Infof("Error DeletedFinalStateUnknown contained non-Namespace object: %v", deletedState.Obj)
return
}
}
glog.V(3).Infof("Removing Namespace from list of watched Namespaces: %v", ns.Name)
lbc.AddSyncQueue(obj)
},
UpdateFunc: func(old, cur interface{}) {
if !reflect.DeepEqual(old, cur) {
glog.V(3).Infof("Namespace %v changed, syncing", cur.(*v1.Namespace).Name)
lbc.AddSyncQueue(cur)
}
},
}
}

func zeroOutVirtualServerSplitWeights(vs *conf_v1.VirtualServer) {
for _, route := range vs.Spec.Routes {
for _, match := range route.Matches {
Expand Down
118 changes: 118 additions & 0 deletions internal/k8s/namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package k8s

import (
"context"
"reflect"

"github.com/golang/glog"
api_v1 "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/tools/cache"
)

// createNamespaceHandlers builds the handler funcs for namespaces
func createNamespaceHandlers(lbc *LoadBalancerController) cache.ResourceEventHandlerFuncs {
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
ns := obj.(*api_v1.Namespace)
glog.V(3).Infof("Adding Namespace to list of watched Namespaces: %v", ns.Name)
lbc.AddSyncQueue(obj)
},
DeleteFunc: func(obj interface{}) {
ns, isNs := obj.(*api_v1.Namespace)
if !isNs {
deletedState, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
glog.V(3).Infof("Error received unexpected object: %v", obj)
return
}
ns, ok = deletedState.Obj.(*api_v1.Namespace)
if !ok {
glog.V(3).Infof("Error DeletedFinalStateUnknown contained non-Namespace object: %v", deletedState.Obj)
return
}
}
glog.V(3).Infof("Removing Namespace from list of watched Namespaces: %v", ns.Name)
lbc.AddSyncQueue(obj)
},
UpdateFunc: func(old, cur interface{}) {
if !reflect.DeepEqual(old, cur) {
glog.V(3).Infof("Namespace %v changed, syncing", cur.(*api_v1.Namespace).Name)
lbc.AddSyncQueue(cur)
}
},
}
}

func (lbc *LoadBalancerController) addNamespaceHandler(handlers cache.ResourceEventHandlerFuncs, nsLabel string) {
optionsModifier := func(options *meta_v1.ListOptions) {
options.LabelSelector = nsLabel
}
nsInformer := informers.NewSharedInformerFactoryWithOptions(lbc.client, lbc.resync, informers.WithTweakListOptions(optionsModifier)).Core().V1().Namespaces().Informer()
nsInformer.AddEventHandler(handlers) //nolint:errcheck,gosec
lbc.namespaceLabeledLister = nsInformer.GetStore()
lbc.namespaceWatcherController = nsInformer

lbc.cacheSyncs = append(lbc.cacheSyncs, nsInformer.HasSynced)
}

func (lbc *LoadBalancerController) syncNamespace(task task) {
key := task.Key
// process namespace and add to / remove from watched namespace list
_, exists, err := lbc.namespaceLabeledLister.GetByKey(key)
if err != nil {
lbc.syncQueue.Requeue(task, err)
return
}

if !exists {
// Check if change is because of a new label, or because of a deleted namespace
ns, _ := lbc.client.CoreV1().Namespaces().Get(context.TODO(), key, meta_v1.GetOptions{})

if ns != nil && ns.Status.Phase == api_v1.NamespaceActive {
// namespace still exists
glog.Infof("Removing Configuration for Unwatched Namespace: %v", key)
// Watched label for namespace was removed
// delete any now unwatched namespaced informer groups if required
nsi := lbc.getNamespacedInformer(key)
if nsi != nil {
lbc.cleanupUnwatchedNamespacedResources(nsi)
delete(lbc.namespacedInformers, key)
}
} else {
glog.Infof("Deleting Watchers for Deleted Namespace: %v", key)
nsi := lbc.getNamespacedInformer(key)
if nsi != nil {
lbc.removeNamespacedInformer(nsi, key)
}
}
if lbc.certManagerController != nil {
lbc.certManagerController.RemoveNamespacedInformer(key)
}
if lbc.externalDNSController != nil {
lbc.externalDNSController.RemoveNamespacedInformer(key)
}
} else {
// check if informer group already exists
// if not create new namespaced informer group
// update cert-manager informer group if required
// update external-dns informer group if required
glog.V(3).Infof("Adding or Updating Watched Namespace: %v", key)
nsi := lbc.getNamespacedInformer(key)
if nsi == nil {
glog.Infof("Adding New Watched Namespace: %v", key)
nsi = lbc.newNamespacedInformer(key)
nsi.start()
}
if lbc.certManagerController != nil {
lbc.certManagerController.AddNewNamespacedInformer(key)
}
if lbc.externalDNSController != nil {
lbc.externalDNSController.AddNewNamespacedInformer(key)
}
if !cache.WaitForCacheSync(nsi.stopCh, nsi.cacheSyncs...) {
return
}
}
}

0 comments on commit 988bc8f

Please sign in to comment.