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

@@ -1,22 +1,6 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Code generated by "stringer -type=loadRestrictions"; DO NOT EDIT.
package loader
package krusty
import "strconv"

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(),
}
}

View File

@@ -6,50 +6,9 @@ package loader
import (
"fmt"
"github.com/spf13/pflag"
"sigs.k8s.io/kustomize/api/filesys"
)
//go:generate stringer -type=loadRestrictions
type loadRestrictions int
const (
unknown loadRestrictions = iota
rootOnly
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() (LoadRestrictorFunc, error) {
switch flagValue {
case rootOnly.String():
return RestrictionRootOnly, nil
case none.String():
return RestrictionNone, nil
default:
return nil, fmt.Errorf(
"illegal flag value --%s %s; legal values: %v",
flagName, flagValue,
[]string{rootOnly.String(), none.String()})
}
}
type LoadRestrictorFunc func(
filesys.FileSystem, filesys.ConfirmedDir, string) (string, error)

View File

@@ -1,18 +1,5 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package loader

View File

@@ -11,15 +11,11 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/ifc"
fLdr "sigs.k8s.io/kustomize/api/loader"
"sigs.k8s.io/kustomize/api/krusty"
"sigs.k8s.io/kustomize/api/pgmconfig"
"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"
"sigs.k8s.io/kustomize/api/target"
"sigs.k8s.io/yaml"
)
@@ -27,8 +23,8 @@ import (
type Options struct {
kustomizationPath string
outputPath string
loadRestrictor fLdr.LoadRestrictorFunc
outOrder reorderOutput
pluginsEnabled bool
}
// NewOptions creates a Options object
@@ -36,7 +32,6 @@ func NewOptions(p, o string) *Options {
return &Options{
kustomizationPath: p,
outputPath: o,
loadRestrictor: fLdr.RestrictionRootOnly,
}
}
@@ -58,15 +53,8 @@ https://github.com/hashicorp/go-getter#url-format
`
// NewCmdBuild creates a new build command.
func NewCmdBuild(
out io.Writer, fSys filesys.FileSystem,
v ifc.Validator, rf *resmap.Factory,
ptf resmap.PatchFactory) *cobra.Command {
func NewCmdBuild(out io.Writer) *cobra.Command {
var o Options
pluginConfig := config.DefaultPluginConfig()
pl := pLdr.NewLoader(pluginConfig, rf)
cmd := &cobra.Command{
Use: "build {path}",
Short: "Print configuration per contents of " +
@@ -78,7 +66,7 @@ func NewCmdBuild(
if err != nil {
return err
}
return o.RunBuild(out, v, fSys, rf, ptf, pl)
return o.RunBuild(out)
},
}
@@ -86,14 +74,16 @@ func NewCmdBuild(
&o.outputPath,
"output", "o", "",
"If specified, write the build output to this path.")
fLdr.AddFlagLoadRestrictor(cmd.Flags())
krusty.AddFlagLoadRestrictor(cmd.Flags())
config.AddFlagEnablePlugins(
cmd.Flags(), &pluginConfig.Enabled)
cmd.Flags(), &o.pluginsEnabled)
addFlagReorderOutput(cmd.Flags())
cmd.AddCommand(NewCmdBuildPrune(out, v, fSys, rf, ptf, pl))
cmd.AddCommand(NewCmdBuildPrune(out))
return cmd
}
const CWD = "."
// Validate validates build command.
func (o *Options) Validate(args []string) (err error) {
if len(args) > 1 {
@@ -102,11 +92,11 @@ func (o *Options) Validate(args []string) (err error) {
pgmconfig.DefaultKustomizationFileName())
}
if len(args) == 0 {
o.kustomizationPath = fLdr.CWD
o.kustomizationPath = CWD
} else {
o.kustomizationPath = args[0]
}
o.loadRestrictor, err = fLdr.ValidateFlagLoadRestrictor()
err = krusty.ValidateFlagLoadRestrictor()
if err != nil {
return err
}
@@ -114,43 +104,30 @@ func (o *Options) Validate(args []string) (err error) {
return
}
// RunBuild runs build command.
func (o *Options) RunBuild(
out io.Writer, v ifc.Validator, fSys filesys.FileSystem,
rf *resmap.Factory, ptf resmap.PatchFactory,
pl *pLdr.Loader) error {
ldr, err := fLdr.NewLoader(
o.loadRestrictor, o.kustomizationPath, fSys)
if err != nil {
return err
}
defer ldr.Cleanup()
kt, err := target.NewKustTarget(ldr, v, rf, ptf, pl)
if err != nil {
return err
}
m, err := kt.MakeCustomizedResMap()
func (o *Options) makeOptions() *krusty.Options {
opts := krusty.MakeDefaultOptions()
opts.LoadRestrictions = krusty.GetFlagLoadRestrictorValue()
opts.DoLegacyResourceSort = o.outOrder == legacy
opts.PluginConfig.Enabled = o.pluginsEnabled
return opts
}
func (o *Options) RunBuild(out io.Writer) error {
fSys := filesys.MakeFsOnDisk()
k := krusty.MakeKustomizer(fSys, o.makeOptions())
m, err := k.Run(o.kustomizationPath)
if err != nil {
return err
}
return o.emitResources(out, fSys, m)
}
func (o *Options) RunBuildPrune(
out io.Writer, v ifc.Validator, fSys filesys.FileSystem,
rf *resmap.Factory, ptf resmap.PatchFactory,
pl *pLdr.Loader) error {
ldr, err := fLdr.NewLoader(
o.loadRestrictor, o.kustomizationPath, fSys)
if err != nil {
return err
}
defer ldr.Cleanup()
kt, err := target.NewKustTarget(ldr, v, rf, ptf, pl)
if err != nil {
return err
}
m, err := kt.MakePruneConfigMap()
func (o *Options) RunBuildPrune(out io.Writer) error {
fSys := filesys.MakeFsOnDisk()
opts := o.makeOptions()
opts.DoPrune = true
k := krusty.MakeKustomizer(fSys, opts)
m, err := k.Run(o.kustomizationPath)
if err != nil {
return err
}
@@ -162,13 +139,6 @@ func (o *Options) emitResources(
if o.outputPath != "" && fSys.IsDir(o.outputPath) {
return writeIndividualFiles(fSys, o.outputPath, 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.
builtins.NewLegacyOrderTransformerPlugin().Transform(m)
}
res, err := m.AsYaml()
if err != nil {
return err
@@ -180,12 +150,8 @@ func (o *Options) emitResources(
return err
}
func NewCmdBuildPrune(
out io.Writer, v ifc.Validator, fSys filesys.FileSystem,
rf *resmap.Factory, ptf resmap.PatchFactory,
pl *pLdr.Loader) *cobra.Command {
func NewCmdBuildPrune(out io.Writer) *cobra.Command {
var o Options
cmd := &cobra.Command{
Use: "alpha-inventory [path]",
Short: "Print the inventory object which contains a list of all other objects",
@@ -196,7 +162,7 @@ func NewCmdBuildPrune(
if err != nil {
return err
}
return o.RunBuildPrune(out, v, fSys, rf, ptf, pl)
return o.RunBuildPrune(out)
},
}
return cmd

View File

@@ -11,11 +11,8 @@ import (
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/api/k8sdeps/transformer"
"sigs.k8s.io/kustomize/api/k8sdeps/validator"
"sigs.k8s.io/kustomize/api/pgmconfig"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/resource"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/build"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/config"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/create"
@@ -36,15 +33,10 @@ Manages declarative configuration of Kubernetes.
See https://sigs.k8s.io/kustomize
`,
}
uf := kunstruct.NewKunstructuredFactoryImpl()
pf := transformer.NewFactoryImpl()
rf := resmap.NewFactory(resource.NewFactory(uf), pf)
v := validator.NewKustValidator()
c.AddCommand(
build.NewCmdBuild(
stdOut, fSys, v,
rf, pf),
build.NewCmdBuild(stdOut),
edit.NewCmdEdit(fSys, v, uf),
create.NewCmdCreate(fSys, uf),
config.NewCmdConfig(fSys),