From e9dfdf0c336ee7fba5ad5829a727bded820695fd Mon Sep 17 00:00:00 2001 From: Abhay Krishna Date: Tue, 7 Jan 2025 13:08:02 -0800 Subject: [PATCH] Add flag to indicate script is running on EKS Hybrid nodes (#2097) --- log-collector-script/linux/README.md | 2 ++ .../linux/eks-log-collector.sh | 29 +++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/log-collector-script/linux/README.md b/log-collector-script/linux/README.md index ce4b52c28..c0a4a822b 100644 --- a/log-collector-script/linux/README.md +++ b/log-collector-script/linux/README.md @@ -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. ``` diff --git a/log-collector-script/linux/eks-log-collector.sh b/log-collector-script/linux/eks-log-collector.sh index 7ff8010ab..e1246ff51 100755 --- a/log-collector-script/linux/eks-log-collector.sh +++ b/log-collector-script/linux/eks-log-collector.sh @@ -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 @@ -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 "" } @@ -117,6 +121,9 @@ parse_options() { ignore_metrics) eval "${param}"="${val}" ;; + eks_hybrid) + eval "${param}"="${val}" + ;; help) help && exit 0 ;; @@ -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 @@ -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 @@ -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 @@ -258,6 +279,10 @@ init() { is_root systemd_check get_pkgtype + + if [[ "${eks_hybrid}" == "false" ]]; then + load_imds_token + fi } collect() {