Skip to content

Commit

Permalink
fixups
Browse files Browse the repository at this point in the history
Signed-off-by: Anatolii Bazko <[email protected]>
  • Loading branch information
tolusha committed Jan 17, 2024
1 parent 602614a commit 5f22247
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
44 changes: 17 additions & 27 deletions pkg/deploy/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
"context"
"fmt"
"reflect"

ctrl "sigs.k8s.io/controller-runtime"

"github.com/eclipse-che/che-operator/pkg/common/chetypes"
"github.com/google/go-cmp/cmp"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -153,13 +153,12 @@ func UpdateWithClient(client client.Client, deployContext *chetypes.DeployContex
}

if isUpdateUsingDeleteCreate(actual.GetObjectKind().GroupVersionKind().Kind) {
done, err := DeleteWithClient(client, actual)
done, err := doDeleteIgnoreIfNotFound(context.TODO(), client, actual)
if !done {
return false, err
}
return CreateWithClient(client, deployContext, blueprint, false)
} else {
logrus.Infof("Updating existing object: %s, name: %s", GetObjectType(actualMeta), actualMeta.GetName())
err := setOwnerReferenceIfNeeded(deployContext, blueprint)
if err != nil {
return false, err
Expand All @@ -168,25 +167,25 @@ func UpdateWithClient(client client.Client, deployContext *chetypes.DeployContex
// to be able to update, we need to set the resource version of the object that we know of
blueprint.(metav1.Object).SetResourceVersion(actualMeta.GetResourceVersion())
err = client.Update(context.TODO(), blueprint)
syncLog.Info("Object updated", "namespace", actual.GetNamespace(), "kind", GetObjectType(actual), "name", actual.GetName())
return false, err
}
}
return true, nil
}

func CreateWithClient(client client.Client, deployContext *chetypes.DeployContext, blueprint client.Object, returnTrueIfAlreadyExists bool) (bool, error) {
logrus.Infof("Creating a new object: %s, name: %s", GetObjectType(blueprint), blueprint.GetName())

err := setOwnerReferenceIfNeeded(deployContext, blueprint)
if err != nil {
return false, err
}

err = client.Create(context.TODO(), blueprint)
if err == nil {
syncLog.Info("Object created", "namespace", blueprint.GetNamespace(), "kind", GetObjectType(blueprint), "name", blueprint.GetName())
return true, nil
} else if errors.IsAlreadyExists(err) {
return returnTrueIfAlreadyExists, nil
return false, nil
} else {
return false, err
}
Expand All @@ -206,18 +205,7 @@ func DeleteByKeyWithClient(cli client.Client, key client.ObjectKey, objectMeta c
return false, err
}

return DeleteWithClient(cli, actual)
}

func DeleteWithClient(client client.Client, actual client.Object) (bool, error) {
logrus.Infof("Deleting object: %s, name: %s", GetObjectType(actual), actual.GetName())

err := client.Delete(context.TODO(), actual)
if err == nil || errors.IsNotFound(err) {
return true, nil
} else {
return false, err
}
return doDeleteIgnoreIfNotFound(context.TODO(), cli, actual)
}

func GetWithClient(client client.Client, key client.ObjectKey, object client.Object) (bool, error) {
Expand Down Expand Up @@ -277,7 +265,8 @@ func DeleteIgnoreIfNotFound(context context.Context, cli client.Client, key clie

exists, err := doGet(context, cli, key, actual)
if exists {
return doDelete(context, cli, actual)
_, err := doDeleteIgnoreIfNotFound(context, cli, actual)
return err
}

return err
Expand All @@ -286,30 +275,31 @@ func DeleteIgnoreIfNotFound(context context.Context, cli client.Client, key clie
// doCreate creates object.
// Returns true if object created otherwise returns false.
// Throws error if object cannot be created or already exists otherwise returns nil.
func doCreate(context context.Context, client client.Client, deployContext *chetypes.DeployContext, blueprint client.Object) error {
func doCreate(context context.Context, client client.Client, deployContext *chetypes.DeployContext, blueprint client.Object) (bool, error) {
err := setOwnerReferenceIfNeeded(deployContext, blueprint)
if err != nil {
return err
return false, err
}

err = client.Create(context, blueprint)
if err == nil {
syncLog.Info("Object created", "namespace", blueprint.GetNamespace(), "kind", GetObjectType(blueprint), "name", blueprint.GetName())
return nil
return true, nil
} else {
return err
return false, err
}
}

// doDelete deletes object.
// doDeleteIgnoreIfNotFound deletes object.
// Returns true if object deleted or not found otherwise returns false.
// Returns error if object cannot be deleted otherwise returns nil.
func doDelete(context context.Context, cli client.Client, actual client.Object) error {
func doDeleteIgnoreIfNotFound(context context.Context, cli client.Client, actual client.Object) (bool, error) {
err := cli.Delete(context, actual)
if err == nil || errors.IsNotFound(err) {
syncLog.Info("Object deleted", "namespace", actual.GetNamespace(), "kind", GetObjectType(actual), "name", actual.GetName())
return nil
return true, nil
} else {
return err
return false, err
}
}

Expand Down
23 changes: 23 additions & 0 deletions pkg/deploy/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,29 @@ func TestGet(t *testing.T) {
}
}

func TestCreate(t *testing.T) {
cli, deployContext := initDeployContext()

done, err := Create(deployContext, testObj.DeepCopy())
if err != nil {
t.Fatalf("Failed to create object: %v", err)
}

if !done {
t.Fatalf("Object has not been created")
}

actual := &corev1.Secret{}
err = cli.Get(context.TODO(), testKey, actual)
if err != nil && !errors.IsNotFound(err) {
t.Fatalf("Failed to get object: %v", err)
}

if actual == nil {
t.Fatalf("Object not found")
}
}

func TestCreateIfNotExistsShouldReturnTrueIfObjectCreated(t *testing.T) {
cli, deployContext := initDeployContext()

Expand Down

0 comments on commit 5f22247

Please sign in to comment.