Skip to content

Commit

Permalink
Use make targets to install binary dependencies
Browse files Browse the repository at this point in the history
* Make use of file-based rules as make targets to ensure that binaries
  are only installed if not already available
* Configure `GOBIN` env var to `./bin`. Thus `go install` installs
  binaries in `./bin`
* Add `GOBIN` to path
* Use simple binary names as `GOBIN` is on the `PATH`

Co-authored-by: Georgi Sabev <[email protected]>
  • Loading branch information
danail-branekov and georgethebeatle committed Sep 16, 2024
1 parent 2f656af commit 76f0528
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 190 deletions.
69 changes: 35 additions & 34 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,47 @@ help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Development
BIN_PATH = $(shell pwd)/bin
export GOBIN = $(shell pwd)/bin
export PATH := $(shell pwd)/bin:$(PATH)

CONTROLLERS=controllers job-task-runner kpack-image-builder statefulset-runner
COMPONENTS=api $(CONTROLLERS)

manifests: install-controller-gen
$(CONTROLLER_GEN) \
manifests: bin/controller-gen
controller-gen \
paths="./model/..." \
crd \
output:crd:artifacts:config=helm/korifi/controllers/crds
@for comp in $(COMPONENTS); do make -C $$comp manifests; done

generate: install-controller-gen
$(CONTROLLER_GEN) object:headerFile="controllers/hack/boilerplate.go.txt" paths="./model/..."
generate: bin/controller-gen
controller-gen object:headerFile="controllers/hack/boilerplate.go.txt" paths="./model/..."
@for comp in $(CONTROLLERS); do make -C $$comp generate; done
go run ./scripts/helmdoc/main.go > README.helm.md

CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
install-controller-gen:
GOBIN=$(shell pwd)/bin go install sigs.k8s.io/controller-tools/cmd/controller-gen
bin/controller-gen: bin
go install sigs.k8s.io/controller-tools/cmd/controller-gen

generate-fakes:
go generate ./...

fmt: install-gofumpt install-shfmt
$(GOFUMPT) -w .
$(SHFMT) -f . | grep -v '^tests/vendor' | xargs $(SHFMT) -w -i 2 -ci
fmt: bin/gofumpt bin/shfmt
gofumpt -w .
shfmt -f . | grep -v '^tests/vendor' | xargs shfmt -w -i 2 -ci

vet: ## Run go vet against code.
go vet ./...

lint: fmt vet gosec staticcheck golangci-lint

gosec: install-gosec
$(GOSEC) --exclude=G101,G304,G401,G404,G505 --exclude-dir=tests ./...
gosec: bin/gosec
gosec --exclude=G101,G304,G401,G404,G505 --exclude-dir=tests ./...

staticcheck: install-staticcheck
$(STATICCHECK) ./...
staticcheck: bin/staticcheck
staticcheck ./...

golangci-lint: install-golangci-lint
$(GOLANGCILINT) run
golangci-lint: bin/golangci-lint
golangci-lint run

test: lint
@for comp in $(COMPONENTS); do make -C $$comp test; done
Expand All @@ -87,37 +86,39 @@ build-dorifi:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -C tests/assets/dorifi-golang -o ../multi-process/dorifi .
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -C tests/assets/sample-broker-golang -o ../sample-broker/sample-broker .

GOFUMPT = $(shell go env GOPATH)/bin/gofumpt
install-gofumpt:
bin:
mkdir -p bin

bin/gofumpt:
go install mvdan.cc/gofumpt@latest

SHFMT = $(shell go env GOPATH)/bin/shfmt
install-shfmt:
bin/shfmt:
go install mvdan.cc/sh/v3/cmd/shfmt@latest

VENDIR = $(shell go env GOPATH)/bin/vendir
install-vendir:
bin/vendir:
go install carvel.dev/vendir/cmd/vendir@latest

GOSEC = $(shell go env GOPATH)/bin/gosec
install-gosec:
bin/gosec:
go install github.com/securego/gosec/v2/cmd/gosec@latest

STATICCHECK = $(shell go env GOPATH)/bin/staticcheck
install-staticcheck:
bin/staticcheck:
go install honnef.co/go/tools/cmd/staticcheck@latest

GOLANGCILINT = $(shell go env GOPATH)/bin/golangci-lint
install-golangci-lint:
bin/golangci-lint:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

bin/cf:
mkdir -p $(BIN_PATH)
mkdir -p $(GOBIN)
curl -fsSL "https://packages.cloudfoundry.org/stable?release=linux64-binary&version=v8&source=github" \
| tar -zx cf8 \
&& mv cf8 $(BIN_PATH)/cf \
&& chmod +x $(BIN_PATH)/cf
&& mv cf8 $(GOBIN)/cf \
&& chmod +x $(GOBIN)/cf

bin/yq: bin
go install github.com/mikefarah/yq/v4@latest

bin/setup-envtest: bin
go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

vendir-update-dependencies: install-vendir
$(VENDIR) sync --chdir tests
vendir-update-dependencies: bin/vendir
vendir sync --chdir tests
33 changes: 15 additions & 18 deletions api/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# 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
else
GOBIN=$(shell go env GOBIN)
endif

# Use gsed on Mac, sed on linux
ifeq (,$(shell which gsed))
SED=sed
Expand Down Expand Up @@ -35,25 +28,29 @@ help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Development
export GOBIN = $(shell pwd)/bin
export PATH := $(shell pwd)/bin:$(PATH)

manifests: install-controller-gen install-yq
$(CONTROLLER_GEN) \
manifests: bin/controller-gen bin/yq
controller-gen \
paths=./... \
output:rbac:artifacts:config=../helm/korifi/api \
rbac:roleName=korifi-api-system-role

$(YQ) -i 'with(.metadata | select(.namespace == "ROOT_NAMESPACE"); .namespace="{{ .Values.rootNamespace }}")' ../helm/korifi/api/role.yaml
yq -i 'with(.metadata | select(.namespace == "ROOT_NAMESPACE"); .namespace="{{ .Values.rootNamespace }}")' ../helm/korifi/api/role.yaml

test: install-ginkgo

test: bin/ginkgo
../scripts/run-tests.sh --skip-package=test

CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
install-controller-gen:
GOBIN=$(shell pwd)/bin go install sigs.k8s.io/controller-tools/cmd/controller-gen
bin:
mkdir -p bin

bin/controller-gen: bin
go install sigs.k8s.io/controller-tools/cmd/controller-gen

install-ginkgo:
bin/ginkgo: bin
go install github.com/onsi/ginkgo/v2/ginkgo

YQ = $(shell pwd)/bin/yq
install-yq:
GOBIN=$(shell pwd)/bin go install github.com/mikefarah/yq/v4@latest
bin/yq: bin
go install github.com/mikefarah/yq/v4@latest
42 changes: 19 additions & 23 deletions controllers/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ ifdef GINKGO_NODES
CONTROLLERS_GINKGO_NODES = $(GINKGO_NODES)
endif

# 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
else
GOBIN=$(shell go env GOBIN)
endif

# Setting SHELL to bash allows bash commands to be executed by recipes.
# This is a requirement for 'setup-envtest.sh' in the test target.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
Expand All @@ -35,10 +28,12 @@ help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Development
export GOBIN = $(shell pwd)/bin
export PATH := $(shell pwd)/bin:$(PATH)

webhooks-file = ../helm/korifi/controllers/manifests.yaml
manifests: install-controller-gen install-yq
$(CONTROLLER_GEN) \
manifests: bin/controller-gen bin/yq
controller-gen \
paths="./..." \
crd \
rbac:roleName=korifi-controllers-manager-role \
Expand All @@ -47,24 +42,25 @@ manifests: install-controller-gen install-yq
output:rbac:artifacts:config=../helm/korifi/controllers \
output:webhook:artifacts:config=../helm/korifi/controllers

$(YQ) -i 'with(.metadata; .annotations["cert-manager.io/inject-ca-from"]="{{ .Release.Namespace }}/korifi-controllers-serving-cert")' $(webhooks-file)
$(YQ) -i 'with(.metadata; .name="korifi-controllers-" + .name)' $(webhooks-file)
$(YQ) -i 'with(.webhooks[]; .clientConfig.service.namespace="{{ .Release.Namespace }}")' $(webhooks-file)
$(YQ) -i 'with(.webhooks[]; .clientConfig.service.name="korifi-controllers-" + .clientConfig.service.name)' $(webhooks-file)
yq -i 'with(.metadata; .annotations["cert-manager.io/inject-ca-from"]="{{ .Release.Namespace }}/korifi-controllers-serving-cert")' $(webhooks-file)
yq -i 'with(.metadata; .name="korifi-controllers-" + .name)' $(webhooks-file)
yq -i 'with(.webhooks[]; .clientConfig.service.namespace="{{ .Release.Namespace }}")' $(webhooks-file)
yq -i 'with(.webhooks[]; .clientConfig.service.name="korifi-controllers-" + .clientConfig.service.name)' $(webhooks-file)

generate: install-controller-gen
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
generate: bin/controller-gen
controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."

test: install-ginkgo manifests generate ## Run tests.
test: bin/ginkgo manifests generate ## Run tests.
GINKGO_NODES=$(CONTROLLERS_GINKGO_NODES) ../scripts/run-tests.sh

CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
install-controller-gen:
GOBIN=$(shell pwd)/bin go install sigs.k8s.io/controller-tools/cmd/controller-gen
bin:
mkdir -p bin

bin/controller-gen: bin
go install sigs.k8s.io/controller-tools/cmd/controller-gen

install-ginkgo:
bin/ginkgo: bin
go install github.com/onsi/ginkgo/v2/ginkgo

YQ = $(shell pwd)/bin/yq
install-yq:
GOBIN=$(shell pwd)/bin go install github.com/mikefarah/yq/v4@latest
bin/yq: bin
go install github.com/mikefarah/yq/v4@latest
34 changes: 14 additions & 20 deletions job-task-runner/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,12 @@ IMG_JTR ?= cloudfoundry/korifi-job-task-runner:latest
ENVTEST_K8S_VERSION = 1.24.1
CLUSTER_NAME ?= "e2e"

# 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
else
GOBIN=$(shell go env GOBIN)
endif

# Setting SHELL to bash allows bash commands to be executed by recipes.
# This is a requirement for 'setup-envtest.sh' in the test target.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec

.PHONY: all
all: build

##@ General

# The help target prints out all targets with their descriptions organized
Expand All @@ -39,26 +29,30 @@ help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Development
export GOBIN = $(shell pwd)/bin
export PATH := $(shell pwd)/bin:$(PATH)

.PHONY: manifests
manifests: install-controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) \
manifests: bin/controller-gen
controller-gen \
paths="./..." \
rbac:roleName=korifi-job-task-runner-taskworkload-manager-role \
output:rbac:artifacts:config=../helm/korifi/job-task-runner

.PHONY: generate
generate: install-controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
generate: bin/controller-gen
controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."

.PHONY: test
test: install-ginkgo manifests generate ## Run tests.
test: bin/ginkgo manifests generate
../scripts/run-tests.sh

##@ Build Dependencies
.PHONY: install-controller-gen
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
install-controller-gen:
GOBIN=$(shell pwd)/bin go install sigs.k8s.io/controller-tools/cmd/controller-gen
bin:
mkdir -p bin

bin/controller-gen: bin
go install sigs.k8s.io/controller-tools/cmd/controller-gen

install-ginkgo:
bin/ginkgo: bin
go install github.com/onsi/ginkgo/v2/ginkgo
48 changes: 20 additions & 28 deletions kpack-image-builder/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,12 @@ IMG_KIB ?= cloudfoundry/korifi-kpack-image-builder:latest
ENVTEST_K8S_VERSION = 1.23
CLUSTER_NAME ?= "e2e"

# 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
else
GOBIN=$(shell go env GOBIN)
endif

# Setting SHELL to bash allows bash commands to be executed by recipes.
# This is a requirement for 'setup-envtest.sh' in the test target.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec

.PHONY: all
all: build

##@ General

# The help target prints out all targets with their descriptions organized
Expand All @@ -38,42 +28,44 @@ help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Development
export GOBIN = $(shell pwd)/bin
export PATH := $(shell pwd)/bin:$(PATH)

.PHONY: manifests
manifests: install-controller-gen install-yq
manifests: bin/controller-gen bin/yq
webhooks-file = ../helm/korifi/kpack-image-builder/manifests.yaml
manifests: install-controller-gen
$(CONTROLLER_GEN) \
manifests: bin/controller-gen
controller-gen \
paths="./..." \
rbac:roleName=korifi-kpack-build-manager-role \
webhook \
output:rbac:artifacts:config=../helm/korifi/kpack-image-builder \
output:webhook:artifacts:config=../helm/korifi/kpack-image-builder

$(YQ) -i 'with(.metadata; .annotations["cert-manager.io/inject-ca-from"]="{{ .Release.Namespace }}/korifi-controllers-serving-cert")' $(webhooks-file)
$(YQ) -i 'with(.metadata; .name="korifi-kpack-image-builder-" + .name)' $(webhooks-file)
$(YQ) -i 'with(.webhooks[]; .clientConfig.service.namespace="{{ .Release.Namespace }}")' $(webhooks-file)
$(YQ) -i 'with(.webhooks[]; .clientConfig.service.name="korifi-controllers-" + .clientConfig.service.name)' $(webhooks-file)
yq -i 'with(.metadata; .annotations["cert-manager.io/inject-ca-from"]="{{ .Release.Namespace }}/korifi-controllers-serving-cert")' $(webhooks-file)
yq -i 'with(.metadata; .name="korifi-kpack-image-builder-" + .name)' $(webhooks-file)
yq -i 'with(.webhooks[]; .clientConfig.service.namespace="{{ .Release.Namespace }}")' $(webhooks-file)
yq -i 'with(.webhooks[]; .clientConfig.service.name="korifi-controllers-" + .clientConfig.service.name)' $(webhooks-file)




.PHONY: generate
generate: install-controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
generate: bin/controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."

.PHONY: test
test: install-ginkgo manifests generate ## Run tests.
test: bin/ginkgo manifests generate ## Run tests.
../scripts/run-tests.sh

.PHONY: install-controller-gen
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
install-controller-gen:
GOBIN=$(shell pwd)/bin go install sigs.k8s.io/controller-tools/cmd/controller-gen
bin:
mkdir -p bin

bin/controller-gen: bin
go install sigs.k8s.io/controller-tools/cmd/controller-gen

install-ginkgo:
bin/ginkgo: bin
go install github.com/onsi/ginkgo/v2/ginkgo

YQ = $(shell pwd)/bin/yq
install-yq:
GOBIN=$(shell pwd)/bin go install github.com/mikefarah/yq/v4@latest
bin/yq: bin
go install github.com/mikefarah/yq/v4@latest
Loading

0 comments on commit 76f0528

Please sign in to comment.