|
| 1 | +## Transformer Configurations - CRD |
| 2 | + |
| 3 | +This tutorial shows how to add transformer configurations to support a CRD type. |
| 4 | + |
| 5 | +### Get Default Config |
| 6 | +Get the default transformer configurations by |
| 7 | + |
| 8 | +<!-- @saveConfig @test --> |
| 9 | +``` |
| 10 | +kustomize config save -d ~/.kustomize/config |
| 11 | +``` |
| 12 | +The default configurations are save in directory `~/.kustomize/config` as several files |
| 13 | + |
| 14 | +> ``` |
| 15 | +> commonannotations.yaml commonlabels.yaml nameprefix.yaml namereference.yaml namespace.yaml varreference.yaml |
| 16 | +> ``` |
| 17 | +
|
| 18 | +### Add Config for a CRD |
| 19 | +All transformers will be involved for a CRD type. The default configurations already include some common fieldSpec for all types: |
| 20 | +
|
| 21 | +- nameprefix is added to `.metadata.name` |
| 22 | +- namespace is added to `.metadata.namespace` |
| 23 | +- labels is added to `.metadata.labels` |
| 24 | +- annotations is added to `.metadata.annotations` |
| 25 | +
|
| 26 | +Thus those fieldSpec don't need to be added to support a CRD type. |
| 27 | +Consider a CRD type `MyKind` with fields |
| 28 | +- `.spec.secretRef.name` reference a Secret |
| 29 | +- `.spec.beeRef.name` reference an instance of CRD `Bee` |
| 30 | +- `.spec.containers.commands` as the list of container commands |
| 31 | +- `.spec.selectors` as the label selectors |
| 32 | +
|
| 33 | +Add following file to configure the transformers for the above fields |
| 34 | +<!-- @addConfig @test --> |
| 35 | +``` |
| 36 | +cat > ~/.kustomize/config/mykind.yaml << EOF |
| 37 | +
|
| 38 | +commonLabels: |
| 39 | +- path: spec/selectors |
| 40 | + create: true |
| 41 | + kind: MyKind |
| 42 | + |
| 43 | +nameReference: |
| 44 | +- kind: Bee |
| 45 | + fieldSpecs: |
| 46 | + - path: spec/beeRef/name |
| 47 | + kind: MyKind |
| 48 | +- kind: Secret |
| 49 | + fieldSpecs: |
| 50 | + - path: spec/secretRef/name |
| 51 | + kind: MyKind |
| 52 | + |
| 53 | +varReference: |
| 54 | +- path: spec/containers/commands |
| 55 | + kind: MyKind |
| 56 | + |
| 57 | +EOF |
| 58 | +``` |
| 59 | +
|
| 60 | +### Apply config |
| 61 | +Create a kustomization with a `MyKind` instance. |
| 62 | +
|
| 63 | +<!-- @createKustomization @test --> |
| 64 | +``` |
| 65 | +DEMO_HOME=$(mktemp -d) |
| 66 | + |
| 67 | +cat > $DEMO_HOME/kustomization.yaml << EOF |
| 68 | +resources: |
| 69 | +- resources.yaml |
| 70 | + |
| 71 | +namePrefix: test- |
| 72 | +EOF |
| 73 | + |
| 74 | +cat > $DEMO_HOME/resources.yaml << EOF |
| 75 | +apiVersion: v1 |
| 76 | +kind: Secret |
| 77 | +metadata: |
| 78 | + name: crdsecret |
| 79 | +data: |
| 80 | + PATH: YmJiYmJiYmIK |
| 81 | +--- |
| 82 | +apiVersion: v1beta1 |
| 83 | +kind: Bee |
| 84 | +metadata: |
| 85 | + name: bee |
| 86 | +spec: |
| 87 | + action: fly |
| 88 | +--- |
| 89 | +apiVersion: jingfang.example.com/v1beta1 |
| 90 | +kind: MyKind |
| 91 | +metadata: |
| 92 | + name: mykind |
| 93 | +spec: |
| 94 | + secretRef: |
| 95 | + name: crdsecret |
| 96 | + beeRef: |
| 97 | + name: bee |
| 98 | +EOF |
| 99 | +``` |
| 100 | +
|
| 101 | +Run `kustomize build` with customized transformer configurations and verify that |
| 102 | +the namereference is correctly resolved. |
| 103 | +
|
| 104 | +<!-- @build @test --> |
| 105 | +``` |
| 106 | +test 2 == \ |
| 107 | +$(kustomize build $DEMO_HOME -t ~/.kustomize/config | grep -A 1 ".*Ref" | grep "test-" | wc -l); \ |
| 108 | +echo $? |
| 109 | +``` |
| 110 | +
|
| 111 | +To understand this better, compare the output using default transformer configurations. |
| 112 | +
|
| 113 | +<!-- @compareOutput --> |
| 114 | +``` |
| 115 | +diff \ |
| 116 | + <(kustomize build $DEMO_HOME) \ |
| 117 | + <(kustomize build $DEMO_HOME -t ~/.kustomize/config ) |\ |
| 118 | + more |
| 119 | +``` |
| 120 | +
|
| 121 | +The difference output should look something like |
| 122 | +> ``` |
| 123 | +> 14c14 |
| 124 | +> < name: bee |
| 125 | +> --- |
| 126 | +> > name: test-bee |
| 127 | +> 16c16 |
| 128 | +> < name: crdsecret |
| 129 | +> --- |
| 130 | +> > name: test-crdsecret |
| 131 | +> ``` |
| 132 | +
|
| 133 | +
|
0 commit comments