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