Merge pull request #1722 from monopole/moveLoadRestrictions

push flag code from api module to kustomize internal
This commit is contained in:
Jeff Regan
2019-11-02 11:07:36 -07:00
committed by GitHub
18 changed files with 243 additions and 139 deletions

View File

@@ -128,6 +128,7 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20181011042414-1f849cf54d09/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190614205625-5aca471b1d59 h1:QjA/9ArTfVTLfEhClDCG7SGrZkZixxWpwNCDiwJfh88=
golang.org/x/tools v0.0.0-20190614205625-5aca471b1d59/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View File

@@ -14,6 +14,7 @@ import (
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/resource"
"sigs.k8s.io/kustomize/api/target"
"sigs.k8s.io/kustomize/api/types"
)
// Kustomizer performs kustomizations. It's meant to behave
@@ -55,7 +56,7 @@ func (b *Kustomizer) Run(path string) (resmap.ResMap, error) {
kunstruct.NewKunstructuredFactoryImpl()),
pf)
lr := fLdr.RestrictionNone
if b.options.LoadRestrictions == rootOnly {
if b.options.LoadRestrictions == types.LoadRestrictionsRootOnly {
lr = fLdr.RestrictionRootOnly
}
ldr, err := fLdr.NewLoader(lr, path, b.fSys)

View File

@@ -1,68 +0,0 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package krusty
import (
"fmt"
"github.com/spf13/pflag"
)
//go:generate stringer -type=loadRestrictions
type loadRestrictions int
const (
unknown loadRestrictions = iota
// With this restriction, the files referenced by a
// kustomization file must be in or under the directory
// holding the kustomization file itself.
rootOnly
// The kustomization file may specify absolute or
// relative paths to patch or resources files outside
// its own tree.
none
)
const (
flagName = "load_restrictor"
)
var (
flagValue = rootOnly.String()
flagHelp = "if set to '" + none.String() +
"', local kustomizations may load files from outside their root. " +
"This does, however, break the relocatability of the kustomization."
)
func AddFlagLoadRestrictor(set *pflag.FlagSet) {
set.StringVar(
&flagValue, flagName,
rootOnly.String(), flagHelp)
}
func ValidateFlagLoadRestrictor() error {
switch flagValue {
case rootOnly.String():
return nil
case none.String():
return nil
default:
return fmt.Errorf(
"illegal flag value --%s %s; legal values: %v",
flagName, flagValue,
[]string{rootOnly.String(), none.String()})
}
}
func GetFlagLoadRestrictorValue() loadRestrictions {
switch flagValue {
case rootOnly.String():
return rootOnly
case none.String():
return none
default:
return unknown
}
}

View File

@@ -1,25 +0,0 @@
// Code generated by "stringer -type=loadRestrictions"; DO NOT EDIT.
package krusty
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[unknown-0]
_ = x[rootOnly-1]
_ = x[none-2]
}
const _loadRestrictions_name = "unknownrootOnlynone"
var _loadRestrictions_index = [...]uint8{0, 7, 15, 19}
func (i loadRestrictions) String() string {
if i < 0 || i >= loadRestrictions(len(_loadRestrictions_index)-1) {
return "loadRestrictions(" + strconv.FormatInt(int64(i), 10) + ")"
}
return _loadRestrictions_name[_loadRestrictions_index[i]:_loadRestrictions_index[i+1]]
}

View File

@@ -20,7 +20,7 @@ type Options struct {
// Restrictions on what can be loaded from the file system.
// See type definition.
LoadRestrictions loadRestrictions
LoadRestrictions types.LoadRestrictions
// Create an inventory object for pruning.
DoPrune bool
@@ -33,7 +33,7 @@ type Options struct {
func MakeDefaultOptions() *Options {
return &Options{
DoLegacyResourceSort: true,
LoadRestrictions: rootOnly,
LoadRestrictions: types.LoadRestrictionsRootOnly,
DoPrune: false,
PluginConfig: pgmconfig.DefaultPluginConfig(),
}

View File

@@ -4,12 +4,10 @@
package pgmconfig
import (
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/spf13/pflag"
"sigs.k8s.io/kustomize/api/types"
)
@@ -31,45 +29,21 @@ const (
// Name of directory housing all plugins.
PluginRoot = "plugin"
flagEnablePluginsName = "enable_alpha_plugins"
flagEnablePluginsHelp = `enable plugins, an alpha feature.
See https://github.com/kubernetes-sigs/kustomize/blob/master/docs/plugins/README.md
`
flagErrorFmt = `
unable to load external plugin %s because plugins disabled
specify the flag
--%s
to %s`
)
func ActivePluginConfig() *types.PluginConfig {
pc := DefaultPluginConfig()
pc.Enabled = true
pc.PluginRestrictions = types.PluginRestrictionsNone
return pc
}
func DefaultPluginConfig() *types.PluginConfig {
return &types.PluginConfig{
Enabled: false,
DirectoryPath: filepath.Join(configRoot(), PluginRoot),
PluginRestrictions: types.PluginRestrictionsBuiltinsOnly,
DirectoryPath: filepath.Join(configRoot(), PluginRoot),
}
}
func NotEnabledErr(name string) error {
return fmt.Errorf(
flagErrorFmt,
name,
flagEnablePluginsName,
flagEnablePluginsHelp)
}
func AddFlagEnablePlugins(set *pflag.FlagSet, v *bool) {
set.BoolVar(
v, flagEnablePluginsName,
false, flagEnablePluginsHelp)
}
// Use https://github.com/kirsle/configdir instead?
func configRoot() string {
dir := os.Getenv(XdgConfigHomeEnv)

View File

@@ -114,10 +114,10 @@ func (l *Loader) loadAndConfigurePlugin(
// function (see "pluginator"). Being able to do this
// is what makes a plugin "builtin".
c, err = l.makeBuiltinPlugin(res.GetGvk())
} else if l.pc.Enabled {
} else if l.pc.PluginRestrictions == types.PluginRestrictionsNone {
c, err = l.loadPlugin(res.OrgId())
} else {
err = pgmconfig.NotEnabledErr(res.OrgId().Kind)
err = types.NewErrOnlyBuiltinPluginsAllowed(res.OrgId().Kind)
}
if err != nil {
return nil, err

View File

@@ -4,10 +4,10 @@
package target_test
import (
"strings"
"testing"
"sigs.k8s.io/kustomize/api/testutils/kusttest"
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
"sigs.k8s.io/kustomize/api/types"
)
func writeDeployment(th *kusttest_test.KustTestHarness, path string) {
@@ -112,7 +112,7 @@ transformers:
if err == nil {
t.Fatalf("expected error")
}
if !strings.Contains(err.Error(), "unable to load external plugin StringPrefixer") {
if !types.IsErrOnlyBuiltinPluginsAllowed(err) {
t.Fatalf("unexpected err: %v", err)
}
}

View File

@@ -0,0 +1,32 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package types
import (
"fmt"
"github.com/pkg/errors"
)
type errOnlyBuiltinPluginsAllowed struct {
name string
}
func (e *errOnlyBuiltinPluginsAllowed) Error() string {
return fmt.Sprintf(
"external plugins disabled; unable to load external plugin '%s'",
e.name)
}
func NewErrOnlyBuiltinPluginsAllowed(n string) *errOnlyBuiltinPluginsAllowed {
return &errOnlyBuiltinPluginsAllowed{name: n}
}
func IsErrOnlyBuiltinPluginsAllowed(err error) bool {
_, ok := err.(*errOnlyBuiltinPluginsAllowed)
if ok {
return true
}
_, ok = errors.Cause(err).(*errOnlyBuiltinPluginsAllowed)
return ok
}

View File

@@ -0,0 +1,24 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package types
// Restrictions on what things can be referred to
// in a kustomization file.
//
//go:generate stringer -type=LoadRestrictions
type LoadRestrictions int
const (
LoadRestrictionsUnknown LoadRestrictions = iota
// Files referenced by a kustomization file must be in
// or under the directory holding the kustomization
// file itself.
LoadRestrictionsRootOnly
// The kustomization file may specify absolute or
// relative paths to patch or resources files outside
// its own tree.
LoadRestrictionsNone
)

View File

@@ -0,0 +1,25 @@
// Code generated by "stringer -type=LoadRestrictions"; DO NOT EDIT.
package types
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[LoadRestrictionsUnknown-0]
_ = x[LoadRestrictionsRootOnly-1]
_ = x[LoadRestrictionsNone-2]
}
const _LoadRestrictions_name = "LoadRestrictionsUnknownLoadRestrictionsRootOnlyLoadRestrictionsNone"
var _LoadRestrictions_index = [...]uint8{0, 23, 47, 67}
func (i LoadRestrictions) String() string {
if i < 0 || i >= LoadRestrictions(len(_LoadRestrictions_index)-1) {
return "LoadRestrictions(" + strconv.FormatInt(int64(i), 10) + ")"
}
return _LoadRestrictions_name[_LoadRestrictions_index[i]:_LoadRestrictions_index[i+1]]
}

View File

@@ -11,6 +11,7 @@ type PluginConfig struct {
// further categorizing plugins.
DirectoryPath string
// Enabled is true if plugins are enabled.
Enabled bool
// PluginRestrictions defines the plugin restriction state.
// See type for more information.
PluginRestrictions PluginRestrictions
}

View File

@@ -0,0 +1,28 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package types
// Some plugin classes
// - builtin: plugins defined in the kustomize repo.
// May be freely used and re-configured.
// - local: plugins that aren't builtin but are
// locally defined (presumably by the user), meaning
// the kustomization refers to them via a relative
// file path, not a URL.
// - remote: require a build-time download to obtain.
// Unadvised, unless one controls the
// serving site.
//
//go:generate stringer -type=PluginRestrictions
type PluginRestrictions int
const (
PluginRestrictionsUnknown PluginRestrictions = iota
// Non-builtin plugins completely disabled.
PluginRestrictionsBuiltinsOnly
// No restrictions, do whatever you want.
PluginRestrictionsNone
)

View File

@@ -0,0 +1,25 @@
// Code generated by "stringer -type=PluginRestrictions"; DO NOT EDIT.
package types
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[PluginRestrictionsUnknown-0]
_ = x[PluginRestrictionsBuiltinsOnly-1]
_ = x[PluginRestrictionsNone-2]
}
const _PluginRestrictions_name = "PluginRestrictionsUnknownPluginRestrictionsBuiltinsOnlyPluginRestrictionsNone"
var _PluginRestrictions_index = [...]uint8{0, 25, 55, 77}
func (i PluginRestrictions) String() string {
if i < 0 || i >= PluginRestrictions(len(_PluginRestrictions_index)-1) {
return "PluginRestrictions(" + strconv.FormatInt(int64(i), 10) + ")"
}
return _PluginRestrictions_name[_PluginRestrictions_index[i]:_PluginRestrictions_index[i+1]]
}

View File

@@ -15,6 +15,7 @@ import (
"sigs.k8s.io/kustomize/api/pgmconfig"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/resource"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/yaml"
)
@@ -23,7 +24,6 @@ type Options struct {
kustomizationPath string
outputPath string
outOrder reorderOutput
pluginsEnabled bool
}
// NewOptions creates a Options object
@@ -73,9 +73,8 @@ func NewCmdBuild(out io.Writer) *cobra.Command {
&o.outputPath,
"output", "o", "",
"If specified, write the build output to this path.")
krusty.AddFlagLoadRestrictor(cmd.Flags())
pgmconfig.AddFlagEnablePlugins(
cmd.Flags(), &o.pluginsEnabled)
addFlagLoadRestrictor(cmd.Flags())
addFlagEnablePlugins(cmd.Flags())
addFlagReorderOutput(cmd.Flags())
cmd.AddCommand(NewCmdBuildPrune(out))
return cmd
@@ -95,7 +94,7 @@ func (o *Options) Validate(args []string) (err error) {
} else {
o.kustomizationPath = args[0]
}
err = krusty.ValidateFlagLoadRestrictor()
err = validateFlagLoadRestrictor()
if err != nil {
return err
}
@@ -105,9 +104,12 @@ func (o *Options) Validate(args []string) (err error) {
func (o *Options) makeOptions() *krusty.Options {
opts := krusty.MakeDefaultOptions()
opts.LoadRestrictions = krusty.GetFlagLoadRestrictorValue()
opts.LoadRestrictions = getFlagLoadRestrictorValue()
opts.DoLegacyResourceSort = o.outOrder == legacy
opts.PluginConfig.Enabled = o.pluginsEnabled
opts.PluginConfig.PluginRestrictions = types.PluginRestrictionsBuiltinsOnly
if isFlagEnablePluginsSet() {
opts.PluginConfig.PluginRestrictions = types.PluginRestrictionsNone
}
return opts
}

View File

@@ -0,0 +1,29 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package build
import (
"github.com/spf13/pflag"
)
const (
flagEnablePluginsName = "enable_alpha_plugins"
flagEnablePluginsHelp = `enable plugins, an alpha feature.
See https://github.com/kubernetes-sigs/kustomize/blob/master/docs/plugins/README.md
`
)
var (
flagPluginsEnabledValue = false
)
func addFlagEnablePlugins(set *pflag.FlagSet) {
set.BoolVar(
&flagPluginsEnabledValue, flagEnablePluginsName,
false, flagEnablePluginsHelp)
}
func isFlagEnablePluginsSet() bool {
return flagPluginsEnabledValue
}

View File

@@ -0,0 +1,51 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package build
import (
"fmt"
"github.com/spf13/pflag"
"sigs.k8s.io/kustomize/api/types"
)
const (
flagName = "load_restrictor"
)
var (
flagLrValue = types.LoadRestrictionsRootOnly.String()
flagLrHelp = "if set to '" + types.LoadRestrictionsNone.String() +
"', local kustomizations may load files from outside their root. " +
"This does, however, break the relocatability of the kustomization."
)
func addFlagLoadRestrictor(set *pflag.FlagSet) {
set.StringVar(
&flagLrValue, flagName,
types.LoadRestrictionsRootOnly.String(), flagLrHelp)
}
func validateFlagLoadRestrictor() error {
switch getFlagLoadRestrictorValue() {
case types.LoadRestrictionsRootOnly, types.LoadRestrictionsNone:
return nil
default:
return fmt.Errorf(
"illegal flag value --%s %s; legal values: %v",
flagName, flagLrValue,
[]string{types.LoadRestrictionsRootOnly.String(), types.LoadRestrictionsNone.String()})
}
}
func getFlagLoadRestrictorValue() types.LoadRestrictions {
switch flagLrValue {
case types.LoadRestrictionsRootOnly.String(), "rootOnly":
return types.LoadRestrictionsRootOnly
case types.LoadRestrictionsNone.String(), "none":
return types.LoadRestrictionsNone
default:
return types.LoadRestrictionsUnknown
}
}

View File

@@ -34,6 +34,10 @@ function removeBin {
/bin/rm -f $d
}
# TODO: go install golang.org/x/tools/cmd/stringer
# TODO: kubeval?
# TODO: helm? no. except then we should not requirekubeval either,
# since they're both only used in demo (non-builtin) plugins.
function installTools {
# TODO(2019/Oct/19): After the API is in place, and
# there's a new pluginator compiled against that API,