| 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 |
REMOTE_SNAPSHOT_LABEL="containerd.io/snapshot/remote" |
| 20 |
TEST_POD_NAME=testpod-$(head /dev/urandom | tr -dc a-z0-9 | head -c 10) |
| 21 |
TEST_POD_NS=ns1 |
| 22 |
TEST_CONTAINER_NAME=testcontainer-$(head /dev/urandom | tr -dc a-z0-9 | head -c 10) |
| 23 |
|
| 24 |
K3S_NODENAME="${1}" |
| 25 |
K3S_KUBECONFIG="${2}" |
| 26 |
TESTIMAGE="${3}" |
| 27 |
|
| 28 |
echo "Creating testing pod...." |
| 29 |
cat <<EOF | KUBECONFIG="${K3S_KUBECONFIG}" kubectl apply -f - |
| 30 |
apiVersion: v1 |
| 31 |
kind: Pod |
| 32 |
metadata: |
| 33 |
name: ${TEST_POD_NAME} |
| 34 |
namespace: ${TEST_POD_NS} |
| 35 |
spec: |
| 36 |
containers: |
| 37 |
- name: ${TEST_CONTAINER_NAME} |
| 38 |
image: ${TESTIMAGE} |
| 39 |
command: ["sleep"] |
| 40 |
args: ["infinity"] |
| 41 |
imagePullSecrets: |
| 42 |
- name: testsecret |
| 43 |
EOF |
| 44 |
|
| 45 |
echo "Checking created pod..." |
| 46 |
IDX=0 |
| 47 |
DEADLINE=120 |
| 48 |
for (( ; ; )) ; do |
| 49 |
STATUS=$(KUBECONFIG="${K3S_KUBECONFIG}" kubectl get pods "${TEST_POD_NAME}" --namespace="${TEST_POD_NS}" -o 'jsonpath={..status.containerStatuses[0].state.running.startedAt}${..status.containerStatuses[0].state.waiting.reason}') |
| 50 |
echo "Status: ${STATUS}" |
| 51 |
STARTEDAT=$(echo "${STATUS}" | cut -f 1 -d '$') |
| 52 |
if [ "${STARTEDAT}" != "" ] ; then |
| 53 |
echo "Pod created" |
| 54 |
break |
| 55 |
elif [ ${IDX} -gt ${DEADLINE} ] ; then |
| 56 |
echo "Deadline exeeded to wait for pod creation" |
| 57 |
exit 1 |
| 58 |
fi |
| 59 |
((IDX+=1)) |
| 60 |
sleep 1 |
| 61 |
done |
| 62 |
|
| 63 |
echo "Getting topmost layer from ${K3S_NODENAME}..." |
| 64 |
TARGET_CONTAINER= |
| 65 |
for (( RETRY=1; RETRY<=50; RETRY++ )) ; do |
| 66 |
echo "[${RETRY}]Trying to get container id..." |
| 67 |
TARGET_CONTAINER=$(docker exec -i "${K3S_NODENAME}" ctr --namespace="k8s.io" c ls -q labels."io.kubernetes.container.name"=="${TEST_CONTAINER_NAME}" | sed -n 1p) |
| 68 |
if [ "${TARGET_CONTAINER}" != "" ] ; then |
| 69 |
break |
| 70 |
fi |
| 71 |
sleep 3 |
| 72 |
done |
| 73 |
if [ "${TARGET_CONTAINER}" == "" ] ; then |
| 74 |
echo "no container created for ${TESTIMAGE}" |
| 75 |
docker exec -i "${K3S_NODENAME}" ctr --namespace="k8s.io" c ls |
| 76 |
exit 1 |
| 77 |
else |
| 78 |
echo "container ${TARGET_CONTAINER} created for ${TESTIMAGE}" |
| 79 |
fi |
| 80 |
LAYER=$(docker exec -i "${K3S_NODENAME}" ctr --namespace="k8s.io" \ |
| 81 |
c info "${TARGET_CONTAINER}" \ |
| 82 |
| jq -r '.SnapshotKey') # We don't check this *active* snapshot |
| 83 |
|
| 84 |
echo "Checking all layers being remote snapshots..." |
| 85 |
LAYERSNUM=0 |
| 86 |
for (( ; ; )) ; do |
| 87 |
LAYER=$(docker exec -i "${K3S_NODENAME}" ctr --namespace="k8s.io" \ |
| 88 |
snapshot --snapshotter=stargz info "${LAYER}" \ |
| 89 |
| jq -r '.Parent') |
| 90 |
if [ "${LAYER}" == "null" ] ; then |
| 91 |
break |
| 92 |
elif [ ${LAYERSNUM} -gt 100 ] ; then |
| 93 |
echo "testing image contains too many layes > 100" |
| 94 |
exit 1 |
| 95 |
fi |
| 96 |
((LAYERSNUM+=1)) |
| 97 |
LABEL=$(docker exec -i "${K3S_NODENAME}" ctr --namespace="k8s.io" \ |
| 98 |
snapshots --snapshotter=stargz info "${LAYER}" \ |
| 99 |
| jq -r ".Labels.\"${REMOTE_SNAPSHOT_LABEL}\"") |
| 100 |
echo "Checking layer ${LAYER} : ${LABEL}" |
| 101 |
if [ "${LABEL}" == "null" ] ; then |
| 102 |
echo "layer ${LAYER} isn't remote snapshot" |
| 103 |
exit 1 |
| 104 |
fi |
| 105 |
done |
| 106 |
|
| 107 |
if [ ${LAYERSNUM} -eq 0 ] ; then |
| 108 |
echo "cannot get layers" |
| 109 |
exit 1 |
| 110 |
fi |
| 111 |
|
| 112 |
exit 0 |