Adding CNFM testing basic setup
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / package / docker / src / main / docker / docker-files / kind-cluster / entrypoint-original.sh
1 #!/bin/bash
2 # ============LICENSE_START=======================================================
3 #   Copyright (C) 2023 Nordix Foundation.
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 #  SPDX-License-Identifier: Apache-2.0
18 # ============LICENSE_END=========================================================
19
20 # Script copied from https://hub.docker.com/r/devopps/kind-cluster-buster
21
22 set -o errexit
23 set -o nounset
24 set -o pipefail
25
26 # This is copied from official dind script:
27 # https://raw.githubusercontent.com/docker/docker/master/hack/dind
28 if [ -d /sys/kernel/security ] && ! mountpoint -q /sys/kernel/security; then
29     mount -t securityfs none /sys/kernel/security || {
30         echo >&2 'Could not mount /sys/kernel/security.'
31         echo >&2 'AppArmor detection and --privileged mode might break.'
32     }
33 fi
34
35 # Mount /tmp (conditionally)
36 if ! mountpoint -q /tmp; then
37     mount -t tmpfs none /tmp
38 fi
39
40 # Check cgroupfs.
41 # Verify the filesystem.
42 if [ ! -d /sys/fs/cgroup/ ]; then
43     echo >&2 'ERROR: Cgroupfs is not mounted'
44     exit 1
45 fi
46
47 # Determine cgroup parent for docker daemon.
48 # We need to make sure cgroups created by the docker daemon do not
49 # interfere with other cgroups on the host, and do not leak after this
50 # container is terminated.
51 if [ -f /sys/fs/cgroup/systemd/release_agent ]; then
52   # This means the user has bind mounted host /sys/fs/cgroup to the
53   # same location in the container (e.g., using the following docker
54   # run flags: `-v /sys/fs/cgroup:/sys/fs/cgroup`). In this case, we
55   # need to make sure the docker daemon in the container does not
56   # pollute the host cgroups hierarchy.
57   # Note that `release_agent` file is only created at the root of a
58   # cgroup hierarchy.
59   CGROUP_PARENT="$(grep systemd /proc/self/cgroup | cut -d: -f3)/docker"
60 else
61   CGROUP_PARENT="/docker"
62   # For each cgroup subsystem, Docker does a bind mount from the
63   # current cgroup to the root of the cgroup subsystem. For instance:
64   #   /sys/fs/cgroup/memory/docker/<cid> -> /sys/fs/cgroup/memory
65   #
66   # This will confuse some system software that manipulate cgroups
67   # (e.g., kubelet/cadvisor, etc.) sometimes because
68   # `/proc/<pid>/cgroup` is not affected by the bind mount. The
69   # following is a workaround to recreate the original cgroup
70   # environment by doing another bind mount for each subsystem.
71   CURRENT_CGROUP=$(grep systemd /proc/self/cgroup | cut -d: -f3)
72   CGROUP_SUBSYSTEMS=$(findmnt -lun -o source,target -t cgroup | grep "${CURRENT_CGROUP}" | awk '{print $2}')
73   
74   echo "${CGROUP_SUBSYSTEMS}" |
75   while IFS= read -r SUBSYSTEM; do
76     mkdir -p "${SUBSYSTEM}${CURRENT_CGROUP}"
77     mount --bind "${SUBSYSTEM}" "${SUBSYSTEM}${CURRENT_CGROUP}"
78   done
79 fi
80
81 setsid dockerd \
82   --cgroup-parent="${CGROUP_PARENT}" \
83   --bip="${DOCKERD_BIP:-172.17.1.1/24}" \
84   --mtu="${DOCKERD_MTU:-1400}" \
85   --raw-logs \
86   ${DOCKER_ARGS:-} >/var/log/docker/dockerd.log 2>&1 &
87   
88 # Wait until dockerd is ready.
89 until docker ps >/dev/null 2>&1
90 do
91   echo "Waiting for dockerd..."
92   sleep 1
93 done
94
95 exec "$@"