From 2c6198ff980ec156cb39975d6da2b364c793a538 Mon Sep 17 00:00:00 2001 From: Dean Troyer Date: Thu, 19 Oct 2023 21:48:05 -0500 Subject: [PATCH] Add build-image Makefile target and build version support * Use the commit for BUILD_VERSION since we do not tag the repo * Use the commit date rather than build date to provide a bit of ordering to the builds Signed-off-by: Dean Troyer --- Makefile | 44 ++++++++++++++++++++++++++++++++++++++++---- README.md | 22 +++++++++++++++++++--- docker/Dockerfile | 3 ++- 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index c706701..1eda245 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,42 @@ # virtual-kubelet-saladcloud +# Standard Development Targets +# build - build the project, binaries go into ./bin +# build-image - build a Docker image with the virtual-kubelet binary +# clean - clean up built binaries and cached Go artifacts +# lint - run golangci-lint (set args in LINT_ARGS) +# tidy - "go mod tidy" + +IMAGE_TAG ?= latest +CMDS := bin/virtual-kubelet + + +# The conventional BUILD_VERSION is not very useful at the moment since we are not tagging the repo +# Use the sha for the build as a version for now. +# BUILD_VERSION ?= $(shell git describe --tags --always --dirty="-dev") +BUILD_VERSION ?= $(shell git rev-parse --short HEAD) + +# It seems more useful to have the commit date than the build date for ordering versions +# since commit shas have no order +# BUILD_DATE ?= $(shell date -u '+%Y-%m-%d-%H:%M UTC') +BUILD_DATE ?= $(shell git log -1 --format=%cd --date=format:"%Y%m%d") + +VERSION_FLAGS := -ldflags='-X "main.buildVersion=$(BUILD_VERSION)" -X "main.buildTime=$(BUILD_DATE)"' + .PHONY: build -build: CGO_ENABLED=0 -build: - go build -o ./bin/virtual-kubelet ./cmd/virtual-kubelet/main.go +build: $(CMDS) + +.PHONY: build-image +build-image: + docker build \ + --tag ghcr.io/saladtechnologies/virtual-kubelet-saladcloud:$(IMAGE_TAG) \ + --file docker/Dockerfile \ + --build-arg VERSION_FLAGS=$(VERSION_FLAGS) \ + . .PHONY: clean clean: - rm -rf ./bin + rm $(CMDS) go clean .PHONY: lint @@ -16,6 +45,13 @@ lint: .PHONY: test test: + go test -v ./... tidy: go mod tidy + +bin/virtual-kubelet: + +bin/%: CGO_ENABLED=0 +bin/%: + go build -ldflags '-extldflags "-static"' -o bin/$(*) $(VERSION_FLAGS) ./cmd/$(*) diff --git a/README.md b/README.md index d35c897..df6a930 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ SaladCloud Virtual Kubelet Provider -Salad's Virtual Kubelet (VK) provider for SaladCloud enables running Kubernetes (K8s) pods as container group deployments. +Salad's Virtual Kubelet (VK) provider for SaladCloud enables running Kubernetes (K8s) pods as SaladCloud Container Group deployments. ## How it Works @@ -46,11 +46,27 @@ Follow the steps below to get started with local development. 3. Build the project. ```sh - go build -o ./bin/virtual-kubelet ./cmd/virtual-kubelet/main.go + make build ``` -4. Run the project. +4. Run the project in foreground. ```sh ./bin/virtual-kubelet --sce-api-key {apiKey} --sce-project-name {projectName} --sce-organization-name {organizationName} ``` + +or + +4. Run the project in K8s via Helm. + +```sh +helm install \ + --create-namespace \ + --namespace saladcloud \ + --set salad.apiKey=${apiKey} \ + --set salad.organizationName=${organizationName} \ + --set salad.projectName=${projectName} \ + --set provider.image.tag=${imageTag} \ + saladcloud-node \ + ./charts/virtual-kubelet +``` diff --git a/docker/Dockerfile b/docker/Dockerfile index fcb551d..526724f 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -2,13 +2,14 @@ FROM golang:1.21.3-alpine3.18 AS build WORKDIR /app +ARG VERSION_FLAGS=-ldflags= COPY go.mod go.sum ./ RUN go mod download && \ go mod verify COPY . . -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /app/bin/virtual-kubelet ./cmd/virtual-kubelet/main.go +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build "${VERSION_FLAGS}" -o /app/bin/virtual-kubelet ./cmd/virtual-kubelet/main.go FROM scratch AS final