Skip to content

Commit a7084d4

Browse files
committed
fix: string helpers and unit tests
1 parent eecfd88 commit a7084d4

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

pkg/controller/controllerutil/controllerutil.go

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,8 @@ type MutateFn func() error
172172

173173
// AddFinalizer accepts a metav1 object and adds the provided finalizer if not present.
174174
func AddFinalizer(o metav1.Object, finalizer string) {
175-
finalizers := o.GetFinalizers()
176-
for _, f := range finalizers {
177-
if f == finalizer {
178-
return
179-
}
180-
}
181-
o.SetFinalizers(append(finalizers, finalizer))
175+
newFinalizers := AddString(o.GetFinalizers(), finalizer)
176+
o.SetFinalizers(append(newFinalizers, finalizer))
182177
}
183178

184179
// AddFinalizerIfPossible tries to convert a runtime object to a metav1 object and add the provided finalizer.
@@ -194,16 +189,8 @@ func AddFinalizerIfPossible(o runtime.Object, finalizer string) error {
194189

195190
// RemoveFinalizer accepts a metav1 object and removes the provided finalizer if present.
196191
func RemoveFinalizer(o metav1.Object, finalizer string) {
197-
finalizers := o.GetFinalizers()
198-
n := 0
199-
for _, f := range finalizers {
200-
if f != finalizer {
201-
finalizers[n] = f
202-
n++
203-
}
204-
}
205-
finalizers = finalizers[:n]
206-
o.SetFinalizers(finalizers)
192+
newFinalizers := RemoveString(o.GetFinalizers(), finalizer)
193+
o.SetFinalizers(newFinalizers)
207194
}
208195

209196
// RemoveFinalizerIfPossible tries to convert a runtime object to a metav1 object and remove the provided finalizer.
@@ -216,3 +203,27 @@ func RemoveFinalizerIfPossible(o runtime.Object, finalizer string) error {
216203
RemoveFinalizer(m, finalizer)
217204
return nil
218205
}
206+
207+
// AddString returns a []string with s appended if it is not already found in the provided slice.
208+
func AddString(slice []string, s string) []string {
209+
newSlice := make([]string, 0)
210+
for i, item := range slice {
211+
if item == s {
212+
return append(newSlice, slice[i+1:]...)
213+
}
214+
newSlice = append(newSlice, item)
215+
}
216+
return append(slice, s)
217+
}
218+
219+
// RemoveString returns a newly created []string that contains all items from slice that are not equal to s.
220+
func RemoveString(slice []string, s string) []string {
221+
newSlice := make([]string, 0)
222+
for _, item := range slice {
223+
if item == s {
224+
continue
225+
}
226+
newSlice = append(newSlice, item)
227+
}
228+
return newSlice
229+
}

pkg/controller/controllerutil/controllerutil_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,25 @@ var _ = Describe("Controllerutil", func() {
306306
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{}))
307307
})
308308
})
309+
310+
Describe("String Helpers", func() {
311+
var testStrings []string
312+
testString := "foobar"
313+
It("should add the string when not present", func() {
314+
testStrings = controllerutil.AddString(testStrings, testString)
315+
Expect(testStrings).To(Equal([]string{testString}))
316+
})
317+
318+
It("should not add the string when already present", func() {
319+
testStrings = controllerutil.AddString(testStrings, testString)
320+
Expect(testStrings).To(Equal([]string{testString}))
321+
})
322+
323+
It("should remove string if present", func() {
324+
testStrings = controllerutil.RemoveString(testStrings, testString)
325+
Expect(testStrings).To(Equal([]string{}))
326+
})
327+
})
309328
})
310329
})
311330

0 commit comments

Comments
 (0)