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

Add a descriptive message for an invalid Patch delete #2111

Merged
merged 2 commits into from Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -1,5 +1,6 @@
## Unreleased

- Add a descriptive message for an invalid Patch delete (https://github.com/pulumi/pulumi-kubernetes/pull/2111)
- Fix erroneous resourceVersion diff for CRDs managed with SSA (https://github.com/pulumi/pulumi-kubernetes/pull/2121)

## 3.20.2 (July 25, 2022)
Expand Down
7 changes: 7 additions & 0 deletions provider/pkg/await/error.go
Expand Up @@ -4,6 +4,7 @@ package await

import (
"fmt"
"strings"

"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -129,3 +130,9 @@ func IsResourceExistsErr(err error) bool {

return errors.IsAlreadyExists(err)
}

// IsDeleteRequiredFieldErr is true if the user attempted to delete a Patch resource that was the sole manager of a
// required field.
func IsDeleteRequiredFieldErr(err error) bool {
return errors.IsInvalid(err) && strings.Contains(err.Error(), "Required value")
}
8 changes: 8 additions & 0 deletions provider/pkg/provider/provider.go
Expand Up @@ -2432,6 +2432,14 @@ func (k *kubeProvider) Delete(ctx context.Context, req *pulumirpc.DeleteRequest)
// to consider the CR to be deleted as well in this case.
return &pbempty.Empty{}, nil
}
if strings.HasSuffix(urn.Type().String(), "Patch") && await.IsDeleteRequiredFieldErr(awaitErr) {
if cause, ok := errors.StatusCause(awaitErr, metav1.CauseTypeFieldValueRequired); ok {
awaitErr = fmt.Errorf(
"this Patch resource is currently managing a required field, so it can't be deleted "+
"directly. Either set the `retainOnDelete` resource option, or transfer ownership of the "+
"field before deleting: %s", cause.Field)
Copy link
Contributor

Choose a reason for hiding this comment

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

Did we want to add a link to some tutorial for this once we have one?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I'm planning to update the message once we have some better docs online

}
}
partialErr, isPartialErr := awaitErr.(await.PartialError)
if !isPartialErr {
// There was an error executing the delete operation. The resource is still present and tracked.
Expand Down