Skip to content

Commit

Permalink
Remove -t flag in build and add configurations field in kustomization…
Browse files Browse the repository at this point in the history
….yaml
  • Loading branch information
Liujingfang1 committed Nov 16, 2018
1 parent 38b7f42 commit 25415c5
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 227 deletions.
4 changes: 2 additions & 2 deletions examples/transformerconfigs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ nameReference:

## cusotmizing transformer configurations

Kustomize has a default set of configurations. They can be saved to local directory through `kustomize config save -d`. Kustomize allows modifying those configuration files and using them in `kustomize build` through `-t`. This tutorial shows how to customize those configurations to
- [support a crd type](crd/README.md)
Kustomize has a default set of configurations. They can be saved to local directory through `kustomize config save -d`. Kustomize allows modifying those configuration files and using them in kustomization.yaml file. This tutorial shows how to customize those configurations to
- support a crd type
- disabling adding commonLabels to fields in some kind of resources
- add extra fields for variable substitution
- add extra fields for name reference
171 changes: 0 additions & 171 deletions examples/transformerconfigs/crd/README.md

This file was deleted.

51 changes: 5 additions & 46 deletions pkg/commands/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package build

import (
"io"
"strings"

"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand All @@ -28,13 +27,11 @@ import (
"sigs.k8s.io/kustomize/pkg/loader"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/target"
"sigs.k8s.io/kustomize/pkg/transformers/config"
)

type buildOptions struct {
kustomizationPath string
outputPath string
transformerconfigPaths []string
kustomizationPath string
outputPath string
}

var examples = `
Expand All @@ -50,11 +47,6 @@ url examples:
sigs.k8s.io/kustomize//examples/multibases?ref=v1.0.6
github.com/Liujingfang1/mysql
github.com/Liujingfang1/kustomize//examples/helloWorld?ref=repoUrl2
Advanced usage:
Use different transformer configurations by passing files to kustomize
build somedir -t someconfigdir
build somedir -t some-transformer-configfile,another-transformer-configfile
`

// NewCmdBuild creates a new build command.
Expand All @@ -63,15 +55,14 @@ func NewCmdBuild(
rf *resmap.Factory,
ptf transformer.Factory) *cobra.Command {
var o buildOptions
var p string

cmd := &cobra.Command{
Use: "build [path]",
Short: "Print current configuration per contents of " + constants.KustomizationFileName,
Example: examples,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
err := o.Validate(args, p, fs)
err := o.Validate(args)
if err != nil {
return err
}
Expand All @@ -82,15 +73,11 @@ func NewCmdBuild(
&o.outputPath,
"output", "o", "",
"If specified, write the build output to this path.")
cmd.Flags().StringVarP(
&p,
"transformer-config", "t", "",
"If specified, use the transformer configs load from these files.")
return cmd
}

// Validate validates build command.
func (o *buildOptions) Validate(args []string, p string, fs fs.FileSystem) error {
func (o *buildOptions) Validate(args []string) error {
if len(args) > 1 {
return errors.New("specify one path to " + constants.KustomizationFileName)
}
Expand All @@ -100,20 +87,6 @@ func (o *buildOptions) Validate(args []string, p string, fs fs.FileSystem) error
o.kustomizationPath = args[0]
}

if p == "" {
return nil
}

if fs.IsDir(p) {
paths, err := fs.Glob(p + "/*")
if err != nil {
return err
}
o.transformerconfigPaths = paths
} else {
o.transformerconfigPaths = strings.Split(p, ",")
}

return nil
}

Expand All @@ -125,12 +98,8 @@ func (o *buildOptions) RunBuild(
if err != nil {
return err
}
tc, err := makeTransformerconfig(fSys, o.transformerconfigPaths)
if err != nil {
return err
}
defer ldr.Cleanup()
kt, err := target.NewKustTarget(ldr, fSys, rf, ptf, tc)
kt, err := target.NewKustTarget(ldr, fSys, rf, ptf)
if err != nil {
return err
}
Expand All @@ -149,13 +118,3 @@ func (o *buildOptions) RunBuild(
_, err = out.Write(res)
return err
}

// makeTransformerConfig returns a complete TransformerConfig object from either files
// or the default configs
func makeTransformerconfig(
fSys fs.FileSystem, paths []string) (*config.TransformerConfig, error) {
if paths == nil || len(paths) == 0 {
return config.NewFactory(nil).DefaultConfig(), nil
}
return config.NewFactory(loader.NewFileLoaderAtCwd(fSys)).FromFiles(paths)
}
2 changes: 1 addition & 1 deletion pkg/commands/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestBuildValidate(t *testing.T) {
}
for _, mycase := range cases {
opts := buildOptions{}
e := opts.Validate(mycase.args, "", nil)
e := opts.Validate(mycase.args)
if len(mycase.erMsg) > 0 {
if e == nil {
t.Errorf("%s: Expected an error %v", mycase.name, mycase.erMsg)
Expand Down
1 change: 1 addition & 0 deletions pkg/commands/kustfile/kustomizationfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func determineFieldOrder() []string {
"GeneratorOptions",
"Vars",
"ImageTags",
"Configurations",
}

// Add deprecated fields here.
Expand Down
1 change: 1 addition & 0 deletions pkg/commands/kustfile/kustomizationfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestFieldOrder(t *testing.T) {
"GeneratorOptions",
"Vars",
"ImageTags",
"Configurations",
}
actual := determineFieldOrder()
if len(expected) != len(actual) {
Expand Down
20 changes: 16 additions & 4 deletions pkg/target/kusttarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ type KustTarget struct {
func NewKustTarget(
ldr ifc.Loader, fSys fs.FileSystem,
rFactory *resmap.Factory,
tFactory transformer.Factory,
tConfig *config.TransformerConfig) (*KustTarget, error) {
tFactory transformer.Factory) (*KustTarget, error) {
content, err := loadKustFile(ldr)
if err != nil {
return nil, err
Expand All @@ -67,6 +66,10 @@ func NewKustTarget(
return nil, err
}
k.DealWithDeprecatedFields()
tConfig, err := makeTransformerConfig(ldr, k.Configurations)
if err != nil {
return nil, err
}
return &KustTarget{
kustomization: &k,
ldr: ldr,
Expand All @@ -87,6 +90,15 @@ func unmarshal(y []byte, o interface{}) error {
return dec.Decode(o)
}

// makeTransformerConfig returns a complete TransformerConfig object from either files
// or the default configs
func makeTransformerConfig(ldr ifc.Loader, paths []string) (*config.TransformerConfig, error) {
if paths == nil || len(paths) == 0 {
return config.NewFactory(nil).DefaultConfig(), nil
}
return config.NewFactory(ldr).FromFiles(paths)
}

// MakeCustomizedResMap creates a ResMap per kustomization instructions.
// The Resources in the returned ResMap are fully customized.
func (kt *KustTarget) MakeCustomizedResMap() (resmap.ResMap, error) {
Expand Down Expand Up @@ -229,7 +241,7 @@ func (kt *KustTarget) loadCustomizedBases() (resmap.ResMap, *interror.Kustomizat
}
target, err := NewKustTarget(
ldr, kt.fSys,
kt.rFactory, kt.tFactory, kt.tConfig)
kt.rFactory, kt.tFactory)
if err != nil {
errs.Append(errors.Wrap(err, "couldn't make target for "+path))
continue
Expand Down Expand Up @@ -259,7 +271,7 @@ func (kt *KustTarget) loadBasesAsFlatList() ([]*KustTarget, error) {
continue
}
target, err := NewKustTarget(
ldr, kt.fSys, kt.rFactory, kt.tFactory, kt.tConfig)
ldr, kt.fSys, kt.rFactory, kt.tFactory)
if err != nil {
errs.Append(err)
continue
Expand Down
4 changes: 1 addition & 3 deletions pkg/target/kusttarget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/transformers/config"
"sigs.k8s.io/kustomize/pkg/types"
)

Expand Down Expand Up @@ -96,8 +95,7 @@ func makeKustTarget(t *testing.T, l ifc.Loader) *KustTarget {
fakeFs := fs.MakeFakeFS()
fakeFs.Mkdir("/")
kt, err := NewKustTarget(
l, fakeFs, rf, transformer.NewFactoryImpl(),
config.NewFactory(l).DefaultConfig())
l, fakeFs, rf, transformer.NewFactoryImpl())
if err != nil {
t.Fatalf("Unexpected construction error %v", err)
}
Expand Down
Loading

0 comments on commit 25415c5

Please sign in to comment.