diff --git a/pkg/kwok/controllers/finalizers.go b/pkg/kwok/controllers/finalizers.go index 79eb0e927..1841b9320 100644 --- a/pkg/kwok/controllers/finalizers.go +++ b/pkg/kwok/controllers/finalizers.go @@ -100,7 +100,10 @@ func finalizersModify(metaFinalizers []string, finalizers *internalversion.Stage ops = append(ops, finalizersAdd(metaFinalizers, finalizers.Add)...) } } else { - ops = append(ops, finalizersEmpty) + if len(metaFinalizers) != 0 { + ops = append(ops, finalizersEmpty) + } + if len(finalizers.Add) != 0 { ops = append(ops, finalizersAdd(nil, finalizers.Add)...) } diff --git a/pkg/kwok/controllers/finalizers_test.go b/pkg/kwok/controllers/finalizers_test.go index 2c2d3c7dc..ad03c023c 100644 --- a/pkg/kwok/controllers/finalizers_test.go +++ b/pkg/kwok/controllers/finalizers_test.go @@ -121,3 +121,76 @@ func Test_finalizersRemove(t *testing.T) { }) } } + +func Test_finalizersModify(t *testing.T) { + type args struct { + metaFinalizers []string + finalizers *internalversion.StageFinalizers + } + tests := []struct { + name string + args args + want []jsonpathOperation + }{ + { + args: args{ + metaFinalizers: nil, + finalizers: &internalversion.StageFinalizers{ + Empty: true, + }, + }, + want: nil, + }, + { + args: args{ + metaFinalizers: []string{"a"}, + finalizers: &internalversion.StageFinalizers{ + Empty: true, + }, + }, + want: []jsonpathOperation{ + {Op: "remove", Path: "/metadata/finalizers", Value: nil}, + }, + }, + { + args: args{ + metaFinalizers: []string{"a"}, + finalizers: &internalversion.StageFinalizers{ + Empty: true, + Add: []internalversion.FinalizerItem{ + { + Value: "b", + }, + }, + }, + }, + want: []jsonpathOperation{ + {Op: "remove", Path: "/metadata/finalizers", Value: nil}, + {Op: "add", Path: "/metadata/finalizers", Value: []string{"b"}}, + }, + }, + { + args: args{ + metaFinalizers: nil, + finalizers: &internalversion.StageFinalizers{ + Empty: true, + Add: []internalversion.FinalizerItem{ + { + Value: "b", + }, + }, + }, + }, + want: []jsonpathOperation{ + {Op: "add", Path: "/metadata/finalizers", Value: []string{"b"}}, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := finalizersModify(tt.args.metaFinalizers, tt.args.finalizers); !reflect.DeepEqual(got, tt.want) { + t.Errorf("finalizersModify() = %v, want %v", got, tt.want) + } + }) + } +}