Created
May 19, 2020 04:11
-
-
Save niusmallnan/eb2563dd1b210a32d7306f08faba089d to your computer and use it in GitHub Desktop.
To address the fixed ip race-condition issue for Pandaria
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
run() | |
{ | |
subnet=$1 | |
#fixed_ips=$(kubectl get pod -A -l macvlan.pandaria.cattle.io/subnet=$subnet,macvlan.panda.io/macvlanIpType=specific -o=jsonpath="{.items[*].metadata.annotations.macvlan\.pandaria\.cattle\.io/ip}" | sed "s/-/ /g" | xargs -n1 | sort -u | xargs) | |
fixed_ips="" | |
all_pods=$(kubectl get pod -A -o=jsonpath='{range .items[*]}{"\n"}{.metadata.annotations.macvlan\.pandaria\.cattle\.io/subnet}{","}{.metadata.annotations.macvlan\.pandaria\.cattle\.io/ip}{","}{.metadata.name}{","}{.metadata.namespace}{","}{.metadata.labels.workload\.user\.cattle\.io/workloadselector}{","}{.status.phase}{end}') | |
for a_pod in $all_pods; do | |
a_pod_subnet=$(echo $a_pod | cut -f1 -d,) | |
a_pod_ip=$(echo $a_pod | cut -f2 -d,) | |
if [[ "$a_pod_subnet" == "$subnet" ]] && [[ "$a_pod_ip" != "auto" ]]; then | |
echo "INFO: fixed ip pod is founded: $a_pod" | |
fixed_ips="$a_pod_ip $fixed_ips" | |
fi | |
done | |
fixed_ips=$(echo $fixed_ips | xargs -n1 | sort -u | xargs) | |
echo "INFO: all fixed ips: $fixed_ips" | |
if [[ -z "$fixed_ips" ]]; then | |
echo "INFO: no fixed ips, return" | |
return | |
fi | |
used_fixed_ips=$(kubectl get macvlanips -A -l subnet=$subnet,macvlan.panda.io/macvlanIpType=specific -o=jsonpath="{.items[*].spec.cidr}" | sed -E "s/\/[0-9]+//g" | xargs -n1 | sort -u | xargs) | |
echo "INFO: all used fixed ips: $used_fixed_ips" | |
unused_fixed_ips=$(echo $fixed_ips | sed "s/$used_fixed_ips//g") | |
echo "INFO: all unused fixed ips: $unused_fixed_ips" | |
if [[ -z "$unused_fixed_ips" ]]; then | |
echo "INFO: no unused fixed ips , return" | |
return | |
fi | |
auto_pods=$(kubectl get macvlanips -A -l subnet=$subnet,macvlan.panda.io/macvlanIpType=auto -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{","}{.metadata.namespace}{","}{.spec.cidr}{end}' | sed -E "s/\/[0-9]+//g") | |
for missed_ip in $unused_fixed_ips; do | |
echo "WARNING: an fixed $missed_ip may be used by auto pod." | |
for pod in $auto_pods; do | |
name=$(echo $pod | cut -f1 -d,) | |
namespace=$(echo $pod | cut -f2 -d,) | |
ip=$(echo $pod | cut -f3 -d,) | |
if [[ "$ip" == "$missed_ip" ]]; then | |
project_id=$(kubectl get ns $namespace -o=jsonpath="{.metadata.annotations.field\.cattle\.io/projectId}") | |
echo "WARNING: an bad auto-pod is founded in project $project_id: $pod, need to remove..." | |
echo "INFO: kubectl delete --timeout=10s --grace-period=0 --force=true pod $name -n $namespace" | |
kubectl delete --timeout=10s --grace-period=0 --force=true pod $name -n $namespace | |
echo "INFO: done." | |
fi | |
done | |
done | |
} | |
subnets=$(kubectl get macvlansubnets -A -o=jsonpath="{.items[*].metadata.name}") | |
for name in $subnets; do | |
echo "INFO: processing subnet $name..." | |
run $name | |
sleep 1 | |
echo "INFO: done subnet $name." | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
可以多次执行,比如: