|
| 1 | +# Demo: multi namespaces with a common base |
| 2 | + |
| 3 | +`kustomize` supports defining multiple variants with different namespace, as overlays on a common base. |
| 4 | + |
| 5 | +It's possible to create an additional overlay to compose these variants together - just declare the overlays as the bases of a new kustomization. The following demonstrates this using a base that's just one pod. |
| 6 | + |
| 7 | +Define a place to work: |
| 8 | + |
| 9 | +<!-- @makeWorkplace @test --> |
| 10 | +``` |
| 11 | +DEMO_HOME=$(mktemp -d) |
| 12 | +``` |
| 13 | + |
| 14 | +Define a common base: |
| 15 | +<!-- @makeBase @test --> |
| 16 | +``` |
| 17 | +BASE=$DEMO_HOME/base |
| 18 | +mkdir $BASE |
| 19 | +
|
| 20 | +cat <<EOF >$BASE/kustomization.yaml |
| 21 | +resources: |
| 22 | +- pod.yaml |
| 23 | +EOF |
| 24 | +
|
| 25 | +cat <<EOF >$BASE/pod.yaml |
| 26 | +apiVersion: v1 |
| 27 | +kind: Pod |
| 28 | +metadata: |
| 29 | + name: myapp-pod |
| 30 | + labels: |
| 31 | + app: myapp |
| 32 | +spec: |
| 33 | + containers: |
| 34 | + - name: nginx |
| 35 | + image: nginx:1.7.9 |
| 36 | +EOF |
| 37 | +``` |
| 38 | + |
| 39 | +Define a variant in namespace-a overlaying base: |
| 40 | +<!-- @makeNamespaceA @test --> |
| 41 | +``` |
| 42 | +NSA=$DEMO_HOME/namespace-a |
| 43 | +mkdir $NSA |
| 44 | +
|
| 45 | +cat <<EOF >$NSA/kustomization.yaml |
| 46 | +bases: |
| 47 | +- ./../base |
| 48 | +resources: |
| 49 | +- namespace.yaml |
| 50 | +namespace: namespace-a |
| 51 | +EOF |
| 52 | +
|
| 53 | +cat <<EOF >$NSA/namespace.yaml |
| 54 | +apiVersion: v1 |
| 55 | +kind: Namespace |
| 56 | +metadata: |
| 57 | + name: namespace-a |
| 58 | +EOF |
| 59 | +``` |
| 60 | + |
| 61 | +Define a variant in namespace-b overlaying base: |
| 62 | +<!-- @makeNamespaceB @test --> |
| 63 | +``` |
| 64 | +NSB=$DEMO_HOME/namespace-b |
| 65 | +mkdir $NSB |
| 66 | +
|
| 67 | +cat <<EOF >$NSB/kustomization.yaml |
| 68 | +bases: |
| 69 | +- ./../base |
| 70 | +resources: |
| 71 | +- namespace.yaml |
| 72 | +namespace: namespace-b |
| 73 | +EOF |
| 74 | +
|
| 75 | +cat <<EOF >$NSA/namespace.yaml |
| 76 | +apiVersion: v1 |
| 77 | +kind: Namespace |
| 78 | +metadata: |
| 79 | + name: namespace-b |
| 80 | +EOF |
| 81 | +``` |
| 82 | + |
| 83 | +Then define a _Kustomization_ composing two variants together: |
| 84 | +<!-- @makeTopLayer @test --> |
| 85 | +``` |
| 86 | +cat <<EOF >$DEMO_HOME/kustomization.yaml |
| 87 | +bases: |
| 88 | +- ./namespace-a |
| 89 | +- ./namespace-b |
| 90 | +EOF |
| 91 | +``` |
| 92 | + |
| 93 | +Now the workspace has following directories |
| 94 | +> ``` |
| 95 | +> . |
| 96 | +> ├── base |
| 97 | +> │ ├── kustomization.yaml |
| 98 | +> │ └── pod.yaml |
| 99 | +> ├── kustomization.yaml |
| 100 | +> ├── namespace-a |
| 101 | +> │ ├── kustomization.yaml |
| 102 | +> │ └── namespace.yaml |
| 103 | +> └── namespace-b |
| 104 | +> ├── kustomization.yaml |
| 105 | +> └── namespace.yaml |
| 106 | +> ``` |
| 107 | +
|
| 108 | +Confirm that the `kustomize build` output contains two pod objects from namespace-a and namespace-b. |
| 109 | +
|
| 110 | +<!-- @confirmVariants @test --> |
| 111 | +``` |
| 112 | +test 2 == \ |
| 113 | + $(kustomize build $DEMO_HOME| grep -B 4 "namespace: namespace-[ab]" | grep "name: myapp-pod" | wc -l); \ |
| 114 | + echo $? |
| 115 | +``` |
0 commit comments