Skip to content

Commit ef6b003

Browse files
committed
fixup! add global lables and annotations to liqo-created resources
1 parent a26279b commit ef6b003

File tree

8 files changed

+78
-93
lines changed

8 files changed

+78
-93
lines changed

pkg/liqo-controller-manager/authentication/utils/nonce.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ func EnsureSignedNonceSecret(ctx context.Context, cl client.Client,
5555
return fmt.Errorf("nonce not provided and no nonce secret found")
5656
}
5757
secret := forge.SignedNonce(remoteClusterID, tenantNamespace, *nonce)
58-
if _, err := resource.CreateOrUpdate(ctx, cl, secret, func() error {
59-
return nil
60-
}); err != nil {
58+
59+
resource.AddGlobalLabels(secret)
60+
resource.AddGlobalAnnotations(secret)
61+
62+
if err := cl.Create(ctx, secret); err != nil {
6163
return fmt.Errorf("unable to create nonce secret: %w", err)
6264
}
6365
return nil

pkg/liqo-controller-manager/networking/external-network/client-operator/client_controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ func (r *ClientReconciler) EnsureGatewayClient(ctx context.Context, gwClient *ne
220220
objChildMetadata["annotations"] = annotations
221221
}
222222

223+
resource.AddGlobalAnnotations(objChild)
224+
223225
objChild.SetOwnerReferences([]metav1.OwnerReference{
224226
{
225227
APIVersion: gwClient.APIVersion,

pkg/liqo-controller-manager/networking/external-network/server-operator/server_controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ func (r *ServerReconciler) EnsureGatewayServer(ctx context.Context, gwServer *ne
220220
objChildMetadata["annotations"] = annotations
221221
}
222222

223+
resource.AddGlobalAnnotations(objChild)
224+
223225
objChild.SetOwnerReferences([]metav1.OwnerReference{
224226
{
225227
APIVersion: gwServer.APIVersion,

pkg/liqo-controller-manager/offloading/shadowendpointslice-controller/shadowendpointslice_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
145145
utilruntime.Must(ctrl.SetControllerReference(&shadowEps, &newEps, r.Scheme))
146146

147147
resource.AddGlobalLabels(&newEps)
148+
resource.AddGlobalAnnotations(&newEps)
148149

149150
if err := r.Create(ctx, &newEps, client.FieldOwner(ctrlFieldManager)); err != nil {
150151
klog.Errorf("unable to create endpointslice for shadowendpointslice %q: %v", klog.KObj(&shadowEps), err)

pkg/tenantNamespace/bindings.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ func (nm *tenantNamespaceManager) bindClusterRole(ctx context.Context, cluster l
104104
}
105105

106106
resource.AddGlobalLabels(rb)
107+
resource.AddGlobalAnnotations(rb)
107108

108109
rb, err := nm.client.RbacV1().RoleBindings(namespace.Name).Create(ctx, rb, metav1.CreateOptions{})
109110
if apierrors.IsAlreadyExists(err) {
@@ -174,6 +175,7 @@ func (nm *tenantNamespaceManager) bindClusterRoleClusterWide(ctx context.Context
174175
}
175176

176177
resource.AddGlobalLabels(crb)
178+
resource.AddGlobalAnnotations(crb)
177179

178180
crb, err := nm.client.RbacV1().ClusterRoleBindings().Create(ctx, crb, metav1.CreateOptions{})
179181
if apierrors.IsAlreadyExists(err) {

pkg/utils/resource/annotations.go

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package resource
1616

1717
import (
18+
"maps"
19+
1820
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1921
)
2022

@@ -34,26 +36,7 @@ func SetGlobalAnnotations(annotations map[string]string) {
3436

3537
// GetGlobalAnnotations returns a copy of the current global annotations.
3638
func GetGlobalAnnotations() map[string]string {
37-
if globalAnnotations == nil {
38-
return make(map[string]string)
39-
}
40-
result := make(map[string]string, len(globalAnnotations))
41-
for k, v := range globalAnnotations {
42-
result[k] = v
43-
}
44-
return result
45-
}
46-
47-
// MergeAnnotations creates a new map containing all global annotations.
48-
// Optionally, additional annotations can be provided which will be merged with the global annotations.
49-
func MergeAnnotations(additionalAnnotations ...map[string]string) map[string]string {
50-
result := GetGlobalAnnotations()
51-
for _, annotations := range additionalAnnotations {
52-
for k, v := range annotations {
53-
result[k] = v
54-
}
55-
}
56-
return result
39+
return maps.Clone(globalAnnotations)
5740
}
5841

5942
// AddGlobalAnnotations adds the global annotations to the given object's annotations.
@@ -62,9 +45,5 @@ func AddGlobalAnnotations(obj metav1.Object) {
6245
if obj.GetAnnotations() == nil {
6346
obj.SetAnnotations(make(map[string]string))
6447
}
65-
annotations := obj.GetAnnotations()
66-
for k, v := range globalAnnotations {
67-
annotations[k] = v
68-
}
69-
obj.SetAnnotations(annotations)
48+
maps.Copy(obj.GetAnnotations(), globalAnnotations)
7049
}

pkg/utils/resource/labels.go

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
package resource
1616

1717
import (
18-
"context"
18+
"maps"
1919

2020
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21-
"sigs.k8s.io/controller-runtime/pkg/client"
22-
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2321
)
2422

2523
var (
@@ -38,14 +36,7 @@ func SetGlobalLabels(labels map[string]string) {
3836

3937
// GetGlobalLabels returns a copy of the current global labels.
4038
func GetGlobalLabels() map[string]string {
41-
if globalLabels == nil {
42-
return make(map[string]string)
43-
}
44-
result := make(map[string]string, len(globalLabels))
45-
for k, v := range globalLabels {
46-
result[k] = v
47-
}
48-
return result
39+
return maps.Clone(globalLabels)
4940
}
5041

5142
// AddGlobalLabels adds the global labels to the given object's labels.
@@ -54,58 +45,5 @@ func AddGlobalLabels(obj metav1.Object) {
5445
if obj.GetLabels() == nil {
5546
obj.SetLabels(make(map[string]string))
5647
}
57-
labels := obj.GetLabels()
58-
for k, v := range globalLabels {
59-
labels[k] = v
60-
}
61-
obj.SetLabels(labels)
62-
}
63-
64-
// MergeLabels creates a new map containing all global labels.
65-
// Optionally, additional labels can be provided which will be merged with the global labels.
66-
func MergeLabels(additionalLabels ...map[string]string) map[string]string {
67-
result := GetGlobalLabels()
68-
for _, labels := range additionalLabels {
69-
for k, v := range labels {
70-
result[k] = v
71-
}
72-
}
73-
return result
74-
}
75-
76-
// CreateOrUpdate is a wrapper around controllerutil.CreateOrUpdate that automatically adds global labels and annotations.
77-
// It takes the same parameters as controllerutil.CreateOrUpdate plus an optional list of additional labels to merge.
78-
func CreateOrUpdate(ctx context.Context, c client.Client, obj client.Object,
79-
mutateFn func() error) (controllerutil.OperationResult, error) {
80-
// Create a wrapper mutation function that adds labels before calling the original
81-
wrappedMutateFn := func() error {
82-
// First call the original mutation function
83-
if err := mutateFn(); err != nil {
84-
return err
85-
}
86-
87-
// Then add global labels and any additional labels
88-
if obj.GetLabels() == nil {
89-
obj.SetLabels(make(map[string]string))
90-
}
91-
labels := obj.GetLabels()
92-
for k, v := range GetGlobalLabels() {
93-
labels[k] = v
94-
}
95-
obj.SetLabels(labels)
96-
97-
// Add global annotations
98-
if obj.GetAnnotations() == nil {
99-
obj.SetAnnotations(make(map[string]string))
100-
}
101-
annotations := obj.GetAnnotations()
102-
for k, v := range GetGlobalAnnotations() {
103-
annotations[k] = v
104-
}
105-
obj.SetAnnotations(annotations)
106-
107-
return nil
108-
}
109-
110-
return controllerutil.CreateOrUpdate(ctx, c, obj, wrappedMutateFn)
48+
maps.Copy(obj.GetLabels(), globalLabels)
11149
}

pkg/utils/resource/wrappers.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2019-2024 The Liqo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package resource
16+
17+
import (
18+
"context"
19+
20+
"sigs.k8s.io/controller-runtime/pkg/client"
21+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
22+
)
23+
24+
// CreateOrUpdate is a wrapper around controllerutil.CreateOrUpdate that automatically adds global labels and annotations.
25+
// It takes the same parameters as controllerutil.CreateOrUpdate plus an optional list of additional labels to merge.
26+
func CreateOrUpdate(ctx context.Context, c client.Client, obj client.Object,
27+
mutateFn func() error) (controllerutil.OperationResult, error) {
28+
// Create a wrapper mutation function that adds labels before calling the original
29+
wrappedMutateFn := func() error {
30+
// First call the original mutation function
31+
if err := mutateFn(); err != nil {
32+
return err
33+
}
34+
35+
// Then add global labels and any additional labels
36+
if obj.GetLabels() == nil {
37+
obj.SetLabels(make(map[string]string))
38+
}
39+
labels := obj.GetLabels()
40+
for k, v := range GetGlobalLabels() {
41+
labels[k] = v
42+
}
43+
obj.SetLabels(labels)
44+
45+
// Add global annotations
46+
if obj.GetAnnotations() == nil {
47+
obj.SetAnnotations(make(map[string]string))
48+
}
49+
annotations := obj.GetAnnotations()
50+
for k, v := range GetGlobalAnnotations() {
51+
annotations[k] = v
52+
}
53+
obj.SetAnnotations(annotations)
54+
55+
return nil
56+
}
57+
58+
return controllerutil.CreateOrUpdate(ctx, c, obj, wrappedMutateFn)
59+
}

0 commit comments

Comments
 (0)