From ce7e9725ddf800c5051e2ee6e531cd34f6620b8f Mon Sep 17 00:00:00 2001 From: Lukasz Zajaczkowski Date: Tue, 17 Sep 2024 09:41:49 +0200 Subject: [PATCH] merge labels and annotations --- api/v1alpha1/ingressreplica_types.go | 3 ++ api/v1alpha1/zz_generated.deepcopy.go | 5 +++ ...deployments.plural.sh_ingressreplicas.yaml | 2 ++ ...deployments.plural.sh_ingressreplicas.yaml | 2 ++ .../controller/ingressreplica_controller.go | 34 ++++++++++++++++--- 5 files changed, 42 insertions(+), 4 deletions(-) diff --git a/api/v1alpha1/ingressreplica_types.go b/api/v1alpha1/ingressreplica_types.go index 86372261..c970a1db 100644 --- a/api/v1alpha1/ingressreplica_types.go +++ b/api/v1alpha1/ingressreplica_types.go @@ -40,6 +40,9 @@ type IngressReplicaSpec struct { // +kubebuilder:validation:Required IngressRef corev1.ObjectReference `json:"ingressRef"` + // +kubebuilder:validation:Optional + IngressClassName *string `json:"ingressClassName,omitempty"` + // +kubebuilder:validation:Optional TLS []v1.IngressTLS `json:"tls,omitempty"` diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index e2f55450..0b99b29e 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -411,6 +411,11 @@ func (in *IngressReplicaList) DeepCopyObject() runtime.Object { func (in *IngressReplicaSpec) DeepCopyInto(out *IngressReplicaSpec) { *out = *in out.IngressRef = in.IngressRef + if in.IngressClassName != nil { + in, out := &in.IngressClassName, &out.IngressClassName + *out = new(string) + **out = **in + } if in.TLS != nil { in, out := &in.TLS, &out.TLS *out = make([]networkingv1.IngressTLS, len(*in)) diff --git a/charts/deployment-operator/crds/deployments.plural.sh_ingressreplicas.yaml b/charts/deployment-operator/crds/deployments.plural.sh_ingressreplicas.yaml index 5cf32a4d..ce8969c2 100644 --- a/charts/deployment-operator/crds/deployments.plural.sh_ingressreplicas.yaml +++ b/charts/deployment-operator/crds/deployments.plural.sh_ingressreplicas.yaml @@ -43,6 +43,8 @@ spec: additionalProperties: type: string type: object + ingressClassName: + type: string ingressRef: description: ObjectReference contains enough information to let you inspect or modify the referred object. diff --git a/config/crd/bases/deployments.plural.sh_ingressreplicas.yaml b/config/crd/bases/deployments.plural.sh_ingressreplicas.yaml index 5cf32a4d..ce8969c2 100644 --- a/config/crd/bases/deployments.plural.sh_ingressreplicas.yaml +++ b/config/crd/bases/deployments.plural.sh_ingressreplicas.yaml @@ -43,6 +43,8 @@ spec: additionalProperties: type: string type: object + ingressClassName: + type: string ingressRef: description: ObjectReference contains enough information to let you inspect or modify the referred object. diff --git a/internal/controller/ingressreplica_controller.go b/internal/controller/ingressreplica_controller.go index a48e9173..fb803140 100644 --- a/internal/controller/ingressreplica_controller.go +++ b/internal/controller/ingressreplica_controller.go @@ -5,6 +5,7 @@ import ( "github.com/pluralsh/deployment-operator/api/v1alpha1" "github.com/pluralsh/deployment-operator/internal/utils" + "github.com/samber/lo" networkv1 "k8s.io/api/networking/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -110,10 +111,8 @@ func (r *IngressReplicaReconciler) SetupWithManager(mgr ctrl.Manager) error { func genIngress(ingressReplica *v1alpha1.IngressReplica, oldIngress *networkv1.Ingress) *networkv1.Ingress { newIngress := &networkv1.Ingress{ ObjectMeta: metav1.ObjectMeta{ - Name: ingressReplica.Name, - Namespace: ingressReplica.Namespace, - Labels: oldIngress.Labels, - Annotations: oldIngress.Annotations, + Name: ingressReplica.Name, + Namespace: ingressReplica.Namespace, }, Spec: networkv1.IngressSpec{ IngressClassName: oldIngress.Spec.IngressClassName, @@ -125,6 +124,33 @@ func genIngress(ingressReplica *v1alpha1.IngressReplica, oldIngress *networkv1.I } func updateIngress(ingressReplica *v1alpha1.IngressReplica, newIngress *networkv1.Ingress, oldIngress *networkv1.Ingress) { + if newIngress.Labels == nil { + newIngress.Labels = map[string]string{} + } + if oldIngress.Labels == nil { + oldIngress.Labels = map[string]string{} + } + if ingressReplica.Labels == nil { + ingressReplica.Labels = map[string]string{} + } + // merge from left to right + newIngress.Labels = lo.Assign(newIngress.Labels, oldIngress.Labels, ingressReplica.Labels) + + if newIngress.Annotations == nil { + newIngress.Annotations = map[string]string{} + } + if oldIngress.Annotations == nil { + oldIngress.Annotations = map[string]string{} + } + if ingressReplica.Annotations == nil { + ingressReplica.Annotations = map[string]string{} + } + // merge from left to right + newIngress.Annotations = lo.Assign(newIngress.Annotations, oldIngress.Annotations, ingressReplica.Annotations) + + if ingressReplica.Spec.IngressClassName != nil { + newIngress.Spec.IngressClassName = ingressReplica.Spec.IngressClassName + } if len(ingressReplica.Spec.TLS) > 0 { newIngress.Spec.TLS = ingressReplica.Spec.TLS }