Skip to content

Commit c91d783

Browse files
authored
Adding go-runner image build based on go version (#1926)
1 parent 578ffe0 commit c91d783

9 files changed

Lines changed: 675 additions & 3 deletions

File tree

projects/go-runner/0.18.0/GIT_TAG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v0.18.0

projects/go-runner/Makefile

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
RELEASE_BRANCH?=0.18.0
2+
GIT_TAG?=$(shell cat ./$(RELEASE_BRANCH)/GIT_TAG)
3+
MIN_GO_VERSION?=1.24
4+
5+
BASE_DIRECTORY:=$(shell git rev-parse --show-toplevel)
6+
PROJECT_DIRECTORY:=$(BASE_DIRECTORY)/projects/go-runner
7+
8+
# Get available go versions on the builder image
9+
GO_VERSIONS := $(wildcard /go/go*)
10+
AWS_ACCOUNT_ID?=$(shell aws sts get-caller-identity --query Account --output text)
11+
IMAGE_REPO?=$(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_REGION).amazonaws.com
12+
IMAGE_NAME?=go-runner
13+
14+
# REPO_FOLDER can't be release since there is a phony release target
15+
# using release-src and overriding component
16+
REPO_FOLDER=release-src
17+
REPO_OWNER=kubernetes
18+
COMPONENT=release
19+
CLONE_URL=https://github.com/$(REPO_OWNER)/$(COMPONENT).git
20+
21+
BINARY_TARGET_FILES=go-runner
22+
GO_MOD_PATHS=images/build/go-runner
23+
FIX_LICENSES_GO_RUNNER_TARGET=$(REPO_FOLDER)/images/build/go-runner/LICENSE
24+
25+
GOOS?=linux
26+
ARCH_LOWER?=amd64
27+
GOLANG_ARCHIVE_PATH=$(GO_VERSION_DIRECTORY)/archives/$(GOOS)/$(ARCH_LOWER)/$(GO_BIN_VERSION_WITHOUT_RELEASE).$(GOOS)-$(ARCH_LOWER).tar.gz
28+
ARCHITECTURES="amd64" "arm64"
29+
30+
AL_TAG?=2
31+
# Remove 20 from 2023 for latest image tags
32+
AL_TAG_LATEST=$(subst 20,,$(AL_TAG))
33+
BASE_IMAGE_REPO?=public.ecr.aws/eks-distro-build-tooling
34+
BASE_IMAGE_NAME?=eks-distro-minimal-base
35+
BASE_IMAGE?=$(BASE_IMAGE_REPO)/$(BASE_IMAGE_NAME):$(call BASE_TAG_FROM_TAG_FILE,$(BASE_IMAGE_NAME))
36+
PUSH_IMAGES?=false
37+
38+
39+
.PHONY: process-go-versions
40+
process-go-versions:
41+
@for GOROOT in $(GO_VERSIONS); do \
42+
echo "Processing for $$GOROOT"; \
43+
GO_VERSION=$${GOROOT/\/go\/go/}; \
44+
if [[ "$$GO_VERSION" < $(MIN_GO_VERSION) ]]; then \
45+
echo "Skipping go-runner build with older go version than $(MIN_GO_VERSION)"; \
46+
continue; \
47+
fi; \
48+
GO_PATCH_VERSION=`$$GOROOT/bin/go version | awk '{print $$3}' | sed 's/go//'` ; \
49+
IMAGE_TAG=$(GIT_TAG)-go-$$GO_PATCH_VERSION.$(AL_TAG); \
50+
echo "Checking if image already pushed: $(IMAGE_REPO)/$(IMAGE_NAME):$$IMAGE_TAG"; \
51+
if docker manifest inspect $(IMAGE_REPO)/$(IMAGE_NAME):$$IMAGE_TAG > /dev/null 2>&1; then \
52+
echo "$$IMAGE_TAG exists in repository, skipping build.."; \
53+
else \
54+
echo "$$IMAGE_TAG doesn't exist in repository"; \
55+
LATEST_IMAGE_TAG=$(GIT_TAG)-go-$$GO_VERSION-latest.al$(AL_TAG_LATEST); \
56+
IMAGES=$(IMAGE_REPO)/$(IMAGE_NAME):$$IMAGE_TAG,$(IMAGE_REPO)/$(IMAGE_NAME):$$LATEST_IMAGE_TAG; \
57+
$(MAKE) build-multi-arch-images GO_VERSION=$$GO_VERSION IMAGES=$$IMAGES; \
58+
fi; \
59+
done
60+
61+
62+
.PHONY: build-multi-arch-images
63+
build-multi-arch-images:
64+
@echo "Building $(IMAGES)"
65+
$(MAKE) build-binaries GO_VERSION=$(GO_VERSION)
66+
$(MAKE) fix-licenses GO_VERSION=$(GO_VERSION)
67+
$(MAKE) images GO_VERSION=$(GO_VERSION) IMAGES=$(IMAGES)
68+
$(MAKE) clean-image-build-data
69+
70+
71+
.PHONY: build-binaries
72+
build-binaries: clone
73+
echo "Architecture is: $(ARCHITECTURES)"
74+
@for ARCHITECTURE in $(ARCHITECTURES); do \
75+
echo "Building go-runner for $${ARCHITECTURE}"; \
76+
$(BASE_DIRECTORY)/scripts/simple_create_binaries.sh $(PROJECT_DIRECTORY) \
77+
$(PROJECT_DIRECTORY)/$(RELEASE_BRANCH)/_output/$(GO_VERSION)/bin/$(GOOS)-$${ARCHITECTURE}/go-runner \
78+
release-src $(GO_VERSION) "$(GOOS)/$${ARCHITECTURE}" "." "build" "" \
79+
"-s -w -buildid= -extldflags -static " 0 "" "images/build/go-runner" "go-runner"; \
80+
done
81+
82+
83+
.PHONY: fix-licenses
84+
fix-licenses:
85+
# go-licenses requires a LICENSE file in each folder with the go.mod
86+
cp $(REPO_FOLDER)/LICENSE $(FIX_LICENSES_GO_RUNNER_TARGET)
87+
bash $(BASE_DIRECTORY)/scripts/gather_licenses.sh release-src \
88+
$(PROJECT_DIRECTORY)/$(RELEASE_BRANCH)/_output "." images/build/go-runner $(GO_VERSION)
89+
build/fix_licenses.sh $(PROJECT_DIRECTORY)/$(RELEASE_BRANCH)/_output
90+
build/generate_attribution.sh $(PROJECT_DIRECTORY)/$(RELEASE_BRANCH) $(GO_VERSION)
91+
92+
93+
.PHONY: setup-permissions
94+
setup-permissions:
95+
aws ecr get-login-password --region $(AWS_REGION) | docker login --username AWS --password-stdin $(IMAGE_REPO)
96+
97+
98+
.PHONY: images
99+
images: buildkit-check
100+
$(BASE_DIRECTORY)/scripts/buildkit.sh \
101+
build \
102+
--frontend dockerfile.v0 \
103+
--opt platform=linux/amd64,linux/arm64 \
104+
--opt build-arg:GO_VERSION=$(GO_VERSION) \
105+
--opt build-arg:BASE_IMAGE=$(BASE_IMAGE) \
106+
--local dockerfile=$(PROJECT_DIRECTORY)/docker/go-runner \
107+
--local context=$(PROJECT_DIRECTORY)/$(RELEASE_BRANCH)/_output \
108+
--progress plain \
109+
--output type=image,oci-mediatypes=true,\"name=$(IMAGES)\",push=$(PUSH_IMAGES)
110+
111+
112+
.PHONY: clean-image-build-data
113+
clean-image-build-data:
114+
rm -rf $(PROJECT_DIRECTORY)/$(RELEASE_BRANCH)/_output
115+
116+
117+
.PHONY: clone
118+
clone:
119+
@if [ ! -d "$(REPO_FOLDER)" ]; then \
120+
git clone $(CLONE_URL) $(REPO_FOLDER); \
121+
else \
122+
echo "$(REPO_FOLDER) already cloned, skipping..."; \
123+
fi
124+
cd $(REPO_FOLDER) && git checkout $(GIT_TAG)
125+
126+
127+
.PHONY: buildkit-check
128+
buildkit-check:
129+
$(BASE_DIRECTORY)/scripts/buildkit_check.sh
130+
131+
132+
.PHONY: release
133+
release: setup-permissions process-go-versions clean
134+
135+
136+
.PHONY: clean
137+
clean:
138+
rm -rf $(REPO_FOLDER)
139+
140+
141+
define BASE_TAG_FROM_TAG_FILE
142+
$(shell yq e ".al$(AL_TAG).\"$(1)\"" $(BASE_DIRECTORY)/EKS_DISTRO_TAG_FILE.yaml)
143+
endef
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
set -o errexit
17+
set -o nounset
18+
set -o pipefail
19+
20+
OUTPUT_DIR="$1"
21+
22+
MAKE_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
23+
ATTRIBUTION_DIR="${OUTPUT_DIR}/attribution"
24+
source "${MAKE_ROOT}/../../scripts/common.sh"
25+
26+
27+
# go-licenses calls the main module command-line-arguments in the csv output
28+
MODULE_NAME=$(cat "${ATTRIBUTION_DIR}/root-module.txt")
29+
SEARCH='command-line-arguments'
30+
REPLACE=$(build::common::re_quote $MODULE_NAME)
31+
sed -i.bak "s/^$SEARCH/$REPLACE/" "${ATTRIBUTION_DIR}/go-license.csv"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
set -o errexit
17+
set -o nounset
18+
set -o pipefail
19+
20+
PROJECT_ROOT="$1"
21+
GO_VERSION="$2"
22+
23+
MAKE_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
24+
source "${MAKE_ROOT}/../../scripts/common.sh"
25+
26+
build::generate_attribution $PROJECT_ROOT $GO_VERSION
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
ARG BASE_IMAGE
16+
FROM $BASE_IMAGE
17+
18+
ARG TARGETARCH
19+
ARG TARGETOS
20+
ARG GO_VERSION
21+
22+
23+
COPY LICENSES /LICENSES
24+
COPY attribution/ATTRIBUTION.txt /ATTRIBUTION.txt
25+
COPY $GO_VERSION/bin/$TARGETOS-$TARGETARCH/go-runner /go-runner
26+
ENTRYPOINT ["/go-runner"]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env bash
2+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
if [ "$ARCHITECTURE" == "ARM64" ]; then
17+
echo "Won't perform image release for ARM64 arch"
18+
exit 0
19+
fi
20+
21+
if [ "$AWS_ROLE_ARN" == "" ]; then
22+
echo "Empty AWS_ROLE_ARN"
23+
exit 1
24+
fi
25+
26+
if [ "$ECR_PUBLIC_PUSH_ROLE_ARN" == "" ]; then
27+
echo "Empty ECR_PUBLIC_PUSH_ROLE_ARN"
28+
exit 1
29+
fi
30+
31+
BASE_DIRECTORY=$(git rev-parse --show-toplevel)
32+
cd ${BASE_DIRECTORY} || exit
33+
34+
cat <<EOF >awscliconfig
35+
[default]
36+
output=json
37+
region=${AWS_REGION:-${AWS_DEFAULT_REGION:-us-west-2}}
38+
role_arn=$AWS_ROLE_ARN
39+
web_identity_token_file=/var/run/secrets/eks.amazonaws.com/serviceaccount/token
40+
41+
[profile ecr-public-push]
42+
role_arn=$ECR_PUBLIC_PUSH_ROLE_ARN
43+
region=us-east-1
44+
source_profile=default
45+
EOF
46+
export AWS_CONFIG_FILE=$(pwd)/awscliconfig
47+
export AWS_PROFILE=ecr-public-push
48+
unset AWS_ROLE_ARN AWS_WEB_IDENTITY_TOKEN_FILE
49+
50+
make -C ${BASE_DIRECTORY}/projects/go-runner "release"

0 commit comments

Comments
 (0)