diff --git a/examples/transformerconfigs/README.md b/examples/transformerconfigs/README.md index d8bf13a24..e7cd7d3ef 100644 --- a/examples/transformerconfigs/README.md +++ b/examples/transformerconfigs/README.md @@ -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 diff --git a/examples/transformerconfigs/crd/README.md b/examples/transformerconfigs/crd/README.md deleted file mode 100644 index 16896e8e8..000000000 --- a/examples/transformerconfigs/crd/README.md +++ /dev/null @@ -1,171 +0,0 @@ -## Transformer Configurations - CRD - -This tutorial shows how to add transformer configurations to support a CRD type. - -### Get Default Config -Get the default transformer configurations by - - -``` -kustomize config save -d ~/.kustomize/config -``` -The default configurations are save in directory `~/.kustomize/config` as several files - -> ``` -> commonannotations.yaml commonlabels.yaml nameprefix.yaml namereference.yaml namespace.yaml varreference.yaml -> ``` - -### Add Config for a CRD -All transformers will be involved for a CRD type. The default configurations already include some common fieldSpec for all types: - -- nameprefix is added to `.metadata.name` -- namespace is added to `.metadata.namespace` -- labels is added to `.metadata.labels` -- annotations is added to `.metadata.annotations` - -Thus those fieldSpec don't need to be added to support a CRD type. -Consider a CRD type `MyKind` with fields -- `.spec.secretRef.name` reference a Secret -- `.spec.beeRef.name` reference an instance of CRD `Bee` -- `.spec.containers.command` as the list of container commands -- `.spec.selectors` as the label selectors - -Add following file to configure the transformers for the above fields - -``` -cat > ~/.kustomize/config/mykind.yaml << EOF - -commonLabels: -- path: spec/selectors - create: true - kind: MyKind - -nameReference: -- kind: Bee - fieldSpecs: - - path: spec/beeRef/name - kind: MyKind -- kind: Secret - fieldSpecs: - - path: spec/secretRef/name - kind: MyKind - -varReference: -- path: spec/containers/command - kind: MyKind -- path: spec/beeRef/action - kind: MyKind - -EOF -``` - -### Apply config -Create a kustomization with a `MyKind` instance. - - -``` -DEMO_HOME=$(mktemp -d) - -cat > $DEMO_HOME/kustomization.yaml << EOF -resources: -- resources.yaml - -namePrefix: test- - -commonLabels: - foo: bar - -vars: -- name: BEE_ACTION - objref: - kind: Bee - name: bee - apiVersion: v1beta1 - fieldref: - fieldpath: spec.action -EOF - -cat > $DEMO_HOME/resources.yaml << EOF -apiVersion: v1 -kind: Secret -metadata: - name: crdsecret -data: - PATH: YmJiYmJiYmIK ---- -apiVersion: v1beta1 -kind: Bee -metadata: - name: bee -spec: - action: fly ---- -apiVersion: jingfang.example.com/v1beta1 -kind: MyKind -metadata: - name: mykind -spec: - secretRef: - name: crdsecret - beeRef: - name: bee - action: \$(BEE_ACTION) - containers: - - command: - - "echo" - - "\$(BEE_ACTION)" - image: myapp -EOF -``` - -Run `kustomize build` with customized transformer configurations and verify that -the namereference is correctly resolved. - - -``` -test 2 == \ -$(kustomize build $DEMO_HOME -t ~/.kustomize/config | grep -A 2 ".*Ref" | grep "test-" | wc -l); \ -echo $? -``` - -Run `kustomize build` with customized transformer configurations and verify that -the vars correctly resolved. - - -``` -test 0 == \ -$(kustomize build $DEMO_HOME -t ~/.kustomize/config | grep "BEE_ACTION" | wc -l); \ -echo $? -``` - -To understand this better, compare the output using default transformer configurations. - - -``` -diff \ - <(kustomize build $DEMO_HOME) \ - <(kustomize build $DEMO_HOME -t ~/.kustomize/config ) |\ - more -``` - -The difference output should look something like -> ``` -> 20,21c20,21 -> < action: $(BEE_ACTION) -> < name: bee -> --- -> > action: fly -> > name: test-bee -> 25c25 -> < - $(BEE_ACTION) -> --- -> > - fly -> 28c28,30 -> < name: crdsecret -> --- -> > name: test-crdsecret -> > selectors: -> > foo: bar -> ``` - - diff --git a/pkg/commands/build/build.go b/pkg/commands/build/build.go index 9c318482d..b306e2620 100644 --- a/pkg/commands/build/build.go +++ b/pkg/commands/build/build.go @@ -18,7 +18,6 @@ package build import ( "io" - "strings" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -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 = ` @@ -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. @@ -63,7 +55,6 @@ func NewCmdBuild( rf *resmap.Factory, ptf transformer.Factory) *cobra.Command { var o buildOptions - var p string cmd := &cobra.Command{ Use: "build [path]", @@ -71,7 +62,7 @@ func NewCmdBuild( 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 } @@ -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) } @@ -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 } @@ -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 } @@ -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) -} diff --git a/pkg/commands/build/build_test.go b/pkg/commands/build/build_test.go index ed42777b7..98c2f99d3 100644 --- a/pkg/commands/build/build_test.go +++ b/pkg/commands/build/build_test.go @@ -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) diff --git a/pkg/commands/kustfile/kustomizationfile.go b/pkg/commands/kustfile/kustomizationfile.go index a94665465..373f5052c 100644 --- a/pkg/commands/kustfile/kustomizationfile.go +++ b/pkg/commands/kustfile/kustomizationfile.go @@ -66,6 +66,7 @@ func determineFieldOrder() []string { "GeneratorOptions", "Vars", "ImageTags", + "Configurations", } // Add deprecated fields here. diff --git a/pkg/commands/kustfile/kustomizationfile_test.go b/pkg/commands/kustfile/kustomizationfile_test.go index 3ad489304..496590a25 100644 --- a/pkg/commands/kustfile/kustomizationfile_test.go +++ b/pkg/commands/kustfile/kustomizationfile_test.go @@ -44,6 +44,7 @@ func TestFieldOrder(t *testing.T) { "GeneratorOptions", "Vars", "ImageTags", + "Configurations", } actual := determineFieldOrder() if len(expected) != len(actual) { diff --git a/pkg/target/kusttarget.go b/pkg/target/kusttarget.go index 5e43fc0aa..a4f214d0c 100644 --- a/pkg/target/kusttarget.go +++ b/pkg/target/kusttarget.go @@ -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 @@ -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, @@ -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) { @@ -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 @@ -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 diff --git a/pkg/target/kusttarget_test.go b/pkg/target/kusttarget_test.go index 4bc6dc18a..0df4d799b 100644 --- a/pkg/target/kusttarget_test.go +++ b/pkg/target/kusttarget_test.go @@ -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" ) @@ -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) } diff --git a/pkg/types/kustomization.go b/pkg/types/kustomization.go index bfa0037e3..dba8fe556 100644 --- a/pkg/types/kustomization.go +++ b/pkg/types/kustomization.go @@ -116,6 +116,9 @@ type Kustomization struct { // GeneratorOptions modify behavior of all ConfigMap and Secret generators. GeneratorOptions *GeneratorOptions `json:"generatorOptions,omitempty" yaml:"generatorOptions,omitempty"` + // Configurations is a list of transformer configuration files + Configurations []string `json:"configurations,omitempty" yaml:"configurations,omitempty"` + // // Deprecated fields - See DealWithDeprecatedFields //