Skip to content

Commit 0102bf5

Browse files
authored
move Namespace Controller to it's own file (#6396)
1 parent 0998e06 commit 0102bf5

File tree

3 files changed

+118
-106
lines changed

3 files changed

+118
-106
lines changed

internal/k8s/controller.go

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -548,18 +548,6 @@ func (nsi *namespacedInformer) addVirtualServerRouteHandler(handlers cache.Resou
548548
nsi.cacheSyncs = append(nsi.cacheSyncs, informer.HasSynced)
549549
}
550550

551-
func (lbc *LoadBalancerController) addNamespaceHandler(handlers cache.ResourceEventHandlerFuncs, nsLabel string) {
552-
optionsModifier := func(options *meta_v1.ListOptions) {
553-
options.LabelSelector = nsLabel
554-
}
555-
nsInformer := informers.NewSharedInformerFactoryWithOptions(lbc.client, lbc.resync, informers.WithTweakListOptions(optionsModifier)).Core().V1().Namespaces().Informer()
556-
nsInformer.AddEventHandler(handlers)
557-
lbc.namespaceLabeledLister = nsInformer.GetStore()
558-
lbc.namespaceWatcherController = nsInformer
559-
560-
lbc.cacheSyncs = append(lbc.cacheSyncs, nsInformer.HasSynced)
561-
}
562-
563551
// Run starts the loadbalancer controller
564552
func (lbc *LoadBalancerController) Run() {
565553
lbc.ctx, lbc.cancel = context.WithCancel(context.Background())
@@ -1015,66 +1003,6 @@ func (lbc *LoadBalancerController) sync(task task) {
10151003
}
10161004
}
10171005

1018-
func (lbc *LoadBalancerController) syncNamespace(task task) {
1019-
key := task.Key
1020-
// process namespace and add to / remove from watched namespace list
1021-
_, exists, err := lbc.namespaceLabeledLister.GetByKey(key)
1022-
if err != nil {
1023-
lbc.syncQueue.Requeue(task, err)
1024-
return
1025-
}
1026-
1027-
if !exists {
1028-
// Check if change is because of a new label, or because of a deleted namespace
1029-
ns, _ := lbc.client.CoreV1().Namespaces().Get(context.TODO(), key, meta_v1.GetOptions{})
1030-
1031-
if ns != nil && ns.Status.Phase == api_v1.NamespaceActive {
1032-
// namespace still exists
1033-
glog.Infof("Removing Configuration for Unwatched Namespace: %v", key)
1034-
// Watched label for namespace was removed
1035-
// delete any now unwatched namespaced informer groups if required
1036-
nsi := lbc.getNamespacedInformer(key)
1037-
if nsi != nil {
1038-
lbc.cleanupUnwatchedNamespacedResources(nsi)
1039-
delete(lbc.namespacedInformers, key)
1040-
}
1041-
} else {
1042-
glog.Infof("Deleting Watchers for Deleted Namespace: %v", key)
1043-
nsi := lbc.getNamespacedInformer(key)
1044-
if nsi != nil {
1045-
lbc.removeNamespacedInformer(nsi, key)
1046-
}
1047-
}
1048-
if lbc.certManagerController != nil {
1049-
lbc.certManagerController.RemoveNamespacedInformer(key)
1050-
}
1051-
if lbc.externalDNSController != nil {
1052-
lbc.externalDNSController.RemoveNamespacedInformer(key)
1053-
}
1054-
} else {
1055-
// check if informer group already exists
1056-
// if not create new namespaced informer group
1057-
// update cert-manager informer group if required
1058-
// update external-dns informer group if required
1059-
glog.V(3).Infof("Adding or Updating Watched Namespace: %v", key)
1060-
nsi := lbc.getNamespacedInformer(key)
1061-
if nsi == nil {
1062-
glog.Infof("Adding New Watched Namespace: %v", key)
1063-
nsi = lbc.newNamespacedInformer(key)
1064-
nsi.start()
1065-
}
1066-
if lbc.certManagerController != nil {
1067-
lbc.certManagerController.AddNewNamespacedInformer(key)
1068-
}
1069-
if lbc.externalDNSController != nil {
1070-
lbc.externalDNSController.AddNewNamespacedInformer(key)
1071-
}
1072-
if !cache.WaitForCacheSync(nsi.stopCh, nsi.cacheSyncs...) {
1073-
return
1074-
}
1075-
}
1076-
}
1077-
10781006
func (lbc *LoadBalancerController) removeNamespacedInformer(nsi *namespacedInformer, key string) {
10791007
nsi.lock.Lock()
10801008
defer nsi.lock.Unlock()

internal/k8s/handlers.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -349,40 +349,6 @@ func areResourcesDifferent(oldresource, resource *unstructured.Unstructured) (bo
349349
return !eq, nil
350350
}
351351

352-
// createNamespaceHandlers builds the handler funcs for namespaces
353-
func createNamespaceHandlers(lbc *LoadBalancerController) cache.ResourceEventHandlerFuncs {
354-
return cache.ResourceEventHandlerFuncs{
355-
AddFunc: func(obj interface{}) {
356-
ns := obj.(*v1.Namespace)
357-
glog.V(3).Infof("Adding Namespace to list of watched Namespaces: %v", ns.Name)
358-
lbc.AddSyncQueue(obj)
359-
},
360-
DeleteFunc: func(obj interface{}) {
361-
ns, isNs := obj.(*v1.Namespace)
362-
if !isNs {
363-
deletedState, ok := obj.(cache.DeletedFinalStateUnknown)
364-
if !ok {
365-
glog.V(3).Infof("Error received unexpected object: %v", obj)
366-
return
367-
}
368-
ns, ok = deletedState.Obj.(*v1.Namespace)
369-
if !ok {
370-
glog.V(3).Infof("Error DeletedFinalStateUnknown contained non-Namespace object: %v", deletedState.Obj)
371-
return
372-
}
373-
}
374-
glog.V(3).Infof("Removing Namespace from list of watched Namespaces: %v", ns.Name)
375-
lbc.AddSyncQueue(obj)
376-
},
377-
UpdateFunc: func(old, cur interface{}) {
378-
if !reflect.DeepEqual(old, cur) {
379-
glog.V(3).Infof("Namespace %v changed, syncing", cur.(*v1.Namespace).Name)
380-
lbc.AddSyncQueue(cur)
381-
}
382-
},
383-
}
384-
}
385-
386352
func zeroOutVirtualServerSplitWeights(vs *conf_v1.VirtualServer) {
387353
for _, route := range vs.Spec.Routes {
388354
for _, match := range route.Matches {

internal/k8s/namespace.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package k8s
2+
3+
import (
4+
"context"
5+
"reflect"
6+
7+
"github.com/golang/glog"
8+
api_v1 "k8s.io/api/core/v1"
9+
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"k8s.io/client-go/informers"
11+
"k8s.io/client-go/tools/cache"
12+
)
13+
14+
// createNamespaceHandlers builds the handler funcs for namespaces
15+
func createNamespaceHandlers(lbc *LoadBalancerController) cache.ResourceEventHandlerFuncs {
16+
return cache.ResourceEventHandlerFuncs{
17+
AddFunc: func(obj interface{}) {
18+
ns := obj.(*api_v1.Namespace)
19+
glog.V(3).Infof("Adding Namespace to list of watched Namespaces: %v", ns.Name)
20+
lbc.AddSyncQueue(obj)
21+
},
22+
DeleteFunc: func(obj interface{}) {
23+
ns, isNs := obj.(*api_v1.Namespace)
24+
if !isNs {
25+
deletedState, ok := obj.(cache.DeletedFinalStateUnknown)
26+
if !ok {
27+
glog.V(3).Infof("Error received unexpected object: %v", obj)
28+
return
29+
}
30+
ns, ok = deletedState.Obj.(*api_v1.Namespace)
31+
if !ok {
32+
glog.V(3).Infof("Error DeletedFinalStateUnknown contained non-Namespace object: %v", deletedState.Obj)
33+
return
34+
}
35+
}
36+
glog.V(3).Infof("Removing Namespace from list of watched Namespaces: %v", ns.Name)
37+
lbc.AddSyncQueue(obj)
38+
},
39+
UpdateFunc: func(old, cur interface{}) {
40+
if !reflect.DeepEqual(old, cur) {
41+
glog.V(3).Infof("Namespace %v changed, syncing", cur.(*api_v1.Namespace).Name)
42+
lbc.AddSyncQueue(cur)
43+
}
44+
},
45+
}
46+
}
47+
48+
func (lbc *LoadBalancerController) addNamespaceHandler(handlers cache.ResourceEventHandlerFuncs, nsLabel string) {
49+
optionsModifier := func(options *meta_v1.ListOptions) {
50+
options.LabelSelector = nsLabel
51+
}
52+
nsInformer := informers.NewSharedInformerFactoryWithOptions(lbc.client, lbc.resync, informers.WithTweakListOptions(optionsModifier)).Core().V1().Namespaces().Informer()
53+
nsInformer.AddEventHandler(handlers) //nolint:errcheck,gosec
54+
lbc.namespaceLabeledLister = nsInformer.GetStore()
55+
lbc.namespaceWatcherController = nsInformer
56+
57+
lbc.cacheSyncs = append(lbc.cacheSyncs, nsInformer.HasSynced)
58+
}
59+
60+
func (lbc *LoadBalancerController) syncNamespace(task task) {
61+
key := task.Key
62+
// process namespace and add to / remove from watched namespace list
63+
_, exists, err := lbc.namespaceLabeledLister.GetByKey(key)
64+
if err != nil {
65+
lbc.syncQueue.Requeue(task, err)
66+
return
67+
}
68+
69+
if !exists {
70+
// Check if change is because of a new label, or because of a deleted namespace
71+
ns, _ := lbc.client.CoreV1().Namespaces().Get(context.TODO(), key, meta_v1.GetOptions{})
72+
73+
if ns != nil && ns.Status.Phase == api_v1.NamespaceActive {
74+
// namespace still exists
75+
glog.Infof("Removing Configuration for Unwatched Namespace: %v", key)
76+
// Watched label for namespace was removed
77+
// delete any now unwatched namespaced informer groups if required
78+
nsi := lbc.getNamespacedInformer(key)
79+
if nsi != nil {
80+
lbc.cleanupUnwatchedNamespacedResources(nsi)
81+
delete(lbc.namespacedInformers, key)
82+
}
83+
} else {
84+
glog.Infof("Deleting Watchers for Deleted Namespace: %v", key)
85+
nsi := lbc.getNamespacedInformer(key)
86+
if nsi != nil {
87+
lbc.removeNamespacedInformer(nsi, key)
88+
}
89+
}
90+
if lbc.certManagerController != nil {
91+
lbc.certManagerController.RemoveNamespacedInformer(key)
92+
}
93+
if lbc.externalDNSController != nil {
94+
lbc.externalDNSController.RemoveNamespacedInformer(key)
95+
}
96+
} else {
97+
// check if informer group already exists
98+
// if not create new namespaced informer group
99+
// update cert-manager informer group if required
100+
// update external-dns informer group if required
101+
glog.V(3).Infof("Adding or Updating Watched Namespace: %v", key)
102+
nsi := lbc.getNamespacedInformer(key)
103+
if nsi == nil {
104+
glog.Infof("Adding New Watched Namespace: %v", key)
105+
nsi = lbc.newNamespacedInformer(key)
106+
nsi.start()
107+
}
108+
if lbc.certManagerController != nil {
109+
lbc.certManagerController.AddNewNamespacedInformer(key)
110+
}
111+
if lbc.externalDNSController != nil {
112+
lbc.externalDNSController.AddNewNamespacedInformer(key)
113+
}
114+
if !cache.WaitForCacheSync(nsi.stopCh, nsi.cacheSyncs...) {
115+
return
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)