Simplify building api.

This commit is contained in:
Jeffrey Regan
2019-10-28 11:11:29 -07:00
parent a186144a78
commit 4716cb026f
8 changed files with 135 additions and 164 deletions

View File

@@ -10,7 +10,6 @@ import (
"sigs.k8s.io/kustomize/api/k8sdeps/validator"
fLdr "sigs.k8s.io/kustomize/api/loader"
"sigs.k8s.io/kustomize/api/plugins/builtins"
"sigs.k8s.io/kustomize/api/plugins/config"
pLdr "sigs.k8s.io/kustomize/api/plugins/loader"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/resource"
@@ -43,20 +42,20 @@ func MakeKustomizer(fSys filesys.FileSystem, o *Options) *Kustomizer {
// It uses its internal filesystem reference to read the file at
// the given path argument, interpret it as a kustomization.yaml
// file, perform the kustomization it represents, and return the
// resulting YAML as bytes.
// resulting resources.
//
// Any files referenced by the kustomization must be present in the
// internal filesystem. One may call Run any number of times,
// on any number of internal paths (e.g. the filesystem may contain
// multiple overlays, and Run can be called on each of them).
func (b *Kustomizer) Run(path string) ([]byte, error) {
func (b *Kustomizer) Run(path string) (resmap.ResMap, error) {
pf := transformer.NewFactoryImpl()
rf := resmap.NewFactory(
resource.NewFactory(
kunstruct.NewKunstructuredFactoryImpl()),
pf)
lr := fLdr.RestrictionNone
if b.options.RestrictToRootOnly {
if b.options.LoadRestrictions == rootOnly {
lr = fLdr.RestrictionRootOnly
}
ldr, err := fLdr.NewLoader(lr, path, b.fSys)
@@ -69,17 +68,22 @@ func (b *Kustomizer) Run(path string) ([]byte, error) {
validator.NewKustValidator(),
rf,
pf,
pLdr.NewLoader(config.DefaultPluginConfig(), rf),
pLdr.NewLoader(b.options.PluginConfig, rf),
)
if err != nil {
return nil, err
}
m, err := kt.MakeCustomizedResMap()
var m resmap.ResMap
if b.options.DoPrune {
m, err = kt.MakePruneConfigMap()
} else {
m, err = kt.MakeCustomizedResMap()
}
if err != nil {
return nil, err
}
if b.options.DoLegacyResourceSort {
builtins.NewLegacyOrderTransformerPlugin().Transform(m)
}
return m.AsYaml()
return m, nil
}

View File

@@ -0,0 +1,68 @@
// 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

@@ -0,0 +1,25 @@
// 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

@@ -3,27 +3,38 @@
package krusty
// Options holds high-level configuration options, e.g.
// are plugins enabled, should the loader be restricted to
// the kustomization root, etc.
import (
"sigs.k8s.io/kustomize/api/plugins/config"
"sigs.k8s.io/kustomize/api/types"
)
// Options holds high-level kustomize configuration options,
// e.g. are plugins enabled, should the loader be restricted
// to the kustomization root, etc.
type Options struct {
// When true, sort the resources before emitting them,
// per a particular sort order. When false, don't do the
// sort, and instead respect the depth-first resource input
// order as specified by the kustomization file(s).
DoLegacyResourceSort bool
// When true, the files referenced by a kustomization file
// must be in or under the directory holding the kustomization
// file itself. When false, the kustomization file may specify
// absolute or relative paths to patch or resources files outside
// its own tree.
RestrictToRootOnly bool
// Restrictions on what can be loaded from the file system.
// See type definition.
LoadRestrictions loadRestrictions
// Create an inventory object for pruning.
DoPrune bool
// Options related to kustomize plugins.
PluginConfig *types.PluginConfig
}
// MakeDefaultOptions returns a default instance of Options.
func MakeDefaultOptions() *Options {
return &Options{
DoLegacyResourceSort: true,
RestrictToRootOnly: true,
LoadRestrictions: rootOnly,
DoPrune: false,
PluginConfig: config.DefaultPluginConfig(),
}
}