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

fix normalieze manifests issue #585

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
31 changes: 20 additions & 11 deletions manifest/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,11 @@ func parseContent(content string, defaultNamespace string, normalizeManifests bo
}

if normalizeManifests {
// Unmarshal and marshal again content to normalize yaml structure
// This avoids style differences to show up as diffs but it can
// make the output different from the original template (since it is in normalized form)
var object map[interface{}]interface{}
if err := yaml.Unmarshal([]byte(content), &object); err != nil {
log.Fatalf("YAML unmarshal error: %s\nCan't unmarshal %s", err, content)
}
normalizedContent, err := yaml.Marshal(object)
if err != nil {
log.Fatalf("YAML marshal error: %s\nCan't marshal %v", err, object)
var normalizeErr error
content, normalizeErr = ContentNormalizeManifests(content)
if normalizeErr != nil {
log.Fatalf("Error normalizing manifests: %v", normalizeErr)
}
content = string(normalizedContent)
}

if isHook(parsedMetadata, excludedHooks...) {
Expand All @@ -176,6 +169,22 @@ func parseContent(content string, defaultNamespace string, normalizeManifests bo
}, nil
}

func ContentNormalizeManifests(content string) (string, error) {
// Unmarshal and marshal again content to normalize yaml structure
// This avoids style differences to show up as diffs but it can
// make the output different from the original template (since it is in normalized form)
log.Printf("Normalizing content: \n%s", content)
var object map[interface{}]interface{}
if err := yaml.Unmarshal([]byte(content), &object); err != nil {
return "", err
}
normalizedContent, err := yaml.Marshal(object)
if err != nil {
return "", err
}
return string(normalizedContent), nil
}

func isHook(metadata metadata, hooks ...string) bool {
for _, hook := range hooks {
if metadata.Metadata.Annotations[hookAnnotation] == hook {
Expand Down
44 changes: 44 additions & 0 deletions manifest/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,47 @@ func TestBaseNameAnnotation(t *testing.T) {
foundObjects(Parse(string(spec), "default", false)),
)
}
func TestContentNormalizeManifests(t *testing.T) {
tests := []struct {
name string
content string
expectedOutput string
expectedError error
}{
{
name: "Valid content",
content: `apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx`,
expectedOutput: `apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- image: nginx
name: my-container
`,
expectedError: nil,
},
{
name: "Empty content",
content: "",
expectedOutput: "{}\n",
expectedError: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
output, err := ContentNormalizeManifests(tt.content)
require.Equal(t, tt.expectedError, err)
require.Equal(t, tt.expectedOutput, output)
})
}
}
Loading