Skip to content

Commit

Permalink
Merge pull request #4733 from koba1t/feat/add_edit-fix_for_patchesStr…
Browse files Browse the repository at this point in the history
…ategicMerge_to_patches

add `edit fix` for patchesStrategicMerge to patches
  • Loading branch information
k8s-ci-robot authored Oct 21, 2022
2 parents ccb68aa + 0d68e0c commit 7ee6dd5
Show file tree
Hide file tree
Showing 3 changed files with 320 additions and 62 deletions.
17 changes: 16 additions & 1 deletion api/types/kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"fmt"

"sigs.k8s.io/kustomize/kyaml/filesys"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -217,11 +218,25 @@ func (k *Kustomization) FixKustomizationPostUnmarshalling() {
// FixKustomizationPreMarshalling fixes things
// that should occur after the kustomization file
// has been processed.
func (k *Kustomization) FixKustomizationPreMarshalling() error {
func (k *Kustomization) FixKustomizationPreMarshalling(fSys filesys.FileSystem) error {
// PatchesJson6902 should be under the Patches field.
k.Patches = append(k.Patches, k.PatchesJson6902...)
k.PatchesJson6902 = nil

if k.PatchesStrategicMerge != nil {
for _, patchStrategicMerge := range k.PatchesStrategicMerge {
// check this patch is file path select.
if _, err := fSys.ReadFile(string(patchStrategicMerge)); err == nil {
// path patch
k.Patches = append(k.Patches, Patch{Path: string(patchStrategicMerge)})
} else {
// inline string patch
k.Patches = append(k.Patches, Patch{Patch: string(patchStrategicMerge)})
}
}
k.PatchesStrategicMerge = nil
}

// this fix is not in FixKustomizationPostUnmarshalling because
// it will break some commands like `create` and `add`. those
// commands depend on 'commonLabels' field
Expand Down
7 changes: 4 additions & 3 deletions kustomize/commands/edit/fix/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ func RunFix(fSys filesys.FileSystem, w io.Writer) error {
return err
}

err = m.FixKustomizationPreMarshalling()
if err != nil {
if err := m.FixKustomizationPreMarshalling(fSys); err != nil {
return err
}

Expand All @@ -68,12 +67,14 @@ func RunFix(fSys filesys.FileSystem, w io.Writer) error {
fmt.Fprintln(w, `
Fixed fields:
patchesJson6902 -> patches
patchesStrategicMerge -> patches
commonLabels -> labels
vars -> replacements`)
} else {
fmt.Fprintln(w, `
Fixed fields:
patchesJson6902 -> patches
patchesStrategicMerge -> patches
commonLabels -> labels
To convert vars -> replacements, run the command `+"`kustomize edit fix --vars`"+`
Expand All @@ -89,7 +90,7 @@ We recommend doing this in a clean git repository where the change is easy to un
fixedBuildCmd := build.NewCmdBuild(fSys, build.MakeHelp(konfig.ProgramName, "build"), &fixedOutput)
err = fixedBuildCmd.RunE(fixedBuildCmd, nil)
if err != nil {
fmt.Fprintf(w, "Warning: 'Fixed' kustomization now produces the error when running `kustomize build`: %s", err.Error())
fmt.Fprintf(w, "Warning: 'Fixed' kustomization now produces the error when running `kustomize build`: %s\n", err.Error())
} else if fixedOutput.String() != oldOutput.String() {
fmt.Fprintf(w, "Warning: 'Fixed' kustomization now produces different output when running `kustomize build`:\n...%s...\n", fixedOutput.String())
}
Expand Down
Loading

0 comments on commit 7ee6dd5

Please sign in to comment.