Skip to content
This repository was archived by the owner on Dec 6, 2024. It is now read-only.

Commit e37b86f

Browse files
committed
standardize make tooling with top level Makefile
Standardize make tooling. Remove unnecessary duplication where possible, including with common copies of go fmt/vet/test targets. Slighty shift around controller and sidecar 'cmd/' directories to allow specifying a template Dockerfile that can be used to generate Dockerfiles for each image easily. Where it makes sense, this work adopts the make target naming and Dockerfile usage of the kubebuilder project. The biggest divergence from that template is because we build 2 images from one repo, and because we have a 'proto/' dir for gRPC code. Significant testing was done to ensure that the tooling works with Unix make on MacOS as well as GNU-Make (gmake). Testing was also done to ensure the `make all` target works well with `make -j`'s parallel execution flag, allowing dev speedup where possible. Some proto targets were adjusted to allow make to do a better job detecting when file/code generation is not necessary, saving time in the common case where there are no proto changes. A future goal, which this helps lay groundwork for, will be to make sure that there are top-level make targets that are usable by Prow CI. For example, 'test' can be used to run all unit tests. Any unit tests added to any component can be included in 'make test' to ensure we don't need to make frequent changes to Prow CI definitions. Similarly 'make build' should build all binaries and images to ensure builds work. 'make generate' and 'make codegen' should include targets that generate files in any way, which will allow Prow CI to ensure developers are running all generators. In the future, we have room to add intall and/or e2e tests as make targets. Signed-off-by: Blaine Gardner <[email protected]>
1 parent 00090de commit e37b86f

File tree

12 files changed

+219
-171
lines changed

12 files changed

+219
-171
lines changed

Diff for: Makefile

+90-18
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,98 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
PROJECTNAME := $(shell basename "$(PWD)")
16-
GOFILES := $(wildcard controller/*.go)
17-
GOBIN := $(GOBASE)/bin
15+
.DEFAULT_GOAL := help
16+
.SUFFIXES: # remove legacy builtin suffixes to allow easier make debugging
17+
SHELL = /usr/bin/env bash
1818

19+
.PHONY: help
20+
help: ## Display this help.
21+
@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)
1922

20-
#CMDS=cosi-controller-manager
21-
all: unit build
22-
#.PHONY: reltools
23-
reltools: release-tools/build.make
24-
release-tools/build.make:
25-
echo "TODO: update kubernetes/test-infra when controller and sidecar can build successfully"
23+
##
24+
## ==== ARGS ===== #
2625

27-
build:
28-
test:
29-
unit:
30-
codegen:
31-
@echo "Running update-codegen to generate the code..."
32-
bash ./hack/update-codegen.sh
26+
## Container build tool compatible with `docker` API
27+
DOCKER ?= docker
3328

34-
@echo "Running update-crd to generate the crd..."
35-
bash ./hack/update-crd.sh
29+
## Platform for 'build'
30+
PLATFORM ?= linux/$(GOARCH)
3631

37-
include release-tools/build.make
32+
## Image tag for controller image build
33+
CONTROLLER_TAG ?= cosi-controller:latest
34+
35+
## Image tag for sidecar image build
36+
SIDECAR_TAG ?= cosi-provisioner-sidecar:latest
37+
38+
39+
##@ Development
40+
41+
.PHONY: all .gen
42+
.gen: generate codegen # can be done in parallel with 'make -j'
43+
.NOTPARALLEL: all # codegen must be finished before fmt/vet
44+
all: .gen fmt vet build ## Build all targets, plus their prerequisites (faster with 'make -j')
45+
46+
.PHONY: generate
47+
generate: controller/Dockerfile sidecar/Dockerfile ## Generate files
48+
$(MAKE) -C client crds
49+
$(MAKE) -C proto generate
50+
51+
.PHONY: codegen
52+
codegen: codegen.client codegen.proto ## Generate code
53+
54+
.PHONY: fmt
55+
fmt: fmt.client fmt.controller fmt.sidecar ## Format code
56+
57+
.PHONY: vet
58+
vet: vet.client vet.controller vet.sidecar ## Vet code
59+
60+
.PHONY: test
61+
test: .test.proto test.client test.controller test.sidecar ## Run tests including unit tests
62+
63+
64+
##@ Build
65+
66+
.PHONY: build
67+
build: build.controller build.sidecar ## Build all container images for development
68+
69+
.PHONY: build.controller build.sidecar
70+
build.controller: controller/Dockerfile ## Build only the controller container image
71+
$(DOCKER) build --file controller/Dockerfile --platform $(PLATFORM) --tag $(CONTROLLER_TAG) .
72+
build.sidecar: sidecar/Dockerfile ## Build only the sidecar container image
73+
$(DOCKER) build --file sidecar/Dockerfile --platform $(PLATFORM) --tag $(SIDECAR_TAG) .
74+
75+
.PHONY: clean
76+
## Clean build environment
77+
clean:
78+
$(MAKE) -C proto clean
79+
80+
.PHONY: clobber
81+
## Clean build environment and cached tools
82+
clobber:
83+
$(MAKE) -C proto clobber
84+
85+
86+
##
87+
## === INTERMEDIATES === #
88+
89+
%/Dockerfile: hack/Dockerfile.in hack/gen-dockerfile.sh
90+
hack/gen-dockerfile.sh $* > "$@"
91+
92+
codegen.%: FORCE
93+
$(MAKE) -C $* codegen
94+
95+
fmt.%: FORCE
96+
cd $* && go fmt ./...
97+
98+
vet.%: FORCE
99+
cd $* && go vet ./...
100+
101+
test.%: fmt.% vet.% FORCE
102+
cd $* && go test ./...
103+
104+
.PHONY: .test.proto
105+
.test.proto: # gRPC proto has a special unit test
106+
$(MAKE) -C proto check
107+
108+
.PHONY: FORCE # use this to force phony behavior for targets with pattern rules
109+
FORCE:

Diff for: client/Makefile

+2-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
GO ?= go
2-
31
# # Setting SHELL to bash allows bash commands to be executed by recipes.
42
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
53
SHELL = /usr/bin/env bash -o pipefail
@@ -15,34 +13,14 @@ help: ## Display this help.
1513
##@ Development
1614

1715
.PHONY: generate
18-
generate: crds codegen fmt module vendor ## Run all code generation/modification tools.
19-
20-
.PHONY: test
21-
test: vet ## Run all tests
16+
generate: crds codegen fmt ## Run all code generation/modification tools.
2217

2318
##@ Generators
2419

2520
.PHONY: crds
2621
crds: ## Generate CustomResourceDefinitions.
27-
$(GO) run -v ./vendor/sigs.k8s.io/controller-tools/cmd/controller-gen crd paths="./apis/objectstorage/..."
22+
go run -v ./vendor/sigs.k8s.io/controller-tools/cmd/controller-gen crd paths="./apis/objectstorage/..."
2823

2924
.PHONY:
3025
codegen: ## Generate deepcopy, client, informer, and lister implementations.
3126
./hack/update-codegen.sh
32-
33-
.PHONY: fmt
34-
fmt: ## Run go fmt
35-
$(GO) fmt ./...
36-
37-
.PHONY: module ## Update go mod
38-
$(GO) mod tidy
39-
40-
.PHONY: vendor
41-
vendor: ## Update vendor dir
42-
$(GO) mod vendor
43-
44-
##@ Tests
45-
46-
.PHONY: vet
47-
vet: ## Run go vet
48-
$(GO) vet ./...

Diff for: controller/Dockerfile

+18-15
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,46 @@
1-
# NOTE: context is expected to be repo root, not controller/ dir
1+
# DO NOT EDIT! This file is generated by 'hack/gen-dockerfile.sh' from 'Dockerfile.in'
22

33
#
44
# BUILDER
55
#
6-
FROM docker.io/library/golang:1.22.5 AS builder
6+
FROM docker.io/library/golang:1.22 AS builder
77

88
WORKDIR /buildroot
99

1010
# Cache deps before building and copying source, so that we don't need to re-download
1111
# as much and so that source changes don't invalidate our downloaded layer.
1212
COPY client/ client/
13+
COPY proto/ proto/
1314
COPY internal/ internal/
15+
1416
COPY controller/go.mod controller/go.mod
1517
COPY controller/go.sum controller/go.sum
1618

17-
WORKDIR /buildroot/controller
18-
19-
RUN go mod download
19+
RUN cd controller && go mod download
2020

21-
COPY controller/cmd/ cmd/
22-
COPY controller/pkg/ pkg/
21+
COPY controller/cmd/ controller/cmd/
22+
COPY controller/pkg/ controller/pkg/
2323

24-
ENV CGO_ENABLED=0
24+
WORKDIR /buildroot/controller
2525

26-
RUN go build -o artifacts/controller-manager cmd/controller-manager/*.go
26+
RUN CGO_ENABLED=0 go build -o /buildroot/artifacts/controller cmd/*.go
2727

2828

2929
#
3030
# FINAL IMAGE
3131
#
32-
FROM gcr.io/distroless/static:latest
32+
FROM gcr.io/distroless/static:nonroot
3333

3434
LABEL maintainers="Kubernetes Authors"
35-
LABEL description="COSI Controller"
35+
LABEL description="COSI controller"
3636

37-
LABEL org.opencontainers.image.title="COSI Controller"
38-
LABEL org.opencontainers.image.description="Container Object Storage Interface (COSI) Controller"
37+
LABEL org.opencontainers.image.title="COSI controller"
38+
LABEL org.opencontainers.image.description="Container Object Storage Interface (COSI) controller"
3939
LABEL org.opencontainers.image.source="https://github.com/kubernetes-sigs/container-object-storage-interface-api/controller"
4040
LABEL org.opencontainers.image.licenses="APACHE-2.0"
4141

42-
COPY --from=builder /buildroot/controller/artifacts/controller-manager .
43-
ENTRYPOINT ["/controller-manager"]
42+
WORKDIR /
43+
COPY --from=builder /buildroot/artifacts/controller .
44+
USER 65532:65532
45+
46+
ENTRYPOINT ["/controller"]

Diff for: controller/Makefile

-38
This file was deleted.

Diff for: hack/Dockerfile.in

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#
2+
# BUILDER
3+
#
4+
FROM docker.io/library/golang:1.22 AS builder
5+
6+
WORKDIR /buildroot
7+
8+
# Cache deps before building and copying source, so that we don't need to re-download
9+
# as much and so that source changes don't invalidate our downloaded layer.
10+
COPY client/ client/
11+
COPY proto/ proto/
12+
COPY internal/ internal/
13+
14+
COPY {{COMPONENT}}/go.mod {{COMPONENT}}/go.mod
15+
COPY {{COMPONENT}}/go.sum {{COMPONENT}}/go.sum
16+
17+
RUN cd {{COMPONENT}} && go mod download
18+
19+
COPY {{COMPONENT}}/cmd/ {{COMPONENT}}/cmd/
20+
COPY {{COMPONENT}}/pkg/ {{COMPONENT}}/pkg/
21+
22+
WORKDIR /buildroot/{{COMPONENT}}
23+
24+
RUN CGO_ENABLED=0 go build -o /buildroot/artifacts/{{COMPONENT}} cmd/*.go
25+
26+
27+
#
28+
# FINAL IMAGE
29+
#
30+
FROM gcr.io/distroless/static:nonroot
31+
32+
LABEL maintainers="Kubernetes Authors"
33+
LABEL description="COSI {{COMPONENT}}"
34+
35+
LABEL org.opencontainers.image.title="COSI {{COMPONENT}}"
36+
LABEL org.opencontainers.image.description="Container Object Storage Interface (COSI) {{COMPONENT}}"
37+
LABEL org.opencontainers.image.source="https://github.com/kubernetes-sigs/container-object-storage-interface-api/{{COMPONENT}}"
38+
LABEL org.opencontainers.image.licenses="APACHE-2.0"
39+
40+
WORKDIR /
41+
COPY --from=builder /buildroot/artifacts/{{COMPONENT}} .
42+
USER 65532:65532
43+
44+
ENTRYPOINT ["/{{COMPONENT}}"]

Diff for: hack/gen-dockerfile.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
set -o errexit
3+
set -o nounset
4+
# set -o xtrace
5+
6+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
7+
8+
COMPONENT="$1"
9+
10+
echo "# DO NOT EDIT! This file is generated by '${BASH_SOURCE[0]}' from 'Dockerfile.in'"
11+
echo "" # empty line
12+
sed "s/{{COMPONENT}}/$COMPONENT/g" "$SCRIPT_DIR"/Dockerfile.in

0 commit comments

Comments
 (0)