From 99b6ac313cdba570b9f0ac6f889138b3c3ab2a2f Mon Sep 17 00:00:00 2001 From: Moemen MHEDHBI Date: Wed, 1 Dec 2021 04:43:30 +0100 Subject: [PATCH] BUG/MEDIUM: Update only status of assigned Ingress resources The Ingress code base has diverged between IC 1.6 and newer versions. Thus instead of backporting this Fix, a different solution was applied here by moving handling of ingress status into "controller" package where "igClassIsSupported" can be used to update only status of assigned Ingresses. --- controller/controller.go | 9 ++++----- .../{status/ingress.go => ingress-status.go} | 17 ++++++++++------- controller/kubernetes.go | 9 ++++----- controller/status/status.go | 14 -------------- controller/types.go | 7 +++++++ 5 files changed, 25 insertions(+), 31 deletions(-) rename controller/{status/ingress.go => ingress-status.go} (91%) delete mode 100644 controller/status/status.go diff --git a/controller/controller.go b/controller/controller.go index fd5c6f90..fc0b0579 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -25,7 +25,6 @@ import ( "github.com/haproxytech/kubernetes-ingress/controller/haproxy" "github.com/haproxytech/kubernetes-ingress/controller/haproxy/api" "github.com/haproxytech/kubernetes-ingress/controller/route" - "github.com/haproxytech/kubernetes-ingress/controller/status" "github.com/haproxytech/kubernetes-ingress/controller/store" "github.com/haproxytech/kubernetes-ingress/controller/utils" ) @@ -39,7 +38,7 @@ type HAProxyController struct { PublishService *utils.NamespaceValue AuxCfgModTime int64 eventChan chan SyncDataEvent - statusChan chan status.SyncIngress + statusChan chan SyncIngress k8s *K8s ready bool reload bool @@ -118,8 +117,8 @@ func (c *HAProxyController) Start() { go c.monitorChanges() if c.PublishService != nil { // Update Ingress status - c.statusChan = make(chan status.SyncIngress, watch.DefaultChanSize*6) - go status.UpdateIngress(c.k8s.API, c.Store, c.statusChan) + c.statusChan = make(chan SyncIngress, watch.DefaultChanSize*6) + go c.UpdateIngress() } } @@ -166,7 +165,7 @@ func (c *HAProxyController) updateHAProxy() { } if c.PublishService != nil && ingress.Status == ADDED { select { - case c.statusChan <- status.SyncIngress{Ingress: ingress}: + case c.statusChan <- SyncIngress{Ingress: ingress}: default: logger.Errorf("Ingress %s/%s: unable to sync status: sync channel full", ingress.Namespace, ingress.Name) } diff --git a/controller/status/ingress.go b/controller/ingress-status.go similarity index 91% rename from controller/status/ingress.go rename to controller/ingress-status.go index 1a25d7b5..93e39671 100644 --- a/controller/status/ingress.go +++ b/controller/ingress-status.go @@ -1,4 +1,4 @@ -package status +package controller import ( "context" @@ -16,20 +16,23 @@ import ( "github.com/haproxytech/kubernetes-ingress/controller/store" ) -func UpdateIngress(client *kubernetes.Clientset, k store.K8s, channel chan SyncIngress) { +func (c *HAProxyController) UpdateIngress() { addresses := []string{} - for status := range channel { + for status := range c.statusChan { // Published Service updated: Update all Ingresses if status.Service != nil && getServiceAddresses(status.Service, &addresses) { logger.Debug("Addresses of Ingress Controller service changed, status of all ingress resources are going to be updated") - for _, ns := range k.Namespaces { - for _, ingress := range k.Namespaces[ns.Name].Ingresses { - logger.Error(updateIngressStatus(client, ingress, addresses)) + for _, ns := range c.Store.Namespaces { + for _, ingress := range c.Store.Namespaces[ns.Name].Ingresses { + if !c.igClassIsSupported(ingress) { + continue + } + logger.Error(updateIngressStatus(c.k8s.API, ingress, addresses)) } } } if status.Ingress != nil { - logger.Error(updateIngressStatus(client, status.Ingress, addresses)) + logger.Error(updateIngressStatus(c.k8s.API, status.Ingress, addresses)) } } } diff --git a/controller/kubernetes.go b/controller/kubernetes.go index ba360757..6f110bbf 100644 --- a/controller/kubernetes.go +++ b/controller/kubernetes.go @@ -23,7 +23,6 @@ import ( "k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/clientcmd" - ingstatus "github.com/haproxytech/kubernetes-ingress/controller/status" "github.com/haproxytech/kubernetes-ingress/controller/store" "github.com/haproxytech/kubernetes-ingress/controller/utils" ) @@ -336,7 +335,7 @@ func (k *K8s) EventsIngresses(channel chan SyncDataEvent, stop chan struct{}, in go informer.Run(stop) } -func (k *K8s) EventsServices(channel chan SyncDataEvent, ingChan chan ingstatus.SyncIngress, stop chan struct{}, informer cache.SharedIndexInformer, publishSvc *utils.NamespaceValue) { +func (k *K8s) EventsServices(channel chan SyncDataEvent, ingChan chan SyncIngress, stop chan struct{}, informer cache.SharedIndexInformer, publishSvc *utils.NamespaceValue) { informer.AddEventHandler(cache.ResourceEventHandlerFuncs{ AddFunc: func(obj interface{}) { data, ok := obj.(*corev1.Service) @@ -374,7 +373,7 @@ func (k *K8s) EventsServices(channel chan SyncDataEvent, ingChan chan ingstatus. k.Logger.Tracef("%s %s: %s", SERVICE, item.Status, item.Name) channel <- SyncDataEvent{SyncType: SERVICE, Namespace: item.Namespace, Data: item} if publishSvc != nil && publishSvc.Namespace == data.Namespace && publishSvc.Name == data.Name { - ingChan <- ingstatus.SyncIngress{Service: data} + ingChan <- SyncIngress{Service: data} } }, DeleteFunc: func(obj interface{}) { @@ -400,7 +399,7 @@ func (k *K8s) EventsServices(channel chan SyncDataEvent, ingChan chan ingstatus. k.Logger.Tracef("%s %s: %s", SERVICE, item.Status, item.Name) channel <- SyncDataEvent{SyncType: SERVICE, Namespace: item.Namespace, Data: item} if publishSvc != nil && publishSvc.Namespace == data.Namespace && publishSvc.Name == data.Name { - ingChan <- ingstatus.SyncIngress{Service: data} + ingChan <- SyncIngress{Service: data} } }, UpdateFunc: func(oldObj, newObj interface{}) { @@ -423,7 +422,7 @@ func (k *K8s) EventsServices(channel chan SyncDataEvent, ingChan chan ingstatus. return } if publishSvc != nil && publishSvc.Namespace == data2.Namespace && publishSvc.Name == data2.Name { - ingChan <- ingstatus.SyncIngress{Service: data2} + ingChan <- SyncIngress{Service: data2} } status := MODIFIED item1 := &store.Service{ diff --git a/controller/status/status.go b/controller/status/status.go deleted file mode 100644 index e190eccd..00000000 --- a/controller/status/status.go +++ /dev/null @@ -1,14 +0,0 @@ -package status - -import ( - "github.com/haproxytech/kubernetes-ingress/controller/store" - "github.com/haproxytech/kubernetes-ingress/controller/utils" - corev1 "k8s.io/api/core/v1" -) - -var logger = utils.GetLogger() - -type SyncIngress struct { - Service *corev1.Service - Ingress *store.Ingress -} diff --git a/controller/types.go b/controller/types.go index 21e06834..70101dbe 100644 --- a/controller/types.go +++ b/controller/types.go @@ -15,6 +15,8 @@ package controller import ( + corev1 "k8s.io/api/core/v1" + "github.com/haproxytech/kubernetes-ingress/controller/store" "github.com/haproxytech/kubernetes-ingress/controller/utils" ) @@ -30,6 +32,11 @@ type SyncDataEvent struct { Data interface{} } +type SyncIngress struct { + Service *corev1.Service + Ingress *store.Ingress +} + type Mode string //nolint:golint,stylecheck