Skip to content

Commit

Permalink
support additional build args to external kustomize path
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Walton <mwalton.us@gmail.com>
  • Loading branch information
dosferatu committed May 7, 2023
1 parent b665cc0 commit 3b1806e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
10 changes: 5 additions & 5 deletions pkg/gitkustomizediff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package gitkustomizediff

import (
"fmt"
"path/filepath"
"regexp"

Expand Down Expand Up @@ -130,12 +129,13 @@ type BuildOpts struct {

func Build(dirPath string, opts BuildOpts) (string, error) {
if opts.KustomizePath != "" {
buildCmd := "build"
buildArgs := []string {"build"}
if (opts.KustomizeLoadRestrictor != "") {
loadRestrictorFlag := fmt.Sprintf("--load-restrictor=%q", opts.KustomizeLoadRestrictor)
buildCmd = fmt.Sprintf("%q %q", buildCmd, loadRestrictorFlag)
buildArgs = append(buildArgs, "--load-restrictor")
buildArgs = append(buildArgs, opts.KustomizeLoadRestrictor)
}
stdout, _, err := (&utils.WorkDir{}).RunCommand(opts.KustomizePath, buildCmd, dirPath)
buildArgs = append(buildArgs, dirPath)
stdout, _, err := (&utils.WorkDir{}).RunCommand(opts.KustomizePath, buildArgs...)
if err != nil {
return "", err
}
Expand Down
16 changes: 10 additions & 6 deletions pkg/gitkustomizediff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,33 @@ func TestMakeBuildOptions(t *testing.T) {
var err error
var kustomizeLoadRestrictor string
var options *krusty.Options
var defaultOptions *krusty.Options
defaultOptions = krusty.MakeDefaultOptions()
defaultOptions := krusty.MakeDefaultOptions()

kustomizeLoadRestrictor = ""
options, err = MakeBuildOptions(kustomizeLoadRestrictor)
assert.Equal(t, err, nil)
assert.Equal(t, options, defaultOptions)

kustomizeLoadRestrictor = "LoadRestrictionsUnknown"
options, err = MakeBuildOptions(kustomizeLoadRestrictor)
assert.Equal(t, err, nil)
assert.Equal(t, options.LoadRestrictions, types.LoadRestrictionsUnknown)

kustomizeLoadRestrictor = "LoadRestrictionsRootOnly"
options, err = MakeBuildOptions(kustomizeLoadRestrictor)
assert.Equal(t, err, nil)
assert.Equal(t, options.LoadRestrictions, types.LoadRestrictionsRootOnly)

kustomizeLoadRestrictor = "LoadRestrictionsNone"
options, err = MakeBuildOptions(kustomizeLoadRestrictor)
assert.Equal(t, err, nil)
assert.Equal(t, options.LoadRestrictions, types.LoadRestrictionsNone)

invalidType := "invalid-load-restrictions-type--"
kustomizeLoadRestrictor = invalidType
options, err = MakeBuildOptions(kustomizeLoadRestrictor)
assert.Equal(t, options, (*krusty.Options)(nil))
assert.NotEqual(t, err, nil)
assert.Error(t, err, "unknown LoadRestrictions type given by kustomizeLoadRestrictor: %q", invalidType)
}

Expand Down Expand Up @@ -95,11 +99,11 @@ spec:
`, "\n")

fixturesDirPath := filepath.Join(wd, "fixtures", "diff-load-restrictions-none", "base", "sub1/nested")
actualYaml, err := Build(fixturesDirPath, BuildOpts{})
_, err := Build(fixturesDirPath, BuildOpts{})
assert.NotEqual(t, err, nil)

buildOpts := BuildOpts{"", "LoadRestrictionsNone"}
actualYaml, err = Build(fixturesDirPath, buildOpts)
actualYaml, err := Build(fixturesDirPath, buildOpts)
if !assert.NoError(t, err) {
t.FailNow()
}
Expand Down Expand Up @@ -147,13 +151,13 @@ func TestDiffLoadRestrictionsNone(t *testing.T) {

baseDirPath := filepath.Join(wd, "fixtures", "diff-load-restrictions-none", "base")
targetDirPath := filepath.Join(wd, "fixtures", "diff-load-restrictions-none", "target")
diffMap, err := Diff(baseDirPath, targetDirPath, DiffOpts{})
_, err := Diff(baseDirPath, targetDirPath, DiffOpts{})
if !assert.NoError(t, err) {
t.FailNow()
}

diffOpts := DiffOpts{nil, nil, "", "LoadRestrictionsNone"}
diffMap, err = Diff(baseDirPath, targetDirPath, diffOpts)
diffMap, err := Diff(baseDirPath, targetDirPath, diffOpts)
if !assert.NoError(t, err) {
t.FailNow()
}
Expand Down

0 comments on commit 3b1806e

Please sign in to comment.