Skip to content

Commit c08b1c4

Browse files
Normalize YAML manifests before running diff
The current behavior of helm-diff is to show diffs even when the YAML files are semantically identical but they contain style differences, such as indentation or key ordering. As an example, the following two YAMLs would be showing diffs: ``` apiVersion: v1 kind: Service metadata: name: app-name labels: app.kubernetes.io/name: app-name app.kubernetes.io/managed-by: Helm spec: ports: - name: http port: 80 targetPort: http selector: app.kubernetes.io/name: app-name type: ClusterIP ``` ``` apiVersion: v1 kind: Service metadata: labels: app.kubernetes.io/managed-by: Helm app.kubernetes.io/name: app-name name: app-name spec: ports: - name: http port: 80 targetPort: http selector: app.kubernetes.io/name: app-name type: ClusterIP ``` even though they are semantically the same. This commit exploits the reordering and normalization that is performed by the yaml package when marshaling by unmarshaling and re-marshaling all manifests before running the diff, thus eliminating the issue.
1 parent 818e596 commit c08b1c4

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

manifest/parse.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func parseContent(content string, defaultNamespace string, excludedHooks ...stri
135135

136136
if parsedMetadata.Kind == "List" {
137137
type ListV1 struct {
138-
Items []yaml.MapSlice `yaml:"items"`
138+
Items []map[interface{}]interface{} `yaml:"items"`
139139
}
140140

141141
var list ListV1
@@ -163,6 +163,16 @@ func parseContent(content string, defaultNamespace string, excludedHooks ...stri
163163
return result, nil
164164
}
165165

166+
var object map[interface{}]interface{}
167+
if err := yaml.Unmarshal([]byte(content), &object); err != nil {
168+
log.Fatalf("YAML unmarshal error: %s\nCan't unmarshal %s", err, content)
169+
}
170+
normalizedContent, err := yaml.Marshal(object)
171+
if err != nil {
172+
log.Fatalf("YAML marshal error: %s\nCan't marshal %v", err, object)
173+
}
174+
content = string(normalizedContent)
175+
166176
if isHook(parsedMetadata, excludedHooks...) {
167177
return nil, nil
168178
}

0 commit comments

Comments
 (0)