pve_tests/run.sh
2024-09-05 21:13:37 +03:00

103 lines
2.4 KiB
Bash
Executable File

#!/bin/sh
set -xeuo pipefail
function print_help() {
cat <<EOF
usage:
$0 <branch> <k8s_version> <flannel_tag> <flannel_cni_tag> <registry> [<task_number>]
EOF
}
if [ "$#" -lt 5 ]; then
print_help
exit 1
fi
function at_err() {
if [ -s tmp/hosts ]; then
cat tmp/hosts | awk '{print $1}' | xargs -I {} ssh-keygen -R {}
fi
}
trap at_err ERR
branch="$1"
k8s_version="$2"
flannel_tag="$3"
flannel_cni_tag="$4"
registry="$5"
task_number="${6:-}"
ssh_options="-o IdentitiesOnly=yes -o StrictHostKeyChecking=no"
# Create VMs
python3 ./main.py "$branch"
# Send run/hosts to every vm
cat tmp/hosts | awk '{print $1}' | xargs -I {} rsync -e "ssh $ssh_options" tmp/hosts root@{}:/etc/hosts
# Restart network on every vm
cat tmp/hosts | awk '{print $1}' | xargs -I {} ssh $ssh_options root@{} "systemctl restart network"
# Add insecure registry entry
cat tmp/hosts | awk '{print $1}' | xargs -I {} ssh $ssh_options root@{} "mkdir -p /etc/containers/registries.conf.d"
if [ -f '001-insecure-registries.conf' ]; then
cat tmp/hosts | awk '{print $1}' | xargs -I {} scp $ssh_options 001-insecure-registries.conf root@{}:/etc/containers/registries.conf.d/001-insecure-registries.conf
fi
ansible-playbook ansible/playbook.yaml -i tmp/generated_inventory.yaml \
-e branch="$branch" \
-e k8s_version="$k8s_version" \
-e flannel_tag="$flannel_tag" \
-e flannel_cni_tag="$flannel_cni_tag" \
-e registry="$registry" \
-e task_number="$task_number"
head -n 1 tmp/hosts | awk '{print $1}' | xargs -I {} rsync --mkpath root@{}:/etc/kubernetes/admin.conf ~/.kube/config
kubectl apply -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2 # tells deployment to run 2 pods matching the template
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: obirvalger/my
ports:
- containerPort: 80
EOF
kubectl apply -f - <<EOF
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 30007
selector:
app: nginx
EOF
sleep 120
head -n 1 tmp/hosts | awk '{print $1}' | xargs -I {} curl {}:30007
kubectl exec $(kubectl get po | awk 'END{print $1}') -- nslookup nginx
# Remove ip addresses from known_hosts
cat tmp/hosts | awk '{print $1}' | xargs -I {} ssh-keygen -R {}