Debian Codegraph @

script/k3s-argo-workflow/run.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 CONTEXT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/"
20 REPO="${CONTEXT}../../"
21
22 K3S_VERSION=master
23 K3S_REPO=https://github.com/k3s-io/k3s
24 K3S_CONTAINERD_REPO=https://github.com/k3s-io/containerd
25
26 K3S_NODE_REPO=ghcr.io/stargz-containers
27 K3S_NODE_IMAGE_NAME=k3s
28 K3S_NODE_TAG=1
29 K3S_NODE_IMAGE="${K3S_NODE_REPO}/${K3S_NODE_IMAGE_NAME}:${K3S_NODE_TAG}"
30 K3S_CLUSTER_NAME="k3s-demo-cluster-$(date +%s%N | shasum | base64 | fold -w 10 | head -1)"
31
32 ORG_ARGOYAML=$(mktemp)
33 TMP_K3S_REPO=$(mktemp -d)
34 TMP_GOLANGCI=$(mktemp)
35 TMP_K3S_CONTAINERD_REPO=$(mktemp -d)
36 function cleanup {
37 ORG_EXIT_CODE="${1}"
38 rm "${ORG_ARGOYAML}" || true
39 rm -rf "${TMP_K3S_REPO}" || true
40 rm -rf "${TMP_K3S_CONTAINERD_REPO}"
41 exit "${ORG_EXIT_CODE}"
42 }
43 trap 'cleanup "$?"' EXIT SIGHUP SIGINT SIGQUIT SIGTERM
44
45 function argo_yaml() {
46 local IMAGE_TYPE="${1}"
47
48 local TMP_CUSTOM_ARGOYAML=$(mktemp)
49 cp "${ORG_ARGOYAML}" "${TMP_CUSTOM_ARGOYAML}"
50 sed -i 's|containerRuntimeExecutor: docker|containerRuntimeExecutor: pns|g' "${TMP_CUSTOM_ARGOYAML}"
51
52 cat "${TMP_CUSTOM_ARGOYAML}"
53
54 rm "${TMP_CUSTOM_ARGOYAML}"
55 }
56
57 function replace_image() {
58 local TARGET_FILE="${1}"
59 local IMAGE_ORG="${2}"
60 local IMAGE_NEW="${3}"
61 if ! cat "${TARGET_FILE}" | grep "${IMAGE_ORG}" > /dev/null 2>&1 ; then
62 echo "error: image ${IMAGE_ORG} not specified" 1>&2
63 cat "${TARGET_FILE}"
64 exit 1
65 fi
66 sed -i "s|${IMAGE_ORG}|${IMAGE_NEW}|g" "${TARGET_FILE}"
67 }
68
69 function go_ci_yaml() {
70 IMAGE_TYPE="${1}" envsubst < "${CONTEXT}/go.yaml.template"
71 }
72
73 function run {
74 local IMAGE_TYPE="${1}"
75 local SNAPSHOTTER="${2}"
76 local ELAPSED_RESULT_FILE="${3}"
77
78 # Prepare cluster configuration
79 local CUSTOM_ARGOYAML=$(mktemp)
80 argo_yaml "${IMAGE_TYPE}" > "${CUSTOM_ARGOYAML}"
81 local TMP_GOCI_YAML=$(mktemp)
82 go_ci_yaml "${IMAGE_TYPE}" > "${TMP_GOCI_YAML}"
83
84 # Create argo cluster
85 k3d cluster create "${K3S_CLUSTER_NAME}" --image="${K3S_NODE_IMAGE}" \
86 --k3s-arg='--snapshotter='"${SNAPSHOTTER}"'@server:*;agent:*'
87 kubectl create ns argo
88 kubectl apply -n argo -f "${CUSTOM_ARGOYAML}"
89
90 # Wait for the cluster is ready
91 local RETRYNUM=30
92 local RETRYINTERVAL=1
93 local TIMEOUTSEC=180
94 for i in $(seq ${RETRYNUM}) ; do
95 if [ $(kubectl get -n argo pods -o json | jq -r '.items[]' | wc -l) -ne 0 ] ; then
96 if [ $(kubectl get -n argo pods -o json | jq '.items[] | select(.status.phase != "Running" and .status.phase != "Succeeded")' | wc -l) -eq 0 ]
97 then
98 echo "argo is ready"
99 break
100 fi
101 fi
102 echo "Waiting for argo is ready..."
103 sleep ${RETRYINTERVAL}
104 done
105
106 # Run the workflow and get the elapsed time
107 argo submit -n argo --watch "${TMP_GOCI_YAML}"
108 local START=$(argo list -n argo --completed -o json | jq -r '.[0].status.startedAt')
109 local FINISH=$(argo list -n argo --completed -o json | jq -r '.[0].status.finishedAt')
110 local ELAPSED=$(expr $(date --date "${FINISH}" +%s) - $(date --date "${START}" +%s))
111 echo '{"type" : "'"${IMAGE_TYPE}"'", "snapshotter" : "'"${SNAPSHOTTER}"'", "elapsed" : "'"${ELAPSED}"'"}' | tee -a "${ELAPSED_RESULT_FILE}"
112
113 # Finalize
114 k3d cluster delete "${K3S_CLUSTER_NAME}"
115 rm "${CUSTOM_ARGOYAML}"
116 rm "${TMP_GOCI_YAML}"
117 }
118
119 RESULT_FILE="${RESULT:-}"
120 if [ "${RESULT_FILE}" == "" ] ; then
121 RESULT_FILE=$(mktemp)
122 fi
123 echo "result to ${RESULT_FILE}"
124
125 wget -O "${ORG_ARGOYAML}" https://raw.githubusercontent.com/argoproj/argo-workflows/v3.4.3/manifests/quick-start-minimal.yaml
126
127 git clone -b ${K3S_VERSION} --depth 1 "${K3S_REPO}" "${TMP_K3S_REPO}"
128 sed -i "s|github.com/k3s-io/stargz-snapshotter .*$|$(realpath ${REPO})|g" "${TMP_K3S_REPO}/go.mod"
129 echo "replace github.com/containerd/stargz-snapshotter/estargz => $(realpath ${REPO})/estargz" >> "${TMP_K3S_REPO}/go.mod"
130
131 # typeurl version stargz-snapshotter indirectly depends on is incompatible to the one github.com/k3s-io/containerd depends on.
132 # We use older version of typeurl which the both of the above are compatible to.
133 # We can remove this directive once k3s upgrades typeurl version to newer than v1.0.3-0.20220324183432-6193a0e03259.
134 echo "replace github.com/containerd/typeurl => github.com/containerd/typeurl v1.0.2" >> "${TMP_K3S_REPO}/go.mod"
135
136 cat "${TMP_K3S_REPO}/go.mod"
137
138 sed -i -E 's|(ENV DAPPER_RUN_ARGS .*)|\1 -v '"$(realpath ${REPO})":"$(realpath ${REPO})"':ro|g' "${TMP_K3S_REPO}/Dockerfile.dapper"
139 sed -i -E 's|(ENV DAPPER_ENV .*)|\1 DOCKER_BUILDKIT|g' "${TMP_K3S_REPO}/Dockerfile.dapper"
140 (
141 cd "${TMP_K3S_REPO}" && \
142 git config user.email "[email protected]" && \
143 git config user.name "dummy" && \
144 cat ./.golangci.json | jq '.run.deadline|="10m"' > "${TMP_GOLANGCI}" && \
145 cp "${TMP_GOLANGCI}" ./.golangci.json && \
146 go mod tidy && \
147 make deps && \
148 git add . && \
149 git commit -m tmp && \
150 REPO="${K3S_NODE_REPO}" IMAGE_NAME="${K3S_NODE_IMAGE_NAME}" TAG="${K3S_NODE_TAG}" SKIP_VALIDATE=1 make
151 )
152
153 #1
154 run "org" "overlayfs" "${RESULT_FILE}"
155 run "esgz" "stargz" "${RESULT_FILE}"
156
157 #2
158 run "org" "overlayfs" "${RESULT_FILE}"
159 run "esgz" "stargz" "${RESULT_FILE}"
160
161 #3
162 run "org" "overlayfs" "${RESULT_FILE}"
163 run "esgz" "stargz" "${RESULT_FILE}"