Skip to content

Commit

Permalink
add kustomize build
Browse files Browse the repository at this point in the history
  • Loading branch information
sanaasy committed Feb 22, 2024
1 parent cedf4f8 commit b32e146
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
46 changes: 38 additions & 8 deletions kustomize/commands/localize/localize.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ package localize

import (
"log"
"os/exec"
"path/filepath"
"strings"

"github.com/spf13/cobra"
lclzr "sigs.k8s.io/kustomize/api/krusty/localizer"
Expand All @@ -30,14 +33,14 @@ func NewCmdLocalize(fs filesys.FileSystem) *cobra.Command {
cmd := &cobra.Command{
Use: "localize [target [destination]]",
Short: "[Alpha] Creates localized copy of target kustomization root at destination",
Long: `[Alpha] Creates copy of target kustomization directory or
versioned URL at destination, where remote references in the original
Long: `[Alpha] Creates copy of target kustomization directory or
versioned URL at destination, where remote references in the original
are replaced by local references to the downloaded remote content.
If target is not specified, the current working directory will be used.
Destination is a path to a new directory in an existing directory. If
destination is not specified, a new directory will be created in the current
working directory.
If target is not specified, the current working directory will be used.
Destination is a path to a new directory in an existing directory. If
destination is not specified, a new directory will be created in the current
working directory.
For details, see: https://kubectl.docs.kubernetes.io/references/kustomize/cmd/
Expand All @@ -47,7 +50,7 @@ alphabetizes kustomization fields in the localized copy.
`,
Example: `
# Localize the current working directory, with default scope and destination
kustomize localize
kustomize localize
# Localize some local directory, with scope and default destination
kustomize localize /home/path/scope/target --scope /home/path/scope
Expand All @@ -63,6 +66,33 @@ kustomize localize https://github.com/kubernetes-sigs/kustomize//api/krusty/test
if err != nil {
return errors.Wrap(err)
}

if !f.noVerify {
currBuildOutput, err := exec.Command("kustomize", "build", args.target).Output()
if err != nil {
return errors.Wrap(err)
}

buildDst := dst
if f.scope != args.target || f.scope == "" {
buildDst = filepath.Join(dst, filepath.Base(args.target))
}

localizedBuildOutput, err := exec.Command("kustomize", "build", buildDst).Output()
if err != nil {
return errors.Wrap(err)
}

diff := strings.Compare(string(currBuildOutput), string(localizedBuildOutput))
if diff == 1 {
log.Print("The source Kustomization file is different from the generated localized copy.")
} else if diff == -1 {
log.Print("The generated localized copy is different from the source Kustomization file.")
} else {
log.Print("The source Kustomization file and the generated localized copy are the same.")
}
}

log.Printf("SUCCESS: localized %q to directory %s\n", args.target, dst)
return nil
},
Expand All @@ -78,7 +108,7 @@ If not specified for local target, scope defaults to target.
cmd.Flags().BoolVar(&f.noVerify,
"noVerify",
false,
`Does not verify that the outputs of kustomize build for target and newDir are the same after localization.
`Does not verify that the outputs of kustomize build for target and newDir are the same after localization.
If not specified, noVerify defaults to false and will run kustomize build.
`)
return cmd
Expand Down
28 changes: 28 additions & 0 deletions kustomize/commands/localize/localize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@ func TestScopeFlag(t *testing.T) {
loctest.CheckFs(t, testDir.String(), expected, actual)
}

func TestNoVerifyFlag(t *testing.T) {
kustomization := map[string]string{
"kustomization.yaml": `namePrefix: test-
`,
}
expected, actual, target := loctest.PrepareFs(t, nil, kustomization)

buffy := new(bytes.Buffer)
log.SetOutput(buffy)
defer func() {
log.SetOutput(os.Stderr)
}()
cmd := localize.NewCmdLocalize(actual)
require.NoError(t, cmd.Flags().Set("noVerify", "true"))
err := cmd.RunE(cmd, []string{
target.String(),
target.Join("dst"),
})
require.NoError(t, err)

loctest.SetupDir(t, expected, target.Join("dst"), kustomization)
loctest.CheckFs(t, target.String(), expected, actual)

successMsg := fmt.Sprintf(`SUCCESS: localized "%s" to directory %s
`, target.String(), target.Join("dst"))
require.Contains(t, buffy.String(), successMsg)
}

func TestOptionalArgs(t *testing.T) {
for name, args := range map[string][]string{
"no_target": {},
Expand Down

0 comments on commit b32e146

Please sign in to comment.