add unit test and documentation for finalizers (#2509)

* add unit test and documentation for finalizers
* error msg with lower case and cover sync case
* try to avoid adding json-patch dependency
* use Update to remove finalizer
* changing status and finalizer during create
* do not call Delete() twice
This commit is contained in:
Felix Kunde
2024-01-22 12:13:40 +01:00
committed by GitHub
parent 3bad9aaded
commit 4a0c483514
14 changed files with 245 additions and 118 deletions
+31
View File
@@ -213,6 +213,37 @@ func (client *KubernetesClient) SetPostgresCRDStatus(clusterName spec.Namespaced
return pg, nil
}
// SetFinalizer of Postgres cluster
func (client *KubernetesClient) SetFinalizer(clusterName spec.NamespacedName, pg *apiacidv1.Postgresql, finalizers []string) (*apiacidv1.Postgresql, error) {
var (
updatedPg *apiacidv1.Postgresql
patch []byte
err error
)
pg.ObjectMeta.SetFinalizers(finalizers)
if len(finalizers) > 0 {
patch, err = json.Marshal(struct {
PgMetadata interface{} `json:"metadata"`
}{&pg.ObjectMeta})
if err != nil {
return pg, fmt.Errorf("could not marshal ObjectMeta: %v", err)
}
updatedPg, err = client.PostgresqlsGetter.Postgresqls(clusterName.Namespace).Patch(
context.TODO(), clusterName.Name, types.MergePatchType, patch, metav1.PatchOptions{})
} else {
// in case finalizers are empty and update is needed to remove
updatedPg, err = client.PostgresqlsGetter.Postgresqls(clusterName.Namespace).Update(
context.TODO(), pg, metav1.UpdateOptions{})
}
if err != nil {
return updatedPg, fmt.Errorf("could not set finalizer: %v", err)
}
return updatedPg, nil
}
// SamePDB compares the PodDisruptionBudgets
func SamePDB(cur, new *apipolicyv1.PodDisruptionBudget) (match bool, reason string) {
//TODO: improve comparison
+11
View File
@@ -152,6 +152,17 @@ func IsEqualIgnoreOrder(a, b []string) bool {
return reflect.DeepEqual(a_copy, b_copy)
}
// Iterate through slice and remove certain string, then return cleaned slice
func RemoveString(slice []string, s string) (result []string) {
for _, item := range slice {
if item == s {
continue
}
result = append(result, item)
}
return result
}
// SliceReplaceElement
func StringSliceReplaceElement(s []string, a, b string) (result []string) {
tmp := make([]string, 0, len(s))
+20
View File
@@ -66,6 +66,17 @@ var substractTest = []struct {
{[]string{"a"}, []string{""}, []string{"a"}, false},
}
var removeStringTest = []struct {
slice []string
item string
result []string
}{
{[]string{"a", "b", "c"}, "b", []string{"a", "c"}},
{[]string{"a"}, "b", []string{"a"}},
{[]string{"a"}, "a", []string{}},
{[]string{}, "a", []string{}},
}
var sliceContaintsTest = []struct {
slice []string
item string
@@ -200,6 +211,15 @@ func TestFindNamedStringSubmatch(t *testing.T) {
}
}
func TestRemoveString(t *testing.T) {
for _, tt := range removeStringTest {
res := RemoveString(tt.slice, tt.item)
if !IsEqualIgnoreOrder(res, tt.result) {
t.Errorf("RemoveString expected: %#v, got: %#v", tt.result, res)
}
}
}
func TestSliceContains(t *testing.T) {
for _, tt := range sliceContaintsTest {
res := SliceContains(tt.slice, tt.item)