Skip to content

Commit

Permalink
Merge pull request #223 from SomtochiAma/new-manger
Browse files Browse the repository at this point in the history
Replaces kubectl field managers with the resource manager owner in ssa apply
  • Loading branch information
stefanprodan authored Jan 20, 2022
2 parents 9fa16a9 + d5ab2c5 commit 6083da5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ssa/manager_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (m *ResourceManager) cleanupMetadata(ctx context.Context, object *unstructu
}

if len(opts.FieldManagers) > 0 {
patches = append(patches, patchRemoveFieldsManagers(existingObject, opts.FieldManagers)...)
patches = append(patches, patchReplaceFieldsManagers(existingObject, opts.FieldManagers, m.owner.Field)...)
}

// no patching is needed exit early
Expand Down
2 changes: 1 addition & 1 deletion ssa/manager_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ func TestApply_Cleanup(t *testing.T) {
}
})

t.Run("removes kubectl server-side-apply manager", func(t *testing.T) {
t.Run("replaces kubectl server-side-apply manager", func(t *testing.T) {
for _, object := range objects {
obj := object.DeepCopy()
if err := manager.client.Patch(ctx, obj, client.Apply, client.FieldOwner("kubectl")); err != nil {
Expand Down
28 changes: 28 additions & 0 deletions ssa/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,34 @@ func patchRemoveFieldsManagers(object *unstructured.Unstructured, managers []Fil
return append(patches, newPatchReplace("/metadata/managedFields", entries))
}

// patchReplaceFieldsManagers returns a jsonPatch array for replacing the managers with matching prefix and operation type
// with the specified manager name and an apply operation.
func patchReplaceFieldsManagers(object *unstructured.Unstructured, managers []FiledManager, name string) []jsonPatch {
objEntries := object.GetManagedFields()

var patches []jsonPatch
entries := make([]metav1.ManagedFieldsEntry, 0, len(objEntries))
renamed := false
for _, entry := range objEntries {
for _, manager := range managers {
if strings.HasPrefix(entry.Manager, manager.Name) &&
entry.Operation == manager.OperationType &&
entry.Subresource == "" {
entry.Manager = name
entry.Operation = metav1.ManagedFieldsOperationApply
renamed = true
}
}
entries = append(entries, entry)
}

if !renamed {
return nil
}

return append(patches, newPatchReplace("/metadata/managedFields", entries))
}

// patchRemoveAnnotations returns a jsonPatch array for removing annotations with matching keys.
func patchRemoveAnnotations(object *unstructured.Unstructured, keys []string) []jsonPatch {
var patches []jsonPatch
Expand Down

0 comments on commit 6083da5

Please sign in to comment.