Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

don't render empty kubernetes yaml lists #825

Merged
merged 3 commits into from
Feb 8, 2019
Merged
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
12 changes: 6 additions & 6 deletions pkg/lifecycle/kustomize/daemonless.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (l *Kustomizer) Execute(ctx context.Context, release *api.Release, step api
return nil
}

func (l *Kustomizer) kustomizeBuild(kustomizePath string) ([]postKustomizeFile, error) {
func (l *Kustomizer) kustomizeBuild(kustomizePath string) ([]util.PostKustomizeFile, error) {
debug := level.Debug(log.With(l.Logger, "struct", "daemonless.kustomizer", "method", "kustomizeBuild"))

builtYAML, err := l.Patcher.RunKustomize(kustomizePath)
Expand All @@ -127,7 +127,7 @@ func (l *Kustomizer) kustomizeBuild(kustomizePath string) ([]postKustomizeFile,
}

files := strings.Split(string(builtYAML), "\n---\n")
postKustomizeFiles := make([]postKustomizeFile, 0)
postKustomizeFiles := make([]util.PostKustomizeFile, 0)
for idx, file := range files {
var fullYaml interface{}

Expand All @@ -142,10 +142,10 @@ func (l *Kustomizer) kustomizeBuild(kustomizePath string) ([]postKustomizeFile,
return postKustomizeFiles, errors.Wrap(err, "unmarshal part of rendered to minimal")
}

postKustomizeFiles = append(postKustomizeFiles, postKustomizeFile{
order: idx,
minimal: minimal,
full: fullYaml,
postKustomizeFiles = append(postKustomizeFiles, util.PostKustomizeFile{
Order: idx,
Minimal: minimal,
Full: fullYaml,
})
}

Expand Down
96 changes: 5 additions & 91 deletions pkg/lifecycle/kustomize/post_kustomize.go
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)
}
16 changes: 5 additions & 11 deletions pkg/lifecycle/kustomize/pre_kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ import (
ktypes "sigs.k8s.io/kustomize/pkg/types"
)

type ListK8sYaml struct {
APIVersion string `json:"apiVersion" yaml:"apiVersion"`
Kind string `json:"kind" yaml:"kind" hcl:"kind"`
Items []interface{} `json:"items" yaml:"items"`
}

func (l *Kustomizer) PreExecute(ctx context.Context, step api.Step) error {
// Check if the 'base' already includes a kustomization.yaml
// if it does, and that refers to another base, we should apply those patches to the upstream base, and then use that in the future
Expand Down Expand Up @@ -126,7 +120,7 @@ func (l *Kustomizer) maybeSplitListYaml(ctx context.Context, path string) error
return errors.Wrapf(err, "read %s", filePath)
}

k8sYaml := ListK8sYaml{}
k8sYaml := util.ListK8sYaml{}
if err := yaml.Unmarshal(fileB, &k8sYaml); err != nil {
return errors.Wrapf(err, "unmarshal %s", filePath)
}
Expand Down Expand Up @@ -218,10 +212,10 @@ func (l *Kustomizer) runProvidedOverlays(ctx context.Context, originalBase, newB
return nil
}

func (l *Kustomizer) replaceOriginal(base string, built []postKustomizeFile) error {
builtMap := make(map[util.MinimalK8sYaml]postKustomizeFile)
func (l *Kustomizer) replaceOriginal(base string, built []util.PostKustomizeFile) error {
builtMap := make(map[util.MinimalK8sYaml]util.PostKustomizeFile)
for _, builtFile := range built {
builtMap[builtFile.minimal] = builtFile
builtMap[builtFile.Minimal] = builtFile
}

if err := l.FS.Walk(base, func(targetPath string, info os.FileInfo, err error) error {
Expand Down Expand Up @@ -264,7 +258,7 @@ func (l *Kustomizer) replaceOriginal(base string, built []postKustomizeFile) err
return errors.Wrap(err, "remove original file")
}

initKustomizedB, err := yaml.Marshal(initKustomized.full)
initKustomizedB, err := yaml.Marshal(initKustomized.Full)
if err != nil {
return errors.Wrap(err, "marshal init kustomized")
}
Expand Down
30 changes: 15 additions & 15 deletions pkg/lifecycle/kustomize/pre_kustomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ func TestKustomizer_replaceOriginal(t *testing.T) {
tests := []struct {
name string
step api.Kustomize
built []postKustomizeFile
built []util.PostKustomizeFile
original []testFile
expect []testFile
wantErr bool
Expand All @@ -455,15 +455,15 @@ func TestKustomizer_replaceOriginal(t *testing.T) {
step: api.Kustomize{
Base: "",
},
built: []postKustomizeFile{
built: []util.PostKustomizeFile{
{
minimal: util.MinimalK8sYaml{
Minimal: util.MinimalK8sYaml{
Kind: "Fruit",
Metadata: util.MinimalK8sMetadata{
Name: "strawberry",
},
},
full: map[string]interface{}{
Full: map[string]interface{}{
"kind": "Fruit",
"metadata": map[string]interface{}{
"name": "strawberry",
Expand Down Expand Up @@ -502,15 +502,15 @@ spec:
step: api.Kustomize{
Base: "",
},
built: []postKustomizeFile{
built: []util.PostKustomizeFile{
{
minimal: util.MinimalK8sYaml{
Minimal: util.MinimalK8sYaml{
Kind: "CustomResourceDefinition",
Metadata: util.MinimalK8sMetadata{
Name: "strawberry",
},
},
full: map[string]interface{}{
Full: map[string]interface{}{
"kind": "CustomResourceDefinition",
"metadata": map[string]interface{}{
"name": "strawberry",
Expand Down Expand Up @@ -549,15 +549,15 @@ spec:
step: api.Kustomize{
Base: "",
},
built: []postKustomizeFile{
built: []util.PostKustomizeFile{
{
minimal: util.MinimalK8sYaml{
Minimal: util.MinimalK8sYaml{
Kind: "Fruit",
Metadata: util.MinimalK8sMetadata{
Name: "banana",
},
},
full: map[string]interface{}{
Full: map[string]interface{}{
"kind": "Fruit",
"metadata": map[string]interface{}{
"name": "banana",
Expand Down Expand Up @@ -596,15 +596,15 @@ spec:
step: api.Kustomize{
Base: "",
},
built: []postKustomizeFile{
built: []util.PostKustomizeFile{
{
minimal: util.MinimalK8sYaml{
Minimal: util.MinimalK8sYaml{
Kind: "Fruit",
Metadata: util.MinimalK8sMetadata{
Name: "dragonfruit",
},
},
full: map[string]interface{}{
Full: map[string]interface{}{
"kind": "Fruit",
"metadata": map[string]interface{}{
"name": "dragonfruit",
Expand All @@ -615,13 +615,13 @@ spec:
},
},
{
minimal: util.MinimalK8sYaml{
Minimal: util.MinimalK8sYaml{
Kind: "Fruit",
Metadata: util.MinimalK8sMetadata{
Name: "pomegranate",
},
},
full: map[string]interface{}{
Full: map[string]interface{}{
"kind": "Fruit",
"metadata": map[string]interface{}{
"name": "pomegranate",
Expand Down
14 changes: 7 additions & 7 deletions pkg/lifecycle/unfork/daemonless.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,16 @@ func (l *Unforker) Execute(ctx context.Context, release *api.Release, step api.U
return nil
}

func (l *Unforker) unforkBuild(kustomizePath string) ([]postKustomizeFile, error) {
debug := level.Debug(log.With(l.Logger, "struct", "daemonless.unforker", "method", "kustomizeBuild"))
func (l *Unforker) unforkBuild(kustomizePath string) ([]util.PostKustomizeFile, error) {
Copy link

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.

debug := level.Debug(log.With(l.Logger, "struct", "daemonless.unforker", "method", "unforkBuild"))

builtYAML, err := l.Patcher.RunKustomize(kustomizePath)
if err != nil {
return nil, errors.Wrap(err, "run kustomize")
}

files := strings.Split(string(builtYAML), "\n---\n")
postKustomizeFiles := make([]postKustomizeFile, 0)
postKustomizeFiles := make([]util.PostKustomizeFile, 0)
for idx, file := range files {
var fullYaml interface{}

Expand All @@ -145,10 +145,10 @@ func (l *Unforker) unforkBuild(kustomizePath string) ([]postKustomizeFile, error
return postKustomizeFiles, errors.Wrap(err, "unmarshal part of rendered to minimal")
}

postKustomizeFiles = append(postKustomizeFiles, postKustomizeFile{
order: idx,
minimal: minimal,
full: fullYaml,
postKustomizeFiles = append(postKustomizeFiles, util.PostKustomizeFile{
Order: idx,
Minimal: minimal,
Full: fullYaml,
})
}

Expand Down
Loading