Idempotency for deploy-k8s script

The change makes sure that the script runs idempotently
and if run twice in a row no extra work is going to be done

Change-Id: Ifc055b32528bc4702b480f2de92c544578c96d73
This commit is contained in:
Kostiantyn Kalynovskyi 2022-07-07 20:19:55 +00:00
parent 772198f15d
commit 4c7031a6d9

View File

@ -39,26 +39,46 @@ function configure_resolvconf {
old_ns=$(grep -P --no-filename "^nameserver\s+(?!127\.0\.0\.|${kube_dns_ip})" \ old_ns=$(grep -P --no-filename "^nameserver\s+(?!127\.0\.0\.|${kube_dns_ip})" \
/etc/resolv.conf /run/systemd/resolve/resolv.conf | sort | uniq) /etc/resolv.conf /run/systemd/resolve/resolv.conf | sort | uniq)
if [[ -f "/run/systemd/resolve/resolv.conf" ]]; then
sudo cp --remove-destination /run/systemd/resolve/resolv.conf /etc/resolv.conf sudo cp --remove-destination /run/systemd/resolve/resolv.conf /etc/resolv.conf
fi
sudo systemctl disable systemd-resolved
sudo systemctl stop systemd-resolved
# Remove localhost as a nameserver, since we stopped systemd-resolved
sudo sed -i "/^nameserver\s\+127.*/d" /etc/resolv.conf
# Insert kube DNS as first nameserver instead of entirely overwriting /etc/resolv.conf # Insert kube DNS as first nameserver instead of entirely overwriting /etc/resolv.conf
grep -q "nameserver ${kube_dns_ip}" /etc/resolv.conf || \ grep -q "nameserver ${kube_dns_ip}" /etc/resolv.conf || \
sudo sed -i -e "1inameserver ${kube_dns_ip}" /etc/resolv.conf sudo sed -i -e "1inameserver ${kube_dns_ip}" /etc/resolv.conf
local dns_servers
if [ -z "${HTTP_PROXY}" ]; then if [ -z "${HTTP_PROXY}" ]; then
sudo bash -c "printf 'nameserver 8.8.8.8\nnameserver 8.8.4.4\n' > /run/systemd/resolve/resolv.conf" dns_servers="nameserver 8.8.8.8\nnameserver 8.8.4.4\n"
sudo bash -c "printf 'nameserver 8.8.8.8\nnameserver 8.8.4.4\n' >> /etc/resolv.conf"
else else
sudo bash -c "echo \"${old_ns}\" > /run/systemd/resolve/resolv.conf" dns_servers="${old_ns}"
sudo bash -c "echo \"${old_ns}\" >> /etc/resolv.conf"
fi fi
for file in /etc/resolv.conf /run/systemd/resolve/resolv.conf; do grep -q "${dns_servers}" /etc/resolv.conf || \
sudo bash -c "echo 'search svc.cluster.local cluster.local' >> ${file}" echo -e ${dns_servers} | sudo tee -a /etc/resolv.conf
sudo bash -c "echo 'options ndots:5 timeout:1 attempts:1' >> ${file}"
done
sudo systemctl disable systemd-resolved grep -q "${dns_servers}" /run/systemd/resolve/resolv.conf || \
sudo systemctl stop systemd-resolved echo -e ${dns_servers} | sudo tee /run/systemd/resolve/resolv.conf
local search_options='search svc.cluster.local cluster.local'
grep -q "${search_options}" /etc/resolv.conf || \
echo "${search_options}" | sudo tee -a /etc/resolv.conf
grep -q "${search_options}" /run/systemd/resolve/resolv.conf || \
echo "${search_options}" | sudo tee -a /run/systemd/resolve/resolv.conf
local dns_options='options ndots:5 timeout:1 attempts:1'
grep -q "${dns_options}" /etc/resolv.conf || \
echo ${dns_options} | sudo tee -a /etc/resolv.conf
grep -q "${dns_options}" /run/systemd/resolve/resolv.conf || \
echo ${dns_options} | sudo tee -a /run/systemd/resolve/resolv.conf
} }
# NOTE: Clean Up hosts file # NOTE: Clean Up hosts file
@ -176,7 +196,13 @@ sudo -E minikube config set vm-driver none
# https://github.com/kubernetes/enhancements/issues/1164 # https://github.com/kubernetes/enhancements/issues/1164
export CHANGE_MINIKUBE_NONE_USER=true export CHANGE_MINIKUBE_NONE_USER=true
export MINIKUBE_IN_STYLE=false export MINIKUBE_IN_STYLE=false
sudo -E minikube start \
set +e
api_server_status="$(set +e; sudo -E minikube status --format='{{.APIServer}}')"
set -e
echo "Minikube api server status is \"${api_server_status}\""
if [[ "${api_server_status}" != "Running" ]]; then
sudo -E minikube start \
--docker-env HTTP_PROXY="${HTTP_PROXY}" \ --docker-env HTTP_PROXY="${HTTP_PROXY}" \
--docker-env HTTPS_PROXY="${HTTPS_PROXY}" \ --docker-env HTTPS_PROXY="${HTTPS_PROXY}" \
--docker-env NO_PROXY="${NO_PROXY},10.96.0.0/12" \ --docker-env NO_PROXY="${NO_PROXY},10.96.0.0/12" \
@ -191,6 +217,8 @@ sudo -E minikube start \
--extra-config=kubelet.resolv-conf=/run/systemd/resolve/resolv.conf \ --extra-config=kubelet.resolv-conf=/run/systemd/resolve/resolv.conf \
--feature-gates=RemoveSelfLink=false \ --feature-gates=RemoveSelfLink=false \
--embed-certs --embed-certs
fi
sudo -E systemctl enable --now kubelet sudo -E systemctl enable --now kubelet
sudo -E minikube addons list sudo -E minikube addons list
@ -254,15 +282,15 @@ helm repo remove stable || true
kubectl label --overwrite namespace default name=default kubectl label --overwrite namespace default name=default
kubectl label --overwrite namespace kube-system name=kube-system kubectl label --overwrite namespace kube-system name=kube-system
kubectl label --overwrite namespace kube-public name=kube-public kubectl label --overwrite namespace kube-public name=kube-public
kubectl label nodes --all openstack-control-plane=enabled kubectl label --overwrite nodes --all openstack-control-plane=enabled
kubectl label nodes --all openstack-compute-node=enabled kubectl label --overwrite nodes --all openstack-compute-node=enabled
kubectl label nodes --all openvswitch=enabled kubectl label --overwrite nodes --all openvswitch=enabled
kubectl label nodes --all linuxbridge=enabled kubectl label --overwrite nodes --all linuxbridge=enabled
kubectl label nodes --all ceph-mon=enabled kubectl label --overwrite nodes --all ceph-mon=enabled
kubectl label nodes --all ceph-osd=enabled kubectl label --overwrite nodes --all ceph-osd=enabled
kubectl label nodes --all ceph-mds=enabled kubectl label --overwrite nodes --all ceph-mds=enabled
kubectl label nodes --all ceph-rgw=enabled kubectl label --overwrite nodes --all ceph-rgw=enabled
kubectl label nodes --all ceph-mgr=enabled kubectl label --overwrite nodes --all ceph-mgr=enabled
for NAMESPACE in ceph openstack osh-infra; do for NAMESPACE in ceph openstack osh-infra; do
tee /tmp/${NAMESPACE}-ns.yaml << EOF tee /tmp/${NAMESPACE}-ns.yaml << EOF
@ -275,7 +303,7 @@ metadata:
name: ${NAMESPACE} name: ${NAMESPACE}
EOF EOF
kubectl create -f /tmp/${NAMESPACE}-ns.yaml kubectl apply -f /tmp/${NAMESPACE}-ns.yaml
done done
make all make all