diff --git a/controller/annotations/ingress/httpsRedirect.go b/controller/annotations/ingress/httpsRedirect.go index afb68518..63da4ec7 100644 --- a/controller/annotations/ingress/httpsRedirect.go +++ b/controller/annotations/ingress/httpsRedirect.go @@ -90,10 +90,8 @@ func tlsEnabled(ingress *store.Ingress) bool { if ingress == nil { return false } - for _, tls := range ingress.TLS { - if tls.Status != store.DELETED { - return true - } + if len(ingress.TLS) == 0 { + return false } - return false + return true } diff --git a/controller/controller.go b/controller/controller.go index eca39837..355c96f1 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -160,9 +160,6 @@ func (c *HAProxyController) updateHAProxy() { continue } for _, ingResource := range namespace.Ingresses { - if ingResource.Status == DELETED { - continue - } i := ingress.New(c.Store, ingResource, c.OSArgs.IngressClass, c.OSArgs.EmptyIngressClass) if i == nil { logger.Debugf("ingress '%s/%s' ignored: no matching IngressClass", ingResource.Namespace, ingResource.Name) diff --git a/controller/ingress/ingress.go b/controller/ingress/ingress.go index 4d6c4d15..dedae2bb 100644 --- a/controller/ingress/ingress.go +++ b/controller/ingress/ingress.go @@ -56,7 +56,7 @@ func New(k store.K8s, resource *store.Ingress, class string, emptyClass bool) *I func (i Ingress) supported(k8s store.K8s) (supported bool) { var igClassAnn, igClassSpec string igClassAnn = annotations.String("ingress.class", i.resource.Annotations) - if igClassResource := k8s.IngressClasses[i.resource.Class]; igClassResource != nil && igClassResource.Status != store.DELETED { + if igClassResource := k8s.IngressClasses[i.resource.Class]; igClassResource != nil { igClassSpec = igClassResource.Controller } if igClassAnn == "" && i.resource.Class == "" { @@ -99,9 +99,6 @@ func (i *Ingress) handlePath(k store.K8s, cfg *configuration.ControllerCfg, api if err != nil { return } - if path.Status == store.DELETED { - return - } // Backend backendReload, err := svc.HandleBackend(api, k) if err != nil { @@ -210,9 +207,6 @@ func (i *Ingress) Update(k store.K8s, cfg *configuration.ControllerCfg, api api. // Ingress secrets logger.Tracef("Ingress '%s/%s': processing secrets...", i.resource.Namespace, i.resource.Name) for _, tls := range i.resource.TLS { - if tls.Status == store.DELETED { - continue - } secret, secErr := k.GetSecret(i.resource.Namespace, tls.SecretName) if secErr != nil { logger.Warningf("Ingress '%s/%s': %s", i.resource.Namespace, i.resource.Name, secErr) diff --git a/controller/kubernetes.go b/controller/kubernetes.go index b780f660..44908852 100644 --- a/controller/kubernetes.go +++ b/controller/kubernetes.go @@ -385,25 +385,15 @@ func (k *K8s) EventsIngressClass(channel chan SyncDataEvent, stop chan struct{}, channel <- SyncDataEvent{SyncType: INGRESS_CLASS, Data: item} }, UpdateFunc: func(oldObj, newObj interface{}) { - item1, err := store.ConvertToIngressClass(oldObj) - if err != nil { - k.Logger.Errorf("%s: Invalid data from k8s api, %s", INGRESS_CLASS, oldObj) - return - } - item1.Status = MODIFIED - - item2, err := store.ConvertToIngressClass(newObj) + item, err := store.ConvertToIngressClass(newObj) if err != nil { k.Logger.Errorf("%s: Invalid data from k8s api, %s", INGRESS, oldObj) return } - item1.Status = MODIFIED + item.Status = MODIFIED - if item2.Equal(item1) { - return - } - k.Logger.Tracef("%s %s: %s", INGRESS_CLASS, item2.Status, item2.Name) - channel <- SyncDataEvent{SyncType: INGRESS_CLASS, Data: item2} + k.Logger.Tracef("%s %s: %s", INGRESS_CLASS, item.Status, item.Name) + channel <- SyncDataEvent{SyncType: INGRESS_CLASS, Data: item} }, }, ) @@ -433,25 +423,14 @@ func (k *K8s) EventsIngresses(channel chan SyncDataEvent, stop chan struct{}, in channel <- SyncDataEvent{SyncType: INGRESS, Namespace: item.Namespace, Data: item} }, UpdateFunc: func(oldObj, newObj interface{}) { - item1, err := store.ConvertToIngress(oldObj) - if err != nil { - k.Logger.Errorf("%s: Invalid data from k8s api, %s", INGRESS, oldObj) - return - } - item1.Status = MODIFIED - - item2, err := store.ConvertToIngress(newObj) + item, err := store.ConvertToIngress(newObj) if err != nil { k.Logger.Errorf("%s: Invalid data from k8s api, %s", INGRESS, oldObj) return } - item1.Status = MODIFIED - - if item2.Equal(item1) { - return - } - k.Logger.Tracef("%s %s: %s", INGRESS, item2.Status, item2.Name) - channel <- SyncDataEvent{SyncType: INGRESS, Namespace: item2.Namespace, Data: item2} + item.Status = MODIFIED + k.Logger.Tracef("%s %s: %s", INGRESS, item.Status, item.Name) + channel <- SyncDataEvent{SyncType: INGRESS, Namespace: item.Namespace, Data: item} }, }, ) diff --git a/controller/monitor.go b/controller/monitor.go index d1cd5e27..98766cdc 100644 --- a/controller/monitor.go +++ b/controller/monitor.go @@ -113,7 +113,7 @@ func (c *HAProxyController) SyncData() { case NAMESPACE: change = c.Store.EventNamespace(ns, job.Data.(*store.Namespace)) case INGRESS: - change = c.Store.EventIngress(ns, job.Data.(*store.Ingress), c.OSArgs.IngressClass) + change = c.Store.EventIngress(ns, job.Data.(*store.Ingress)) case INGRESS_CLASS: change = c.Store.EventIngressClass(job.Data.(*store.IngressClass)) case ENDPOINTS: diff --git a/controller/store/convert.go b/controller/store/convert.go index 9bb29552..251c019a 100644 --- a/controller/store/convert.go +++ b/controller/store/convert.go @@ -110,7 +110,6 @@ func (n ingressNetworkingV1Beta1Strategy) ConvertIngress() *Ingress { SvcName: k8sPath.Backend.ServiceName, SvcPortInt: int64(k8sPath.Backend.ServicePort.IntValue()), SvcPortString: k8sPath.Backend.ServicePort.StrVal, - Status: "", } } if rule, ok := rules[k8sRule.Host]; ok { @@ -119,9 +118,8 @@ func (n ingressNetworkingV1Beta1Strategy) ConvertIngress() *Ingress { } } else { rules[k8sRule.Host] = &IngressRule{ - Host: k8sRule.Host, - Paths: paths, - Status: "", + Host: k8sRule.Host, + Paths: paths, } } } @@ -137,7 +135,6 @@ func (n ingressNetworkingV1Beta1Strategy) ConvertIngress() *Ingress { SvcPortInt: int64(ingressBackend.ServicePort.IntValue()), SvcPortString: ingressBackend.ServicePort.StrVal, IsDefaultBackend: true, - Status: "", } }(n.ig.Spec.Backend), TLS: func(ingressTLS []networkingv1beta1.IngressTLS) map[string]*IngressTLS { @@ -147,18 +144,11 @@ func (n ingressNetworkingV1Beta1Strategy) ConvertIngress() *Ingress { tls[host] = &IngressTLS{ Host: host, SecretName: k8sTLS.SecretName, - Status: EMPTY, } } } return tls }(n.ig.Spec.TLS), - Status: func() Status { - if n.ig.ObjectMeta.GetDeletionTimestamp() != nil { - return DELETED - } - return ADDED - }(), } } @@ -172,12 +162,6 @@ func (n ingressNetworkingV1Beta1Strategy) ConvertClass() *IngressClass { Name: n.class.GetName(), Controller: n.class.Spec.Controller, Annotations: annotations, - Status: func() Status { - if n.class.ObjectMeta.GetDeletionTimestamp() != nil { - return DELETED - } - return ADDED - }(), } } @@ -213,7 +197,6 @@ func (e ingressExtensionsStrategy) ConvertIngress() *Ingress { SvcName: k8sPath.Backend.ServiceName, SvcPortInt: int64(k8sPath.Backend.ServicePort.IntValue()), SvcPortString: k8sPath.Backend.ServicePort.StrVal, - Status: "", } } if rule, ok := rules[k8sRule.Host]; ok { @@ -222,9 +205,8 @@ func (e ingressExtensionsStrategy) ConvertIngress() *Ingress { } } else { rules[k8sRule.Host] = &IngressRule{ - Host: k8sRule.Host, - Paths: paths, - Status: "", + Host: k8sRule.Host, + Paths: paths, } } } @@ -240,7 +222,6 @@ func (e ingressExtensionsStrategy) ConvertIngress() *Ingress { SvcPortInt: int64(ingressBackend.ServicePort.IntValue()), SvcPortString: ingressBackend.ServicePort.StrVal, IsDefaultBackend: true, - Status: "", } }(e.ig.Spec.Backend), TLS: func(ingressTLS []extensionsv1beta1.IngressTLS) map[string]*IngressTLS { @@ -250,18 +231,11 @@ func (e ingressExtensionsStrategy) ConvertIngress() *Ingress { tls[host] = &IngressTLS{ Host: host, SecretName: k8sTLS.SecretName, - Status: EMPTY, } } } return tls }(e.ig.Spec.TLS), - Status: func() Status { - if e.ig.ObjectMeta.GetDeletionTimestamp() != nil { - return DELETED - } - return ADDED - }(), } } @@ -297,7 +271,6 @@ func (n ingressNetworkingV1Strategy) ConvertIngress() *Ingress { Path: k8sPath.Path, PathTypeMatch: pathType, SvcNamespace: n.ig.GetNamespace(), - Status: "", } if k8sPath.Backend.Service != nil { paths[pathKey].SvcName = k8sPath.Backend.Service.Name @@ -311,9 +284,8 @@ func (n ingressNetworkingV1Strategy) ConvertIngress() *Ingress { } } else { rules[k8sRule.Host] = &IngressRule{ - Host: k8sRule.Host, - Paths: paths, - Status: "", + Host: k8sRule.Host, + Paths: paths, } } } @@ -326,7 +298,6 @@ func (n ingressNetworkingV1Strategy) ConvertIngress() *Ingress { ingPath := &IngressPath{ SvcNamespace: n.ig.GetNamespace(), IsDefaultBackend: true, - Status: "", } if ingressBackend.Service != nil { ingPath.SvcName = ingressBackend.Service.Name @@ -342,18 +313,11 @@ func (n ingressNetworkingV1Strategy) ConvertIngress() *Ingress { tls[host] = &IngressTLS{ Host: host, SecretName: k8sTLS.SecretName, - Status: EMPTY, } } } return tls }(n.ig.Spec.TLS), - Status: func() Status { - if n.ig.ObjectMeta.GetDeletionTimestamp() != nil { - return DELETED - } - return ADDED - }(), } } @@ -367,12 +331,6 @@ func (n ingressNetworkingV1Strategy) ConvertClass() *IngressClass { Name: n.class.GetName(), Controller: n.class.Spec.Controller, Annotations: annotations, - Status: func() Status { - if n.class.ObjectMeta.GetDeletionTimestamp() != nil { - return DELETED - } - return ADDED - }(), } } diff --git a/controller/store/events.go b/controller/store/events.go index 7eab61f6..cbbd9fa7 100644 --- a/controller/store/events.go +++ b/controller/store/events.go @@ -32,176 +32,21 @@ func (k *K8s) EventNamespace(ns *Namespace, data *Namespace) (updateRequired boo } func (k *K8s) EventIngressClass(data *IngressClass) (updateRequired bool) { - switch data.Status { - case MODIFIED: - newIgClass := data - oldIgClass, ok := k.IngressClasses[data.Name] - if !ok { - logger.Warningf("IngressClass '%s' not registered with controller !", data.Name) - return false - } - if newIgClass.Equal(oldIgClass) { - return false - } - k.IngressClasses[data.Name] = newIgClass - updateRequired = true - case ADDED: - if old, ok := k.IngressClasses[data.Name]; ok { - if old.Status == DELETED { - k.IngressClasses[data.Name].Status = ADDED - } - if !old.Equal(data) { - data.Status = MODIFIED - return k.EventIngressClass(data) - } - return false - } + if data.Status == DELETED { + delete(k.IngressClasses, data.Name) + } else { k.IngressClasses[data.Name] = data - updateRequired = true - case DELETED: - igClass, ok := k.IngressClasses[data.Name] - if ok { - igClass.Status = DELETED - updateRequired = true - } else { - logger.Warningf("IngressClass '%s' not registered with controller, cannot delete !", data.Name) - } } - return updateRequired + return true } -func (k *K8s) EventIngress(ns *Namespace, data *Ingress, controllerClass string) (updateRequired bool) { - updateRequired = false - switch data.Status { - case MODIFIED: - newIngress := data - oldIngress, ok := ns.Ingresses[data.Name] - if !ok { - newIngress.Status = ADDED - return k.EventIngress(ns, newIngress, controllerClass) - } - newIngress.Ignored = oldIngress.Ignored - if oldIngress.Equal(data) { - return false - } - for host, tls := range newIngress.TLS { - _, ok := oldIngress.TLS[host] - if !ok { - tls.Status = ADDED - continue - } - } - for host, tls := range oldIngress.TLS { - _, ok := newIngress.TLS[host] - if !ok { - newIngress.TLS[host] = &IngressTLS{ - Host: host, - SecretName: tls.SecretName, - Status: DELETED, - } - continue - } - } - - // so see what exactly has changed in there - // DefaultBackend - newDtBd := newIngress.DefaultBackend - oldDtBd := oldIngress.DefaultBackend - if newDtBd != nil && !oldDtBd.Equal(newDtBd) { - newDtBd.Status = MODIFIED - } - // Rules - for _, newRule := range newIngress.Rules { - if oldRule, ok := oldIngress.Rules[newRule.Host]; ok { - // so we need to compare if anything is different - for _, newPath := range newRule.Paths { - if oldPath, ok := oldRule.Paths[newPath.Path]; ok { - // compare path for differences - if newPath.SvcName != oldPath.SvcName || - newPath.SvcPortInt != oldPath.SvcPortInt || - newPath.SvcPortString != oldPath.SvcPortString { - newPath.Status = MODIFIED - newRule.Status = MODIFIED - } - // Sync internal data - newPath.IsDefaultBackend = oldPath.IsDefaultBackend - } else { - newPath.Status = ADDED - newRule.Status = ADDED - } - } - for _, oldPath := range oldRule.Paths { - if _, ok := newRule.Paths[oldPath.Path]; !ok { - oldPath.Status = DELETED - newRule.Paths[oldPath.Path] = oldPath - } - } - } else { - newRule.Status = ADDED - for _, path := range newRule.Paths { - path.Status = ADDED - } - } - } - for _, oldRule := range oldIngress.Rules { - if _, ok := newIngress.Rules[oldRule.Host]; !ok { - oldRule.Status = DELETED - for _, path := range oldRule.Paths { - path.Status = DELETED - } - newIngress.Rules[oldRule.Host] = oldRule - } - } - ns.Ingresses[data.Name] = newIngress - updateRequired = true - case ADDED: - if old, ok := ns.Ingresses[data.Name]; ok { - if old.Status == DELETED { - ns.Ingresses[data.Name].Status = ADDED - } - data.Status = old.Status - if !old.Equal(data) { - data.Status = MODIFIED - return k.EventIngress(ns, data, controllerClass) - } - return updateRequired - } +func (k *K8s) EventIngress(ns *Namespace, data *Ingress) (updateRequired bool) { + if data.Status == DELETED { + delete(ns.Ingresses, data.Name) + } else { ns.Ingresses[data.Name] = data - - if data.DefaultBackend != nil { - data.DefaultBackend.Status = ADDED - } - for _, newRule := range data.Rules { - for _, newPath := range newRule.Paths { - newPath.Status = ADDED - } - } - for _, tls := range data.TLS { - tls.Status = ADDED - } - updateRequired = true - case DELETED: - ingress, ok := ns.Ingresses[data.Name] - if ok { - ingress.Status = DELETED - if ingress.DefaultBackend != nil { - ingress.DefaultBackend.Status = DELETED - } - for _, rule := range ingress.Rules { - rule.Status = DELETED - for _, path := range rule.Paths { - path.Status = DELETED - } - } - for _, tls := range ingress.TLS { - tls.Status = DELETED - } - updateRequired = true - } else { - logger.Warningf("Ingress '%s' not registered with controller, cannot delete !", data.Name) - } } - return updateRequired + return true } func getEndpoints(slices map[string]*Endpoints) (endpoints map[string]PortEndpoints) { diff --git a/controller/store/store.go b/controller/store/store.go index 4dbf3f10..a68f98c1 100644 --- a/controller/store/store.go +++ b/controller/store/store.go @@ -71,41 +71,7 @@ func NewK8sStore(args utils.OSArgs) K8s { func (k K8s) Clean() { for _, namespace := range k.Namespaces { for _, data := range namespace.Ingresses { - for _, tls := range data.TLS { - switch tls.Status { - case DELETED: - delete(data.TLS, tls.Host) - continue - default: - tls.Status = EMPTY - } - } - if data.DefaultBackend != nil { - data.DefaultBackend.Status = EMPTY - } - for _, rule := range data.Rules { - switch rule.Status { - case DELETED: - delete(data.Rules, rule.Host) - continue - default: - rule.Status = EMPTY - for _, path := range rule.Paths { - switch path.Status { - case DELETED: - delete(rule.Paths, path.Path) - default: - path.Status = EMPTY - } - } - } - } - switch data.Status { - case DELETED: - delete(namespace.Ingresses, data.Name) - default: - data.Status = EMPTY - } + data.Status = EMPTY } for _, data := range namespace.Services { switch data.Status { @@ -143,6 +109,9 @@ func (k K8s) Clean() { } } } + for _, igClass := range k.IngressClasses { + igClass.Status = EMPTY + } for _, cm := range []*ConfigMap{k.ConfigMaps.Main, k.ConfigMaps.TCPServices, k.ConfigMaps.Errorfiles} { switch cm.Status { case DELETED: @@ -152,14 +121,6 @@ func (k K8s) Clean() { cm.Status = EMPTY } } - for _, igClass := range k.IngressClasses { - switch igClass.Status { - case DELETED: - delete(k.IngressClasses, igClass.Name) - default: - igClass.Status = EMPTY - } - } } // GetNamespace returns Namespace. Creates one if not existing diff --git a/controller/store/types-equal.go b/controller/store/types-equal.go index 2c833c10..5500bd47 100644 --- a/controller/store/types-equal.go +++ b/controller/store/types-equal.go @@ -23,118 +23,6 @@ func (a *ServicePort) Equal(b *ServicePort) bool { return true } -// Equal checks if IngressClasses are equal -func (a *IngressClass) Equal(b *IngressClass) bool { - if a == nil || b == nil { - return false - } - if a.Name != b.Name { - return false - } - if a.Controller != b.Controller { - return false - } - return true -} - -// Equal checks if Ingress Paths are equal -func (a *IngressPath) Equal(b *IngressPath) bool { - if a == nil || b == nil { - return false - } - if a.Path != b.Path { - return false - } - if a.SvcName != b.SvcName { - return false - } - if a.SvcPortInt != b.SvcPortInt { - return false - } - if a.SvcPortString != b.SvcPortString { - return false - } - return true -} - -// Equal checks if Ingress Rules are equal -func (a *IngressRule) Equal(b *IngressRule) bool { - if a == nil || b == nil { - return false - } - if a.Host != b.Host { - return false - } - if len(a.Paths) != len(b.Paths) { - return false - } - for key, value := range a.Paths { - value2, ok := b.Paths[key] - if !ok || !value.Equal(value2) { - return false - } - } - return true -} - -// Equal checks if Ingress secrets are equal -func (a *IngressTLS) Equal(b *IngressTLS) bool { - if a == nil || b == nil { - return false - } - if a.Host != b.Host { - return false - } - if a.SecretName != b.SecretName { - return false - } - return true -} - -// Equal compares two Ingresses, ignores -func (a *Ingress) Equal(b *Ingress) bool { - if a == nil || b == nil { - return false - } - if a.Name != b.Name { - return false - } - if a.Class != b.Class { - return false - } - if len(a.Rules) != len(b.Rules) { - return false - } - for k, v := range a.Rules { - value, ok := b.Rules[k] - if !ok || !v.Equal(value) { - return false - } - } - if len(a.TLS) != len(b.TLS) { - return false - } - for k, v := range a.TLS { - value, ok := b.TLS[k] - if !ok || !v.Equal(value) { - return false - } - } - if a.DefaultBackend != b.DefaultBackend && !a.DefaultBackend.Equal(b.DefaultBackend) { - return false - } - if len(a.Annotations) != len(b.Annotations) { - return false - } - for name, value1 := range a.Annotations { - value2 := b.Annotations[name] - if value1 != value2 { - return false - } - } - return true -} - // Equal compares two services, ignores statuses and old values func (a *Service) Equal(b *Service) bool { if a == nil || b == nil { diff --git a/controller/store/types.go b/controller/store/types.go index a124a251..407ac001 100644 --- a/controller/store/types.go +++ b/controller/store/types.go @@ -99,7 +99,7 @@ type IngressClass struct { Name string Controller string Annotations map[string]string - Status Status + Status Status // Used for store purpose } // IngressPath is useful data from k8s structures about ingress path @@ -112,14 +112,12 @@ type IngressPath struct { Path string PathTypeMatch string IsDefaultBackend bool - Status Status } // IngressRule is useful data from k8s structures about ingress rule type IngressRule struct { - Host string - Paths map[string]*IngressPath - Status Status + Host string + Paths map[string]*IngressPath } // Ingress is useful data from k8s structures about ingress @@ -133,15 +131,14 @@ type Ingress struct { Rules map[string]*IngressRule DefaultBackend *IngressPath TLS map[string]*IngressTLS - Ignored bool // true if resource ignored because of non matching Controller Class - Status Status + Ignored bool // true if resource ignored because of non matching Controller Class + Status Status // Used for store purpose } // IngressTLS describes the transport layer security associated with an Ingress. type IngressTLS struct { Host string SecretName string - Status Status } type ConfigMaps struct {