| 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 |
NODE_IMAGE_NAME="cri-stargz-snapshotter-node:1" |
| 20 |
NODE_BASE_IMAGE_NAME="cri-stargz-snapshotter-node-base:1" |
| 21 |
NODE_TEST_CERT_FILE="/usr/local/share/ca-certificates/registry.crt" |
| 22 |
REGISTRY_HOST=kind-cri-private-registry |
| 23 |
|
| 24 |
# Arguments |
| 25 |
KIND_CLUSTER_NAME="${1}" |
| 26 |
KIND_USER_KUBECONFIG="${2}" |
| 27 |
KIND_REGISTRY_CA="${3}" |
| 28 |
REPO="${4}" |
| 29 |
REGISTRY_NETWORK="${5}" |
| 30 |
DOCKERCONFIGJSON_DATA="${6}" |
| 31 |
|
| 32 |
TMP_BUILTIN_CONF=$(mktemp) |
| 33 |
TMP_CONTEXT=$(mktemp -d) |
| 34 |
SN_KUBECONFIG=$(mktemp) |
| 35 |
function cleanup { |
| 36 |
local ORG_EXIT_CODE="${1}" |
| 37 |
rm "${SN_KUBECONFIG}" |
| 38 |
rm -rf "${TMP_CONTEXT}" |
| 39 |
rm -rf "${TMP_BUILTIN_CONF}" |
| 40 |
exit "${ORG_EXIT_CODE}" |
| 41 |
} |
| 42 |
trap 'cleanup "$?"' EXIT SIGHUP SIGINT SIGQUIT SIGTERM |
| 43 |
|
| 44 |
if [ "${KIND_NO_RECREATE:-}" != "true" ] ; then |
| 45 |
echo "Preparing node image..." |
| 46 |
|
| 47 |
TARGET_STAGE= |
| 48 |
if [ "${BUILTIN_SNAPSHOTTER:-}" == "true" ] ; then |
| 49 |
TARGET_STAGE="--target kind-builtin-snapshotter" |
| 50 |
fi |
| 51 |
|
| 52 |
docker build ${DOCKER_BUILD_ARGS:-} -t "${NODE_BASE_IMAGE_NAME}" ${TARGET_STAGE} "${REPO}" |
| 53 |
fi |
| 54 |
|
| 55 |
# Prepare the testing node with enabling k8s keychain |
| 56 |
cat <<EOF > "${TMP_CONTEXT}/config.containerd.append.toml" |
| 57 |
[plugins."io.containerd.grpc.v1.cri".registry.configs."${REGISTRY_HOST}:5000".tls] |
| 58 |
ca_file = "${NODE_TEST_CERT_FILE}" |
| 59 |
EOF |
| 60 |
BUILTIN_HACK_INST= |
| 61 |
if [ "${BUILTIN_SNAPSHOTTER:-}" == "true" ] ; then |
| 62 |
# Special configuration for CRI containerd + builtin stargz snapshotter |
| 63 |
cat <<EOF > "${TMP_CONTEXT}/containerd.hack.toml" |
| 64 |
version = 2 |
| 65 |
|
| 66 |
[debug] |
| 67 |
format = "json" |
| 68 |
level = "debug" |
| 69 |
[plugins."io.containerd.grpc.v1.cri".containerd] |
| 70 |
default_runtime_name = "runc" |
| 71 |
snapshotter = "stargz" |
| 72 |
disable_snapshot_annotations = false |
| 73 |
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc] |
| 74 |
runtime_type = "io.containerd.runc.v2" |
| 75 |
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.test-handler] |
| 76 |
runtime_type = "io.containerd.runc.v2" |
| 77 |
[plugins."io.containerd.grpc.v1.cri".registry.configs."${REGISTRY_HOST}:5000".tls] |
| 78 |
ca_file = "${NODE_TEST_CERT_FILE}" |
| 79 |
[plugins."io.containerd.snapshotter.v1.stargz"] |
| 80 |
cri_keychain_image_service_path = "/run/containerd-stargz-grpc/containerd-stargz-grpc.sock" |
| 81 |
[plugins."io.containerd.snapshotter.v1.stargz".cri_keychain] |
| 82 |
enable_keychain = true |
| 83 |
[plugins."io.containerd.snapshotter.v1.stargz".registry.configs."${REGISTRY_HOST}:5000".tls] |
| 84 |
ca_file = "${NODE_TEST_CERT_FILE}" |
| 85 |
EOF |
| 86 |
BUILTIN_HACK_INST="COPY containerd.hack.toml /etc/containerd/config.toml" |
| 87 |
fi |
| 88 |
cp "${KIND_REGISTRY_CA}" "${TMP_CONTEXT}/registry.crt" |
| 89 |
cat <<EOF > "${TMP_CONTEXT}/Dockerfile" |
| 90 |
FROM ${NODE_BASE_IMAGE_NAME} |
| 91 |
|
| 92 |
COPY registry.crt "${NODE_TEST_CERT_FILE}" |
| 93 |
COPY ./config.containerd.append.toml /tmp/ |
| 94 |
RUN cat /tmp/config.containerd.append.toml >> /etc/containerd/config.toml && \ |
| 95 |
update-ca-certificates |
| 96 |
|
| 97 |
${BUILTIN_HACK_INST} |
| 98 |
|
| 99 |
EOF |
| 100 |
docker build -t "${NODE_IMAGE_NAME}" ${DOCKER_BUILD_ARGS:-} "${TMP_CONTEXT}" |
| 101 |
|
| 102 |
# cluster must be single node |
| 103 |
echo "Cleating kind cluster and connecting to the registry network..." |
| 104 |
kind create cluster --name "${KIND_CLUSTER_NAME}" \ |
| 105 |
--kubeconfig "${KIND_USER_KUBECONFIG}" \ |
| 106 |
--image "${NODE_IMAGE_NAME}" |
| 107 |
KIND_NODENAME=$(kind get nodes --name "${KIND_CLUSTER_NAME}" | sed -n 1p) # must be single node |
| 108 |
docker network connect "${REGISTRY_NETWORK}" "${KIND_NODENAME}" |
| 109 |
|
| 110 |
echo "===== VERSION INFORMATION =====" |
| 111 |
docker exec "${KIND_NODENAME}" containerd --version |
| 112 |
docker exec "${KIND_NODENAME}" runc --version |
| 113 |
echo "===============================" |
| 114 |
|
| 115 |
echo "Configuring kubernetes cluster..." |
| 116 |
CONFIGJSON_BASE64="$(cat ${DOCKERCONFIGJSON_DATA} | base64 -i -w 0)" |
| 117 |
cat <<EOF | KUBECONFIG="${KIND_USER_KUBECONFIG}" kubectl apply -f - |
| 118 |
apiVersion: v1 |
| 119 |
kind: Namespace |
| 120 |
metadata: |
| 121 |
name: ns1 |
| 122 |
--- |
| 123 |
apiVersion: v1 |
| 124 |
kind: Secret |
| 125 |
metadata: |
| 126 |
name: testsecret |
| 127 |
namespace: ns1 |
| 128 |
data: |
| 129 |
.dockerconfigjson: ${CONFIGJSON_BASE64} |
| 130 |
type: kubernetes.io/dockerconfigjson |
| 131 |
EOF |