From f13759dc35ddfcb9924263eaf6f34cf337eb67a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20H=C3=BCbel?= Date: Tue, 11 Jul 2023 15:01:34 +0200 Subject: [PATCH] Image (#401) * generate ocmcli-image * docker cli build: add sources, remove platforms * Update Makefile with correct platform for image build --------- Co-authored-by: Uwe Krueger Co-authored-by: Gerald Morrison <67469729+morri-son@users.noreply.github.com> --- components/ocmcli/Dockerfile | 10 ++++++ components/ocmcli/Makefile | 52 +++++++++++++++++++++++++++++--- components/ocmcli/resources.yaml | 12 +++++++- components/ocmcli/sources.yaml | 7 +++++ 4 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 components/ocmcli/Dockerfile create mode 100644 components/ocmcli/sources.yaml diff --git a/components/ocmcli/Dockerfile b/components/ocmcli/Dockerfile new file mode 100644 index 0000000000..e1aeb5ee8b --- /dev/null +++ b/components/ocmcli/Dockerfile @@ -0,0 +1,10 @@ +ARG SUFFIX +ARG OCM_VERSION +ARG ALPINE_VERSION +FROM --platform=$BUILDPLATFORM alpine:${ALPINE_VERSION} +ARG SUFFIX +ARG OCM_VERSION +ARG ALPINE_VERSION + +LABEL org.opencontainers.image.description Open Component Model Client ${OCM_VERSION} based on Alpine ${ALPINE_VERSION} +COPY gen/ocmcli/ocmcli.$SUFFIX /usr/bin/ocm diff --git a/components/ocmcli/Makefile b/components/ocmcli/Makefile index 4426ce763e..dc5d4a01b0 100644 --- a/components/ocmcli/Makefile +++ b/components/ocmcli/Makefile @@ -1,20 +1,31 @@ NAME = ocmcli CMD = ocm PROVIDER ?= ocm.software +IMAGE := $(NAME)-image GITHUBORG ?= open-component-model COMPONENT = $(PROVIDER)/$(NAME) OCMREPO ?= ghcr.io/$(GITHUBORG)/ocm -PLATFORMS = linux/amd64 linux/arm64 darwin/arm64 darwin/amd64 windows/amd64 +MULTI ?= true +CONTAINER_PLATFORMS ?= linux/amd64 linux/arm64 +PLATFORMS = $(CONTAINER_PLATFORMS) darwin/arm64 darwin/amd64 windows/amd64 REPO_ROOT := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))../.. GIT_TREE_STATE = $(shell [ -z "$$(git status --porcelain 2>/dev/null)" ] && echo clean || echo dirty) VERSION = $(shell go run ../../pkg/version/generate/release_generate.go print-rc-version $(CANDIDATE)) COMMIT = $(shell git rev-parse HEAD) EFFECTIVE_VERSION = $(VERSION)+$(COMMIT) +PLATFORM_OS := $(shell go env GOOS) +PLATFORM_ARCH := $(shell go env GOARCH) CMDSRCS=$(shell find $(REPO_ROOT)/cmds/$(CMD) -type f) Makefile OCMSRCS=$(shell find $(REPO_ROOT)/pkg -type f) $(REPO_ROOT)/go.* +ATTRIBUTES = VERSION="$(VERSION)" NAME="$(NAME)" COMMIT="$(COMMIT)" IMAGE="$(IMAGE):$(VERSION)" PLATFORMS="$(CONTAINER_PLATFORMS)" GEN="$(GEN)" MULTI=$(MULTI) + +ifeq ($(MULTI),true) +FLAGSUF = .multi +endif + CREDS ?= OCM = go run $(REPO_ROOT)/cmds/ocm $(CREDS) @@ -27,11 +38,12 @@ BUILD_FLAGS := "-s -w \ -X github.com/open-component-model/ocm/pkg/version.gitCommit=$(COMMIT) \ -X github.com/open-component-model/ocm/pkg/version.buildDate=$(NOW)" +ALPINE_LATEST_VER=$(shell curl -s https://registry.hub.docker.com/v2/repositories/library/alpine/tags | jq '.results[1].name' | xargs) .PHONY: build build: $(GEN)/build -$(GEN)/build: $(CMDSRCS) $(OCMSRCS) +$(GEN)/build: $(GEN)/.exists $(CMDSRCS) $(OCMSRCS) @for i in $(PLATFORMS); do \ tag=$$(echo $$i | sed -e s:/:-:g); \ echo GOARCH=$$(basename $$i) GOOS=$$(dirname $$i) CGO_ENABLED=0 go build -ldflags $(BUILD_FLAGS) -o $(GEN)/$(NAME).$$tag ../../cmds/$(CMD); \ @@ -39,6 +51,37 @@ $(GEN)/build: $(CMDSRCS) $(OCMSRCS) done @touch $(GEN)/build +.PHONY: image +image: $(GEN)/image + +$(GEN)/image: $(GEN)/.exists Dockerfile $(GEN)/build + @PLATFORM_OS_OVERRIDE=$(PLATFORM_OS); \ + if [ $$PLATFORM_OS_OVERRIDE == darwin ]; then \ + echo; echo "Building linux instead of darwin as there's no native Docker platform for darwin"; echo; \ + docker buildx build -t $(IMAGE):$(VERSION) --platform linux/$(PLATFORM_ARCH) --file Dockerfile $(REPO_ROOT) \ + --build-arg OCM_VERSION=$(EFFECTIVE_VERSION) \ + --build-arg ALPINE_VERSION=$(ALPINE_LATEST_VER) \ + --build-arg SUFFIX=$$(echo linux/$(PLATFORM_ARCH) | sed -e s:/:-:g); \ + else \ + echo; echo "Building for $(PLATFORM_OS)/$(ARCH)"; echo; \ + docker buildx build -t $(IMAGE):$(VERSION) --platform $(PLATFORM_OS)/$(PLATFORM_ARCH) --file Dockerfile $(REPO_ROOT) \ + --build-arg OCM_VERSION=$(EFFECTIVE_VERSION) \ + --build-arg ALPINE_VERSION=$(ALPINE_LATEST_VER) \ + --build-arg SUFFIX=$$(echo $(PLATFORM_OS)/$(PLATFORM_ARCH) | sed -e s:/:-:g); \ + fi + @touch $(GEN)/image + +$(GEN)/image.multi: Dockerfile $(GEN)/build + echo "Building with Alpine version: ${ALPINE_LATEST_VER}" + for i in $(CONTAINER_PLATFORMS); do \ + tag=$$(echo $$i | sed -e s:/:-:g); \ + echo building platform $$i; \ + docker buildx build --load -t $(IMAGE):$(VERSION)-$$tag --platform $$i --file Dockerfile $(REPO_ROOT) \ + --build-arg OCM_VERSION=$(EFFECTIVE_VERSION) \ + --build-arg ALPINE_VERSION=$(ALPINE_LATEST_VER) \ + --build-arg SUFFIX=$$tag; \ + done + @touch $(GEN)/image.multi .PHONY: ctf ctf: $(GEN)/ctf @@ -55,9 +98,10 @@ version: .PHONY: ca ca: $(GEN)/ca.done -$(GEN)/ca.done: $(GEN)/.exists $(GEN)/build resources.yaml $(CHARTSRCS) Makefile +$(GEN)/ca.done: $(GEN)/.exists $(GEN)/build $(GEN)/image$(FLAGSUF) resources.yaml $(CHARTSRCS) Makefile $(OCM) create ca -f $(COMPONENT) "$(VERSION)" --provider $(PROVIDER) --file $(GEN)/ca - $(OCM) add resources --templater=spiff --file $(GEN)/ca VERSION="$(VERSION)" NAME="$(NAME)" COMMIT="$(COMMIT)" GEN="$(GEN)" PLATFORMS="$(PLATFORMS)" resources.yaml + $(OCM) add resources --templater=spiff --file $(GEN)/ca $(ATTRIBUTES) resources.yaml + $(OCM) add sources $(GEN)/ca VERSION="$(VERSION)" COMMIT="$(COMMIT)" sources.yaml @touch $(GEN)/ca.done .PHONY: push diff --git a/components/ocmcli/resources.yaml b/components/ocmcli/resources.yaml index 20eb2baace..d60093d235 100644 --- a/components/ocmcli/resources.yaml +++ b/components/ocmcli/resources.yaml @@ -15,7 +15,17 @@ helper: input: type: file path: (( values.GEN "/" values.NAME "." replace(p,"/","-") )) + image: + <<<: (( &template )) + name: ocmcli-image + type: ociImage + version: (( values.VERSION )) + input: + type: (( values.MULTI ? "dockermulti" :"docker" )) + repository: (( index(values.IMAGE, ":") >= 0 ? substr(values.IMAGE,0,index(values.IMAGE,":")) :values.IMAGE )) + variants: (( values.MULTI ? map[split(" ", values.PLATFORMS)|v|-> values.IMAGE "-" replace(v,"/","-")] :~~ )) + path: (( !values.MULTI ? values.IMAGE :~~ )) -resources: (( map[split(" ", values.PLATFORMS)|p|-> *helper.executable] )) +resources: (( map[split(" ", values.PLATFORMS)|p|-> *helper.executable] *helper.image )) diff --git a/components/ocmcli/sources.yaml b/components/ocmcli/sources.yaml new file mode 100644 index 0000000000..dcc9c5f896 --- /dev/null +++ b/components/ocmcli/sources.yaml @@ -0,0 +1,7 @@ +name: source +type: filesytem +access: + type: github + repoUrl: github.com/open-component-model/ocm + commit: ${COMMIT} +version: ${VERSION}