Skip to content

Commit

Permalink
Simplify and tidy Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdoherty4 committed Dec 23, 2022
1 parent 94f44ed commit 17d0c1c
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 168 deletions.
38 changes: 12 additions & 26 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ jobs:
- ci-checks
- test
- verify
strategy:
matrix:
platform: [amd64, arm64]
steps:
- name: Checkout code
uses: actions/checkout@v2
Expand All @@ -83,31 +86,16 @@ jobs:
with:
go-version: "1.18.5"

- run: make crosscompile -j$(nproc)

- name: Upload tink-server binaries
uses: actions/upload-artifact@v2
with:
name: tink-server
path: cmd/tink-server/tink-server-*

- name: Upload tink-worker binaries
uses: actions/upload-artifact@v2
with:
name: tink-worker
path: cmd/tink-worker/tink-worker-*
- name: Build linux/${{ matrix.platform }}
run: |
make build -j$(nproc) GOOS=linux GOARCH=${{ matrix.platform }}
- name: Upload virtual-worker binaries
- name: Upload binaries
uses: actions/upload-artifact@v2
with:
name: virtual-worker
path: cmd/virtual-worker/virtual-worker-*
name: binaries
path: bin/*

- name: Upload tink-controller binaries
uses: actions/upload-artifact@v2
with:
name: tink-controller
path: cmd/tink-controller/tink-controller-*
docker-images:
runs-on: ubuntu-latest
needs:
Expand Down Expand Up @@ -146,19 +134,17 @@ jobs:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Download ${{ matrix.binary }} artifacts
- name: Download binaries
uses: actions/download-artifact@v2
with:
name: ${{ matrix.binary}}
path: cmd/${{ matrix.binary }}
name: binaries

- name: Fix Permissions
run: chmod +x cmd/${{ matrix.binary }}/${{ matrix.binary }}*
run: chmod +x bin/*

- name: ${{ matrix.repository }}
uses: docker/build-push-action@v2
with:
context: cmd/${{ matrix.binary }}/
cache-from: type=registry,ref=${{ matrix.repository }}:latest
push: ${{ startsWith(github.ref, 'refs/heads/main') }}
tags: ${{ steps.docker-image-tag.outputs.tags }}
Expand Down
216 changes: 92 additions & 124 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,82 +1,88 @@
help: ## Print this help
@grep --no-filename -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sed 's/:.*##/·/' | sort | column -ts '·' -c 120

all: cli server worker ## Build all binaries for host OS and CPU

# Only use the recipes defined in these makefiles
MAKEFLAGS += --no-builtin-rules
.SUFFIXES:
# Delete target files if there's an error
# This avoids a failure to then skip building on next run if the output is created by shell redirection for example
# Not really necessary for now, but just good to have already if it becomes necessary later.
.DELETE_ON_ERROR:
# Treat the whole recipe as a one shell script/invocation instead of one-per-line
.ONESHELL:
# Use bash instead of plain sh
SHELL := bash

# Use bash instead of plain sh and treat the shell as one shell script invocation.
SHELL := bash
.SHELLFLAGS := -o pipefail -euc
.ONESHELL:

## Tools
# Second expansion is used by the image targets to depend on their respective binaries. It is
# necessary because automatic variables are not set on first expansion.
# See https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html.
.SECONDEXPANSION:

GO := go
# Configure Go environment variables for use in the Makefile.
GOARCH ?= $(shell go env GOARCH)
GOOS ?= $(shell go env GOOS)
GOPROXY ?= $(shell go env GOPROXY)

# Runnable tools
GO ?= go
BUF := $(GO) run github.com/bufbuild/buf/cmd/[email protected]
CONTROLLER_GEN := $(GO) run sigs.k8s.io/controller-tools/cmd/[email protected]
GOFUMPT := $(GO) run mvdan.cc/[email protected]
KUSTOMIZE := $(GO) run sigs.k8s.io/kustomize/kustomize/[email protected]
SETUP_ENVTEST := $(GO) run sigs.k8s.io/controller-runtime/tools/[email protected]
GOLANGCI_LINT := $(GO) run github.com/golangci/golangci-lint/tree/master/cmd/[email protected]

####

binaries := cmd/tink-controller/tink-controller cmd/tink-server/tink-server cmd/tink-worker/tink-worker cmd/virtual-worker/virtual-worker
version := $(shell git rev-parse --short HEAD)
tag := $(shell git tag --points-at HEAD | head -n 1)
ifneq (,$(tag))
version := $(tag)-$(version)
endif
LDFLAGS := -ldflags "-X main.version=$(version)"
export CGO_ENABLED := 0

.PHONY: server cli worker virtual-worker test $(binaries)
controller: cmd/tink-controller/tink-controller
server: cmd/tink-server/tink-server
worker : cmd/tink-worker/tink-worker
virtual-worker : cmd/virtual-worker/virtual-worker

crossbinaries := $(addsuffix -linux-,$(binaries))
crossbinaries := $(crossbinaries:=amd64) $(crossbinaries:=arm64)

.PHONY: crosscompile $(crossbinaries)
%-amd64: FLAGS=GOOS=linux GOARCH=amd64
%-arm64: FLAGS=GOOS=linux GOARCH=arm64
$(binaries) $(crossbinaries):
$(FLAGS) $(GO) build $(LDFLAGS) -o $@ ./$(@D)

.PHONY: tink-controller-image tink-server-image tink-worker-image virtual-worker-image
tink-controller-image: cmd/tink-controller/tink-controller-linux-amd64
docker build -t tink-controller cmd/tink-controller/
tink-server-image: cmd/tink-server/tink-server-linux-amd64
docker build -t tink-server cmd/tink-server/
tink-worker-image: cmd/tink-worker/tink-worker-linux-amd64
docker build -t tink-worker cmd/tink-worker/
virtual-worker-image: cmd/virtual-worker/virtual-worker-linux-amd64
docker build -t virtual-worker cmd/virtual-worker/

ifeq ($(origin GOBIN), undefined)
GOBIN := ${PWD}/bin
export GOBIN
PATH := ${GOBIN}:${PATH}
export PATH
endif
# Installed tools
PROTOC_GEN_GO_GRPC := google.golang.org/grpc/cmd/[email protected]
PROTOC_GEN_GO := google.golang.org/protobuf/cmd/[email protected]

# These need changing to a `go run` command and placed in the Tools section of the Makefile
.PHONY: tools ## Install all required tools for use with this Makefile.
tools:
$(GO) install google.golang.org/grpc/cmd/[email protected]
$(GO) install google.golang.org/protobuf/cmd/[email protected]
$(GO) install sigs.k8s.io/controller-runtime/tools/[email protected]
$(GO) install sigs.k8s.io/kustomize/kustomize/[email protected]
.PHONY: help
help: ## Print this help
@awk 'BEGIN {FS = ":.*##"; printf "Usage:\n make \033[36m<target>\033[0m\n"} /^[%\/0-9A-Za-z_-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

# Define all the binaries we build for this project that get packaged into containers.
BINARIES := tink-server tink-worker tink-controller virtual-worker

# Version defines the string injected into binaries that indicates the version of the build.
# Eventually this will be superfluous as we transition to buildinfo provided by the Go standard
# library.
# Override by setting VERSION when invoking make. For example, `make VERSION=1.2.3`.
VERSION ?= $(shell git rev-parse --short HEAD)

.PHONY: build
build: $(BINARIES) ## Build all tink binaries. Cross build by setting GOOS and GOARCH.

# Create targets for all the binaries we build. They can be individually invoked with `make <binary>`.
# For example, `make tink-server`. Callers can cross build by defining the GOOS and GOARCH
# variables. For example, `GOOS=linux GOARCH=arm64 make tink-server`.
# See https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html.
.PHONY: $(BINARIES)
$(BINARIES):
CGO_ENABLED=false \
GOOS=$(GOOS) \
GOARCH=$(GOARCH) \
$(GO) build \
$(LDFLAGS) \
-o ./bin/$@-$(GOOS)-$(GOARCH) \
./cmd/$@

# IMAGE_ARGS is resolved when its used in the `%-image` targets. Consequently, the $* automatic
# variable isn't evaluated until the target is called.
# See https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html.
IMAGE_ARGS ?= -t $*

.PHONY: images
images: $(addsuffix -image, $(BINARIES)) ## Build all tink container images. All images will be for Linux but target the host arch.

# Image building targets are used for local builds only. The CI builds images using
# Github actions as they facilitate build and push functions, and have better support for
# release tagging etc.
.PHONY: tink-server-image tink-worker-image tink-controller-image virtual-worker-image
# Force a Linux build so we have something to copy into our Linux based images. By default we
# build for the host platform hence GOARCH isn't overriden.
%-image: export GOOS=linux
# The $$* leverages .SECONDEXPANSION to specify the matched part of the target name as a
# dependency. In doing so, we ensure the binary is built so it can be copied into the image. For
# example, `make tink-server-image` will depend on `tink-server`.
# See https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html.
%-image: $$*
DOCKER_BUILDKIT=1 docker build $(IMAGE_ARGS) -f cmd/$*/Dockerfile .

.PHONY: pbfiles
pbfiles: buf.gen.yaml buf.lock $(shell git ls-files 'protos/*/*.proto') tools
pbfiles: buf.gen.yaml buf.lock $(shell git ls-files 'protos/*/*.proto') _protoc
$(BUF) mod update
$(BUF) generate
$(GOFUMPT) -w protos/*/*.pb.*
Expand All @@ -88,23 +94,16 @@ check-pbfiles: pbfiles
git diff --no-ext-diff --exit-code -- protos/*/*.pb.*
)

e2etest-setup: tools
setup-envtest use

## --------------------------------------
## Generate
## --------------------------------------

.PHONY: generate
generate: generate-go generate-manifests # Generate code, manifests etc.
generate: generate-go generate-manifests ## Generate code, manifests etc.

.PHONY: generate-go
generate-go: # Generate Go code.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate/boilerplate.generatego.txt" paths="./pkg/apis/..."
$(GOFUMPT) -w ./pkg/apis

.PHONY: generate-manifests
generate-manifests: generate-crds generate-rbacs generate-server-rbacs # Generate manifests e.g. CRD, RBAC etc.
generate-manifests: generate-crds generate-rbacs generate-server-rbacs ## Generate manifests e.g. CRD, RBAC etc.

.PHONY: generate-crds
generate-crds:
Expand Down Expand Up @@ -154,31 +153,27 @@ out/release/tink.yaml: bin/kustomize generate-manifests out/release/default/kust

release-manifests: out/release/tink.yaml ## Builds the manifests to publish with a release.

crosscompile: $(crossbinaries) ## Build all binaries for Linux and all supported CPU arches
images: tink-server-image tink-worker-image virtual-worker-image ## Build all docker images
run: crosscompile run-stack ## Builds and runs the Tink stack (tink, db, cli) via docker-compose

test: e2etest-setup ## Run tests
source <(setup-envtest use -p env) && $(GO) test -coverprofile=coverage.txt ./...
.PHONY: test
test: ## Run tests
$(SETUP_ENVTEST) use
source <($(SETUP_ENVTEST) use -p env) && $(GO) test -coverprofile=coverage.txt ./...

.PHONY: verify
verify: lint check-generated ## Verify code style, is lint free, freshness ...
$(GOFUMPT) -s -d .

.PHONY: generated
generated: pbfiles generate-manifests ## Generate dynamically created files
check-generated: check-pbfiles ## Check if generated files are up to date

.PHONY: all check-generated crosscompile generated help images run test tools verify

# BEGIN: lint-install --dockerfile=warn -makefile=lint.mk .
# http://github.com/tinkerbell/lint-install
.PHONY: check-generated
check-generated: check-pbfiles ## Check if generated files are up to date

.PHONY: lint
lint: _lint
lint: shellcheck hadolint golangci-lint yamllint ## Lint code

LINT_ARCH := $(shell uname -m)
LINT_OS := $(shell uname)
LINT_OS_LOWER := $(shell echo $(LINT_OS) | tr '[:upper:]' '[:lower:]')
LINT_ROOT := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))

# shellcheck and hadolint lack arm64 native binaries: rely on x86-64 emulation
ifeq ($(LINT_OS),Darwin)
Expand All @@ -187,9 +182,6 @@ ifeq ($(LINT_OS),Darwin)
endif
endif

LINTERS :=
FIXERS :=

SHELLCHECK_VERSION ?= v0.8.0
SHELLCHECK_BIN := out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)
$(SHELLCHECK_BIN):
Expand All @@ -202,14 +194,9 @@ $(SHELLCHECK_BIN):
|| printf "#!/usr/bin/env shellcheck\n" > $@
chmod u+x $@

LINTERS += shellcheck-lint
shellcheck-lint: $(SHELLCHECK_BIN)
shellcheck: $(SHELLCHECK_BIN)
$(SHELLCHECK_BIN) $(shell find . -name "*.sh")

FIXERS += shellcheck-fix
shellcheck-fix: $(SHELLCHECK_BIN)
$(SHELLCHECK_BIN) $(shell find . -name "*.sh") -f diff | { read -t 1 line || exit 0; { echo "$$line" && cat; } | git apply -p2; }

HADOLINT_VERSION ?= v2.8.0
HADOLINT_BIN := out/linters/hadolint-$(HADOLINT_VERSION)-$(LINT_ARCH)
$(HADOLINT_BIN):
Expand All @@ -219,26 +206,11 @@ $(HADOLINT_BIN):
test -f $@.dl && mv $(HADOLINT_BIN).dl $@ || printf "#!/usr/bin/env hadolint\n" > $@
chmod u+x $@

LINTERS += hadolint-lint
hadolint-lint: $(HADOLINT_BIN)
hadolint: $(HADOLINT_BIN)
$(HADOLINT_BIN) --no-fail $(shell find . -name "*Dockerfile")

GOLANGCI_LINT_CONFIG := $(LINT_ROOT)/.golangci.yml
GOLANGCI_LINT_VERSION ?= v1.49.0
GOLANGCI_LINT_BIN := out/linters/golangci-lint-$(GOLANGCI_LINT_VERSION)-$(LINT_ARCH)
$(GOLANGCI_LINT_BIN):
mkdir -p out/linters
rm -rf out/linters/golangci-lint-*
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b out/linters $(GOLANGCI_LINT_VERSION)
mv out/linters/golangci-lint $@

LINTERS += golangci-lint-lint
golangci-lint-lint: $(GOLANGCI_LINT_BIN)
$(GOLANGCI_LINT_BIN) run

FIXERS += golangci-lint-fix
golangci-lint-fix: $(GOLANGCI_LINT_BIN)
$(GOLANGCI_LINT_BIN) run --fix
golangci-lint:
$(GOLANGCI_LINT) run

YAMLLINT_VERSION ?= 1.26.3
YAMLLINT_ROOT := out/linters/yamllint-$(YAMLLINT_VERSION)
Expand All @@ -249,14 +221,10 @@ $(YAMLLINT_BIN):
curl -sSfL https://github.com/adrienverge/yamllint/archive/refs/tags/v$(YAMLLINT_VERSION).tar.gz | tar -C out/linters -zxf -
cd $(YAMLLINT_ROOT) && pip3 install --target dist . || pip install --target dist .

LINTERS += yamllint-lint
yamllint-lint: $(YAMLLINT_BIN)
yamllint: $(YAMLLINT_BIN)
PYTHONPATH=$(YAMLLINT_ROOT)/dist $(YAMLLINT_ROOT)/dist/bin/yamllint .

.PHONY: _lint $(LINTERS)
_lint: $(LINTERS)

.PHONY: fix $(FIXERS)
fix: $(FIXERS)

# END: lint-install --dockerfile=warn -makefile=lint.mk .
.PHONY: _protoc ## Install all required tools for use with this Makefile.
_protoc:
$(GO) install $(PROTOC_GEN_GO)
$(GO) install $(PROTOC_GEN_GO_GRPC)
2 changes: 0 additions & 2 deletions cmd/tink-controller/.gitignore

This file was deleted.

10 changes: 5 additions & 5 deletions cmd/tink-controller/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
FROM alpine:3.15
ENTRYPOINT ["/usr/bin/tink-controller"]
EXPOSE 42113
EXPOSE 42114

ARG TARGETOS
ARG TARGETARCH
ARG TARGETVARIANT

RUN apk add --no-cache --update --upgrade ca-certificates
COPY tink-controller-linux-${TARGETARCH:-amd64}${TARGETVARIANT} /usr/bin/tink-controller

COPY bin/tink-controller-${TARGETOS}-${TARGETARCH} /usr/bin/tink-controller

ENTRYPOINT ["/usr/bin/tink-controller"]
2 changes: 0 additions & 2 deletions cmd/tink-server/.gitignore

This file was deleted.

Loading

0 comments on commit 17d0c1c

Please sign in to comment.