diff --git a/examples/multibases/README.md b/examples/multibases/README.md index 86a8aa0b9..29d06ecb9 100644 --- a/examples/multibases/README.md +++ b/examples/multibases/README.md @@ -123,4 +123,5 @@ test 1 == \ $(kustomize build $DEMO_HOME | grep cluster-a-prod-myapp-pod | wc -l); \ echo $? ``` - +Similarly to adding different `namePrefix` in different variants, one can also add different `namespace` and compose those variants in +one _kustomization_. For more details, take a look at [multi-namespaces](multi-namespace.md). diff --git a/examples/multibases/multi-namespace.md b/examples/multibases/multi-namespace.md new file mode 100644 index 000000000..a7435a797 --- /dev/null +++ b/examples/multibases/multi-namespace.md @@ -0,0 +1,115 @@ +# Demo: multi namespaces with a common base + +`kustomize` supports defining multiple variants with different namespace, as overlays on a common base. + +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. + +Define a place to work: + + +``` +DEMO_HOME=$(mktemp -d) +``` + +Define a common base: + +``` +BASE=$DEMO_HOME/base +mkdir $BASE + +cat <$BASE/kustomization.yaml +resources: +- pod.yaml +EOF + +cat <$BASE/pod.yaml +apiVersion: v1 +kind: Pod +metadata: + name: myapp-pod + labels: + app: myapp +spec: + containers: + - name: nginx + image: nginx:1.7.9 +EOF +``` + +Define a variant in namespace-a overlaying base: + +``` +NSA=$DEMO_HOME/namespace-a +mkdir $NSA + +cat <$NSA/kustomization.yaml +bases: +- ./../base +resources: +- namespace.yaml +namespace: namespace-a +EOF + +cat <$NSA/namespace.yaml +apiVersion: v1 +kind: Namespace +metadata: + name: namespace-a +EOF +``` + +Define a variant in namespace-b overlaying base: + +``` +NSB=$DEMO_HOME/namespace-b +mkdir $NSB + +cat <$NSB/kustomization.yaml +bases: +- ./../base +resources: +- namespace.yaml +namespace: namespace-b +EOF + +cat <$NSA/namespace.yaml +apiVersion: v1 +kind: Namespace +metadata: + name: namespace-b +EOF +``` + +Then define a _Kustomization_ composing two variants together: + +``` +cat <$DEMO_HOME/kustomization.yaml +bases: +- ./namespace-a +- ./namespace-b +EOF +``` + +Now the workspace has following directories +> ``` +> . +> ├── base +> │   ├── kustomization.yaml +> │   └── pod.yaml +> ├── kustomization.yaml +> ├── namespace-a +> │   ├── kustomization.yaml +> │   └── namespace.yaml +> └── namespace-b +> ├── kustomization.yaml +> └── namespace.yaml +> ``` + +Confirm that the `kustomize build` output contains two pod objects from namespace-a and namespace-b. + + +``` +test 2 == \ + $(kustomize build $DEMO_HOME| grep -B 4 "namespace: namespace-[ab]" | grep "name: myapp-pod" | wc -l); \ + echo $? +``` \ No newline at end of file