Skip to content

Commit d0c91b1

Browse files
committed
Adds e2e test for init command
1 parent 115a3d0 commit d0c91b1

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

examples/alphaTestExamples/init.md

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
[kind]: https://github.com/kubernetes-sigs/kind
2+
3+
# Demo: Init Command
4+
5+
This demo shows how the kapply init command works.
6+
7+
First define a place to work:
8+
9+
<!-- @makeWorkplace @testE2EAgainstLatestRelease -->
10+
```
11+
DEMO_HOME=$(mktemp -d)
12+
```
13+
14+
Alternatively, use
15+
16+
> ```
17+
> DEMO_HOME=~/demo
18+
> ```
19+
20+
## Establish the base
21+
22+
<!-- @createBase @testE2EAgainstLatestRelease -->
23+
```
24+
BASE=$DEMO_HOME/base
25+
mkdir -p $BASE
26+
OUTPUT=$DEMO_HOME/output
27+
mkdir -p $OUTPUT
28+
29+
function expectedOutputLine() {
30+
test 1 == \
31+
$(grep "$@" $OUTPUT/status | wc -l); \
32+
echo $?
33+
}
34+
```
35+
36+
## Create the first "app"
37+
38+
Create the config yaml for three config maps: (cm-a, cm-b, cm-c).
39+
40+
<!-- @createFirstConfigMaps @testE2EAgainstLatestRelease-->
41+
```
42+
cat <<EOF >$BASE/namespace.yaml
43+
apiVersion: v1
44+
kind: Namespace
45+
metadata:
46+
name: test-namespace
47+
EOF
48+
49+
cat <<EOF >$BASE/config-map-a.yaml
50+
apiVersion: v1
51+
kind: ConfigMap
52+
metadata:
53+
name: cm-a
54+
namespace: test-namespace
55+
labels:
56+
name: test-config-map-label
57+
EOF
58+
59+
cat <<EOF >$BASE/config-map-b.yaml
60+
apiVersion: v1
61+
kind: ConfigMap
62+
metadata:
63+
name: cm-b
64+
namespace: test-namespace
65+
labels:
66+
name: test-config-map-label
67+
EOF
68+
69+
cat <<EOF >$BASE/config-map-c.yaml
70+
apiVersion: v1
71+
kind: ConfigMap
72+
metadata:
73+
name: cm-c
74+
namespace: test-namespace
75+
labels:
76+
name: test-config-map-label
77+
EOF
78+
```
79+
80+
## Run end-to-end tests
81+
82+
The following requires installation of [kind].
83+
84+
Delete any existing kind cluster and create a new one. By default the name of the cluster is "kind".
85+
86+
<!-- @deleteAndCreateKindCluster @testE2EAgainstLatestRelease -->
87+
```
88+
kind delete cluster
89+
kind create cluster
90+
```
91+
92+
Use the kapply init command to generate the inventory template. This contains
93+
the namespace and inventory id used by apply to create inventory objects.
94+
<!-- @createInventoryTemplate @testE2EAgainstLatestRelease-->
95+
```
96+
kapply init $BASE > $OUTPUT/status
97+
expectedOutputLine "namespace: test-namespace is used for inventory object"
98+
```
99+
100+
Add another ConfigMap (cm-d) which is in the default namespace. The init
101+
command should calculate the namespace to be default, since not all
102+
objects are in the test-namespace.
103+
104+
<!-- @updateApp @testE2EAgainstLatestRelease -->
105+
```
106+
107+
cat <<EOF >$BASE/config-map-d.yaml
108+
apiVersion: v1
109+
kind: ConfigMap
110+
metadata:
111+
name: cm-d
112+
labels:
113+
name: test-config-map-label
114+
EOF
115+
116+
# Remove the initial inventory template.
117+
rm -f $BASE/inventory-template.yaml
118+
119+
kapply init $BASE > $OUTPUT/status
120+
expectedOutputLine "namespace: default is used for inventory object"
121+
```
122+
123+
Remove the ConfigMap (cm-d) which is in the default namespace, and
124+
add a cluster-scoped object. This cluster-scoped object should not
125+
be used in the init namespace calculations, so we should calculate the
126+
namespace as test-namespace.
127+
128+
<!-- @updateAppAgain @testE2EAgainstLatestRelease -->
129+
```
130+
131+
# Remove the initial inventory template.
132+
rm -f $BASE/inventory-template.yaml
133+
134+
# Remove the ConfigMap in the default namespace.
135+
rm -f $BASE/config-map-d.yaml
136+
137+
# Add cluster-scoped resource--cluster-role
138+
cat <<EOF >$BASE/cluster-role.yaml
139+
apiVersion: rbac.authorization.k8s.io/v1
140+
kind: ClusterRole
141+
metadata:
142+
# "namespace" omitted since ClusterRoles are not namespaced
143+
name: secret-reader
144+
rules:
145+
- apiGroups: [""]
146+
#
147+
# at the HTTP level, the name of the resource for accessing Secret
148+
# objects is "secrets"
149+
resources: ["secrets"]
150+
verbs: ["get", "watch", "list"]
151+
EOF
152+
153+
kapply init $BASE > $OUTPUT/status
154+
expectedOutputLine "namespace: test-namespace is used for inventory object"
155+
```
156+

pkg/config/initoptions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ func allInSameNamespace(packageDir string) (string, bool, error) {
157157
// Skip found cluster-scoped resources. If not found, just assume namespaced.
158158
namespaced, found := openapi.IsNamespaceScoped(rm.TypeMeta)
159159
if found && !namespaced {
160+
klog.V(6).Infof("cluster-scoped resource %s--skip namespace calc", rm.TypeMeta)
160161
continue
161162
}
162163
if rm.Namespace == "" {

0 commit comments

Comments
 (0)