Debian Codegraph @

script/k3s/run-k3s.sh

raw
1 #!/bin/bash
2
3 # Copyright The containerd Authors.
4
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8
9 # http://www.apache.org/licenses/LICENSE-2.0
10
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 set -euo pipefail
18
19 K3S_VERSION=master
20 K3S_REPO=https://github.com/k3s-io/k3s
21 K3S_CONTAINERD_REPO=https://github.com/k3s-io/containerd
22
23 REGISTRY_HOST=k3s-private-registry
24 K3S_NODE_REPO=ghcr.io/stargz-containers
25 K3S_NODE_IMAGE_NAME=k3s
26 K3S_NODE_TAG=1
27 K3S_NODE_IMAGE="${K3S_NODE_REPO}/${K3S_NODE_IMAGE_NAME}:${K3S_NODE_TAG}"
28
29 # Arguments
30 K3S_CLUSTER_NAME="${1}"
31 K3S_USER_KUBECONFIG="${2}"
32 K3S_REGISTRY_CA="${3}"
33 REPO="${4}"
34 REGISTRY_NETWORK="${5}"
35 DOCKERCONFIGJSON_DATA="${6}"
36
37 TMP_BUILTIN_CONF=$(mktemp)
38 TMP_CONTEXT=$(mktemp -d)
39 SN_KUBECONFIG=$(mktemp)
40 TMP_K3S_REPO=$(mktemp -d)
41 TMP_K3S_CONTAINERD_REPO=$(mktemp -d)
42 TMP_GOLANGCI=$(mktemp)
43 function cleanup {
44 local ORG_EXIT_CODE="${1}"
45 rm "${SN_KUBECONFIG}"
46 rm -rf "${TMP_CONTEXT}"
47 rm -rf "${TMP_BUILTIN_CONF}"
48 rm -rf "${TMP_K3S_REPO}"
49 rm -rf "${TMP_K3S_CONTAINERD_REPO}"
50 rm "${TMP_GOLANGCI}"
51 exit "${ORG_EXIT_CODE}"
52 }
53 trap 'cleanup "$?"' EXIT SIGHUP SIGINT SIGQUIT SIGTERM
54
55 echo "Preparing node image..."
56 git clone -b ${K3S_VERSION} --depth 1 "${K3S_REPO}" "${TMP_K3S_REPO}"
57 sed -i "s|github.com/k3s-io/stargz-snapshotter .*$|$(realpath ${REPO})|g" "${TMP_K3S_REPO}/go.mod"
58 echo "replace github.com/containerd/stargz-snapshotter/estargz => $(realpath ${REPO})/estargz" >> "${TMP_K3S_REPO}/go.mod"
59
60 # typeurl version stargz-snapshotter indirectly depends on is incompatible to the one github.com/k3s-io/containerd depends on.
61 # We use older version of typeurl which the both of the above are compatible to.
62 # We can remove this directive once k3s upgrades typeurl version to newer than v1.0.3-0.20220324183432-6193a0e03259.
63 echo "replace github.com/containerd/typeurl => github.com/containerd/typeurl v1.0.2" >> "${TMP_K3S_REPO}/go.mod"
64
65 cat "${TMP_K3S_REPO}/go.mod"
66
67 sed -i -E 's|(ENV DAPPER_RUN_ARGS .*)|\1 -v '"$(realpath ${REPO})":"$(realpath ${REPO})"':ro|g' "${TMP_K3S_REPO}/Dockerfile.dapper"
68 sed -i -E 's|(ENV DAPPER_ENV .*)|\1 DOCKER_BUILDKIT|g' "${TMP_K3S_REPO}/Dockerfile.dapper"
69 (
70 cd "${TMP_K3S_REPO}" && \
71 git config user.email "[email protected]" && \
72 git config user.name "dummy" && \
73 cat ./.golangci.json | jq '.run.deadline|="10m"' > "${TMP_GOLANGCI}" && \
74 cp "${TMP_GOLANGCI}" ./.golangci.json && \
75 go mod tidy && \
76 make deps && \
77 git add . && \
78 git commit -m tmp && \
79 REPO="${K3S_NODE_REPO}" IMAGE_NAME="${K3S_NODE_IMAGE_NAME}" TAG="${K3S_NODE_TAG}" SKIP_VALIDATE=1 make
80 )
81 cat <<EOF > "${TMP_BUILTIN_CONF}"
82 configs:
83 ${REGISTRY_HOST}:5000:
84 tls:
85 ca_file: /registry.crt
86 EOF
87
88 echo "Createing k3s cluster"
89 k3d cluster create "${K3S_CLUSTER_NAME}" --image="${K3S_NODE_IMAGE}" \
90 --registry-config="${TMP_BUILTIN_CONF}" -v "${K3S_REGISTRY_CA}":/registry.crt:ro \
91 --k3s-arg='--snapshotter=stargz@server:*;agent:*'
92 k3d kubeconfig get "${K3S_CLUSTER_NAME}" > "${K3S_USER_KUBECONFIG}"
93 K3S_NODENAME="$(k3d node list | grep ${K3S_CLUSTER_NAME}-server-0 | cut -d " " -f 1 | tr -d '\n')"
94 docker network connect "${REGISTRY_NETWORK}" "${K3S_NODENAME}"
95
96 echo "Configuring kubernetes cluster..."
97 CONFIGJSON_BASE64="$(cat ${DOCKERCONFIGJSON_DATA} | base64 -i -w 0)"
98 cat <<EOF | KUBECONFIG="${K3S_USER_KUBECONFIG}" kubectl apply -f -
99 apiVersion: v1
100 kind: Namespace
101 metadata:
102 name: ns1
103 ---
104 apiVersion: v1
105 kind: Secret
106 metadata:
107 name: testsecret
108 namespace: ns1
109 data:
110 .dockerconfigjson: ${CONFIGJSON_BASE64}
111 type: kubernetes.io/dockerconfigjson
112 EOF