Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deploy: refactor getResource to return NotFound for both cases #1046

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions pkg/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ import (
"strings"

"golang.org/x/exp/maps"
apierrs "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/yaml"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -208,23 +210,25 @@ func manageResource(ctx context.Context, cli client.Client, obj *unstructured.Un
return nil
}

// Return if error getting resource in cluster
found, err := getResource(ctx, cli, obj)
if err != nil {
return err
}

if !enabled {
// err == nil means found
if err == nil {
if enabled {
return updateResource(ctx, cli, obj, found, owner, componentName)
}
return handleDisabledComponent(ctx, cli, found, componentName)
}

// Create resource if it doesn't exist
if found == nil {
return createResource(ctx, cli, obj, owner)
if apierrs.IsNotFound(err) {
// Create resource if it doesn't exist and enabled
if enabled {
return createResource(ctx, cli, obj, owner)
}
return nil
}

// If resource already exists, update it.
return updateResource(ctx, cli, obj, found, owner, componentName)
return err
}

/*
Expand Down Expand Up @@ -358,23 +362,18 @@ func getResource(ctx context.Context, cli client.Client, obj *unstructured.Unstr
found := &unstructured.Unstructured{}
// Setting gvk is required to do Get request
found.SetGroupVersionKind(obj.GroupVersionKind())
if err := cli.Get(ctx, types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}, found); err != nil {
if errors.Is(err, &meta.NoKindMatchError{}) {
return nil, nil // ignore mising CRD error
}
if client.IgnoreNotFound(err) == nil {
return nil, nil // not found resource
}
err := cli.Get(ctx, types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}, found)
if errors.Is(err, &meta.NoKindMatchError{}) {
// convert the error to NotFound to handle both the same way in the caller
return nil, apierrs.NewNotFound(schema.GroupResource{Group: obj.GroupVersionKind().Group}, obj.GetName())
}
if err != nil {
return nil, err
}
return found, nil
}

func handleDisabledComponent(ctx context.Context, cli client.Client, found *unstructured.Unstructured, componentName string) error {
if found == nil {
return nil
}

resourceLabels := found.GetLabels()
componentCounter := getComponentCounter(resourceLabels)

Expand Down
Loading