Skip to content

Commit

Permalink
Add a descriptive message for an invalid Patch delete
Browse files Browse the repository at this point in the history
  • Loading branch information
lblackstone committed Jul 29, 2022
1 parent ebdf73b commit fccd615
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,7 @@
## Unreleased

- Add a descriptive message for an invalid Patch delete (https://github.com/pulumi/pulumi-kubernetes/pull/2111)

## 3.20.2 (July 25, 2022)

- Add Java SDK (https://github.com/pulumi/pulumi-kubernetes/pull/2096)
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 @@ -2426,6 +2426,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)
}
}
partialErr, isPartialErr := awaitErr.(await.PartialError)
if !isPartialErr {
// There was an error executing the delete operation. The resource is still present and tracked.
Expand Down

0 comments on commit fccd615

Please sign in to comment.