1
1
# ` kind ` on Google Cloud Build
2
2
3
+ This is very much a ** hack** . Don't use this for real, take inspiration from it
4
+ at max!
5
+
6
+ The biggest hacks here are:
7
+ - using ` awk ` to "parse" out the port that exposes the APIServer from an URL in
8
+ a yaml file
9
+ - the forwarding of the port from the host into the container where ` kind ` and
10
+ the actual test is running:
11
+ - inside the container, where ` kind ` and the actual test runs, we start a
12
+ ` socat ` process which listens on a tcp port
13
+ - when a connection to that port inside the container is made, we start
14
+ another container in the host's network namespace. That container is able
15
+ to connect to the exposed port on the host. Both ` socat ` processes are
16
+ connected via their Stdin & Stdout.
17
+ - we've essentially created a tunnel from within the container to the host
18
+ for this one port
19
+ - by default, ` kind ` exposes the APIServer on localhost (on the host), thus
20
+ the kubeconfig file will hold something like ` https://127.0.0.1:XXX/ ` as
21
+ the server URL. Thus if we use the same port inside the container that
22
+ ` kind ` has setup on the host, we can just use the same kubeconfig.
23
+
3
24
## Example ` cloudbuild.yml `
4
25
5
26
The following cloud build config can be used by running
@@ -15,12 +36,15 @@ and will:
15
36
- run ` kind ` to create a cluster
16
37
- start a portforwarder to make the kube-apiserver accessible
17
38
18
- There are two things to be configured via ` --substitutions ` :
19
- - ` KIND_CONFIG ` : a [ configuration file for kind] [ kind-config ] , it must specify an [ apiServerPort] [ api-server-port ] .
20
- - ` KIND_TESTS ` : whatever should be run after the cluster hast been created, ` KUBECONFIG ` is setup for you
39
+ There are two things which can be configured via ` --substitutions ` , none of
40
+ which is mandatory:
41
+ - ` KIND_CONFIG ` : a [ configuration for kind] [ kind-config ]
42
+ - ` KIND_TESTS ` : whatever should be run after the cluster hast been created,
43
+ ` KUBECONFIG ` is setup for you
44
+
45
+ Examples on what the substitutions could look like can be found in the following ` cloudbuild.yml ` .
21
46
22
47
[ kind-config ] : https://kind.sigs.k8s.io/docs/user/quick-start/#configuring-your-kind-cluster
23
- [ api-server-port ] : https://kind.sigs.k8s.io/docs/user/configuration/#api-server
24
48
25
49
``` yaml
26
50
steps :
@@ -69,12 +93,14 @@ steps:
69
93
70
94
kind "$${kindArgs[@]}"
71
95
72
- export KUBECONFIG="$( kind get kubeconfig-path )"
96
+ KUBECONFIG="$$(mktemp)"
97
+ kind get kubeconfig > "$$KUBECONFIG"
98
+ export KUBECONFIG
73
99
74
100
startForwarder() {
75
101
local port
76
102
# Gets the apiServerPort from the KUBECONFIG file.
77
- port="$( awk -F: '/apiServerPort :/{ print $2 }' "$$KUBECONFIG" )"
103
+ port="$( awk -F: '/server :/{ print $4 }' "$$KUBECONFIG" )"
78
104
socat \
79
105
TCP-LISTEN:${port},reuseaddr,fork \
80
106
"EXEC:docker run --rm -i --network=host alpine/socat 'STDIO TCP-CONNECT:localhost:${port}'"
@@ -84,6 +110,12 @@ steps:
84
110
bash -xeuc "${_KIND_TESTS}"
85
111
86
112
substitutions :
87
- _KIND_TESTS : ' kubectl get nodes -o wide'
88
- _KIND_CONFIG : ' '
113
+ _KIND_TESTS : |
114
+ kubectl get nodes -o wide
115
+ _KIND_CONFIG : |
116
+ kind: Cluster
117
+ apiVersion: kind.x-k8s.io/v1alpha4
118
+ nodes:
119
+ - role: control-plane
120
+ - role: worker
89
121
` ` `
0 commit comments