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

Enhance SSA ignoreChanges by having better field manager path comparisons #2828

Merged
merged 3 commits into from
Feb 29, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

- Fix SSA ignoreChanges by enhancing field manager path comparisons (https://github.com/pulumi/pulumi-kubernetes/pull/2828)

## 4.8.1 (February 22, 2024)

- skip normalization in preview w/ computed fields (https://github.com/pulumi/pulumi-kubernetes/pull/2846)
Expand Down
19 changes: 18 additions & 1 deletion provider/pkg/await/await.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,6 @@ func handleSSAIgnoreFields(c *UpdateConfig, liveOldObj *unstructured.Unstructure
managedFields := liveOldObj.GetManagedFields()
// Keep track of fields that are managed by the current field manager, and fields that are managed by other field managers.
theirFields, ourFields := new(fieldpath.Set), new(fieldpath.Set)
fieldpath.MakePathOrDie()

for _, f := range managedFields {
s, err := fluxssa.FieldsToSet(*f.FieldsV1)
Expand All @@ -584,6 +583,10 @@ func handleSSAIgnoreFields(c *UpdateConfig, liveOldObj *unstructured.Unstructure
}
}

// Add all parent paths of the fields to the set.
theirFields = ensureFieldsAreMembers(theirFields)
ourFields = ensureFieldsAreMembers(ourFields)

for _, ignorePath := range c.IgnoreChanges {
ipParsed, err := resource.ParsePropertyPath(ignorePath)
if err != nil {
Expand Down Expand Up @@ -645,6 +648,20 @@ func makeInterfaceSlice[T any](inputs []T) []interface{} {
return s
}

// ensureFieldsAreMembers adds all parent paths of a given fieldpath.Set back to the set. The fieldpath.Set.Insert method specifically mentions that it does not
// add parent paths, so we need to do this manually. We do this by iterating from the start of the path to the end, and
// adding each level of the path tree to the set.
func ensureFieldsAreMembers(s *fieldpath.Set) *fieldpath.Set {
newSet := new(fieldpath.Set)
s.Iterate(func(p fieldpath.Path) {
for i := range p {
newSet.Insert(p[:i+1])
}
})

return newSet
}

// fixCSAFieldManagers patches the field managers for an existing resource that was managed using client-side apply.
// The new server-side apply field manager takes ownership of all these fields to avoid conflicts.
func fixCSAFieldManagers(c *UpdateConfig, liveOldObj *unstructured.Unstructured, client dynamic.ResourceInterface) (*unstructured.Unstructured, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ const depPatch = new k8s.apps.v1.DeploymentPatch(
},
spec: {
template: {
metadata: {
labels: undefined,
},
spec: {
containers: [
{
Expand All @@ -46,4 +43,4 @@ const depPatch = new k8s.apps.v1.DeploymentPatch(
},
}
},
}, { provider: provider, retainOnDelete: true });
}, { provider: provider, retainOnDelete: true, ignoreChanges: ["spec.template.metadata", "spec.selector"]});
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ const depPatch = new k8s.apps.v1.DeploymentPatch(
},
spec: {
template: {
metadata: {
labels: undefined,
},
spec: {
containers: [
{
Expand All @@ -46,4 +43,4 @@ const depPatch = new k8s.apps.v1.DeploymentPatch(
},
}
},
}, { provider: provider, retainOnDelete: true });
}, { provider: provider, retainOnDelete: true, ignoreChanges: ["spec.template.metadata", "spec.selector"]});
21 changes: 21 additions & 0 deletions tests/sdk/nodejs/nodejs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2325,6 +2325,27 @@ func TestFieldManagerPatchResources(t *testing.T) {
depReplicas, err := tests.Kubectl("get deployment -o=jsonpath={.spec.replicas} -n", namespace, "test-mgr-nginx")
assert.NoError(t, err)
assert.Equal(t, "2", string(depReplicas))

// Ensure that we don't inadvertently share ownership of nested fields that we specify in ignoreChanges.
// See: https://github.com/pulumi/pulumi-kubernetes/issues/2714.
liveObj, err := tests.Kubectl("get deployment -o yaml --show-managed-fields -n", namespace, "test-mgr-nginx")
assert.NoError(t, err)
wantString := ` - apiVersion: apps/v1
fieldsType: FieldsV1
fieldsV1:
f:metadata:
f:annotations:
f:pulumi.com/patchForce: {}
f:spec:
f:template:
f:spec:
f:containers:
k:{"name":"nginx"}:
.: {}
f:image: {}
f:name: {}
manager: pulumi-kubernetes-`
assert.Contains(t, string(liveObj), wantString)
},
},
},
Expand Down
Loading