Skip to content

Commit ca67d18

Browse files
committed
feat(spot_termination): get termination info from aws not imds
1 parent 75a7ccb commit ca67d18

1 file changed

Lines changed: 26 additions & 10 deletions

File tree

config/plugin/spot_termination.sh

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,34 @@ OK=0
44
NONOK=1
55
UNKNOWN=2
66

7-
TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" \
8-
-H "X-aws-ec2-metadata-token-ttl-seconds: 21600" \
9-
--max-time 3 --silent --fail 2>/dev/null)
7+
# Get instance ID from Kubernetes node providerID
8+
if [ -z "${NODE_NAME}" ]; then
9+
exit $UNKNOWN
10+
fi
1011

11-
status_code=$(curl --max-time 3 --silent --output /dev/stderr --write-out "%{http_code}" \
12-
-H "X-aws-ec2-metadata-token: $TOKEN" \
13-
"http://169.254.169.254/latest/meta-data/spot/instance-action")
12+
provider_id="$(curl -s -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" \
13+
--cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt \
14+
"https://${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT}/api/v1/nodes/${NODE_NAME}" \
15+
2>/dev/null | jq -r '.spec.providerID')"
1416

15-
if [ "${status_code}" -eq "404" ]; then
17+
if [ -z "${provider_id}" ]; then
18+
exit $UNKNOWN
19+
fi
20+
21+
# Extract instance ID from providerID (format: aws:///region/instance-id)
22+
instance_id="$(echo "${provider_id}" | awk -F'/' '{print $NF}')"
23+
24+
if [ -z "${instance_id}" ]; then
25+
exit $UNKNOWN
26+
fi
27+
28+
# Check for spot instance interruption via EC2 API
29+
interruption_time=$(aws ec2 describe-instances --instance-ids "${instance_id}" \
30+
--query 'Reservations[0].Instances[0].DisruptionTime' \
31+
--output text 2>/dev/null)
32+
33+
if [ "${interruption_time}" = "None" ] || [ -z "${interruption_time}" ]; then
1634
exit $OK
17-
elif [ "${status_code}" -eq "200" ]; then
18-
exit $NONOK
1935
else
20-
exit $UNKNOWN
36+
exit $NONOK
2137
fi

0 commit comments

Comments
 (0)