Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --reorder flag. #1171

Merged
merged 1 commit into from
Jun 11, 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/spf13/cobra v0.0.2
github.com/spf13/pflag v1.0.1
github.com/stretchr/testify v1.3.0 // indirect
golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d // indirect
golang.org/x/tools v0.0.0-20190608022120-eacb66d2a7c3 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.2.1
k8s.io/api v0.0.0-20180510062335-53d615ae3f44
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d h1:bt+R27hbE7uVf7PY9S6wpNg9Xo2WRe/XQT0uGq9RQQw=
golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190608022120-eacb66d2a7c3 h1:sU3tSV6wDhWsvf9NjL0FzRjgAmYnQL5NEhdmcN16UEg=
golang.org/x/tools v0.0.0-20190608022120-eacb66d2a7c3/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
Expand Down
53 changes: 31 additions & 22 deletions pkg/commands/build/build.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// Copyright 2019 The Kubernetes Authors.
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package build
Expand Down Expand Up @@ -28,6 +28,7 @@ type Options struct {
kustomizationPath string
outputPath string
loadRestrictor loader.LoadRestrictorFunc
outOrder reorderOutput
}

// NewOptions creates a Options object
Expand All @@ -40,18 +41,20 @@ func NewOptions(p, o string) *Options {
}

var examples = `
Use the file somedir/kustomization.yaml to generate a set of api resources:
build somedir
To generate the resources specified in 'someDir/kustomization.yaml', run
Use a url pointing to a remote directory/kustomization.yaml to generate a set of api resources:
build url
The url should follow hashicorp/go-getter URL format described in
https://github.com/hashicorp/go-getter#url-format
kustomize build someDir
The default argument to 'build' is '.' (the current working directory).
The argument can be a URL resolving to a directory
with a kustomization.yaml file, e.g.
url examples:
sigs.k8s.io/kustomize//examples/multibases?ref=v1.0.6
github.com/Liujingfang1/mysql
github.com/Liujingfang1/kustomize//examples/helloWorld?ref=repoUrl2
kustomize build \
github.com/kubernetes-sigs/kustomize//examples/multibases/dev/?ref=v1.0.6
The URL should be formulated as described at
https://github.com/hashicorp/go-getter#url-format
`

// NewCmdBuild creates a new build command.
Expand All @@ -65,8 +68,8 @@ func NewCmdBuild(
pl := plugins.NewLoader(pluginConfig, rf)

cmd := &cobra.Command{
Use: "build [path]",
Short: "Print current configuration per contents of " + pgmconfig.KustomizationFileNames[0],
Use: "build {path}",
Short: "Print configuration per contents of " + pgmconfig.KustomizationFileNames[0],
Example: examples,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -82,10 +85,10 @@ func NewCmdBuild(
&o.outputPath,
"output", "o", "",
"If specified, write the build output to this path.")
loader.AddLoadRestrictionsFlag(cmd.Flags())
plugins.AddEnablePluginsFlag(
loader.AddFlagLoadRestrictor(cmd.Flags())
plugins.AddFlagEnablePlugins(
cmd.Flags(), &pluginConfig.Enabled)

addFlagReorderOutput(cmd.Flags())
cmd.AddCommand(NewCmdBuildPrune(out, v, fSys, rf, ptf, pl))
return cmd
}
Expand All @@ -101,7 +104,11 @@ func (o *Options) Validate(args []string) (err error) {
} else {
o.kustomizationPath = args[0]
}
o.loadRestrictor, err = loader.ValidateLoadRestrictorFlag()
o.loadRestrictor, err = loader.ValidateFlagLoadRestrictor()
if err != nil {
return err
}
o.outOrder, err = validateFlagReorderOutput()
return
}

Expand Down Expand Up @@ -153,11 +160,13 @@ func (o *Options) emitResources(
if o.outputPath != "" && fSys.IsDir(o.outputPath) {
return writeIndividualFiles(fSys, o.outputPath, m)
}
// Done this way just to prove that overall sorting
// can be performed by a plugin. This particular
// plugin doesn't require configuration; just make
// it and call transform.
builtin.NewPreferredOrderTransformerPlugin().Transform(m)
if o.outOrder == legacy {
// Done this way just to show how overall sorting
// can be performed by a plugin. This particular
// plugin doesn't require configuration; just make
// it and call transform.
builtin.NewLegacyOrderTransformerPlugin().Transform(m)
}
res, err := m.AsYaml()
if err != nil {
return err
Expand Down
50 changes: 50 additions & 0 deletions pkg/commands/build/reorderoutput.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package build

import (
"fmt"

"github.com/spf13/pflag"
)

//go:generate stringer -type=reorderOutput
type reorderOutput int

const (
unspecified reorderOutput = iota
none
legacy
)

const (
flagReorderOutputName = "reorder"
)

var (
flagReorderOutputValue = legacy.String()
flagReorderOutputHelp = "Reorder the resources just before output. " +
"Use '" + legacy.String() + "' to apply a legacy reordering (Namespaces first, Webhooks last, etc). " +
"Use '" + none.String() + "' to suppress a final reordering."
)

func addFlagReorderOutput(set *pflag.FlagSet) {
set.StringVar(
&flagReorderOutputValue, flagReorderOutputName,
legacy.String(), flagReorderOutputHelp)
}

func validateFlagReorderOutput() (reorderOutput, error) {
switch flagReorderOutputValue {
case none.String():
return none, nil
case legacy.String():
return legacy, nil
default:
return unspecified, fmt.Errorf(
"illegal flag value --%s %s; legal values: %v",
flagReorderOutputName, flagReorderOutputValue,
[]string{legacy.String(), none.String()})
}
}
25 changes: 25 additions & 0 deletions pkg/commands/build/reorderoutput_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions pkg/kusttest/kusttestharness.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ func hint(a, b string) string {

func (th *KustTestHarness) AssertActualEqualsExpected(
m resmap.ResMap, expected string) {
th.assertActualEqualsExpected(m, expected, true)
}

func (th *KustTestHarness) AssertActualEqualsExpectedNoSort(
m resmap.ResMap, expected string) {
th.assertActualEqualsExpected(m, expected, false)
}

func (th *KustTestHarness) assertActualEqualsExpected(
m resmap.ResMap, expected string, doLegacySort bool) {
if m == nil {
th.t.Fatalf("Map should not be nil.")
}
Expand All @@ -194,8 +204,9 @@ func (th *KustTestHarness) AssertActualEqualsExpected(
if len(expected) > 0 && expected[0] == 10 {
expected = expected[1:]
}
// The tests currently expect a particular ordering.
builtin.NewPreferredOrderTransformerPlugin().Transform(m)
if doLegacySort {
builtin.NewLegacyOrderTransformerPlugin().Transform(m)
}
actual, err := m.AsYaml()
if err != nil {
th.t.Fatalf("Unexpected err: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/loader/loadrestrictions.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ var (
"This does, however, break the relocatability of the kustomization."
)

func AddLoadRestrictionsFlag(set *pflag.FlagSet) {
func AddFlagLoadRestrictor(set *pflag.FlagSet) {
set.StringVar(
&flagValue, flagName,
rootOnly.String(), flagHelp)
}

func ValidateLoadRestrictorFlag() (LoadRestrictorFunc, error) {
func ValidateFlagLoadRestrictor() (LoadRestrictorFunc, error) {
switch flagValue {
case rootOnly.String():
return RestrictionRootOnly, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugins/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NotEnabledErr(name string) error {
flagEnablePluginsHelp)
}

func AddEnablePluginsFlag(set *pflag.FlagSet, v *bool) {
func AddFlagEnablePlugins(set *pflag.FlagSet, v *bool) {
set.BoolVar(
v, flagEnablePluginsName,
false, flagEnablePluginsHelp)
Expand Down
67 changes: 66 additions & 1 deletion pkg/target/baseandoverlaysmall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,79 @@ limitations under the License.
package target_test

import (
"sigs.k8s.io/kustomize/pkg/plugins"
"strings"
"testing"

"sigs.k8s.io/kustomize/pkg/kusttest"
"sigs.k8s.io/kustomize/pkg/loader"
"sigs.k8s.io/kustomize/pkg/plugins"
)

// TODO(monopole): Make prefixsuffixtransformer changes
// needed to enable this test.
func disabledTestOrderPreserved(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/app/prod")
th.WriteK("/app/base", `
namePrefix: b-
resources:
- namespace.yaml
- role.yaml
- service.yaml
- deployment.yaml
`)
th.WriteF("/app/base/service.yaml", `
apiVersion: v1
kind: Service
metadata:
name: myService
`)
th.WriteF("/app/base/namespace.yaml", `
apiVersion: v1
kind: Namespace
metadata:
name: myNs
`)
th.WriteF("/app/base/role.yaml", `
apiVersion: v1
kind: Role
metadata:
name: myRole
`)
th.WriteF("/app/base/deployment.yaml", `
apiVersion: v1
kind: Deployment
metadata:
name: myDep
`)
th.WriteK("/app/prod", `
namePrefix: p-
resources:
- ../base
- service.yaml
- namespace.yaml
`)
th.WriteF("/app/prod/service.yaml", `
apiVersion: v1
kind: Service
metadata:
name: myService2
`)
th.WriteF("/app/prod/namespace.yaml", `
apiVersion: v1
kind: Namespace
metadata:
name: myNs2
`)

m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil {
t.Fatalf("Err: %v", err)
}
th.AssertActualEqualsExpectedNoSort(m, `
TBD
./tr `)
}

func TestBaseInResourceList(t *testing.T) {
th := kusttest_test.NewKustTestHarness(t, "/app/prod")
th.WriteK("/app/prod", `
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import (
"sigs.k8s.io/kustomize/plugin"
)

func TestPreferredOrderTransformer(t *testing.T) {
func TestLegacyOrderTransformer(t *testing.T) {
tc := plugin.NewEnvForTest(t).Set()
defer tc.Reset()

tc.BuildGoPlugin(
"builtin", "", "PreferredOrderTransformer")
"builtin", "", "LegacyOrderTransformer")

th := kusttest_test.NewKustTestPluginHarness(t, "/app")
rm := th.LoadAndRunTransformer(`
apiVersion: builtin
kind: PreferredOrderTransformer
kind: LegacyOrderTransformer
metadata:
name: notImportantHere
`, `
Expand Down