Skip to content

Fix --three-way-merge to not show diff on meta.helm.sh annotations #331

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

Merged
merged 1 commit into from
Jan 9, 2022
Merged
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
22 changes: 18 additions & 4 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func genManifest(original, target kube.ResourceList) ([]byte, []byte, error) {
}
// to be updated
out, _ := jsoniterator.ConfigCompatibleWithStandardLibrary.Marshal(currentObj)
pruneObj, err := deleteStatusAndManagedFields(out)
pruneObj, err := deleteStatusAndTidyMetadata(out)
if err != nil {
return errors.Wrapf(err, "prune current obj %q with kind %s", info.Name, kind)
}
Expand All @@ -358,7 +358,7 @@ func genManifest(original, target kube.ResourceList) ([]byte, []byte, error) {
return errors.Wrapf(err, "cannot patch %q with kind %s", info.Name, kind)
}
out, _ = jsoniterator.ConfigCompatibleWithStandardLibrary.Marshal(targetObj)
pruneObj, err = deleteStatusAndManagedFields(out)
pruneObj, err = deleteStatusAndTidyMetadata(out)
if err != nil {
return errors.Wrapf(err, "prune current obj %q with kind %s", info.Name, kind)
}
Expand Down Expand Up @@ -537,14 +537,28 @@ func existingResourceConflict(resources kube.ResourceList) (kube.ResourceList, e
return requireUpdate, err
}

func deleteStatusAndManagedFields(obj []byte) (map[string]interface{}, error) {
func deleteStatusAndTidyMetadata(obj []byte) (map[string]interface{}, error) {
var objectMap map[string]interface{}
err := jsoniterator.Unmarshal(obj, &objectMap)
if err != nil {
return nil, errors.Wrap(err, "could not unmarshal byte sequence")
}

delete(objectMap, "status")
delete(objectMap["metadata"].(map[string]interface{}), "managedFields")

metadata := objectMap["metadata"].(map[string]interface{})

delete(metadata, "managedFields")

if a := metadata["annotations"]; a != nil {
annotations := a.(map[string]interface{})
delete(annotations, "meta.helm.sh/release-name")
delete(annotations, "meta.helm.sh/release-namespace")

if len(annotations) == 0 {
delete(metadata, "annotations")
}
}

return objectMap, nil
}