Skip to content

Commit

Permalink
Image (#401)
Browse files Browse the repository at this point in the history
* generate ocmcli-image

* docker cli build: add sources, remove platforms

* Update Makefile with correct platform for image build

---------

Co-authored-by: Uwe Krueger <[email protected]>
Co-authored-by: Gerald Morrison <[email protected]>
  • Loading branch information
3 people authored Jul 11, 2023
1 parent 8c6e9cf commit 73b4818
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
10 changes: 10 additions & 0 deletions components/ocmcli/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
52 changes: 48 additions & 4 deletions components/ocmcli/Makefile
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -27,18 +38,50 @@ 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); \
GOARCH=$$(basename $$i) GOOS=$$(dirname $$i) CGO_ENABLED=0 go build -ldflags $(BUILD_FLAGS) -o $(GEN)/$(NAME).$$tag ../../cmds/$(CMD); \
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
Expand All @@ -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
Expand Down
12 changes: 11 additions & 1 deletion components/ocmcli/resources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ))

7 changes: 7 additions & 0 deletions components/ocmcli/sources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: source
type: filesytem
access:
type: github
repoUrl: github.com/open-component-model/ocm
commit: ${COMMIT}
version: ${VERSION}

0 comments on commit 73b4818

Please sign in to comment.