Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 ClusterClass: remove empty hook entries from annotation #7930

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions internal/hooks/tracking.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,13 @@ func MarkAsOkToDelete(ctx context.Context, c client.Client, obj client.Object) e

func addToCommaSeparatedList(list string, items ...string) string {
set := sets.NewString(strings.Split(list, ",")...)

// Remove empty strings (that might have been introduced by splitting an empty list)
// from the hook list
set.Delete("")

set.Insert(items...)

return strings.Join(set.List(), ",")
}

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

func removeFromCommaSeparatedList(list string, items ...string) string {
set := sets.NewString(strings.Split(list, ",")...)

// Remove empty strings (that might have been introduced by splitting an empty list)
// from the hook list
set.Delete("")

set.Delete(items...)
return strings.Join(set.List(), ",")
}
62 changes: 50 additions & 12 deletions internal/hooks/tracking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ func TestIsPending(t *testing.T) {

func TestMarkAsPending(t *testing.T) {
tests := []struct {
name string
obj client.Object
hook runtimecatalog.Hook
name string
obj client.Object
hook runtimecatalog.Hook
expectedAnnotation string
}{
{
name: "should add the marker if not already marked as pending",
Expand All @@ -115,7 +116,8 @@ func TestMarkAsPending(t *testing.T) {
Namespace: "test-ns",
},
},
hook: runtimehooksv1.AfterClusterUpgrade,
hook: runtimehooksv1.AfterClusterUpgrade,
expectedAnnotation: "AfterClusterUpgrade",
},
{
name: "should add the marker if not already marked as pending - other hooks are present",
Expand All @@ -128,7 +130,8 @@ func TestMarkAsPending(t *testing.T) {
},
},
},
hook: runtimehooksv1.AfterClusterUpgrade,
hook: runtimehooksv1.AfterClusterUpgrade,
expectedAnnotation: "AfterClusterUpgrade,AfterControlPlaneUpgrade",
},
{
name: "should pass if the marker is already marked as pending",
Expand All @@ -141,7 +144,22 @@ func TestMarkAsPending(t *testing.T) {
},
},
},
hook: runtimehooksv1.AfterClusterUpgrade,
hook: runtimehooksv1.AfterClusterUpgrade,
expectedAnnotation: "AfterClusterUpgrade",
},
{
name: "should pass if the marker is already marked as pending and remove empty string entry",
obj: &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
Namespace: "test-ns",
Annotations: map[string]string{
runtimev1.PendingHooksAnnotation: ",AfterClusterUpgrade",
},
},
},
hook: runtimehooksv1.AfterClusterUpgrade,
expectedAnnotation: "AfterClusterUpgrade",
},
}

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

func TestMarkAsDone(t *testing.T) {
tests := []struct {
name string
obj client.Object
hook runtimecatalog.Hook
name string
obj client.Object
hook runtimecatalog.Hook
expectedAnnotation string
}{
{
name: "should pass if the marker is not already present",
Expand All @@ -171,7 +191,8 @@ func TestMarkAsDone(t *testing.T) {
Namespace: "test-ns",
},
},
hook: runtimehooksv1.AfterClusterUpgrade,
hook: runtimehooksv1.AfterClusterUpgrade,
expectedAnnotation: "",
},
{
name: "should remove if the marker is already present",
Expand All @@ -184,7 +205,8 @@ func TestMarkAsDone(t *testing.T) {
},
},
},
hook: runtimehooksv1.AfterClusterUpgrade,
hook: runtimehooksv1.AfterClusterUpgrade,
expectedAnnotation: "",
},
{
name: "should remove if the marker is already present among multiple hooks",
Expand All @@ -197,7 +219,22 @@ func TestMarkAsDone(t *testing.T) {
},
},
},
hook: runtimehooksv1.AfterClusterUpgrade,
hook: runtimehooksv1.AfterClusterUpgrade,
expectedAnnotation: "AfterControlPlaneUpgrade",
},
{
name: "should remove empty string entry",
obj: &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
Namespace: "test-ns",
Annotations: map[string]string{
runtimev1.PendingHooksAnnotation: ",AfterClusterUpgrade,AfterControlPlaneUpgrade",
},
},
},
hook: runtimehooksv1.AfterClusterUpgrade,
expectedAnnotation: "AfterControlPlaneUpgrade",
},
}

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