From d1c1cd4794a6fe4fece8c9c1f9685bdf8631cb7d Mon Sep 17 00:00:00 2001 From: Erik Rasmussen Date: Fri, 31 May 2024 16:13:58 -0500 Subject: [PATCH 1/4] Check for existing ingress when appending to status --- pkg/controller/ingress-controller.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/pkg/controller/ingress-controller.go b/pkg/controller/ingress-controller.go index 3b82107..ed17847 100644 --- a/pkg/controller/ingress-controller.go +++ b/pkg/controller/ingress-controller.go @@ -3,6 +3,8 @@ package controller import ( "context" "fmt" + "slices" + cloudflarecontroller "github.com/STRRL/cloudflare-tunnel-ingress-controller/pkg/cloudflare-controller" "github.com/STRRL/cloudflare-tunnel-ingress-controller/pkg/exposure" "github.com/go-logr/logr" @@ -99,15 +101,20 @@ func (i *IngressController) Reconcile(ctx context.Context, request reconcile.Req } } - origin.Status.LoadBalancer.Ingress = append(origin.Status.LoadBalancer.Ingress, - networkingv1.IngressLoadBalancerIngress{ - Hostname: i.tunnelClient.TunnelDomain(), - Ports: []networkingv1.IngressPortStatus{{ - Protocol: v1.ProtocolTCP, - Port: 443, - }}, - }, - ) + matchesHostname := func(ingress networkingv1.IngressLoadBalancerIngress) bool { + return ingress.Hostname == i.tunnelClient.TunnelDomain() + } + if !slices.ContainsFunc(origin.Status.LoadBalancer.Ingress, matchesHostname) { + origin.Status.LoadBalancer.Ingress = append(origin.Status.LoadBalancer.Ingress, + networkingv1.IngressLoadBalancerIngress{ + Hostname: i.tunnelClient.TunnelDomain(), + Ports: []networkingv1.IngressPortStatus{{ + Protocol: v1.ProtocolTCP, + Port: 443, + }}, + }, + ) + } if err = i.kubeClient.Status().Update(ctx, &origin); err != nil { return reconcile.Result{}, errors.Wrapf(err, "failed to update ingress status") } From 4a315b0a92582c94e255bbc2a23666991019c92a Mon Sep 17 00:00:00 2001 From: Erik Rasmussen Date: Fri, 31 May 2024 16:17:20 -0500 Subject: [PATCH 2/4] Dry --- pkg/controller/ingress-controller.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/controller/ingress-controller.go b/pkg/controller/ingress-controller.go index ed17847..560ffde 100644 --- a/pkg/controller/ingress-controller.go +++ b/pkg/controller/ingress-controller.go @@ -101,13 +101,14 @@ func (i *IngressController) Reconcile(ctx context.Context, request reconcile.Req } } + hostname := i.tunnelClient.TunnelDomain() matchesHostname := func(ingress networkingv1.IngressLoadBalancerIngress) bool { - return ingress.Hostname == i.tunnelClient.TunnelDomain() + return ingress.Hostname == hostname } if !slices.ContainsFunc(origin.Status.LoadBalancer.Ingress, matchesHostname) { origin.Status.LoadBalancer.Ingress = append(origin.Status.LoadBalancer.Ingress, networkingv1.IngressLoadBalancerIngress{ - Hostname: i.tunnelClient.TunnelDomain(), + Hostname: hostname, Ports: []networkingv1.IngressPortStatus{{ Protocol: v1.ProtocolTCP, Port: 443, From d266e4ae25ca97fee62f69b90ceb8f6a3acb933a Mon Sep 17 00:00:00 2001 From: Erik Rasmussen Date: Sat, 1 Jun 2024 12:01:46 -0500 Subject: [PATCH 3/4] Address PR feedback --- pkg/controller/ingress-controller.go | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/pkg/controller/ingress-controller.go b/pkg/controller/ingress-controller.go index 560ffde..ca5ff66 100644 --- a/pkg/controller/ingress-controller.go +++ b/pkg/controller/ingress-controller.go @@ -106,17 +106,15 @@ func (i *IngressController) Reconcile(ctx context.Context, request reconcile.Req return ingress.Hostname == hostname } if !slices.ContainsFunc(origin.Status.LoadBalancer.Ingress, matchesHostname) { - origin.Status.LoadBalancer.Ingress = append(origin.Status.LoadBalancer.Ingress, - networkingv1.IngressLoadBalancerIngress{ - Hostname: hostname, - Ports: []networkingv1.IngressPortStatus{{ - Protocol: v1.ProtocolTCP, - Port: 443, - }}, - }, - ) - } - if err = i.kubeClient.Status().Update(ctx, &origin); err != nil { + origin.Status.LoadBalancer.Ingress = []networkingv1.IngressLoadBalancerIngress{{ + Hostname: hostname, + Ports: []networkingv1.IngressPortStatus{{ + Protocol: v1.ProtocolTCP, + Port: 443, + }}, + }} + } + if err = i.kubeClient.Status().Update(ctx, origin.DeepCopy()); err != nil { return reconcile.Result{}, errors.Wrapf(err, "failed to update ingress status") } From e81988633a06a1c97d2373bef49fd4248db88ccf Mon Sep 17 00:00:00 2001 From: Erik Rasmussen Date: Sun, 2 Jun 2024 13:04:41 -0500 Subject: [PATCH 4/4] Deep copy the origin earlier --- pkg/controller/ingress-controller.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/controller/ingress-controller.go b/pkg/controller/ingress-controller.go index ca5ff66..610ac2e 100644 --- a/pkg/controller/ingress-controller.go +++ b/pkg/controller/ingress-controller.go @@ -102,11 +102,12 @@ func (i *IngressController) Reconcile(ctx context.Context, request reconcile.Req } hostname := i.tunnelClient.TunnelDomain() + newOrigin := origin.DeepCopy() matchesHostname := func(ingress networkingv1.IngressLoadBalancerIngress) bool { return ingress.Hostname == hostname } if !slices.ContainsFunc(origin.Status.LoadBalancer.Ingress, matchesHostname) { - origin.Status.LoadBalancer.Ingress = []networkingv1.IngressLoadBalancerIngress{{ + newOrigin.Status.LoadBalancer.Ingress = []networkingv1.IngressLoadBalancerIngress{{ Hostname: hostname, Ports: []networkingv1.IngressPortStatus{{ Protocol: v1.ProtocolTCP, @@ -114,7 +115,7 @@ func (i *IngressController) Reconcile(ctx context.Context, request reconcile.Req }}, }} } - if err = i.kubeClient.Status().Update(ctx, origin.DeepCopy()); err != nil { + if err = i.kubeClient.Status().Update(ctx, newOrigin); err != nil { return reconcile.Result{}, errors.Wrapf(err, "failed to update ingress status") }