| 1 |
## Supporting Custom Resources (defined by a CRD) |
| 2 |
|
| 3 |
This tutorial shows how to add transformer configurations to support a custom resource. |
| 4 |
|
| 5 |
Create a workspace by |
| 6 |
<!-- @createws @testAgainstLatestRelease --> |
| 7 |
``` |
| 8 |
DEMO_HOME=$(mktemp -d) |
| 9 |
``` |
| 10 |
|
| 11 |
### Adding a custom resource |
| 12 |
|
| 13 |
Consider a CRD of kind `MyKind` with fields |
| 14 |
- `.spec.secretRef.name` reference a Secret |
| 15 |
- `.spec.beeRef.name` reference an instance of CRD `Bee` |
| 16 |
- `.spec.containers.command` as the list of container commands |
| 17 |
- `.spec.selectors` as the label selectors |
| 18 |
|
| 19 |
Add the following file to configure the transformers for the above fields |
| 20 |
<!-- @addConfig @testAgainstLatestRelease --> |
| 21 |
``` |
| 22 |
mkdir $DEMO_HOME/kustomizeconfig |
| 23 |
cat > $DEMO_HOME/kustomizeconfig/mykind.yaml << EOF |
| 24 |
|
| 25 |
commonLabels: |
| 26 |
- path: spec/selectors |
| 27 |
create: true |
| 28 |
kind: MyKind |
| 29 |
|
| 30 |
nameReference: |
| 31 |
- kind: Bee |
| 32 |
fieldSpecs: |
| 33 |
- path: spec/beeRef/name |
| 34 |
kind: MyKind |
| 35 |
- kind: Secret |
| 36 |
fieldSpecs: |
| 37 |
- path: spec/secretRef/name |
| 38 |
kind: MyKind |
| 39 |
|
| 40 |
varReference: |
| 41 |
- path: spec/containers/command |
| 42 |
kind: MyKind |
| 43 |
- path: spec/beeRef/action |
| 44 |
kind: MyKind |
| 45 |
|
| 46 |
EOF |
| 47 |
``` |
| 48 |
|
| 49 |
### Apply config |
| 50 |
|
| 51 |
Create a file with some resources that |
| 52 |
includes an instance of `MyKind`: |
| 53 |
|
| 54 |
<!-- @createResource @testAgainstLatestRelease --> |
| 55 |
``` |
| 56 |
cat > $DEMO_HOME/resources.yaml << EOF |
| 57 |
apiVersion: v1 |
| 58 |
kind: Secret |
| 59 |
metadata: |
| 60 |
name: crdsecret |
| 61 |
data: |
| 62 |
PATH: YmJiYmJiYmIK |
| 63 |
--- |
| 64 |
apiVersion: v1beta1 |
| 65 |
kind: Bee |
| 66 |
metadata: |
| 67 |
name: bee |
| 68 |
spec: |
| 69 |
action: fly |
| 70 |
--- |
| 71 |
apiVersion: jingfang.example.com/v1beta1 |
| 72 |
kind: MyKind |
| 73 |
metadata: |
| 74 |
name: mykind |
| 75 |
spec: |
| 76 |
secretRef: |
| 77 |
name: crdsecret |
| 78 |
beeRef: |
| 79 |
name: bee |
| 80 |
action: \$(BEE_ACTION) |
| 81 |
containers: |
| 82 |
- command: |
| 83 |
- "echo" |
| 84 |
- "\$(BEE_ACTION)" |
| 85 |
image: myapp |
| 86 |
EOF |
| 87 |
``` |
| 88 |
|
| 89 |
Create a kustomization referring to it: |
| 90 |
|
| 91 |
<!-- @createKustomization @testAgainstLatestRelease --> |
| 92 |
``` |
| 93 |
cat > $DEMO_HOME/kustomization.yaml << EOF |
| 94 |
resources: |
| 95 |
- resources.yaml |
| 96 |
|
| 97 |
namePrefix: test- |
| 98 |
|
| 99 |
commonLabels: |
| 100 |
foo: bar |
| 101 |
|
| 102 |
vars: |
| 103 |
- name: BEE_ACTION |
| 104 |
objref: |
| 105 |
kind: Bee |
| 106 |
name: bee |
| 107 |
apiVersion: v1beta1 |
| 108 |
fieldref: |
| 109 |
fieldpath: spec.action |
| 110 |
EOF |
| 111 |
``` |
| 112 |
|
| 113 |
Use the customized transformer configurations by specifying them |
| 114 |
in the kustomization file: |
| 115 |
<!-- @addTransformerConfigs @testAgainstLatestRelease --> |
| 116 |
``` |
| 117 |
cat >> $DEMO_HOME/kustomization.yaml << EOF |
| 118 |
configurations: |
| 119 |
- kustomizeconfig/mykind.yaml |
| 120 |
EOF |
| 121 |
``` |
| 122 |
|
| 123 |
Run `kustomize build` and verify that the namereference is correctly resolved. |
| 124 |
|
| 125 |
<!-- @build @testAgainstLatestRelease --> |
| 126 |
``` |
| 127 |
test 2 == \ |
| 128 |
$(kustomize build $DEMO_HOME | grep -A 2 ".*Ref" | grep "test-" | wc -l); \ |
| 129 |
echo $? |
| 130 |
``` |
| 131 |
|
| 132 |
Run `kustomize build` and verify that the vars correctly resolved. |
| 133 |
|
| 134 |
<!-- @verify @testAgainstLatestRelease --> |
| 135 |
``` |
| 136 |
test 0 == \ |
| 137 |
$(kustomize build $DEMO_HOME | grep "BEE_ACTION" | wc -l); \ |
| 138 |
echo $? |
| 139 |
``` |