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