Skip to content

Commit d6bcdd2

Browse files
committed
feat: add the ability to build distroless images for optimizely
1 parent a4595a5 commit d6bcdd2

File tree

5 files changed

+93
-10
lines changed

5 files changed

+93
-10
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ cover: check-go static ## runs test suite with coverage profiling
4949
cover-html: cover ## generates test coverage html report
5050
$(GOCMD) tool cover -html=$(COVER_FILE)
5151

52-
setup: check-go ## installs all dev and ci dependencies, but does not install golang
52+
setup: check-go ## installs all dev and ci dependencies, but does not install golang
5353
## "go get" won't work for newer go versions, need to use "go install github.com/rakyll/statik"
5454
ifeq (,$(wildcard $(GOLINT)))
5555
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(GOPATH)/bin v1.54.2
5656
endif
5757
ifeq (,$(wildcard $(GOPATH)/bin/statik))
58-
GO111MODULE=off go get -u github.com/rakyll/statik
58+
go install github.com/rakyll/statik@latest
5959
endif
6060

6161
lint: check-go static ## runs `golangci-lint` linters defined in `.golangci.yml` file

scripts/Makefile.ci

+71-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,85 @@
11
.DEFAULT_GOAL := help
22

3+
# To customize the version of the image, consider setting the value of the
4+
# APP_VERSION variable. For example, to use the current git revision as a
5+
# version, use:
6+
#
7+
# APP_VERSION:=$(shell git rev-parse HEAD)
8+
#
9+
# At the command line this would read, for example:
10+
#
11+
# make \
12+
# APP_VERSION=$(git rev-parse HEAD) \
13+
# CONTAINERIZER=podman \
14+
# ci_build_dockerimage_distroless
15+
16+
# Make the image builder customizable so that we can use alternatives such as
17+
# podman to build these images.
18+
#
19+
# For example, in order to build the distroless image with podman, one can run
20+
#
21+
# make APP_VERSION=4.0.0 CONTAINERIZER=podman ci_build_dockerimage_distroless
22+
CONTAINERIZER:=docker
23+
24+
# The latest available version of Alpine from https://hub.docker.com/_/golang
25+
ALPINE_VERSION:=3.20
26+
27+
# The latest release version of go to address security vulnerabilities.
28+
# See the latest version available at: https://hub.docker.com/_/golang
29+
GIMME_GO_VERSION:=1.22.5
30+
31+
# Should one wish to have the agent image be deployed from a custom artifact
32+
# registry such as the Google Artifact Registry (GAR), one may want to
33+
# customize this tag prefix appropriately.
34+
IMAGE_TAG_PREFIX:="optimizely/agent"
35+
336
ci_build_static_binary: ## build static binary
437
CGO_ENABLED=0 $(GOBUILD) $(LDFLAGS) -o $(GOBIN)/$(TARGET) cmd/optimizely/main.go
538

639
ci_build_dockerimage: ## build minimal docker image of optimizely
7-
docker build \
40+
$(CONTAINERIZER) build \
841
-f scripts/dockerfiles/Dockerfile.static \
9-
-t optimizely/agent:${APP_VERSION} \
10-
-t optimizely/agent:latest \
42+
-t "${IMAGE_TAG_PREFIX}:${APP_VERSION}" \
43+
-t "${IMAGE_TAG_PREFIX}:latest" \
1144
--build-arg GO_VERSION=${GIMME_GO_VERSION:.x=} \
1245
.
1346

1447
ci_build_dockerimage_alpine: ## build alpine docker image of optimizely
15-
docker build \
48+
$(CONTAINERIZER) build \
1649
-f scripts/dockerfiles/Dockerfile.alpine \
17-
-t optimizely/agent:${APP_VERSION}-alpine \
18-
-t optimizely/agent:alpine \
50+
-t "${IMAGE_TAG_PREFIX}:${APP_VERSION}-alpine" \
51+
-t "${IMAGE_TAG_PREFIX}:alpine" \
1952
--build-arg GO_VERSION=${GIMME_GO_VERSION:.x=} \
53+
--build-arg ALPINE_VERSION=${ALPINE_VERSION:.x=} \
2054
.
55+
56+
# Distroless images are tiny, have small attack surface, and security-oriented
57+
# deployments may consider using them.
58+
#
59+
# For more information about distroless, please see:
60+
# https://github.com/GoogleContainerTools/distroless
61+
ci_build_dockerimage_distroless: ## build distroless image of optimizely
62+
$(CONTAINERIZER) build \
63+
-f scripts/dockerfiles/Dockerfile.distroless \
64+
-t "${IMAGE_TAG_PREFIX}:${APP_VERSION}-distroless" \
65+
-t "${IMAGE_TAG_PREFIX}:distroless" \
66+
--build-arg GO_VERSION=${GIMME_GO_VERSION:.x=} \
67+
.
68+
69+
# PHONY target to build all of the above container images.
70+
_ci_build_dockerimage_all: ci_build_dockerimage ci_build_dockerimage_alpine ci_build_dockerimage_distroless
71+
ci_build_dockerimage_all: _ci_build_dockerimage_all ## build all container images
72+
73+
push_image: ## push container image
74+
$(CONTAINERIZER) push "${IMAGE_TAG_PREFIX}:${APP_VERSION}"
75+
$(CONTAINERIZER) push "${IMAGE_TAG_PREFIX}:latest"
76+
77+
push_image_alpine: ## push alpine container image
78+
$(CONTAINERIZER) push "${IMAGE_TAG_PREFIX}:${APP_VERSION}-alpine"
79+
$(CONTAINERIZER) push "${IMAGE_TAG_PREFIX}:alpine"
80+
81+
push_image_distroless: ## push distroless container image
82+
$(CONTAINERIZER) push "${IMAGE_TAG_PREFIX}:${APP_VERSION}-distroless"
83+
$(CONTAINERIZER) push "${IMAGE_TAG_PREFIX}:distroless"
84+
85+
push_all_images: push_image push_image_alpine push_image_distroless ## push all container images

scripts/ci_create_packages.sh

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ if [[ $TRAVIS_OS_NAME == "linux" ]]; then
66
cd $TRAVIS_BUILD_DIR
77
make -e ci_build_dockerimage
88
make -e ci_build_dockerimage_alpine
9+
make -e ci_build_dockerimage_distroless
910
elif [[ $TRAVIS_OS_NAME == "osx" ]]; then
1011
echo "we're on osx"
1112
else

scripts/dockerfiles/Dockerfile.alpine

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
ARG GO_VERSION
2-
FROM golang:$GO_VERSION-alpine3.17 as builder
2+
ARG ALPINE_VERSION
3+
FROM golang:$GO_VERSION-alpine${ALPINE_VERSION} as builder
34
# hadolint ignore=DL3018
45
RUN addgroup -S agentgroup && adduser -S agentuser -G agentgroup
56
RUN apk add --no-cache make gcc libc-dev git curl
67
WORKDIR /go/src/github.com/optimizely/agent
78
COPY . .
89
RUN make setup build
910

10-
FROM alpine:3.17
11+
FROM alpine:${ALPINE_VERSION}
1112
RUN apk add --no-cache ca-certificates
1213
COPY --from=builder /go/src/github.com/optimizely/agent/bin/optimizely /optimizely
1314
COPY --from=builder /etc/passwd /etc/passwd
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
ARG GO_VERSION
2+
FROM golang:${GO_VERSION} as builder
3+
RUN addgroup -u 1000 agentgroup && \
4+
useradd -u 1000 agentuser -g agentgroup
5+
WORKDIR /go/src/github.com/optimizely/agent
6+
COPY . .
7+
RUN make setup build && \
8+
make ci_build_static_binary
9+
10+
FROM gcr.io/distroless/static:nonroot
11+
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
12+
COPY --from=builder /go/src/github.com/optimizely/agent/bin/optimizely /optimizely
13+
COPY --from=builder /etc/passwd /etc/passwd
14+
COPY --from=builder /etc/group /etc/group
15+
USER agentuser:agentgroup
16+
ENTRYPOINT ["/optimizely"]

0 commit comments

Comments
 (0)