Skip to content

Commit

Permalink
Improve deleteUnreachable workflow (#2489)
Browse files Browse the repository at this point in the history
The provider includes a deleteUnreachable option that will delete unreachable cluster resources from state. This was added as an opt-in behavior to avoid accidental state deletion for a temporarily unreachable cluster. This change improves this workflow in two ways:

1. The error messages for both the refresh and destroy cases were revised for clarity, and suggest using the PULUMI_K8S_DELETE_UNREACHABLE environment variable to enable this behavior.
2. The destroy workflow was changed to succeed for unreachable resources when the deleteUnreachable option is set rather than requiring the user to refresh the state as a separate step.

Note that this behavior can also be enabled with the Provider's deleteUnreachable configuration, but we do not mention this in the error message since it could lead to undesired state changes in case of a temporarily unreachable cluster.
  • Loading branch information
lblackstone committed Jul 11, 2023
1 parent 4b04d37 commit 57633f9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

- Improve deleteUnreachable workflow for unreachable clusters (https://github.com/pulumi/pulumi-kubernetes/pull/2489)

## 3.30.1 (June 29, 2023)

- Add experimental helmChart support to kustomize.Directory (https://github.com/pulumi/pulumi-kubernetes/pull/2471)
Expand Down
31 changes: 20 additions & 11 deletions provider/pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1953,18 +1953,18 @@ func (k *kubeProvider) Read(ctx context.Context, req *pulumirpc.ReadRequest) (*p

// If the cluster is unreachable, return an error unless the user has opted in to mark the resources to be deleted
if k.clusterUnreachable {
_ = k.host.Log(ctx, diag.Warning, urn, fmt.Sprintf(
"configured Kubernetes cluster is unreachable: %s", k.clusterUnreachableReason))
if k.deleteUnreachable {
_ = k.host.Log(ctx, diag.Warning, urn, fmt.Sprintf(
"configured Kubernetes cluster is unreachable and configuration is specified to ensure that "+
"the resources are deleted: %s", k.clusterUnreachableReason))
_ = k.host.Log(ctx, diag.Info, urn, fmt.Sprintf(
"configured Kubernetes cluster is unreachable and the `deleteUnreachable` option is enabled. "+
"Deleting the unreachable resource from Pulumi state"))
return deleteResponse, nil
}

_ = k.host.Log(ctx, diag.Warning, urn, fmt.Sprintf(
"configured Kubernetes cluster is unreachable: %s", k.clusterUnreachableReason))
return nil, fmt.Errorf("failed to read resource state due to unreachable cluster. " +
"If the cluster has been deleted, you can edit the pulumi state to remove this resource or retry " +
"with the PULUMI_K8S_DELETE_UNREACHABLE environment variable set to true.")
return nil, fmt.Errorf("failed to read resource state due to unreachable cluster. If the cluster was " +
"deleted, you can remove this resource from Pulumi state by rerunning the operation with the " +
"PULUMI_K8S_DELETE_UNREACHABLE environment variable set to \"true\"")
}

if isHelmRelease(urn) {
Expand Down Expand Up @@ -2459,9 +2459,18 @@ func (k *kubeProvider) Delete(ctx context.Context, req *pulumirpc.DeleteRequest)
}

if k.clusterUnreachable {
return nil, fmt.Errorf("configured Kubernetes cluster is unreachable: %s\n"+
"If the cluster has been deleted, you can edit the pulumi state to remove this resource",
k.clusterUnreachableReason)
_ = k.host.Log(ctx, diag.Warning, urn, fmt.Sprintf(
"configured Kubernetes cluster is unreachable: %s", k.clusterUnreachableReason))
if k.deleteUnreachable {
_ = k.host.Log(ctx, diag.Info, urn, fmt.Sprintf(
"configured Kubernetes cluster is unreachable and the `deleteUnreachable` option is enabled. "+
"Deleting the unreachable resource from Pulumi state"))
return &pbempty.Empty{}, nil
}

return nil, fmt.Errorf("configured Kubernetes cluster is unreachable. If the cluster was deleted, " +
"you can remove this resource from Pulumi state by rerunning the operation with the " +
"PULUMI_K8S_DELETE_UNREACHABLE environment variable set to \"true\"")
}

initialAPIVersion, err := initialAPIVersion(oldState, &unstructured.Unstructured{})
Expand Down

0 comments on commit 57633f9

Please sign in to comment.