Skip to content

Commit baeaca2

Browse files
authored
Merge pull request kubernetes-sigs#7930 from sbueringer/pr-remove-empty-hook-entries
🐛 ClusterClass: remove empty hook entries from annotation
2 parents 0b95966 + 41d619f commit baeaca2

File tree

2 files changed

+61
-12
lines changed

2 files changed

+61
-12
lines changed

internal/hooks/tracking.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,13 @@ func MarkAsOkToDelete(ctx context.Context, c client.Client, obj client.Object) e
136136

137137
func addToCommaSeparatedList(list string, items ...string) string {
138138
set := sets.NewString(strings.Split(list, ",")...)
139+
140+
// Remove empty strings (that might have been introduced by splitting an empty list)
141+
// from the hook list
142+
set.Delete("")
143+
139144
set.Insert(items...)
145+
140146
return strings.Join(set.List(), ",")
141147
}
142148

@@ -147,6 +153,11 @@ func isInCommaSeparatedList(list, item string) bool {
147153

148154
func removeFromCommaSeparatedList(list string, items ...string) string {
149155
set := sets.NewString(strings.Split(list, ",")...)
156+
157+
// Remove empty strings (that might have been introduced by splitting an empty list)
158+
// from the hook list
159+
set.Delete("")
160+
150161
set.Delete(items...)
151162
return strings.Join(set.List(), ",")
152163
}

internal/hooks/tracking_test.go

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ func TestIsPending(t *testing.T) {
103103

104104
func TestMarkAsPending(t *testing.T) {
105105
tests := []struct {
106-
name string
107-
obj client.Object
108-
hook runtimecatalog.Hook
106+
name string
107+
obj client.Object
108+
hook runtimecatalog.Hook
109+
expectedAnnotation string
109110
}{
110111
{
111112
name: "should add the marker if not already marked as pending",
@@ -115,7 +116,8 @@ func TestMarkAsPending(t *testing.T) {
115116
Namespace: "test-ns",
116117
},
117118
},
118-
hook: runtimehooksv1.AfterClusterUpgrade,
119+
hook: runtimehooksv1.AfterClusterUpgrade,
120+
expectedAnnotation: "AfterClusterUpgrade",
119121
},
120122
{
121123
name: "should add the marker if not already marked as pending - other hooks are present",
@@ -128,7 +130,8 @@ func TestMarkAsPending(t *testing.T) {
128130
},
129131
},
130132
},
131-
hook: runtimehooksv1.AfterClusterUpgrade,
133+
hook: runtimehooksv1.AfterClusterUpgrade,
134+
expectedAnnotation: "AfterClusterUpgrade,AfterControlPlaneUpgrade",
132135
},
133136
{
134137
name: "should pass if the marker is already marked as pending",
@@ -141,7 +144,22 @@ func TestMarkAsPending(t *testing.T) {
141144
},
142145
},
143146
},
144-
hook: runtimehooksv1.AfterClusterUpgrade,
147+
hook: runtimehooksv1.AfterClusterUpgrade,
148+
expectedAnnotation: "AfterClusterUpgrade",
149+
},
150+
{
151+
name: "should pass if the marker is already marked as pending and remove empty string entry",
152+
obj: &corev1.ConfigMap{
153+
ObjectMeta: metav1.ObjectMeta{
154+
Name: "test-cluster",
155+
Namespace: "test-ns",
156+
Annotations: map[string]string{
157+
runtimev1.PendingHooksAnnotation: ",AfterClusterUpgrade",
158+
},
159+
},
160+
},
161+
hook: runtimehooksv1.AfterClusterUpgrade,
162+
expectedAnnotation: "AfterClusterUpgrade",
145163
},
146164
}
147165

@@ -153,15 +171,17 @@ func TestMarkAsPending(t *testing.T) {
153171
g.Expect(MarkAsPending(ctx, fakeClient, tt.obj, tt.hook)).To(Succeed())
154172
annotations := tt.obj.GetAnnotations()
155173
g.Expect(annotations[runtimev1.PendingHooksAnnotation]).To(ContainSubstring(runtimecatalog.HookName(tt.hook)))
174+
g.Expect(annotations[runtimev1.PendingHooksAnnotation]).To(Equal(tt.expectedAnnotation))
156175
})
157176
}
158177
}
159178

160179
func TestMarkAsDone(t *testing.T) {
161180
tests := []struct {
162-
name string
163-
obj client.Object
164-
hook runtimecatalog.Hook
181+
name string
182+
obj client.Object
183+
hook runtimecatalog.Hook
184+
expectedAnnotation string
165185
}{
166186
{
167187
name: "should pass if the marker is not already present",
@@ -171,7 +191,8 @@ func TestMarkAsDone(t *testing.T) {
171191
Namespace: "test-ns",
172192
},
173193
},
174-
hook: runtimehooksv1.AfterClusterUpgrade,
194+
hook: runtimehooksv1.AfterClusterUpgrade,
195+
expectedAnnotation: "",
175196
},
176197
{
177198
name: "should remove if the marker is already present",
@@ -184,7 +205,8 @@ func TestMarkAsDone(t *testing.T) {
184205
},
185206
},
186207
},
187-
hook: runtimehooksv1.AfterClusterUpgrade,
208+
hook: runtimehooksv1.AfterClusterUpgrade,
209+
expectedAnnotation: "",
188210
},
189211
{
190212
name: "should remove if the marker is already present among multiple hooks",
@@ -197,7 +219,22 @@ func TestMarkAsDone(t *testing.T) {
197219
},
198220
},
199221
},
200-
hook: runtimehooksv1.AfterClusterUpgrade,
222+
hook: runtimehooksv1.AfterClusterUpgrade,
223+
expectedAnnotation: "AfterControlPlaneUpgrade",
224+
},
225+
{
226+
name: "should remove empty string entry",
227+
obj: &corev1.ConfigMap{
228+
ObjectMeta: metav1.ObjectMeta{
229+
Name: "test-cluster",
230+
Namespace: "test-ns",
231+
Annotations: map[string]string{
232+
runtimev1.PendingHooksAnnotation: ",AfterClusterUpgrade,AfterControlPlaneUpgrade",
233+
},
234+
},
235+
},
236+
hook: runtimehooksv1.AfterClusterUpgrade,
237+
expectedAnnotation: "AfterControlPlaneUpgrade",
201238
},
202239
}
203240

@@ -209,6 +246,7 @@ func TestMarkAsDone(t *testing.T) {
209246
g.Expect(MarkAsDone(ctx, fakeClient, tt.obj, tt.hook)).To(Succeed())
210247
annotations := tt.obj.GetAnnotations()
211248
g.Expect(annotations[runtimev1.PendingHooksAnnotation]).NotTo(ContainSubstring(runtimecatalog.HookName(tt.hook)))
249+
g.Expect(annotations[runtimev1.PendingHooksAnnotation]).To(Equal(tt.expectedAnnotation))
212250
})
213251
}
214252
}

0 commit comments

Comments
 (0)