mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-17 18:25:26 +00:00
Merge pull request #544 from Liujingfang1/config
Remove -t flag in build and add configurations field in kustomization file
This commit is contained in:
@@ -89,8 +89,8 @@ nameReference:
|
|||||||
|
|
||||||
## cusotmizing transformer configurations
|
## 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
|
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](crd/README.md)
|
- support a crd type
|
||||||
- disabling adding commonLabels to fields in some kind of resources
|
- disabling adding commonLabels to fields in some kind of resources
|
||||||
- add extra fields for variable substitution
|
- add extra fields for variable substitution
|
||||||
- add extra fields for name reference
|
- add extra fields for name reference
|
||||||
|
|||||||
@@ -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
|
|
||||||
|
|
||||||
<!-- @saveConfig @test -->
|
|
||||||
```
|
|
||||||
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
|
|
||||||
<!-- @addConfig @test -->
|
|
||||||
```
|
|
||||||
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.
|
|
||||||
|
|
||||||
<!-- @createKustomization @test -->
|
|
||||||
```
|
|
||||||
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.
|
|
||||||
|
|
||||||
<!-- @build @test -->
|
|
||||||
```
|
|
||||||
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.
|
|
||||||
|
|
||||||
<!-- @verify @test -->
|
|
||||||
```
|
|
||||||
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.
|
|
||||||
|
|
||||||
<!-- @compareOutput -->
|
|
||||||
```
|
|
||||||
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
|
|
||||||
> ```
|
|
||||||
|
|
||||||
|
|
||||||
@@ -18,7 +18,6 @@ package build
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@@ -28,13 +27,11 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/pkg/loader"
|
"sigs.k8s.io/kustomize/pkg/loader"
|
||||||
"sigs.k8s.io/kustomize/pkg/resmap"
|
"sigs.k8s.io/kustomize/pkg/resmap"
|
||||||
"sigs.k8s.io/kustomize/pkg/target"
|
"sigs.k8s.io/kustomize/pkg/target"
|
||||||
"sigs.k8s.io/kustomize/pkg/transformers/config"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type buildOptions struct {
|
type buildOptions struct {
|
||||||
kustomizationPath string
|
kustomizationPath string
|
||||||
outputPath string
|
outputPath string
|
||||||
transformerconfigPaths []string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var examples = `
|
var examples = `
|
||||||
@@ -50,11 +47,6 @@ url examples:
|
|||||||
sigs.k8s.io/kustomize//examples/multibases?ref=v1.0.6
|
sigs.k8s.io/kustomize//examples/multibases?ref=v1.0.6
|
||||||
github.com/Liujingfang1/mysql
|
github.com/Liujingfang1/mysql
|
||||||
github.com/Liujingfang1/kustomize//examples/helloWorld?ref=repoUrl2
|
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.
|
// NewCmdBuild creates a new build command.
|
||||||
@@ -63,7 +55,6 @@ func NewCmdBuild(
|
|||||||
rf *resmap.Factory,
|
rf *resmap.Factory,
|
||||||
ptf transformer.Factory) *cobra.Command {
|
ptf transformer.Factory) *cobra.Command {
|
||||||
var o buildOptions
|
var o buildOptions
|
||||||
var p string
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "build [path]",
|
Use: "build [path]",
|
||||||
@@ -71,7 +62,7 @@ func NewCmdBuild(
|
|||||||
Example: examples,
|
Example: examples,
|
||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
err := o.Validate(args, p, fs)
|
err := o.Validate(args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -82,15 +73,11 @@ func NewCmdBuild(
|
|||||||
&o.outputPath,
|
&o.outputPath,
|
||||||
"output", "o", "",
|
"output", "o", "",
|
||||||
"If specified, write the build output to this path.")
|
"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
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate validates build command.
|
// 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 {
|
if len(args) > 1 {
|
||||||
return errors.New("specify one path to " + constants.KustomizationFileName)
|
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]
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,12 +98,8 @@ func (o *buildOptions) RunBuild(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
tc, err := makeTransformerconfig(fSys, o.transformerconfigPaths)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer ldr.Cleanup()
|
defer ldr.Cleanup()
|
||||||
kt, err := target.NewKustTarget(ldr, fSys, rf, ptf, tc)
|
kt, err := target.NewKustTarget(ldr, fSys, rf, ptf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -149,13 +118,3 @@ func (o *buildOptions) RunBuild(
|
|||||||
_, err = out.Write(res)
|
_, err = out.Write(res)
|
||||||
return err
|
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)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ func TestBuildValidate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, mycase := range cases {
|
for _, mycase := range cases {
|
||||||
opts := buildOptions{}
|
opts := buildOptions{}
|
||||||
e := opts.Validate(mycase.args, "", nil)
|
e := opts.Validate(mycase.args)
|
||||||
if len(mycase.erMsg) > 0 {
|
if len(mycase.erMsg) > 0 {
|
||||||
if e == nil {
|
if e == nil {
|
||||||
t.Errorf("%s: Expected an error %v", mycase.name, mycase.erMsg)
|
t.Errorf("%s: Expected an error %v", mycase.name, mycase.erMsg)
|
||||||
|
|||||||
@@ -66,6 +66,7 @@ func determineFieldOrder() []string {
|
|||||||
"GeneratorOptions",
|
"GeneratorOptions",
|
||||||
"Vars",
|
"Vars",
|
||||||
"ImageTags",
|
"ImageTags",
|
||||||
|
"Configurations",
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add deprecated fields here.
|
// Add deprecated fields here.
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ func TestFieldOrder(t *testing.T) {
|
|||||||
"GeneratorOptions",
|
"GeneratorOptions",
|
||||||
"Vars",
|
"Vars",
|
||||||
"ImageTags",
|
"ImageTags",
|
||||||
|
"Configurations",
|
||||||
}
|
}
|
||||||
actual := determineFieldOrder()
|
actual := determineFieldOrder()
|
||||||
if len(expected) != len(actual) {
|
if len(expected) != len(actual) {
|
||||||
|
|||||||
@@ -54,8 +54,7 @@ type KustTarget struct {
|
|||||||
func NewKustTarget(
|
func NewKustTarget(
|
||||||
ldr ifc.Loader, fSys fs.FileSystem,
|
ldr ifc.Loader, fSys fs.FileSystem,
|
||||||
rFactory *resmap.Factory,
|
rFactory *resmap.Factory,
|
||||||
tFactory transformer.Factory,
|
tFactory transformer.Factory) (*KustTarget, error) {
|
||||||
tConfig *config.TransformerConfig) (*KustTarget, error) {
|
|
||||||
content, err := loadKustFile(ldr)
|
content, err := loadKustFile(ldr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -67,6 +66,10 @@ func NewKustTarget(
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
k.DealWithDeprecatedFields()
|
k.DealWithDeprecatedFields()
|
||||||
|
tConfig, err := makeTransformerConfig(ldr, k.Configurations)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return &KustTarget{
|
return &KustTarget{
|
||||||
kustomization: &k,
|
kustomization: &k,
|
||||||
ldr: ldr,
|
ldr: ldr,
|
||||||
@@ -87,6 +90,15 @@ func unmarshal(y []byte, o interface{}) error {
|
|||||||
return dec.Decode(o)
|
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.
|
// MakeCustomizedResMap creates a ResMap per kustomization instructions.
|
||||||
// The Resources in the returned ResMap are fully customized.
|
// The Resources in the returned ResMap are fully customized.
|
||||||
func (kt *KustTarget) MakeCustomizedResMap() (resmap.ResMap, error) {
|
func (kt *KustTarget) MakeCustomizedResMap() (resmap.ResMap, error) {
|
||||||
@@ -229,7 +241,7 @@ func (kt *KustTarget) loadCustomizedBases() (resmap.ResMap, *interror.Kustomizat
|
|||||||
}
|
}
|
||||||
target, err := NewKustTarget(
|
target, err := NewKustTarget(
|
||||||
ldr, kt.fSys,
|
ldr, kt.fSys,
|
||||||
kt.rFactory, kt.tFactory, kt.tConfig)
|
kt.rFactory, kt.tFactory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errs.Append(errors.Wrap(err, "couldn't make target for "+path))
|
errs.Append(errors.Wrap(err, "couldn't make target for "+path))
|
||||||
continue
|
continue
|
||||||
@@ -259,7 +271,7 @@ func (kt *KustTarget) loadBasesAsFlatList() ([]*KustTarget, error) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
target, err := NewKustTarget(
|
target, err := NewKustTarget(
|
||||||
ldr, kt.fSys, kt.rFactory, kt.tFactory, kt.tConfig)
|
ldr, kt.fSys, kt.rFactory, kt.tFactory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errs.Append(err)
|
errs.Append(err)
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/pkg/resid"
|
"sigs.k8s.io/kustomize/pkg/resid"
|
||||||
"sigs.k8s.io/kustomize/pkg/resmap"
|
"sigs.k8s.io/kustomize/pkg/resmap"
|
||||||
"sigs.k8s.io/kustomize/pkg/resource"
|
"sigs.k8s.io/kustomize/pkg/resource"
|
||||||
"sigs.k8s.io/kustomize/pkg/transformers/config"
|
|
||||||
"sigs.k8s.io/kustomize/pkg/types"
|
"sigs.k8s.io/kustomize/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -96,8 +95,7 @@ func makeKustTarget(t *testing.T, l ifc.Loader) *KustTarget {
|
|||||||
fakeFs := fs.MakeFakeFS()
|
fakeFs := fs.MakeFakeFS()
|
||||||
fakeFs.Mkdir("/")
|
fakeFs.Mkdir("/")
|
||||||
kt, err := NewKustTarget(
|
kt, err := NewKustTarget(
|
||||||
l, fakeFs, rf, transformer.NewFactoryImpl(),
|
l, fakeFs, rf, transformer.NewFactoryImpl())
|
||||||
config.NewFactory(l).DefaultConfig())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unexpected construction error %v", err)
|
t.Fatalf("Unexpected construction error %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -116,6 +116,9 @@ type Kustomization struct {
|
|||||||
// GeneratorOptions modify behavior of all ConfigMap and Secret generators.
|
// GeneratorOptions modify behavior of all ConfigMap and Secret generators.
|
||||||
GeneratorOptions *GeneratorOptions `json:"generatorOptions,omitempty" yaml:"generatorOptions,omitempty"`
|
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
|
// Deprecated fields - See DealWithDeprecatedFields
|
||||||
//
|
//
|
||||||
|
|||||||
Reference in New Issue
Block a user