Skip to content

Commit

Permalink
merge labels and annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
zreigz committed Sep 17, 2024
1 parent d8a949f commit ce7e972
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
3 changes: 3 additions & 0 deletions api/v1alpha1/ingressreplica_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/deployments.plural.sh_ingressreplicas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
34 changes: 30 additions & 4 deletions internal/controller/ingressreplica_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
Expand Down

0 comments on commit ce7e972

Please sign in to comment.