Skip to content

Commit e2d5ccc

Browse files
authored
fix: make govcloud builds possible (#2621)
Make various fixes so that govcloud builds work: * Make latest-binaries.sh handle fetching cross-partition * Change s3 copy logic to not check the AWS_ACCESS_KEY_ID variable, which is a fundamentally flawed check, and instead just attempt a copy using the environment's credentials and fall back to --no-sign-request if the initial copy fails. * update the cache-pause-container script to use the eks-distro public mirror The net effect of this change is that by default, none of the provisioners rely on the caller's AWS credentials. I'm leaving in the logic that passes through the AWS credentials via environment variables since removing is a breaking change, but none of them are needed. In a future major release, it would be nice to remove those entirely and require people to use instance roles if they need permissions from the provisioners (e.g. for private artifact buckets).
1 parent b36c9b4 commit e2d5ccc

File tree

6 files changed

+43
-31
lines changed

6 files changed

+43
-31
lines changed

Makefile

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ K8S_VERSION_MINOR := $(word 1,${K8S_VERSION_PARTS}).$(word 2,${K8S_VERSION_PARTS
1414

1515
AMI_VARIANT ?= amazon-eks
1616
AMI_VERSION ?= v$(shell date '+%Y%m%d')
17+
aws_region ?= us-west-2
1718
os_distro ?= al2
1819
arch ?= x86_64
1920

@@ -41,10 +42,9 @@ endif
4142

4243
ami_name ?= $(AMI_VARIANT)-node-$(K8S_VERSION_MINOR)-$(AMI_VERSION)
4344

44-
# ami owner overrides for cn/gov-cloud
4545
ifeq ($(aws_region), cn-northwest-1)
4646
source_ami_owners ?= 141808717104
47-
else ifeq ($(aws_region), us-gov-west-1)
47+
else ifneq ($(filter $(aws_region),us-gov-west-1 us-gov-east-1),)
4848
source_ami_owners ?= 045324592363
4949
endif
5050

@@ -53,7 +53,7 @@ k8s=1.28
5353

5454
.PHONY: build
5555
build: ## Build EKS Optimized AMI, default using AL2, use os_distro=al2023 for AL2023 AMI
56-
$(MAKE) k8s $(shell hack/latest-binaries.sh $(k8s))
56+
$(MAKE) k8s $(shell hack/latest-binaries.sh $(k8s) $(aws_region))
5757

5858
.PHONY: fmt
5959
fmt: ## Format the source files
@@ -112,27 +112,27 @@ k8s: validate ## Build default K8s version of EKS Optimized AMI
112112

113113
.PHONY: 1.23
114114
1.23: ## Build EKS Optimized AMI - K8s 1.23 - DEPRECATED: use the `k8s` variable instead
115-
$(MAKE) k8s $(shell hack/latest-binaries.sh 1.23)
115+
$(MAKE) k8s $(shell hack/latest-binaries.sh 1.23 $(aws_region))
116116

117117
.PHONY: 1.24
118118
1.24: ## Build EKS Optimized AMI - K8s 1.24 - DEPRECATED: use the `k8s` variable instead
119-
$(MAKE) k8s $(shell hack/latest-binaries.sh 1.24)
119+
$(MAKE) k8s $(shell hack/latest-binaries.sh 1.24 $(aws_region))
120120

121121
.PHONY: 1.25
122122
1.25: ## Build EKS Optimized AMI - K8s 1.25 - DEPRECATED: use the `k8s` variable instead
123-
$(MAKE) k8s $(shell hack/latest-binaries.sh 1.25)
123+
$(MAKE) k8s $(shell hack/latest-binaries.sh 1.25 $(aws_region))
124124

125125
.PHONY: 1.26
126126
1.26: ## Build EKS Optimized AMI - K8s 1.26 - DEPRECATED: use the `k8s` variable instead
127-
$(MAKE) k8s $(shell hack/latest-binaries.sh 1.26)
127+
$(MAKE) k8s $(shell hack/latest-binaries.sh 1.26 $(aws_region))
128128

129129
.PHONY: 1.27
130130
1.27: ## Build EKS Optimized AMI - K8s 1.27 - DEPRECATED: use the `k8s` variable instead
131-
$(MAKE) k8s $(shell hack/latest-binaries.sh 1.27)
131+
$(MAKE) k8s $(shell hack/latest-binaries.sh 1.27 $(aws_region))
132132

133133
.PHONY: 1.28
134134
1.28: ## Build EKS Optimized AMI - K8s 1.28 - DEPRECATED: use the `k8s` variable instead
135-
$(MAKE) k8s $(shell hack/latest-binaries.sh 1.28)
135+
$(MAKE) k8s $(shell hack/latest-binaries.sh 1.28 $(aws_region))
136136

137137
.PHONY: lint-docs
138138
lint-docs: ## Lint the docs

doc/usage/overview.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ When building the AMI, binaries such as `kubelet`, `aws-iam-authenticator`, and
2424
It is recommended that the latest available binaries are used, as they may contain important fixes for bugs or security issues.
2525
The latest binaries can be discovered with the following script:
2626
```bash
27-
hack/latest-binaries.sh $KUBERNETES_MINOR_VERSION
27+
hack/latest-binaries.sh $KUBERNETES_MINOR_VERSION $AWS_REGION
2828
```
2929
This script will return the values for the binary-related AMI template variables, for example:
3030
```bash
31-
> hack/latest-binaries.sh 1.28
31+
> hack/latest-binaries.sh 1.28 us-west-2
3232

3333
kubernetes_version=1.28.1 kubernetes_build_date=2023-10-01
3434
```

hack/latest-binaries.sh

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,24 @@ set -o errexit
44
set -o pipefail
55
set -o nounset
66

7-
if [ "$#" -ne 1 ]; then
8-
echo "usage: $0 KUBERNETES_MINOR_VERSION"
7+
if [ "$#" -ne 2 ]; then
8+
echo "usage: $0 KUBERNETES_MINOR_VERSION REGION"
99
exit 1
1010
fi
1111

1212
MINOR_VERSION="${1}"
13+
REGION="${2}"
14+
15+
S3_EXTRA_ARGS=""
16+
if [[ "${REGION}" == us-gov* || "${REGION}" == eusc* ]]; then
17+
# Pull cross-partition from aws since there's no bucket in this partition.
18+
S3_EXTRA_ARGS="--no-sign-request --region us-west-2"
19+
fi
1320

1421
# retrieve the available "VERSION/BUILD_DATE" prefixes (e.g. "1.28.1/2023-09-14")
1522
# from the binary object keys, sorted in descending semver order, and pick the first one
16-
LATEST_BINARIES=$(aws s3api list-objects-v2 --bucket amazon-eks --prefix "${MINOR_VERSION}" --query 'Contents[*].[Key]' --output text | grep linux | cut -d'/' -f-2 | sort -Vru | head -n1)
23+
LATEST_BINARIES=$(aws s3api list-objects-v2 ${S3_EXTRA_ARGS} --bucket amazon-eks --prefix "${MINOR_VERSION}" --query 'Contents[*].[Key]' --output text \
24+
| grep linux | cut -d'/' -f-2 | sort -Vru | head -n1)
1725

1826
if [ "${LATEST_BINARIES}" == "None" ]; then
1927
echo >&2 "No binaries available for minor version: ${MINOR_VERSION}"

templates/al2023/provisioners/install-worker.sh

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -163,22 +163,23 @@ sudo mkdir -p /opt/cni/bin
163163

164164
echo "Downloading binaries from: s3://$BINARY_BUCKET_NAME"
165165
AWS_DOMAIN=$(imds "/latest/meta-data/services/domain")
166-
S3_URL_BASE="https://$BINARY_BUCKET_NAME.s3.$BINARY_BUCKET_REGION.$AWS_DOMAIN/$KUBERNETES_VERSION/$KUBERNETES_BUILD_DATE/bin/linux/$ARCH"
167166
S3_PATH="s3://$BINARY_BUCKET_NAME/$KUBERNETES_VERSION/$KUBERNETES_BUILD_DATE/bin/linux/$ARCH"
168167

169168
BINARIES=(
170169
kubelet
171170
)
172171
for binary in "${BINARIES[@]}"; do
173-
if [[ -n "$AWS_ACCESS_KEY_ID" ]]; then
174-
echo "AWS cli present - using it to copy binaries from s3."
175-
aws s3 cp --region $BINARY_BUCKET_REGION $S3_PATH/$binary .
176-
aws s3 cp --region $BINARY_BUCKET_REGION $S3_PATH/$binary.sha256 .
177-
else
178-
echo "AWS cli missing - using wget to fetch binaries from s3. Note: This won't work for private bucket."
179-
sudo wget $S3_URL_BASE/$binary
180-
sudo wget $S3_URL_BASE/$binary.sha256
181-
fi
172+
FILES=(
173+
"$binary"
174+
"$binary.sha256"
175+
)
176+
for file in "${FILES[@]}"; do
177+
if ! aws s3 cp --region $BINARY_BUCKET_REGION "$S3_PATH/$file" .; then
178+
echo "Fetching ${file} from s3 failed, trying again with unauthenticated request."
179+
aws s3 cp --no-sign-request --region $BINARY_BUCKET_REGION "$S3_PATH/$file" .
180+
fi
181+
done
182+
182183
sudo sha256sum -c $binary.sha256
183184
sudo chmod +x $binary
184185
sudo chown root:root $binary
@@ -198,12 +199,9 @@ sudo systemctl enable ebs-initialize-bin@kubelet
198199

199200
ECR_CREDENTIAL_PROVIDER_BINARY="ecr-credential-provider"
200201

201-
if [[ -n "$AWS_ACCESS_KEY_ID" ]]; then
202-
echo "AWS cli present - using it to copy ${ECR_CREDENTIAL_PROVIDER_BINARY} from s3."
203-
aws s3 cp --region $BINARY_BUCKET_REGION $S3_PATH/$ECR_CREDENTIAL_PROVIDER_BINARY .
204-
else
205-
echo "AWS cli missing - using wget to fetch ${ECR_CREDENTIAL_PROVIDER_BINARY} from s3. Note: This won't work for private bucket."
206-
sudo wget "$S3_URL_BASE/$ECR_CREDENTIAL_PROVIDER_BINARY"
202+
if ! aws s3 cp --region $BINARY_BUCKET_REGION $S3_PATH/$ECR_CREDENTIAL_PROVIDER_BINARY .; then
203+
echo "Fetching ${ECR_CREDENTIAL_PROVIDER_BINARY} from s3 failed, trying again with unauthenticated request."
204+
aws s3 cp --no-sign-request --region $BINARY_BUCKET_REGION $S3_PATH/$ECR_CREDENTIAL_PROVIDER_BINARY .
207205
fi
208206

209207
sudo chmod +x $ECR_CREDENTIAL_PROVIDER_BINARY

templates/al2023/variables-default.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"nvidia_driver_major_version": "580",
2525
"nvidia_repository_url": null,
2626
"nvidia_grid_runfile_bucket_name": "ec2-linux-nvidia-drivers",
27-
"pause_container_image": "602401143452.dkr.ecr.us-west-2.amazonaws.com/eks/pause:3.10",
27+
"pause_container_image": "public.ecr.aws/eks-distro/kubernetes/pause:3.10",
2828
"remote_folder": "/tmp",
2929
"runc_version": "*",
3030
"security_group_id": "",

templates/shared/runtime/bin/cache-pause-container

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ done
2323
PULL_ARGS=""
2424
if [[ "${PAUSE_CONTAINER_IMAGE}" == *"dkr.ecr"* ]]; then
2525
PULL_ARGS="${PULL_ARGS} --user AWS:$(aws ecr get-login-password)"
26+
elif [[ "${PAUSE_CONTAINER_IMAGE}" == *"public.ecr.aws"* ]]; then
27+
if ECR_PUBLIC_PASSWORD=$(aws ecr-public get-login-password); then
28+
PULL_ARGS="${PULL_ARGS} --user AWS:${ECR_PUBLIC_PASSWORD}"
29+
else
30+
echo "Failed to authenticate to public ECR, retrying with unauthenticated pull" >&2
31+
fi
2632
fi
2733
sudo ctr --namespace k8s.io image pull ${PULL_ARGS} ${PAUSE_CONTAINER_IMAGE}
2834
sudo ctr --namespace k8s.io image tag ${PAUSE_CONTAINER_IMAGE} ${TAG}

0 commit comments

Comments
 (0)