...
1[kind]: https://github.com/kubernetes-sigs/kind
2
3# Demo: Namespaces and Prune
4
5This demo shows that namespaces will **not** be pruned if
6there are objects remaining in them. The namespace for the
7inventory object will be the default namespace, since not
8all of the app objects are in the same namespace (pod-b
9is in the default namespace). When pod-a, pod-b, and
10the test-namespace are omitted from the subsequent apply
11the test-namespace will be considered for pruning, but it
12will **not** happend because there is still one object
13in the namespace--pod-c.
14
15First define a place to work:
16
17<!-- @makeWorkplace @testE2EAgainstLatestRelease -->
18```
19DEMO_HOME=$(mktemp -d)
20```
21
22Alternatively, use
23
24> ```
25> DEMO_HOME=~/demo
26> ```
27
28## Establish the base
29
30<!-- @createBase @testE2EAgainstLatestRelease -->
31```
32BASE=$DEMO_HOME/base
33mkdir -p $BASE
34OUTPUT=$DEMO_HOME/output
35mkdir -p $OUTPUT
36GREEN='\033[0;32m'
37RED='\033[0;31m'
38NC='\033[0m' # No Color
39
40function expectedOutputLine() {
41 if ! grep -q "$@" "$OUTPUT/status"; then
42 echo -e "${RED}Error: output line not found${NC}"
43 echo -e "${RED}Expected: $@${NC}"
44 exit 1
45 else
46 echo -e "${GREEN}Success: output line found${NC}"
47 fi
48}
49```
50
51## Create the first "app"
52
53Create the config yaml for three config maps: (cm-a, cm-b, cm-c).
54
55<!-- @createFirstConfigMaps @testE2EAgainstLatestRelease-->
56```
57cat <<EOF >$BASE/namespace.yaml
58apiVersion: v1
59kind: Namespace
60metadata:
61 name: test-namespace
62EOF
63
64cat <<EOF >$BASE/config-map-a.yaml
65apiVersion: v1
66kind: ConfigMap
67metadata:
68 name: cm-a
69 namespace: test-namespace
70 labels:
71 name: test-config-map-label
72EOF
73
74cat <<EOF >$BASE/config-map-b.yaml
75apiVersion: v1
76kind: ConfigMap
77metadata:
78 name: cm-b
79 labels:
80 name: test-config-map-label
81EOF
82
83cat <<EOF >$BASE/config-map-c.yaml
84apiVersion: v1
85kind: ConfigMap
86metadata:
87 name: cm-c
88 namespace: test-namespace
89 labels:
90 name: test-config-map-label
91EOF
92```
93
94## Run end-to-end tests
95
96The following requires installation of [kind].
97
98Delete any existing kind cluster and create a new one. By default the name of the cluster is "kind".
99
100<!-- @deleteAndCreateKindCluster @testE2EAgainstLatestRelease -->
101```
102kind delete cluster
103kind create cluster
104```
105
106Use the kapply init command to generate the inventory template. This contains
107the namespace and inventory id used by apply to create inventory objects.
108<!-- @createInventoryTemplate @testE2EAgainstLatestRelease-->
109```
110kapply init $BASE | tee $OUTPUT/status
111expectedOutputLine "namespace: default is used for inventory object"
112```
113
114Apply the "app" to the cluster. All the config maps should be created, and
115no resources should be pruned.
116<!-- @runApply @testE2EAgainstLatestRelease -->
117```
118kapply apply $BASE --reconcile-timeout=1m | tee $OUTPUT/status
119expectedOutputLine "namespace/test-namespace apply successful"
120expectedOutputLine "configmap/cm-a apply successful"
121expectedOutputLine "configmap/cm-b apply successful"
122expectedOutputLine "configmap/cm-c apply successful"
123expectedOutputLine "apply result: 4 attempted, 4 successful, 0 skipped, 0 failed"
124expectedOutputLine "reconcile result: 4 attempted, 4 successful, 0 skipped, 0 failed, 0 timed out"
125
126# There should be only one inventory object
127kubectl get cm --selector='cli-utils.sigs.k8s.io/inventory-id' --no-headers | wc -l | tee $OUTPUT/status
128expectedOutputLine "1"
129# Capture the inventory object name for later testing
130invName=$(kubectl get cm --selector='cli-utils.sigs.k8s.io/inventory-id' --no-headers | awk '{print $1}')
131# There should be four config maps: one inventory in default, two in test-namespace, one in default namespace
132kubectl get cm --selector='cli-utils.sigs.k8s.io/inventory-id' --no-headers | wc -l | tee $OUTPUT/status
133expectedOutputLine "1"
134kubectl get cm -n test-namespace --selector='name=test-config-map-label' --no-headers | wc -l | tee $OUTPUT/status
135expectedOutputLine "2"
136kubectl get cm --selector='name=test-config-map-label' --no-headers | wc -l | tee $OUTPUT/status
137expectedOutputLine "1"
138# ConfigMap cm-a had been created in the cluster
139kubectl get configmap/cm-a -n test-namespace --no-headers | wc -l | tee $OUTPUT/status
140expectedOutputLine "1"
141# ConfigMap cm-b had been created in the cluster
142kubectl get configmap/cm-b --no-headers | wc -l | tee $OUTPUT/status
143expectedOutputLine "1"
144# ConfigMap cm-c had been created in the cluster
145kubectl get configmap/cm-c -n test-namespace --no-headers | wc -l | tee $OUTPUT/status
146expectedOutputLine "1"
147```
148
149## Update the "app" to remove a two of the config maps, and the
150namespace.
151
152Remove test-namespace
153Remove cm-a
154Remove cm-b
155
156<!-- @createAnotherConfigMap @testE2EAgainstLatestRelease -->
157```
158
159rm -f $BASE/namespace.yaml
160rm -f $BASE/config-map-a.yaml
161rm -f $BASE/config-map-b.yaml
162
163```
164
165## Apply the updated "app"
166
167cm-a should be pruned (since it has been deleted locally).
168cm-b should be pruned (since it has been deleted locally).
169test-namespace should **not** be pruned.
170
171<!-- @applySecondTime @testE2EAgainstLatestRelease -->
172```
173kapply apply $BASE --reconcile-timeout=1m | tee $OUTPUT/status
174
175expectedOutputLine "configmap/cm-c apply skipped: dependency scheduled for delete: _test-namespace__Namespace"
176expectedOutputLine "configmap/cm-c reconcile skipped"
177
178expectedOutputLine "configmap/cm-a prune successful"
179expectedOutputLine "configmap/cm-b prune successful"
180expectedOutputLine "namespace/test-namespace prune skipped: namespace still in use: test-namespace"
181
182expectedOutputLine "namespace/test-namespace reconcile skipped"
183expectedOutputLine "configmap/cm-a reconcile successful"
184expectedOutputLine "configmap/cm-b reconcile successful"
185expectedOutputLine "configmap/cm-c reconcile skipped"
186
187expectedOutputLine "apply result: 1 attempted, 0 successful, 1 skipped, 0 failed"
188expectedOutputLine "prune result: 3 attempted, 2 successful, 1 skipped, 0 failed"
189expectedOutputLine "reconcile result: 4 attempted, 2 successful, 2 skipped, 0 failed, 0 timed out"
190
191# The test-namespace should not be pruned.
192kubectl get ns test-namespace --no-headers | wc -l | tee $OUTPUT/status
193expectedOutputLine "1"
194# Inventory object should have two items: namespace and cm-c.
195kubectl get cm --selector='cli-utils.sigs.k8s.io/inventory-id' --no-headers | awk '{print $2}' | tee $OUTPUT/status
196expectedOutputLine "2"
197# The inventory object should have the same name
198kubectl get configmap/${invName} --no-headers | tee $OUTPUT/status
199expectedOutputLine "${invName}"
200# ConfigMap cm-c remains in the cluster.
201kubectl get configmap/cm-c -n test-namespace --no-headers | wc -l | tee $OUTPUT/status
202expectedOutputLine "1"
203```
View as plain text