Skip to content

Commit

Permalink
gateway-api: Merge externally annotations and labels for kubernetes t…
Browse files Browse the repository at this point in the history
…ypes

Fixes #24584

Signed-off-by: Jan Jansen <jan.jansen@gdata.de>
  • Loading branch information
farodin91 committed Aug 10, 2023
1 parent 74b7b7a commit 8faca30
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
9 changes: 3 additions & 6 deletions operator/pkg/gateway-api/gateway_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ func (r *gatewayReconciler) ensureService(ctx context.Context, desired *corev1.S

temp := existing.DeepCopy()
temp.Spec = desired.Spec
temp.SetAnnotations(desired.GetAnnotations())
temp.SetLabels(desired.GetLabels())
setMergedLabelsAndAnnotations(temp, desired)

return r.Client.Patch(ctx, temp, client.MergeFrom(existing))
}
Expand All @@ -186,8 +185,7 @@ func (r *gatewayReconciler) ensureEndpoints(ctx context.Context, desired *corev1

temp := existing.DeepCopy()
temp.Subsets = desired.Subsets
temp.SetAnnotations(desired.GetAnnotations())
temp.SetLabels(desired.GetLabels())
setMergedLabelsAndAnnotations(temp, desired)

return r.Client.Patch(ctx, temp, client.MergeFrom(existing))
}
Expand All @@ -203,8 +201,7 @@ func (r *gatewayReconciler) ensureEnvoyConfig(ctx context.Context, desired *cili
}
temp := existing.DeepCopy()
temp.Spec = desired.Spec
temp.SetAnnotations(desired.GetAnnotations())
temp.SetLabels(desired.GetLabels())
setMergedLabelsAndAnnotations(temp, desired)

return r.Client.Patch(ctx, temp, client.MergeFrom(existing))
}
Expand Down
11 changes: 11 additions & 0 deletions operator/pkg/gateway-api/gateway_reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ var gwFixture = []client.Object{
},
},

&corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "cilium-gateway-valid-gateway",
Namespace: "default",
Annotations: map[string]string{
"pre-existing-annotation": "true",
},
},
},

// Service in another namespace
&corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -278,6 +288,7 @@ func Test_gatewayReconciler_Reconcile(t *testing.T) {
require.NoError(t, err)
require.Equal(t, corev1.ServiceTypeLoadBalancer, lb.Spec.Type)
require.Equal(t, "valid-gateway", lb.Labels["io.cilium.gateway/owning-gateway"])
require.Equal(t, "true", lb.Annotations["pre-existing-annotation"])

// Update LB status
lb.Status.LoadBalancer.Ingress = []corev1.LoadBalancerIngress{
Expand Down
16 changes: 16 additions & 0 deletions operator/pkg/gateway-api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,19 @@ func getGatewayKindForObject(obj metav1.Object) gatewayv1beta1.Kind {
return "Unknown"
}
}

func mergeMap(left, right map[string]string) map[string]string {
if left == nil {
return right
} else {
for key, value := range right {
left[key] = value
}
}
return left
}

func setMergedLabelsAndAnnotations(temp, desired client.Object) {
temp.SetAnnotations(mergeMap(temp.GetAnnotations(), desired.GetAnnotations()))
temp.SetLabels(mergeMap(temp.GetLabels(), desired.GetLabels()))
}

0 comments on commit 8faca30

Please sign in to comment.