Skip to content

Commit

Permalink
chore: initial fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Oliver Bähler <oliverbaehler@hotmail.com>
  • Loading branch information
oliverbaehler committed Nov 7, 2023
1 parent 2356963 commit 3b92600
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 33 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 29 additions & 17 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -107,43 +120,42 @@ 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
}

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)
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 6 additions & 15 deletions pkg/controller/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions test.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3b92600

Please sign in to comment.