44NONOK=1
55UNKNOWN=2
66
7- # Get instance ID from Kubernetes node providerID
8- if [ -z " ${NODE_NAME} " ] ; then
9- exit $UNKNOWN
10- fi
7+ # Get IMDSv2 token
8+ TOKEN= $( curl -X PUT " http://169.254.169.254/latest/api/token " \
9+ -H " X-aws-ec2-metadata-token-ttl-seconds: 21600 " \
10+ --max-time 3 --silent --fail 2> /dev/null )
1111
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' ) "
16-
17- if [ -z " ${provider_id} " ]; then
12+ if [ -z " ${TOKEN} " ]; then
1813 exit $UNKNOWN
1914fi
2015
21- # Extract instance ID from providerID (format: aws:///region/instance-id)
22- instance_id=" $( echo " ${provider_id} " | awk -F' /' ' {print $NF}' ) "
16+ # Get instance ID and launch template from IMDS
17+ instance_id=$( curl --max-time 3 --silent --fail \
18+ -H " X-aws-ec2-metadata-token: $TOKEN " \
19+ " http://169.254.169.254/latest/meta-data/instance-id" )
2320
2421if [ -z " ${instance_id} " ]; then
2522 exit $UNKNOWN
2623fi
2724
28- instances=" $( aws autoscaling describe-auto-scaling-instances --instance-ids " ${instance_id} " ) "
25+ instance_launch_template=$( curl --max-time 3 --silent --fail \
26+ -H " X-aws-ec2-metadata-token: $TOKEN " \
27+ " http://169.254.169.254/latest/meta-data/tags/instance/aws:ec2launchtemplate:id" )
28+
29+ instance_asg=$( curl --max-time 3 --silent --fail \
30+ -H " X-aws-ec2-metadata-token: $TOKEN " \
31+ " http://169.254.169.254/latest/meta-data/tags/instance/aws:autoscaling:groupName" )
2932
30- if [ " $( echo " ${instances} " | jq ' .AutoScalingInstances | length' ) " -eq " 0" ]
31- then
33+ if [ -z " ${instance_asg} " ] || [ -z " ${instance_launch_template} " ]; then
3234 exit $UNKNOWN
3335fi
3436
35- instance=" $( echo " ${instances} " | jq ' .AutoScalingInstances[0]' ) "
36- instance_launch_config=" $( echo " ${instance} " | jq -r .LaunchTemplate.LaunchTemplateName) "
37- instance_asg=" $( echo " ${instance} " | jq -r .AutoScalingGroupName) "
37+ # Get ASG's current launch template (still requires AWS API)
38+ asgs=" $( aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names " ${instance_asg} " 2> /dev/null) "
3839
39- asgs=" $( aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names ${instance_asg} ) "
40+ if [ -z " ${asgs} " ] || ! echo " ${asgs} " | jq empty 2> /dev/null; then
41+ exit $UNKNOWN
42+ fi
4043
41- if [ " $( echo " ${asgs} " | jq ' .AutoScalingGroups | length' ) " -eq " 0" ]
42- then
44+ if [ " $( echo " ${asgs} " | jq ' .AutoScalingGroups | length' ) " -eq " 0" ]; then
4345 exit $UNKNOWN
4446fi
4547
46- asg_launch_config =" $( echo " ${asgs} " | jq -r ' .AutoScalingGroups[0].MixedInstancesPolicy.LaunchTemplate.LaunchTemplateSpecification.LaunchTemplateName ' ) "
48+ asg_launch_template =" $( echo " ${asgs} " | jq -r ' .AutoScalingGroups[0].MixedInstancesPolicy.LaunchTemplate.LaunchTemplateSpecification.LaunchTemplateId ' ) "
4749
48- if [ " ${instance_launch_config} " = " ${asg_launch_config} " ]
49- then
50+ if [ " ${instance_launch_template} " = " ${asg_launch_template} " ]; then
5051 exit $OK
5152else
5253 exit $NONOK
53- fi
54+ fi
0 commit comments