From 6ec77b27da13649f43fe7bf6840334ff32f47a0c Mon Sep 17 00:00:00 2001 From: Jingfang Liu Date: Tue, 20 Nov 2018 10:31:44 -0800 Subject: [PATCH 1/3] update crd example by using configurations file list --- examples/transformerconfigs/README.md | 2 +- examples/transformerconfigs/crd/README.md | 156 ++++++++++++++++++++++ 2 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 examples/transformerconfigs/crd/README.md diff --git a/examples/transformerconfigs/README.md b/examples/transformerconfigs/README.md index e7cd7d3ef..06700ea64 100644 --- a/examples/transformerconfigs/README.md +++ b/examples/transformerconfigs/README.md @@ -90,7 +90,7 @@ 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 kustomization.yaml file. This tutorial shows how to customize those configurations to -- support a crd type +- [support a crd type](crd/README.md) - 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 new file mode 100644 index 000000000..62300fa90 --- /dev/null +++ b/examples/transformerconfigs/crd/README.md @@ -0,0 +1,156 @@ +## Transformer Configurations - CRD + +This tutorial shows how to add transformer configurations to support a CRD type. + +Create a workspace by + +``` +DEMO_HOME=$(mktemp -d) +``` + +### Get Default Config +Get the default transformer configurations by + + +``` +kustomize config save -d $DEMO_HOME/kustomizeconfig +``` +The default configurations are save in directory `$DEMO_HOME/kusotmizeconfig` 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 > $DEMO_HOME/kustomizeconfig/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. + + +``` +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 +``` + +Use the cusotmized transformer configurations by adding those files in kustomization.yaml + +``` +cat >> $DEMO_HOME/kustomization.yaml << EOF +configurations: +- kustomizeconfig/mykind.yaml +- kustomizeconfig/commonannotations.yaml +- kustomizeconfig/commonlabels.yaml +- kustomizeconfig/nameprefix.yaml +- kustomizeconfig/namereference.yaml +- kustomizeconfig/namespace.yaml +- kustomizeconfig/varreference.yaml +EOF +``` + +Run `kustomize build` and verify that the namereference is correctly resolved. + + +``` +test 2 == \ +$(kustomize build $DEMO_HOME | grep -A 2 ".*Ref" | grep "test-" | wc -l); \ +echo $? +``` + +Run `kustomize build` and verify that the vars correctly resolved. + + +``` +test 0 == \ +$(kustomize build $DEMO_HOME | grep "BEE_ACTION" | wc -l); \ +echo $? +``` From 3e1a3d83da0775761fc3a4933b1c88ef8ec321b2 Mon Sep 17 00:00:00 2001 From: Jeff Regan Date: Wed, 28 Nov 2018 14:53:33 -0800 Subject: [PATCH 2/3] Minor tweaks --- examples/transformerconfigs/crd/README.md | 86 ++++++++++++++--------- 1 file changed, 53 insertions(+), 33 deletions(-) diff --git a/examples/transformerconfigs/crd/README.md b/examples/transformerconfigs/crd/README.md index 62300fa90..faf903f84 100644 --- a/examples/transformerconfigs/crd/README.md +++ b/examples/transformerconfigs/crd/README.md @@ -1,6 +1,6 @@ -## Transformer Configurations - CRD +## Supporting Custom Resources (defined by a CRD) -This tutorial shows how to add transformer configurations to support a CRD type. +This tutorial shows how to add transformer configurations to support a custom resource. Create a workspace by @@ -8,35 +8,47 @@ Create a workspace by DEMO_HOME=$(mktemp -d) ``` -### Get Default Config -Get the default transformer configurations by +### Get the native config as a starting point + +Get the default transformer configurations using this command: ``` kustomize config save -d $DEMO_HOME/kustomizeconfig ``` -The default configurations are save in directory `$DEMO_HOME/kusotmizeconfig` as several files +The default configurations are saved +in the directory `$DEMO_HOME/kusotmizeconfig` as several files > ``` -> commonannotations.yaml commonlabels.yaml nameprefix.yaml namereference.yaml namespace.yaml varreference.yaml +> 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: +These files contain the field specifications for native resources +that transformation directives like `namePrefix`, `commonLabels`, etc. +need to do their work. + +These default configurations already include some common +field specifictions 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 +### Adding a custom resource + +Consider a CRD of kind `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 +Add the following file to configure the transformers for the above fields ``` cat > $DEMO_HOME/kustomizeconfig/mykind.yaml << EOF @@ -66,29 +78,12 @@ EOF ``` ### Apply config -Create a kustomization with a `MyKind` instance. - +Create a file with some resources that +includes an instance of `MyKind`: + + ``` -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 @@ -122,7 +117,32 @@ spec: EOF ``` -Use the cusotmized transformer configurations by adding those files in kustomization.yaml +Create a kustomization referring to it: + + +``` +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 +``` + +Use the customized transformer configurations by specifying them +in the kustomization file: ``` cat >> $DEMO_HOME/kustomization.yaml << EOF From f714e9faf340af909e15bdd5b2cf130def557831 Mon Sep 17 00:00:00 2001 From: Jeff Regan Date: Wed, 28 Nov 2018 14:55:48 -0800 Subject: [PATCH 3/3] another tweak --- examples/transformerconfigs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/transformerconfigs/README.md b/examples/transformerconfigs/README.md index 06700ea64..fec0dc6ec 100644 --- a/examples/transformerconfigs/README.md +++ b/examples/transformerconfigs/README.md @@ -90,7 +90,7 @@ 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 kustomization.yaml file. This tutorial shows how to customize those configurations to -- [support a crd type](crd/README.md) +- [support a CRD type](crd/README.md) - disabling adding commonLabels to fields in some kind of resources - add extra fields for variable substitution - add extra fields for name reference