11# ` kind ` on Google Cloud Build
22
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+
324## Example ` cloudbuild.yml `
425
526The following cloud build config can be used by running
@@ -15,12 +36,15 @@ and will:
1536- run ` kind ` to create a cluster
1637- start a portforwarder to make the kube-apiserver accessible
1738
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 ` .
2146
2247[ 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
2448
2549``` yaml
2650steps :
@@ -69,12 +93,14 @@ steps:
6993
7094 kind "$${kindArgs[@]}"
7195
72- export KUBECONFIG="$( kind get kubeconfig-path )"
96+ KUBECONFIG="$$(mktemp)"
97+ kind get kubeconfig > "$$KUBECONFIG"
98+ export KUBECONFIG
7399
74100 startForwarder() {
75101 local port
76102 # Gets the apiServerPort from the KUBECONFIG file.
77- port="$( awk -F: '/apiServerPort :/{ print $2 }' "$$KUBECONFIG" )"
103+ port="$( awk -F: '/server :/{ print $4 }' "$$KUBECONFIG" )"
78104 socat \
79105 TCP-LISTEN:${port},reuseaddr,fork \
80106 "EXEC:docker run --rm -i --network=host alpine/socat 'STDIO TCP-CONNECT:localhost:${port}'"
@@ -84,6 +110,12 @@ steps:
84110 bash -xeuc "${_KIND_TESTS}"
85111
86112substitutions :
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
89121` ` `
0 commit comments