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

[BOP-1388] Add semver information to binary #102

Merged
merged 5 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:

- name: Save docker image
working-directory: .
run: docker save ${{ env.REGISTRY }}/${{ env.ORG }}/${{ env.IMAGE }}:latest -o /tmp/${{ env.IMAGE }}.tar
run: docker save ${{ env.REGISTRY }}/${{ env.ORG }}/${{ env.IMAGE }}:dev -o /tmp/${{ env.IMAGE }}.tar

- name: Upload image artifact
uses: actions/upload-artifact@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
run: |
COMMIT_SHA=$(git rev-parse --short "$GITHUB_SHA")
echo ${{ env.IMAGE }}:$COMMIT_SHA
docker tag ${{ env.REGISTRY }}/${{ env.ORG }}/${{ env.IMAGE }}:latest ${{ env.IMAGE }}:$COMMIT_SHA
docker tag ${{ env.REGISTRY }}/${{ env.ORG }}/${{ env.IMAGE }}:dev ${{ env.IMAGE }}:$COMMIT_SHA

- name: Generate the operator manifest
run: |
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ coverage.txt
*.swp
*.swo
*~

deploy/static/mke-operator.yaml
12 changes: 11 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ FROM golang:1.22 AS builder
ARG TARGETOS
ARG TARGETARCH

# LDFLAGS]
ARG COMMIT
ARG VERSION
ARG DATE

WORKDIR /workspace

# Copy the go source
Expand All @@ -13,7 +18,12 @@ COPY . /workspace
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager main.go
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -ldflags \
"\
-X main.commit=${COMMIT} \
-X main.version=${VERSION} \
-X main.date=${DATE} \
" -a -o manager main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
18 changes: 7 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
PROJECT_DIR := $(shell pwd)
PACKAGES := $(shell go list ./... | grep -v /test)

VERSION ?= latest

# IMAGE_TAG_BASE defines registry and org name of the blueprint operator image.
IMAGE_REPO ?= ghcr.io/mirantiscontainers
IMAGE_TAG_BASE ?= $(IMAGE_REPO)/blueprint-operator
# This is determined outside of the Makefile so it can be shared elseware
include build/makefiles/vars.mk

# Set the Operator SDK version to use. By default, what is installed on the system is used.
# This is useful for CI or a project to utilize a specific version of the operator-sdk toolkit.
OPERATOR_SDK_VERSION ?= v1.31.0

# Image URL to use all building/pushing image targets
IMG ?= $(IMAGE_TAG_BASE):$(VERSION)

# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.28.0

Expand Down Expand Up @@ -95,12 +90,12 @@ run: manifests generate fmt vet ## Run a controller from your host.
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
.PHONY: docker-build
docker-build: unit ## Build docker image with the manager.
docker build -t ${IMG} .
docker-build: ## Build docker image with the manager.
docker build --build-arg VERSION=${VERSION} --build-arg COMMIT=${COMMIT} --build-arg DATE=${DATE} -t ${IMG} .

.PHONY: docker-build-amd64
docker-build-amd64: ## Build docker image with the manager.
docker build --platform linux/amd64 -t ${IMG} .
docker build --build-arg VERSION=${VERSION} --build-arg COMMIT=${COMMIT} --build-arg DATE=${DATE} --platform linux/amd64 -t ${IMG} .

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
Expand Down Expand Up @@ -148,6 +143,7 @@ undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/confi

.PHONY: build-operator-manifest
build-operator-manifest: kustomize manifests ## builds mke operator manifest file
@mkdir -p deploy/static
@cd config/manager && $(KUSTOMIZE) edit set image ghcr.io/mirantiscontainers/blueprint-operator=${IMG}
@mkdir -p deploy/static
@$(KUSTOMIZE) build config/default > deploy/static/blueprint-operator.yaml
Expand Down
29 changes: 29 additions & 0 deletions build/makefiles/vars.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# The default naming convention for the operator is for a development image
# on a developer's machine. This is so that devs don't have to remember to set
# these values. CI will set vars so that the image is tagged correctly during
# a CI build.
COMMIT=$(shell git rev-parse --short HEAD)
DATE=$(shell date -u '+%Y-%m-%d')
IMAGE_REPO?=ghcr.io/mirantiscontainers
IMAGE_NAME?=blueprint-operator

# A basic dev tag is by default so that the same image is rebuilt during development
VERSION?=dev
ifdef RELEASE_BUILD
VERSION=$(shell git tag -l "v*.*.*" --sort=-version:refname | head -n 1)
endif
ifdef MERGE_BUILD
# This will replace the last 2 '-' characters with '+' to make it a valid semver with build info
# Assumes that the version is in the format vX.Y.Z-<commit count>-<sha>
VERSION=$(shell git describe --tags --always | sed 's/\(.*\)-/\1+/' | sed 's/\(.*\)-/\1+/')
endif

# Setup the full image name
IMAGE_TAG_BASE=$(IMAGE_REPO)/$(IMAGE_NAME)
IMG:=$(IMAGE_TAG_BASE):$(VERSION)

# Docker doesn't allow '+' in the image tag, so merge builds only use the commit
# sha in the image tag.
ifdef MERGE_BUILD
IMG:=$(IMAGE_TAG_BASE):$(COMMIT)
endif
Loading
Loading