From 3b92600b3b95d706adaba3f39d88288efa441940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20B=C3=A4hler?= Date: Tue, 7 Nov 2023 21:46:35 +0100 Subject: [PATCH] chore: initial fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Oliver Bähler --- Makefile | 1 - pkg/controller/controller.go | 46 +++++++++++++++++++++++------------- pkg/controller/finalizer.go | 4 ++++ pkg/controller/transform.go | 21 +++++----------- test.yaml | 18 ++++++++++++++ 5 files changed, 57 insertions(+), 33 deletions(-) create mode 100644 test.yaml diff --git a/Makefile b/Makefile index 0fde28a..8e75938 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,6 @@ FULL_IMG ?= $(REGISTRY)/$(IMG_BASE) #################### KOCACHE ?= /tmp/ko-cache -KO_REGISTRY := ko.local KO_TAGS ?= "latest" ifdef VERSION KO_TAGS := $(KO_TAGS),$(VERSION) diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index 40ecde4..3ba6b49 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -62,9 +62,22 @@ func (i *PropagationController) Reconcile(ctx context.Context, request reconcile i.logger.Info("update propagations", "triggered-by", request.NamespacedName) - err = i.attachFinalizer(ctx, *(origin.DeepCopy())) - if err != nil { - return reconcile.Result{}, errors.Wrapf(err, "attach finalizer to ingress %s", request.NamespacedName) + if origin.DeletionTimestamp == nil { + err = i.attachFinalizer(ctx, *(origin.DeepCopy())) + if err != nil { + return reconcile.Result{}, errors.Wrapf(err, "attach finalizer to ingress %s", request.NamespacedName) + } + } else { + if !i.hasFinalizer(ctx, origin) { + i.logger.V(1).Info("ingress is being deleted and already finillized by this controller", + "ingress", request.NamespacedName, + "controlled-ingress-class", i.options.IngressClassName, + "controlled-controller-class", i.options.ControllerClassName, + ) + return reconcile.Result{ + Requeue: false, + }, nil + } } ingresses, err := i.listControlledIngresses(ctx) @@ -107,16 +120,11 @@ func (i *PropagationController) isControlledByThisController(ctx context.Context return false, nil } - controlledIngressClasses, err := i.listControlledIngressClasses(ctx) + controlledIngressClassNames, err := i.listControlledIngressClasses(ctx) if err != nil { return false, errors.Wrapf(err, "fetch controlled ingress classes with controller name %s", i.options.ControllerClassName) } - var controlledIngressClassNames []string - for _, controlledIngressClass := range controlledIngressClasses { - controlledIngressClassNames = append(controlledIngressClassNames, controlledIngressClass.Name) - } - if stringSliceContains(controlledIngressClassNames, *target.Spec.IngressClassName) { return true, nil } @@ -124,26 +132,30 @@ func (i *PropagationController) isControlledByThisController(ctx context.Context return false, nil } -func (i *PropagationController) listControlledIngressClasses(ctx context.Context) ([]networkingv1.IngressClass, error) { +func (i *PropagationController) listControlledIngressClasses(ctx context.Context) ([]string, error) { list := networkingv1.IngressClassList{} err := i.kubeClient.List(ctx, &list) if err != nil { return nil, errors.Wrap(err, "list ingress classes") } - return list.Items, nil + + var controlledNames []string + for _, ingressClass := range list.Items { + // Check if the IngressClass is controlled by the specified controller + if ingressClass.Spec.Controller == i.options.ControllerClassName { + controlledNames = append(controlledNames, ingressClass.Name) + } + } + + return controlledNames, nil } func (i *PropagationController) listControlledIngresses(ctx context.Context) ([]networkingv1.Ingress, error) { - controlledIngressClasses, err := i.listControlledIngressClasses(ctx) + controlledIngressClassNames, err := i.listControlledIngressClasses(ctx) if err != nil { return nil, errors.Wrapf(err, "fetch controlled ingress classes with controller name %s", i.options.ControllerClassName) } - var controlledIngressClassNames []string - for _, controlledIngressClass := range controlledIngressClasses { - controlledIngressClassNames = append(controlledIngressClassNames, controlledIngressClass.Name) - } - var result []networkingv1.Ingress list := networkingv1.IngressList{} err = i.kubeClient.List(ctx, &list) diff --git a/pkg/controller/finalizer.go b/pkg/controller/finalizer.go index ba0aca8..ff7d9f8 100644 --- a/pkg/controller/finalizer.go +++ b/pkg/controller/finalizer.go @@ -9,6 +9,10 @@ import ( const IngressControllerFinalizer = "svc-ingress-propagator.buttah.cloud/propagated-ingress" +func (i *PropagationController) hasFinalizer(ctx context.Context, ingress networkingv1.Ingress) bool { + return stringSliceContains(ingress.Finalizers, IngressControllerFinalizer) +} + func (i *PropagationController) attachFinalizer(ctx context.Context, ingress networkingv1.Ingress) error { if stringSliceContains(ingress.Finalizers, IngressControllerFinalizer) { return nil diff --git a/pkg/controller/transform.go b/pkg/controller/transform.go index 8f2183d..3e0c687 100644 --- a/pkg/controller/transform.go +++ b/pkg/controller/transform.go @@ -27,26 +27,19 @@ func FromIngressToPropagation(ctx context.Context, logger logr.Logger, kubeClien // Assign Name result.Name = ingress.Name - ing.Name = result.Name - ing.SetNamespace(namespace) + ingress.SetNamespace(namespace) if ingress.Spec.TLS != nil { ing.Spec.TLS = ingress.Spec.TLS } - // Assign Annotations - if ingress.Annotations != nil { - ing.Annotations = ingress.Annotations - } // Assign Labels - if ingress.Labels != nil { - ing.Labels = ingress.Labels - } else { - ing.Labels = make(map[string]string) + if ingress.Labels == nil { + ingress.Labels = make(map[string]string) } - ing.Labels[LabelManaged] = identifier + ingress.Labels[LabelManaged] = identifier - ing.Spec.IngressClassName = &ingressClass + ingress.Spec.IngressClassName = &ingressClass // Store relevant Services var services []v1.Service @@ -97,9 +90,7 @@ func FromIngressToPropagation(ctx context.Context, logger logr.Logger, kubeClien } } } - ing.Spec.Rules = ingress.Spec.Rules - - result.Ingress = ing + result.Ingress = ingress // Load Services and endpoints error := resolveServiceEndpoints(services, &result, identifier, namespace) diff --git a/test.yaml b/test.yaml new file mode 100644 index 0000000..68f52ca --- /dev/null +++ b/test.yaml @@ -0,0 +1,18 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: minimal-ingress + annotations: + nginx.ingress.kubernetes.io/rewrite-target: / +spec: + ingressClassName: public + rules: + - http: + paths: + - path: /testpath + pathType: Prefix + backend: + service: + name: test + port: + number: 80