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

fix: when delete resource if no CRD in the cluster #1044

Merged
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
12 changes: 9 additions & 3 deletions pkg/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"strings"

"golang.org/x/exp/maps"
"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/types"
Expand Down Expand Up @@ -209,7 +210,7 @@ func manageResource(ctx context.Context, cli client.Client, obj *unstructured.Un

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

Expand Down Expand Up @@ -357,8 +358,13 @@ 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())
err := cli.Get(ctx, types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}, found)
if err != nil {
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it be ok to return new NotFound? Argumentation is the same as af749ec

Copy link
Member Author

@zdtsw zdtsw Jun 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we just handle the notfound directly in the getResource()?
and we
e.g

func getResource(ctx context.Context, cli client.Client, obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
	found := &unstructured.Unstructured{}
	// Setting gvk is required to do Get request
	found.SetGroupVersionKind(obj.GroupVersionKind())
	err := cli.Get(ctx, types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}, found)
	if 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
		}
		return nil, err
	}
	return found, nil
}

for the caller:

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will work, but I do not like the idea to return nil object with nil error. It's prone to mistakes. But I do not have a strong opinion. Let's ask one more point of view? :) @bartoszmajsak

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zdtsw @ykaliuta In my opinion, returning nil, nil can cause more harm than good. The response should either be the requested item or an error. This way, you only need to check for nil in the case of an error, allowing you to handle exceptions appropriately. Otherwise, we risk cluttering the codebase with nil checks or encountering nil pointer errors at runtime, as the caller may assume the returned instance is valid if the error is nil. Therefore, I believe it is preferable to return apierrs.NewNotFound as @ykaliuta suggests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

follow up in #1045 to address comments.

}
if client.IgnoreNotFound(err) == nil {
return nil, nil // not found resource
}
return nil, err
}
return found, nil
Expand Down
Loading