Skip to content

Commit

Permalink
use Makefile to build images
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-sva committed Feb 4, 2025
1 parent 13a8cd8 commit 79e636d
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 213 deletions.
12 changes: 9 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
##### BUILD STAGE #####
FROM golang:1.21.13-alpine3.20 AS build
# use BUILDPLATFORM to pin to the native platform to prevent emulation from kicking in
FROM --platform=$BUILDPLATFORM golang:1.22.11-alpine3.21 AS build

# os from --platform linux/amd64
ARG TARGETOS
# architecture from --platform linux/amd64
ARG TARGETARCH

WORKDIR /app
# copy over dependency files and download dependencies
Expand All @@ -10,8 +16,8 @@ RUN go mod download
# copy over source files
COPY . .

## build the service and output the binary to /tmp/app
RUN CGO_ENABLED=0 GOOS=linux go build -o /tmp/app
# build the service and output the binary to /tmp/app
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /tmp/app

##### RUNTIME STAGE #####
FROM alpine:3.21.2
Expand Down
127 changes: 82 additions & 45 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,59 +1,96 @@
# Makefile for building and pushing Docker images
SHELL := /bin/bash

# Image URL to use all building/pushing image targets
IMG ?= hobbyfarm/gargantua:dev
SVC ?= usersvc
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true"
# Default values
IMAGE_REGISTRY ?= hobbyfarm
IMAGE_TAG ?= latest
PLATFORMS ?= linux/amd64
DOCKER_COMMAND ?= $(notdir $(shell command -v docker || command -v podman))
DOCKER_BUILD_COMMAND ?= buildx build
SERVICE_BASE_PATH ?= $(shell realpath ./v3/services)
SERVICE_DOCKERFILE ?= $(shell realpath ./v3/Dockerfile)
GARGANTUA_DOCKERFILE ?= $(shell realpath ./Dockerfile)
DOCKER_PUSH ?=
SERVICES ?=

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
ifeq ($(DOCKER_COMMAND),podman)
DOCKER_BUILD_COMMAND := build
PLATFORM_ARG :=
else
GOBIN=$(shell go env GOBIN)
PLATFORM_ARG := --platform $(PLATFORMS)
endif

all: manager

# Run tests
test: generate fmt vet
go test ./... -coverprofile cover.out

# Build manager binary
manager: generate fmt vet
go build -o bin/gargantua main.go

# Run against the configured Kubernetes cluster in ~/.kube/config
run: generate fmt vet
go run ./main.go
# Validate Docker/Podman installation
ifeq ($(DOCKER_COMMAND),)
$(error Neither "docker" nor "podman" exists on the system)
endif

# Run go fmt against code
fmt:
go fmt ./...
# If no services are specified, get all service directories
ifeq ($(SERVICES),)
SERVICES := gargantua $(shell find $(SERVICE_BASE_PATH) -maxdepth 1 -type d -exec basename {} \; | tail -n +2)
endif

# Run go vet against code
vet:
go vet ./...
.PHONY: help
help:
@echo "Usage:"
@echo " make docker-setup"
@echo " make docker-build [SERVICES=service1 service2] [IMAGE_TAG=tag] [IMAGE_REGISTRY=registry] [PLATFORMS=os/arch,os/arch] [PUSH=true]"
@echo " make docker-push [SERVICES=service1 service2] [IMAGE_TAG=tag] [IMAGE_REGISTRY=registry] [PLATFORMS=os/arch,os/arch]"
@echo " make generate-client"
@echo " make generate-protos"
@echo " make list-services"
@echo " make help"
@echo ""
@echo "Targets:"
@echo " docker-setup Sets up and configures Docker Buildx for the first time"
@echo " docker-build Build the docker images with multi-platform support"
@echo " docker-push Build and push the docker images with multi-platform support"
@echo " generate-client Generate kubernetes glue code for go"
@echo " generate-protos Generate go code from proto files"
@echo " list-services Prints the available services for building"
@echo " help Prints help"
@echo ""
@echo "Options:"
@echo " SERVICES List of services to build (default: all detected)"
@echo " IMAGE_TAG Tag for the built images (default: latest)"
@echo " IMAGE_REGISTRY Registry for images (default: hobbyfarm)"
@echo " PLATFORMS Comma-separated list of target platforms (default: linux/amd64)"
@echo " PUSH If set to true, push images to the registry (default: false)"

# Generate code
generate:
.PHONY: generate-client
generate-client:
./generate-client.sh

# Build the docker image
docker-build: fmt vet
docker build . -t ${IMG}

# Push the docker image
docker-push:
docker push ${IMG}
.PHONY: generate-protos
generate-protos:
./generate-protos.sh

build-svc:
docker build -f ./v3/Dockerfile --build-arg SERVICE_NAME=${SVC} -t ${IMG} .
.PHONY: docker-setup
docker-setup:
@if [ "$(DOCKER_COMMAND)" = "docker" ]; then \
docker buildx create --use --name hobbyfarm || true; \
docker buildx inspect --bootstrap; \
fi

push-svc: build-svc
docker push ${IMG}
.PHONY: docker-build
docker-build:
@for service in $(SERVICES); do \
if [ "$$service" = "gargantua" ]; then \
echo "Building gargantua..."; \
$(DOCKER_COMMAND) $(DOCKER_BUILD_COMMAND) $(if $(PUSH),--push,--load) $(PLATFORM_ARG) --file $(GARGANTUA_DOCKERFILE) --tag $(IMAGE_REGISTRY)/$$service:$(IMAGE_TAG) .; \
else \
echo "Building $$service..."; \
image_tag=$(IMAGE_REGISTRY)/$${service/%svc/-service}:$(IMAGE_TAG); \
$(DOCKER_COMMAND) $(DOCKER_BUILD_COMMAND) $(if $(PUSH),--push,--load) $(PLATFORM_ARG) --build-arg SERVICE_NAME=$$service --file $(SERVICE_DOCKERFILE) --tag $$image_tag .; \
fi; \
done

build-gargantua:
docker build -t ${IMG} .
.PHONY: docker-push
docker-push:
@make docker-build PUSH=true

push-gargantua: build-gargantua
docker push ${IMG}
.PHONY: list-services
list-services:
@for service in $(SERVICES); do \
echo "$$service"; \
done
123 changes: 0 additions & 123 deletions build-container.sh

This file was deleted.

37 changes: 0 additions & 37 deletions docker-compose.yaml

This file was deleted.

Empty file modified generate-protos.sh
100644 → 100755
Empty file.
15 changes: 10 additions & 5 deletions v3/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
##### BUILD STAGE #####
FROM golang:1.21.13-alpine3.20 AS build
# use BUILDPLATFORM to pin to the native platform to prevent emulation from kicking in
FROM --platform=$BUILDPLATFORM golang:1.22.11-alpine3.21 AS build

# os from --platform linux/amd64
ARG TARGETOS
# architecture from --platform linux/amd64
ARG TARGETARCH
ARG SERVICE_NAME

WORKDIR /app/v3/services/${SERVICE_NAME}
# copy over dependency files and download dependencies
COPY /v3/services/${SERVICE_NAME}/go.mod .
COPY /v3/go.mod /app/v3
COPY ./v3/services/${SERVICE_NAME}/go.mod .
COPY ./v3/go.mod /app/v3

RUN go mod download

# copy over source files
COPY . /app

## build the service and output the binary to /tmp/app
RUN CGO_ENABLED=0 GOOS=linux go build -o /tmp/app
# build the service and output the binary to /tmp/app
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /tmp/app

##### RUNTIME STAGE #####
FROM alpine:3.21.2
Expand Down

0 comments on commit 79e636d

Please sign in to comment.