From f79c8434fc8cdab17ac5b65f92a56b2e4dbf87bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Sierant?= Date: Sat, 28 Sep 2024 15:58:49 +0200 Subject: [PATCH 1/2] Changed order or CreateOrUpdate functions --- pkg/kube/configmap/configmap.go | 8 ++++---- pkg/kube/secret/secret.go | 8 ++++---- pkg/kube/statefulset/statefulset.go | 13 +++++++------ 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/pkg/kube/configmap/configmap.go b/pkg/kube/configmap/configmap.go index a94c31c1f..36f38d469 100644 --- a/pkg/kube/configmap/configmap.go +++ b/pkg/kube/configmap/configmap.go @@ -85,14 +85,14 @@ func UpdateField(ctx context.Context, getUpdater GetUpdater, objectKey client.Ob // CreateOrUpdate creates the given ConfigMap if it doesn't exist, // or updates it if it does. func CreateOrUpdate(ctx context.Context, getUpdateCreator GetUpdateCreator, cm corev1.ConfigMap) error { - _, err := getUpdateCreator.GetConfigMap(ctx, types.NamespacedName{Name: cm.Name, Namespace: cm.Namespace}) - if err != nil { + if err := getUpdateCreator.UpdateConfigMap(ctx, cm); err != nil { if apiErrors.IsNotFound(err) { return getUpdateCreator.CreateConfigMap(ctx, cm) + } else { + return err } - return err } - return getUpdateCreator.UpdateConfigMap(ctx, cm) + return nil } // filelikePropertiesToMap converts a file-like field in a ConfigMap to a map[string]string. diff --git a/pkg/kube/secret/secret.go b/pkg/kube/secret/secret.go index fa89193db..93f9b64ea 100644 --- a/pkg/kube/secret/secret.go +++ b/pkg/kube/secret/secret.go @@ -99,14 +99,14 @@ func UpdateField(ctx context.Context, getUpdater GetUpdater, objectKey client.Ob // CreateOrUpdate creates the Secret if it doesn't exist, other wise it updates it func CreateOrUpdate(ctx context.Context, getUpdateCreator GetUpdateCreator, secret corev1.Secret) error { - _, err := getUpdateCreator.GetSecret(ctx, types.NamespacedName{Name: secret.Name, Namespace: secret.Namespace}) - if err != nil { + if err := getUpdateCreator.UpdateSecret(ctx, secret); err != nil { if SecretNotExist(err) { return getUpdateCreator.CreateSecret(ctx, secret) + } else { + return err } - return err } - return getUpdateCreator.UpdateSecret(ctx, secret) + return nil } // HasAllKeys returns true if the provided secret contains an element for every diff --git a/pkg/kube/statefulset/statefulset.go b/pkg/kube/statefulset/statefulset.go index a6c5c35ec..d6e7660cb 100644 --- a/pkg/kube/statefulset/statefulset.go +++ b/pkg/kube/statefulset/statefulset.go @@ -53,15 +53,16 @@ type GetUpdateCreateDeleter interface { // CreateOrUpdate creates the given StatefulSet if it doesn't exist, // or updates it if it does. -func CreateOrUpdate(ctx context.Context, getUpdateCreator GetUpdateCreator, sts appsv1.StatefulSet) (appsv1.StatefulSet, error) { - _, err := getUpdateCreator.GetStatefulSet(ctx, types.NamespacedName{Name: sts.Name, Namespace: sts.Namespace}) - if err != nil { +func CreateOrUpdate(ctx context.Context, getUpdateCreator GetUpdateCreator, statefulSet appsv1.StatefulSet) (appsv1.StatefulSet, error) { + if sts, err := getUpdateCreator.UpdateStatefulSet(ctx, statefulSet); err != nil { if apiErrors.IsNotFound(err) { - return appsv1.StatefulSet{}, getUpdateCreator.CreateStatefulSet(ctx, sts) + return statefulSet, getUpdateCreator.CreateStatefulSet(ctx, statefulSet) + } else { + return appsv1.StatefulSet{}, err } - return appsv1.StatefulSet{}, err + } else { + return sts, nil } - return getUpdateCreator.UpdateStatefulSet(ctx, sts) } // GetAndUpdate applies the provided function to the most recent version of the object From b456e5bc2f80c472ee592629c50309ed01102de8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Sierant?= Date: Sat, 28 Sep 2024 17:04:18 +0200 Subject: [PATCH 2/2] Fixed mockedclient to return NotFound on update --- pkg/kube/client/mocked_client.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/kube/client/mocked_client.go b/pkg/kube/client/mocked_client.go index f9b67e2b1..f4a8e499e 100644 --- a/pkg/kube/client/mocked_client.go +++ b/pkg/kube/client/mocked_client.go @@ -59,6 +59,9 @@ func (m mockedClient) Create(_ context.Context, obj k8sClient.Object, _ ...k8sCl func (m mockedClient) Update(_ context.Context, obj k8sClient.Object, _ ...k8sClient.UpdateOption) error { relevantMap := m.ensureMapFor(obj) objKey := k8sClient.ObjectKeyFromObject(obj) + if _, ok := relevantMap[objKey]; !ok { + return errors.NewNotFound(schema.GroupResource{}, obj.GetName()) + } relevantMap[objKey] = obj return nil }