-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't delete resources unless field managers match
- Loading branch information
Showing
7 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2025, Pulumi Corporation. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/pulumi/providertest/pulumitest" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDeleteDueToRename(t *testing.T) { | ||
t.Parallel() | ||
ctx := context.Background() | ||
test := pulumitest.NewPulumiTest(t, "delete/rename") | ||
t.Cleanup(func() { | ||
test.Destroy(t) | ||
}) | ||
|
||
test.Up(t) | ||
|
||
// Change our Pod's resource name | ||
test.UpdateSource(t, "testdata/delete/rename/step2") | ||
test.Up(t) | ||
|
||
// Renaming the namespace should not have deleted it. Perform a refresh and | ||
// make sure our pod is still running -- if it's not, Pulumi will have | ||
// deleted it from our state. | ||
refresh, err := test.CurrentStack().Refresh(ctx) | ||
assert.NoError(t, err) | ||
assert.NotContains(t, refresh.StdOut, "deleted", refresh.StdOut) | ||
} | ||
|
||
func TestDeletePatchResource(t *testing.T) { | ||
t.Parallel() | ||
ctx := context.Background() | ||
test := pulumitest.NewPulumiTest(t, "testdata/delete/patch") | ||
t.Cleanup(func() { | ||
test.Destroy(t) | ||
}) | ||
|
||
test.Up(t) | ||
|
||
time.Sleep(60 * time.Second) | ||
|
||
outputs, err := test.CurrentStack().Outputs(ctx) | ||
require.NoError(t, err) | ||
|
||
// The ConfigMap should have 3 managed fields -- one for the URN annotation | ||
// and 2 for the patches. | ||
mf, ok := outputs["managedFields"] | ||
require.True(t, ok) | ||
assert.Len(t, mf.Value, 3) | ||
|
||
// Delete a patch. | ||
test.UpdateSource(t, "testdata/delete/patch/step2") | ||
test.Up(t) | ||
|
||
// One ConfigMapPatch should still be applied, plus a manager for our URN | ||
// annotation. | ||
outputs, err = test.CurrentStack().Outputs(ctx) | ||
require.NoError(t, err) | ||
mf, ok = outputs["managedFields"] | ||
require.True(t, ok) | ||
assert.Len(t, mf.Value, 2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: delete-patch-resource | ||
runtime: yaml | ||
description: | | ||
Deleting a logical patch resource should not delete the underlying physical | ||
resource. | ||
outputs: | ||
managedFields: ${patch2.metadata.managedFields} | ||
|
||
resources: | ||
configmap: | ||
type: kubernetes:core/v1:ConfigMap | ||
properties: | ||
metadata: | ||
name: patched-configmap | ||
|
||
patch1: | ||
type: kubernetes:core/v1:ConfigMapPatch | ||
properties: | ||
metadata: | ||
name: patched-configmap | ||
data: | ||
foo: bar | ||
options: | ||
dependsOn: | ||
- ${configmap} | ||
|
||
patch2: | ||
type: kubernetes:core/v1:ConfigMapPatch | ||
properties: | ||
metadata: | ||
name: patched-configmap | ||
data: | ||
boo: baz | ||
options: | ||
dependsOn: | ||
- ${patch1} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
name: delete-patch-resource | ||
runtime: yaml | ||
description: | | ||
Deleting a logical patch resource should not delete the underlying physical | ||
resource. | ||
outputs: | ||
managedFields: ${patch1.metadata.managedFields} | ||
|
||
resources: | ||
configmap: | ||
type: kubernetes:core/v1:ConfigMap | ||
properties: | ||
metadata: | ||
name: patched-configmap | ||
|
||
patch1: | ||
type: kubernetes:core/v1:ConfigMapPatch | ||
properties: | ||
metadata: | ||
name: patched-configmap | ||
data: | ||
foo: bar | ||
options: | ||
dependsOn: | ||
- ${configmap} | ||
|
||
# Delete patch2 - the underlying ConfigMap should not be deleted, and patch1 | ||
# should still be applied. | ||
# | ||
# patch2: | ||
# type: kubernetes:core/v1:ConfigMapPatch | ||
# properties: | ||
# metadata: | ||
# name: patched-configmap | ||
# data: | ||
# boo: baz | ||
# options: | ||
# dependsOn: | ||
# - ${patch1} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
name: delete-with-rename | ||
runtime: yaml | ||
description: | | ||
Changing a resource's name, but leaving .metadata untouched, should not | ||
result in a deletion from the cluster. | ||
resources: | ||
pod: | ||
type: kubernetes:core/v1:Pod | ||
properties: | ||
spec: | ||
containers: | ||
- image: nginx:1.14.2 | ||
name: nginx | ||
ports: | ||
- containerPort: 80 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: delete-with-rename | ||
runtime: yaml | ||
description: | | ||
Changing a resource's name, but leaving .metadata untouched, should not | ||
result in a deletion from the cluster. | ||
resources: | ||
# Change the resource's name from "pod" to "mypod" but leave everything | ||
# else the same. | ||
mypod: | ||
type: kubernetes:core/v1:Pod | ||
properties: | ||
spec: | ||
containers: | ||
- image: nginx:1.14.2 | ||
name: nginx | ||
ports: | ||
- containerPort: 80 |