Skip to content

Commit 4d0d1f2

Browse files
authored
Merge pull request #197 from jnummelin/fix/copy-svc-annotations
Copy both Cluster and Cluster.spec.service.annotations to created svc
2 parents 7b92481 + 31716bf commit 4d0d1f2

File tree

2 files changed

+99
-3
lines changed

2 files changed

+99
-3
lines changed

internal/controller/k0smotron.io/k0smotroncluster_service.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ func (r *ClusterReconciler) generateService(kmc *km.Cluster) v1.Service {
6969

7070
labels := labelsForCluster(kmc)
7171

72+
// Copy both Cluster level annotations and Service annotations
73+
annotations := map[string]string{}
74+
for k, v := range annotationsForCluster(kmc) {
75+
annotations[k] = v
76+
}
77+
for k, v := range kmc.Spec.Service.Annotations {
78+
annotations[k] = v
79+
}
80+
7281
svc := v1.Service{
7382
TypeMeta: metav1.TypeMeta{
7483
APIVersion: "v1",
@@ -78,7 +87,7 @@ func (r *ClusterReconciler) generateService(kmc *km.Cluster) v1.Service {
7887
Name: name,
7988
Namespace: kmc.Namespace,
8089
Labels: labels,
81-
Annotations: annotationsForCluster(kmc),
90+
Annotations: annotations,
8291
},
8392
Spec: v1.ServiceSpec{
8493
Type: kmc.Spec.Service.Type,
@@ -87,8 +96,6 @@ func (r *ClusterReconciler) generateService(kmc *km.Cluster) v1.Service {
8796
},
8897
}
8998

90-
_ = ctrl.SetControllerReference(kmc, &svc, r.Scheme)
91-
9299
return svc
93100
}
94101

@@ -98,6 +105,8 @@ func (r *ClusterReconciler) reconcileServices(ctx context.Context, kmc km.Cluste
98105
logger.Info("Reconciling services")
99106
svc := r.generateService(&kmc)
100107

108+
_ = ctrl.SetControllerReference(&kmc, &svc, r.Scheme)
109+
101110
if err := r.Client.Patch(ctx, &svc, client.Apply, patchOpts...); err != nil {
102111
return err
103112
}

internal/controller/k0smotron.io/k0smotroncluster_service_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ package k0smotronio
1919
import (
2020
"testing"
2121

22+
km "github.com/k0sproject/k0smotron/api/k0smotron.io/v1beta1"
2223
"github.com/stretchr/testify/assert"
2324
v1 "k8s.io/api/core/v1"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2426
)
2527

2628
func TestClusterReconciler_findNodeAddress(t *testing.T) {
@@ -127,3 +129,88 @@ func TestClusterReconciler_findNodeAddress(t *testing.T) {
127129
})
128130
}
129131
}
132+
133+
func TestClusterReconciler_serviceAnnotations(t *testing.T) {
134+
tests := []struct {
135+
name string
136+
kmc *km.Cluster
137+
want map[string]string
138+
}{
139+
{
140+
name: "when no annotations are set on either Cluster on svc",
141+
kmc: &km.Cluster{
142+
ObjectMeta: metav1.ObjectMeta{
143+
Name: "test",
144+
},
145+
Spec: km.ClusterSpec{},
146+
},
147+
want: map[string]string{},
148+
},
149+
{
150+
name: "when annotations are set on only Cluster",
151+
kmc: &km.Cluster{
152+
ObjectMeta: metav1.ObjectMeta{
153+
Name: "test",
154+
Annotations: map[string]string{
155+
"test": "test",
156+
},
157+
},
158+
Spec: km.ClusterSpec{},
159+
},
160+
want: map[string]string{
161+
"test": "test",
162+
},
163+
},
164+
{
165+
name: "when annotations are set on both Cluster and svc",
166+
kmc: &km.Cluster{
167+
ObjectMeta: metav1.ObjectMeta{
168+
Name: "test",
169+
Annotations: map[string]string{
170+
"test": "test",
171+
},
172+
},
173+
Spec: km.ClusterSpec{
174+
Service: km.ServiceSpec{
175+
Annotations: map[string]string{
176+
"foo": "bar",
177+
},
178+
},
179+
},
180+
},
181+
want: map[string]string{
182+
"test": "test",
183+
"foo": "bar",
184+
},
185+
},
186+
{
187+
name: "when same annotation is set on both Cluster and svc the svc annotation wins",
188+
kmc: &km.Cluster{
189+
ObjectMeta: metav1.ObjectMeta{
190+
Name: "test",
191+
Annotations: map[string]string{
192+
"test": "test",
193+
},
194+
},
195+
Spec: km.ClusterSpec{
196+
Service: km.ServiceSpec{
197+
Annotations: map[string]string{
198+
"test": "foobar",
199+
},
200+
},
201+
},
202+
},
203+
want: map[string]string{
204+
"test": "foobar",
205+
},
206+
},
207+
}
208+
for _, tt := range tests {
209+
t.Run(tt.name, func(t *testing.T) {
210+
r := &ClusterReconciler{}
211+
svc := r.generateService(tt.kmc)
212+
got := svc.Annotations
213+
assert.Equal(t, tt.want, got)
214+
})
215+
}
216+
}

0 commit comments

Comments
 (0)