Skip to content

Commit

Permalink
Add flag to indicate script is running on EKS Hybrid nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
abhay-krishna committed Jan 7, 2025
1 parent fe5870e commit db46dfa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
2 changes: 2 additions & 0 deletions log-collector-script/linux/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ OPTIONS:
--ignore_metrics Variable To ignore prometheus metrics collection; Pass this flag if DISABLE_METRICS enabled on CNI
--eks_hybrid Variable To denote that the script is running on an EKS Hybrid node; This will skip IMDS queries for AWS region and instance ID
--help Show this help message.
```

Expand Down
29 changes: 27 additions & 2 deletions log-collector-script/linux/eks-log-collector.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ readonly DAYS_10=$(date -d "-10 days" '+%Y-%m-%d %H:%M')
INSTANCE_ID=""
INIT_TYPE=""
PACKAGE_TYPE=""
IMDS_TOKEN=""

# Script run defaults
ignore_introspection='false'
ignore_metrics='false'
eks_hybrid='false'

REQUIRED_UTILS=(
timeout
Expand Down Expand Up @@ -98,6 +100,8 @@ help() {
echo ""
echo " --ignore_metrics Variable To ignore prometheus metrics collection; Pass this flag if DISABLE_METRICS enabled on CNI"
echo ""
echo " --eks_hybrid Variable To denote that the script is running on an EKS Hybrid node; This will skip IMDS queries for AWS region and instance ID"
echo ""
echo " --help Show this help message."
echo ""
}
Expand All @@ -117,6 +121,9 @@ parse_options() {
ignore_metrics)
eval "${param}"="${val}"
;;
eks_hybrid)
eval "${param}"="${val}"
;;
help)
help && exit 0
;;
Expand Down Expand Up @@ -182,8 +189,14 @@ systemd_check() {
fi
}

# Get token for IMDSv2 calls
IMDS_TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 360")
load_imds_token() {
# Get token for IMDSv2 calls
IMDS_TOKEN=$(curl -X PUT -s --max-time 10 --retry 2 "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 360")
if [[ $? -ne 0 ]]; then
warning "Unable to reach EC2 Metadata Service. Skipping Instance Id, EC2 Region and EC2 AZ"
IMDS_TOKEN=""
fi
}

create_directories() {
# Make sure the directory the script lives in is there. Not an issue if
Expand All @@ -197,6 +210,10 @@ create_directories() {
}

get_instance_id() {
if [[ -z "${IMDS_TOKEN}" ]]; then
return
fi

INSTANCE_ID_FILE="/var/lib/cloud/data/instance-id"

if grep -q '^i-' "$INSTANCE_ID_FILE"; then
Expand All @@ -213,6 +230,10 @@ get_instance_id() {
}

get_region() {
if [[ -z "${IMDS_TOKEN}" ]]; then
return
fi

if REGION=$(curl -H "X-aws-ec2-metadata-token: $IMDS_TOKEN" -f -s --max-time 10 --retry 5 http://169.254.169.254/latest/meta-data/placement/region); then
echo "${REGION}" > "${COLLECT_DIR}"/system/region.txt
else
Expand Down Expand Up @@ -258,6 +279,10 @@ init() {
is_root
systemd_check
get_pkgtype

if [[ "${eks_hybrid}" == "false" ]]; then
load_imds_token
fi
}

collect() {
Expand Down

0 comments on commit db46dfa

Please sign in to comment.