This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
don't render empty kubernetes yaml lists #825
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,21 @@ | ||
package kustomize | ||
|
||
import ( | ||
"os" | ||
"sort" | ||
|
||
"github.com/go-kit/kit/log" | ||
"github.com/go-kit/kit/log/level" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/replicatedhq/ship/pkg/api" | ||
"github.com/replicatedhq/ship/pkg/util" | ||
yaml "gopkg.in/yaml.v2" | ||
) | ||
|
||
type postKustomizeFile struct { | ||
order int | ||
minimal util.MinimalK8sYaml | ||
full interface{} | ||
} | ||
|
||
type postKustomizeFileCollection []postKustomizeFile | ||
|
||
func (c postKustomizeFileCollection) Len() int { | ||
return len(c) | ||
} | ||
|
||
func (c postKustomizeFileCollection) Swap(i, j int) { | ||
c[i], c[j] = c[j], c[i] | ||
} | ||
|
||
func (c postKustomizeFileCollection) Less(i, j int) bool { | ||
return c[i].order < c[j].order | ||
} | ||
|
||
func (l *Kustomizer) rebuildListYaml(lists []util.List, kustomizedYamlFiles []postKustomizeFile) ([]postKustomizeFile, error) { | ||
func (l *Kustomizer) rebuildListYaml(lists []util.List, kustomizedYamlFiles []util.PostKustomizeFile) ([]util.PostKustomizeFile, error) { | ||
debug := level.Debug(log.With(l.Logger, "struct", "daemonless.kustomizer", "method", "rebuildListYaml")) | ||
yamlMap := make(map[util.MinimalK8sYaml]postKustomizeFile) | ||
|
||
for _, postKustomizeFile := range kustomizedYamlFiles { | ||
yamlMap[postKustomizeFile.minimal] = postKustomizeFile | ||
} | ||
|
||
fullReconstructedRendered := make([]postKustomizeFile, 0) | ||
for _, list := range lists { | ||
var allListItems []interface{} | ||
for _, item := range list.Items { | ||
if pkFile, exists := yamlMap[item]; exists { | ||
delete(yamlMap, item) | ||
allListItems = append(allListItems, pkFile.full) | ||
} | ||
} | ||
|
||
debug.Log("event", "reconstruct list") | ||
reconstructedList := ListK8sYaml{ | ||
APIVersion: list.APIVersion, | ||
Kind: "List", | ||
Items: allListItems, | ||
} | ||
|
||
postKustomizeList := postKustomizeFile{ | ||
minimal: util.MinimalK8sYaml{ | ||
Kind: "List", | ||
}, | ||
full: reconstructedList, | ||
} | ||
|
||
fullReconstructedRendered = append(fullReconstructedRendered, postKustomizeList) | ||
} | ||
|
||
for nonListYamlMinimal, pkFile := range yamlMap { | ||
fullReconstructedRendered = append(fullReconstructedRendered, postKustomizeFile{ | ||
order: pkFile.order, | ||
minimal: nonListYamlMinimal, | ||
full: pkFile.full, | ||
}) | ||
} | ||
|
||
return fullReconstructedRendered, nil | ||
return util.RebuildListYaml(debug, lists, kustomizedYamlFiles) | ||
} | ||
|
||
func (l *Kustomizer) writePostKustomizeFiles(step api.Kustomize, postKustomizeFiles []postKustomizeFile) error { | ||
func (l *Kustomizer) writePostKustomizeFiles(step api.Kustomize, postKustomizeFiles []util.PostKustomizeFile) error { | ||
debug := level.Debug(log.With(l.Logger, "struct", "daemonless.kustomizer", "method", "writePostKustomizeFiles")) | ||
|
||
sort.Stable(postKustomizeFileCollection(postKustomizeFiles)) | ||
|
||
var joinedFinal string | ||
for _, file := range postKustomizeFiles { | ||
debug.Log("event", "marshal post kustomize file") | ||
fileB, err := yaml.Marshal(file.full) | ||
if err != nil { | ||
return errors.Wrapf(err, "marshal file %s", file.minimal.Metadata.Name) | ||
} | ||
|
||
if joinedFinal != "" { | ||
joinedFinal += "---\n" + string(fileB) | ||
} else { | ||
joinedFinal += string(fileB) | ||
} | ||
} | ||
|
||
debug.Log("event", "write post kustomize files", "dest", step.Dest) | ||
if err := l.FS.WriteFile(step.Dest, []byte(joinedFinal), os.FileMode(0644)); err != nil { | ||
return errors.Wrapf(err, "write kustomized and post processed yaml at %s", step.Dest) | ||
} | ||
|
||
return nil | ||
return util.WritePostKustomizeFiles(debug, l.FS, step.Dest, postKustomizeFiles) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 2 locations. Consider refactoring.