Skip to content

Commit 9bd4fec

Browse files
authored
add validator script (#519)
1 parent 0021bfe commit 9bd4fec

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

tests/assets/eks_cluster_validator.sh

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#!/bin/bash
2+
3+
# Script to validate AWS VPC CNI configuration
4+
# 1. aws-node vpc cni version is >= 1.19.4
5+
# 2. aws-node container env vars
6+
# 3. VPC subnets have /12 CIDR with prefix reservation
7+
8+
set -e
9+
10+
# Colors
11+
GREEN='\033[0;32m'
12+
RED='\033[0;31m'
13+
YELLOW='\033[0;33m'
14+
NC='\033[0m' # No Color
15+
16+
# Function to check if a version is greater than or equal to another
17+
version_ge() {
18+
[[ "$(echo -e "$1\n$2" | sort -V | head -n1)" == "$2" ]]
19+
}
20+
21+
# 1. Check aws-node VPC CNI version
22+
echo "Checking aws-node VPC CNI version..."
23+
CNI_VERSION=$(kubectl get daemonset aws-node -n kube-system -o jsonpath='{.spec.template.spec.containers[?(@.name=="aws-node")].image}' | grep -oE '[0-9]+\.[0-9]+\.[0-9]+')
24+
25+
if [ -z "$CNI_VERSION" ]; then
26+
echo -e "${RED}[FAIL]${NC} Could not determine aws-node VPC CNI version"
27+
exit 1
28+
fi
29+
30+
if version_ge "$CNI_VERSION" "1.19.4"; then
31+
echo -e "${GREEN}[PASS]${NC} aws-node VPC CNI version $CNI_VERSION is >= 1.19.4"
32+
else
33+
echo -e "${RED}[FAIL]${NC} aws-node VPC CNI version $CNI_VERSION is < 1.19.4"
34+
exit 1
35+
fi
36+
37+
# 2. Check required environment variables
38+
echo "Checking aws-node container environment variables..."
39+
40+
# Get environment variables from the aws-node container
41+
DISABLE_LEAKED_ENI_CLEANUP=$(kubectl get daemonset aws-node -n kube-system -o jsonpath='{.spec.template.spec.containers[?(@.name=="aws-node")].env[?(@.name=="DISABLE_LEAKED_ENI_CLEANUP")].value}')
42+
ENABLE_PREFIX_DELEGATION=$(kubectl get daemonset aws-node -n kube-system -o jsonpath='{.spec.template.spec.containers[?(@.name=="aws-node")].env[?(@.name=="ENABLE_PREFIX_DELEGATION")].value}')
43+
MINIMUM_IP_TARGET=$(kubectl get daemonset aws-node -n kube-system -o jsonpath='{.spec.template.spec.containers[?(@.name=="aws-node")].env[?(@.name=="MINIMUM_IP_TARGET")].value}')
44+
WARM_IP_TARGET=$(kubectl get daemonset aws-node -n kube-system -o jsonpath='{.spec.template.spec.containers[?(@.name=="aws-node")].env[?(@.name=="WARM_IP_TARGET")].value}')
45+
46+
# Check DISABLE_LEAKED_ENI_CLEANUP
47+
if [ "$DISABLE_LEAKED_ENI_CLEANUP" == "true" ]; then
48+
echo -e "${GREEN}[PASS]${NC} DISABLE_LEAKED_ENI_CLEANUP is set to 'true'"
49+
else
50+
echo -e "${RED}[FAIL]${NC} DISABLE_LEAKED_ENI_CLEANUP is not set to 'true'. Current value: $DISABLE_LEAKED_ENI_CLEANUP"
51+
fi
52+
53+
# Check ENABLE_PREFIX_DELEGATION
54+
if [ "$ENABLE_PREFIX_DELEGATION" == "true" ]; then
55+
echo -e "${GREEN}[PASS]${NC} ENABLE_PREFIX_DELEGATION is set to 'true'"
56+
else
57+
echo -e "${RED}[FAIL]${NC} ENABLE_PREFIX_DELEGATION is not set to 'true'. Current value: $ENABLE_PREFIX_DELEGATION"
58+
fi
59+
60+
# Check MINIMUM_IP_TARGET
61+
if [ "$MINIMUM_IP_TARGET" == "30" ]; then
62+
echo -e "${GREEN}[PASS]${NC} MINIMUM_IP_TARGET is set to '30'"
63+
else
64+
echo -e "${RED}[FAIL]${NC} MINIMUM_IP_TARGET is not set to '30'. Current value: $MINIMUM_IP_TARGET"
65+
fi
66+
67+
# Check WARM_IP_TARGET
68+
if [ "$WARM_IP_TARGET" == "5" ]; then
69+
echo -e "${GREEN}[PASS]${NC} WARM_IP_TARGET is set to '5'"
70+
else
71+
echo -e "${RED}[FAIL]${NC} WARM_IP_TARGET is not set to '5'. Current value: $WARM_IP_TARGET"
72+
fi
73+
74+
# 3. Check VPC subnets CIDR blocks and prefix delegation reservations
75+
echo "Checking VPC subnets for /12 CIDR blocks and prefix delegation..."
76+
77+
# Get cluster VPC ID
78+
VPC_ID=$(aws ec2 describe-instances --instance-ids $(kubectl get nodes -o jsonpath='{.items[0].spec.providerID}' | cut -d '/' -f5) --query 'Reservations[0].Instances[0].VpcId' --output text)
79+
80+
if [ -z "$VPC_ID" ]; then
81+
echo -e "${RED}[FAIL]${NC} Could not determine VPC ID"
82+
exit 1
83+
fi
84+
85+
# Get subnets in the VPC
86+
SUBNETS=$(aws ec2 describe-subnets --filters "Name=vpc-id,Values=$VPC_ID" --query 'Subnets[*].{ID:SubnetId,CIDR:CidrBlock}' --output json)
87+
88+
if [ -z "$SUBNETS" ]; then
89+
echo -e "${RED}[FAIL]${NC} Could not retrieve subnets for VPC $VPC_ID"
90+
exit 1
91+
fi
92+
93+
# Check if subnets have /12 CIDR blocks
94+
SUBNET_COUNT=$(echo $SUBNETS | jq length)
95+
VALID_SUBNET_COUNT=0
96+
97+
for ((i=0; i<$SUBNET_COUNT; i++)); do
98+
SUBNET_ID=$(echo $SUBNETS | jq -r ".[$i].ID")
99+
CIDR_BLOCK=$(echo $SUBNETS | jq -r ".[$i].CIDR")
100+
CIDR_PREFIX=$(echo $CIDR_BLOCK | cut -d '/' -f2)
101+
102+
if [ "$CIDR_PREFIX" == "12" ]; then
103+
# Check for subnet CIDR reservations
104+
IPAM_POOLS=$(aws ec2 describe-ipam-pools --filters "Name=description,Values=*$SUBNET_ID*" --query 'IpamPools[*].{ID:IpamPoolId}' --output json)
105+
106+
if [ "$(echo $IPAM_POOLS | jq length)" -gt 0 ]; then
107+
echo -e "${GREEN}[PASS]${NC} Subnet $SUBNET_ID has /12 CIDR block and IPAM pool reservation"
108+
VALID_SUBNET_COUNT=$((VALID_SUBNET_COUNT + 1))
109+
else
110+
# Alternative check for CIDR reservations using subnet attributes
111+
SUBNET_ATTRS=$(aws ec2 describe-subnets --subnet-ids $SUBNET_ID --query 'Subnets[0]' --output json)
112+
113+
# Check if subnet has prefix delegation enabled
114+
if echo "$SUBNET_ATTRS" | grep -q "true"; then
115+
echo -e "${GREEN}[PASS]${NC} Subnet $SUBNET_ID has /12 CIDR block and appears to have prefix delegation enabled"
116+
VALID_SUBNET_COUNT=$((VALID_SUBNET_COUNT + 1))
117+
else
118+
echo -e "${YELLOW}[WARN]${NC} Subnet $SUBNET_ID has /12 CIDR block but could not confirm prefix delegation"
119+
fi
120+
fi
121+
else
122+
echo -e "${YELLOW}[WARN]${NC} Subnet $SUBNET_ID has /$CIDR_PREFIX CIDR block (not /12)"
123+
fi
124+
done
125+
126+
if [ $VALID_SUBNET_COUNT -gt 0 ]; then
127+
echo -e "${GREEN}[PASS]${NC} Found $VALID_SUBNET_COUNT subnets with /12 CIDR blocks and prefix delegation"
128+
else
129+
echo -e "${RED}[FAIL]${NC} No subnets with /12 CIDR blocks and prefix delegation found"
130+
fi
131+
132+
# Check if all validations passed
133+
if [ "$CNI_VERSION" \< "1.19.4" ] || \
134+
[ "$DISABLE_LEAKED_ENI_CLEANUP" != "true" ] || \
135+
[ "$ENABLE_PREFIX_DELEGATION" != "true" ] || \
136+
[ "$MINIMUM_IP_TARGET" != "30" ] || \
137+
[ "$WARM_IP_TARGET" != "5" ] || \
138+
[ $VALID_SUBNET_COUNT -eq 0 ]; then
139+
echo -e "${RED}Validation FAILED.${NC} Please address the issues highlighted above."
140+
exit 1
141+
else
142+
echo -e "${GREEN}All validations PASSED.${NC}"
143+
exit 0
144+
fi

0 commit comments

Comments
 (0)