-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathrun_test.sh
executable file
·301 lines (246 loc) · 8.42 KB
/
run_test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/bin/bash
# NOTE: THE CODE IN THIS FILE IS MAINLY REFERENCED FROM ISTIO INTEGRATION
# FRAMEWORK(https://github.com/istio/istio/tree/master/tests/integration)
# AND ADAPTED FOR KMESH.
# Exit immediately for non zero status
set -e
DEFAULT_KIND_IMAGE="kindest/node:v1.30.0@sha256:047357ac0cfea04663786a612ba1eaba9702bef25227a794b52890dd8bcd692e"
ISTIO_VERSION=${ISTIO_VERSION:-"1.22.0"}
export KMESH_WAYPOINT_IMAGE=${KMESH_WAYPOINT_IMAGE:-"ghcr.io/kmesh-net/waypoint:latest"}
ROOT_DIR=$(git rev-parse --show-toplevel)
TMP="$(mktemp -d)"
TMPBIN="$TMP/bin"
mkdir -p "${TMPBIN}"
export PATH="$PATH:$TMPBIN"
# Provision a kind clustr for testing.
function setup_kind_cluster() {
local NAME="${1:-kmesh-testing}"
local IMAGE="${2:-"${DEFAULT_KIND_IMAGE}"}"
# Delete any previous KinD cluster.
echo "Deleting previous KinD cluster with name=${NAME}"
if ! (kind delete cluster --name="${NAME}" -v9) > /dev/null; then
echo "No existing kind cluster with name ${NAME}. Continue..."
fi
# Create KinD cluster.
if [[ -n "${IPV6:-}" ]]; then
# Create IPv6 KinD cluster
cat <<EOF | kind create cluster --name="${NAME}" -v4 --retain --image "${IMAGE}" --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
ipFamily: ipv6
nodes:
- role: control-plane
- role: worker
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry]
config_path = "/etc/containerd/certs.d"
EOF
else
# Create default IPv4 KinD cluster
cat <<EOF | kind create cluster --name="${NAME}" -v4 --retain --image "${IMAGE}" --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry]
config_path = "/etc/containerd/certs.d"
EOF
fi
status=$?
if [ $status -ne 0 ]; then
echo "Could not setup KinD environment. Something wrong with KinD setup. Exporting logs."
fi
# Add the registry config to the nodes
#
# This is necessary because localhost resolves to loopback addresses that are network-namespace local.
# In other words: localhost in the container is not localhost on the host.
#
# We want a consistent name that works from both ends, so we tell containerd to alias localhost:${reg_port}
# to the registry container when pulling images
REGISTRY_DIR="/etc/containerd/certs.d/localhost:${KIND_REGISTRY_PORT}"
for node in $(kind get nodes --name="${NAME}"); do
docker exec "${node}" mkdir -p "${REGISTRY_DIR}"
cat << EOF | docker exec -i "${node}" cp /dev/stdin "${REGISTRY_DIR}/hosts.toml"
[host."http://${KIND_REGISTRY_NAME}:5000"]
EOF
done
# For KinD environment we need to mount bpf for each node, ref: https://github.com/kmesh-net/kmesh/issues/662
for node in $(kind get nodes --name="${NAME}"); do
docker exec "${node}" sh -c "mount -t bpf none /sys/fs/bpf"
done
# Document the local registry
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ConfigMap
metadata:
name: local-registry-hosting
namespace: kube-public
data:
localRegistryHosting.v1: |
host: "localhost:${reg_port}"
help: "https://kind.sigs.k8s.io/docs/user/local-registry/"
EOF
}
function setup_istio() {
echo "install istio $ISTIO_VERSION"
kubectl get crd gateways.gateway.networking.k8s.io &> /dev/null || \
{ kubectl kustomize "github.com/kubernetes-sigs/gateway-api/config/crd/experimental?ref=v1.1.0" | kubectl apply -f -; }
istioctl install --set profile=ambient --set meshConfig.accessLogFile="/dev/stdout" --set components.ingressGateways[0].enabled=true --set components.ingressGateways[0].name=istio-ingressgateway --skip-confirmation
}
function setup_kmesh() {
helm install kmesh $ROOT_DIR/deploy/charts/kmesh-helm -n kmesh-system --create-namespace --set deploy.kmesh.image.repository=localhost:5000/kmesh \
--set deploy.kmesh.containers.kmeshDaemonArgs="--mode=dual-engine --enable-bypass=false --enable-bpf-log=true --monitoring=true"
# Wait for all Kmesh pods to be ready.
while true; do
pod_statuses=$(kubectl get pods -n kmesh-system -l app=kmesh -o jsonpath='{range .items[*]}{.metadata.name}{" "}{.status.phase}{"\n"}{end}')
running_pods=0
total_pods=0
while read -r pod_name pod_status; do
total_pods=$((total_pods + 1))
if [ "$pod_status" = "Running" ]; then
running_pods=$((running_pods + 1))
fi
done <<< "$pod_statuses"
if [ "$running_pods" -eq "$total_pods" ]; then
echo "All pods of Kmesh daemon are in Running state."
break
fi
echo "Waiting for pods of Kmesh daemon to enter Running state..."
sleep 1
done
}
export KIND_REGISTRY_NAME="kind-registry"
export KIND_REGISTRY_PORT="5000"
export KIND_REGISTRY="localhost:${KIND_REGISTRY_PORT}"
# Provision a local docker registry, so KinD nodes could pull images from.
# https://kind.sigs.k8s.io/docs/user/local-registry/
function setup_kind_registry() {
running="$(docker inspect -f '{{.State.Running}}' "${KIND_REGISTRY_NAME}" 2>/dev/null || true)"
if [[ "${running}" != 'true' ]]; then
docker run \
-d --restart=always -p "${KIND_REGISTRY_PORT}:5000" --name "${KIND_REGISTRY_NAME}" \
gcr.io/istio-testing/registry:2
# Allow kind nodes to reach the registry
docker network connect "kind" "${KIND_REGISTRY_NAME}"
fi
}
function build_and_push_images() {
HUB="${KIND_REGISTRY}" TAG="latest" make docker.push
}
function install_dependencies() {
# 1. Install kind.
if ! which kind &> /dev/null
then
echo "install kind"
go install sigs.k8s.io/kind@v0.23.0
else
echo "kind is already installed"
fi
# 2. Install helm.
if ! which helm &> /dev/null
then
echo "install helm"
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
chmod 700 get_helm.sh
./get_helm.sh
rm get_helm.sh
else
echo "helm is already installed"
fi
# 3. Install istioctl
curl -L https://istio.io/downloadIstio | ISTIO_VERSION=${ISTIO_VERSION} TARGET_ARCH=x86_64 sh -
cp istio-${ISTIO_VERSION}/bin/istioctl $TMPBIN
rm -rf istio-${ISTIO_VERSION}
}
function cleanup_kind_cluster() {
local NAME="${1:-kmesh-testing}"
echo "Deleting KinD cluster with name=${NAME}"
kind delete cluster --name="${NAME}"
echo "KinD cluster ${NAME} cleaned up"
}
function cleanup_docker_registry() {
echo "Stopping Docker registry named '${KIND_REGISTRY_NAME}'..."
docker stop "${KIND_REGISTRY_NAME}" || echo "Failed to stop or no such registry '${KIND_REGISTRY_NAME}'."
echo "Removing Docker registry named '${KIND_REGISTRY_NAME}'..."
docker rm "${KIND_REGISTRY_NAME}" || echo "Failed to remove or no such registry '${KIND_REGISTRY_NAME}'."
}
PARAMS=()
while (( "$#" )); do
case "$1" in
--skip-install-dep)
SKIP_INSTALL_DEPENDENCIES=true
shift
;;
--skip-setup)
SKIP_SETUP=true
shift
;;
--skip-build)
SKIP_BUILD=true
shift
;;
--only-run-tests)
SKIP_INSTALL_DEPENDENCIES=true
SKIP_SETUP=true
SKIP_BUILD=true
shift
;;
--cluster)
NAME="$2"
if ! kind get clusters | grep -qw "$NAME"; then
echo "Error: Cluster '$NAME' does not exist."
exit 1
fi
shift 2
;;
--ipv6)
IPV6=true
shift
;;
--cleanup)
CLEANUP_KIND=true
CLEANUP_REGISTRY=true
shift
;;
--skip-cleanup-apps)
PARAMS+=("-istio.test.nocleanup")
shift
;;
*)
PARAMS+=("$1")
shift
;;
esac
done
NAME="${NAME:-kmesh-testing}"
if [[ -z "${SKIP_INSTALL_DEPENDENCIES:-}" ]]; then
install_dependencies
fi
if [[ -z "${SKIP_SETUP:-}" ]]; then
setup_kind_cluster "$NAME"
fi
if [[ -z "${SKIP_BUILD:-}" ]]; then
setup_kind_registry
build_and_push_images
fi
kubectl config use-context "kind-$NAME"
echo "Running tests in cluster '$NAME'"
# make sure the Kmesh local image is ready.
if [[ -z "${SKIP_SETUP:-}" ]]; then
setup_istio
setup_kmesh
fi
cmd="go test -v -tags=integ $ROOT_DIR/test/e2e/... -istio.test.kube.loadbalancer=false ${PARAMS[*]}"
bash -c "$cmd"
if [[ -n "${CLEANUP_KIND}" ]]; then
cleanup_kind_cluster
fi
if [[ -n "${CLEANUP_REGISTRY}" ]]; then
cleanup_docker_registry
fi
rm -rf "${TMP}"