Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

push images with multi-arch to support amd64/arm64 #491

Merged
merged 2 commits into from
Jul 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM golang:1.10-alpine
FROM BASEIMAGE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think seperate Dockerfiles would be easier to reason about

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconvenient maintenance in the later stage if use seperate Dockerfiles as I think.
Also, same approach in k8s project: single Dockerfile, and use 'sed' to change it for each platform.

MAINTAINER Timothy St. Clair "tstclair@heptio.com"

RUN apk add --no-cache ca-certificates bash
ADD sonobuoy /sonobuoy
CMD1

ADD BINARY /sonobuoy
ADD scripts/run_master.sh /run_master.sh
ADD scripts/run_single_node_worker.sh /run_single_node_worker.sh
WORKDIR /
Expand Down
82 changes: 72 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
EMPTY :=
SPACE := $(EMPTY) $(EMPTY)
COMMA := $(EMPTY),$(EMPTY)

BINARY = sonobuoy
TARGET = sonobuoy
GOTARGET = github.com/heptio/$(TARGET)
GOPATH = $(shell go env GOPATH)
REGISTRY ?= gcr.io/heptio-images
IMAGE = $(REGISTRY)/$(TARGET)
DIR := ${CURDIR}
DOCKER ?= docker
LINUX_ARCH := amd64 arm64
DOCKERFILE :=
PLATFORMS := $(subst $(SPACE),$(COMMA),$(foreach arch,$(LINUX_ARCH),linux/$(arch)))

GIT_VERSION ?= $(shell git describe --always --dirty)
IMAGE_VERSION ?= $(shell git describe --always --dirty)
Expand All @@ -30,8 +38,6 @@ VERBOSE_FLAG = -v
endif
BUILDMNT = /go/src/$(GOTARGET)
BUILD_IMAGE ?= golang:1.10-alpine
BUILDCMD = CGO_ENABLED=0 go build -o $(TARGET) $(VERBOSE_FLAG) -ldflags "-X github.com/heptio/sonobuoy/pkg/buildinfo.Version=$(GIT_VERSION)"
BUILD = $(BUILDCMD) $(GOTARGET)

TESTARGS ?= $(VERBOSE_FLAG) -timeout 60s
TEST_PKGS ?= $(GOTARGET)/cmd/... $(GOTARGET)/pkg/...
Expand Down Expand Up @@ -71,17 +77,48 @@ lint:
vet:
$(DOCKER_BUILD) '$(VET)'

container: sonobuoy
pre:
go get github.com/estesp/manifest-tool

build_container:
$(DOCKER) build \
-t $(REGISTRY)/$(TARGET):$(IMAGE_VERSION) \
-t $(REGISTRY)/$(TARGET):$(IMAGE_BRANCH) \
-t $(REGISTRY)/$(TARGET):$(GIT_REF) \
-t $(REGISTRY)/$(TARGET):$(IMAGE_VERSION) \
-t $(REGISTRY)/$(TARGET):$(IMAGE_BRANCH) \
-t $(REGISTRY)/$(TARGET):$(GIT_REF) \
-f $(DOCKERFILE) \
.

sonobuoy:
$(DOCKER_BUILD) '$(BUILD)'
container: sonobuoy
for arch in $(LINUX_ARCH); do \
if [ $$arch = amd64 ]; then \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given how different these two substitutions are, maybe just having seperate Dockerfiles makes more sense?

Copy link
Contributor Author

@lubinsz lubinsz Jul 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @liztio
In my opinion, inconvenient maintenance in the later stage if use seperate Dockerfiles.
For example: if we need to add a new feature in Dockerfile, we should modify them twice.

3 differences between the two substitutions:
a, baseimage
b, command: In x86, we can't use any 'RUN' command in Dockerfile to build target for CROSS-platform(arm, power).
c, binary file: different binary files correspond to different platforms

sed -e 's|BASEIMAGE|alpine:3.7|g' \
-e 's|CMD1|RUN apk add --no-cache ca-certificates bash|g' \
-e 's|BINARY|build/linux/amd64/sonobuoy|g' Dockerfile > Dockerfile-$$arch; \
$(MAKE) build_container DOCKERFILE=Dockerfile-$$arch; \
$(MAKE) build_container DOCKERFILE="Dockerfile-$$arch" TARGET="sonobuoy-$$arch"; \
elif [ $$arch = arm64 ]; then \
sed -e 's|BASEIMAGE|arm64v8/ubuntu:16.04|g' \
-e 's|CMD1||g' \
-e 's|BINARY|build/linux/arm64/sonobuoy|g' Dockerfile > Dockerfile-$$arch; \
$(MAKE) build_container DOCKERFILE="Dockerfile-$$arch" TARGET="sonobuoy-$$arch"; \
else \
echo "ARCH unknown"; \
fi \
done

build_sonobuoy:
$(DOCKER_BUILD) 'CGO_ENABLED=0 $(SYSTEM) go build -o $(BINARY) $(VERBOSE_FLAG) -ldflags="-s -w -X github.com/heptio/sonobuoy/pkg/buildinfo.Version=$(GIT_VERSION)" $(GOTARGET)'

push:
sonobuoy:
for arch in $(LINUX_ARCH); do \
mkdir -p build/linux/$$arch; \
echo Building: linux/$$arch; \
$(MAKE) build_sonobuoy SYSTEM="GOOS=linux GOARCH=$$arch" BINARY="build/linux/$$arch/sonobuoy"; \
done
@echo Building: host
make build_sonobuoy

push_images:
$(DOCKER) push $(REGISTRY)/$(TARGET):$(IMAGE_BRANCH)
$(DOCKER) push $(REGISTRY)/$(TARGET):$(GIT_REF)
if git describe --tags --exact-match >/dev/null 2>&1; \
Expand All @@ -91,6 +128,31 @@ push:
$(DOCKER) push $(REGISTRY)/$(TARGET):latest; \
fi

push_manifest:
$(GOPATH)/bin/manifest-tool -username oauth2accesstoken --password "`gcloud auth print-access-token`" push from-args --platforms $(PLATFORMS) --template $(REGISTRY)/$(TARGET)-ARCH:$(VERSION) --target $(REGISTRY)/$(TARGET):$(VERSION)

push: pre container
for arch in $(LINUX_ARCH); do \
$(MAKE) push_images TARGET="sonobuoy-$$arch"; \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An image does not exist locally with the tag: gcr.io/heptio-images/sonobuoy-amd64

When should these be created?

done

$(MAKE) push_manifest VERSION=$(IMAGE_BRANCH) TARGET="sonobuoy"
$(MAKE) push_manifest VERSION=$(GIT_REF) TARGET="sonobuoy"

if git describe --tags --exact-match >/dev/null 2>&1; \
then \
$(MAKE) push_manifest VERSION=$(IMAGE_VERSION) TARGET="sonobuoy"; \
$(MAKE) push_manifest VERSION=latest TARGET="sonobuoy"; \
fi

clean_image:
$(DOCKER) rmi -f `$(DOCKER) images $(REGISTRY)/$(TARGET) -a -q` || true

clean:
rm -f $(TARGET)
$(DOCKER) rmi $(REGISTRY)/$(TARGET) || true
rm -f Dockerfile-*
rm -rf build

for arch in $(LINUX_ARCH); do \
$(MAKE) clean_image TARGET=$(TARGET)-$$arch; \
done