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

standardize make tooling with top level Makefile #93

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 90 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,98 @@
# See the License for the specific language governing permissions and
# limitations under the License.

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

.PHONY: help
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)

#CMDS=cosi-controller-manager
all: unit build
#.PHONY: reltools
reltools: release-tools/build.make
release-tools/build.make:
echo "TODO: update kubernetes/test-infra when controller and sidecar can build successfully"
##
## ==== ARGS ===== #

build:
test:
unit:
codegen:
@echo "Running update-codegen to generate the code..."
bash ./hack/update-codegen.sh
## Container build tool compatible with `docker` API
DOCKER ?= docker

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

include release-tools/build.make
## Image tag for controller image build
CONTROLLER_TAG ?= cosi-controller:latest

## Image tag for sidecar image build
SIDECAR_TAG ?= cosi-provisioner-sidecar:latest


##@ Development

.PHONY: all .gen
.gen: generate codegen # can be done in parallel with 'make -j'
.NOTPARALLEL: all # codegen must be finished before fmt/vet
all: .gen fmt vet build ## Build all targets, plus their prerequisites (faster with 'make -j')

.PHONY: generate
generate: controller/Dockerfile sidecar/Dockerfile ## Generate files
$(MAKE) -C client crds
$(MAKE) -C proto generate

.PHONY: codegen
codegen: codegen.client codegen.proto ## Generate code

.PHONY: fmt
fmt: fmt.client fmt.controller fmt.sidecar ## Format code

.PHONY: vet
vet: vet.client vet.controller vet.sidecar ## Vet code

.PHONY: test
test: .test.proto test.client test.controller test.sidecar ## Run tests including unit tests


##@ Build

.PHONY: build
build: build.controller build.sidecar ## Build all container images for development

.PHONY: build.controller build.sidecar
build.controller: controller/Dockerfile ## Build only the controller container image
$(DOCKER) build --file controller/Dockerfile --platform $(PLATFORM) --tag $(CONTROLLER_TAG) .
build.sidecar: sidecar/Dockerfile ## Build only the sidecar container image
$(DOCKER) build --file sidecar/Dockerfile --platform $(PLATFORM) --tag $(SIDECAR_TAG) .

.PHONY: clean
## Clean build environment
clean:
$(MAKE) -C proto clean

.PHONY: clobber
## Clean build environment and cached tools
clobber:
$(MAKE) -C proto clobber


##
## === INTERMEDIATES === #

%/Dockerfile: hack/Dockerfile.in hack/gen-dockerfile.sh
hack/gen-dockerfile.sh $* > "$@"

codegen.%: FORCE
$(MAKE) -C $* codegen

fmt.%: FORCE
cd $* && go fmt ./...

vet.%: FORCE
cd $* && go vet ./...

test.%: fmt.% vet.% FORCE
cd $* && go test ./...

.PHONY: .test.proto
.test.proto: # gRPC proto has a special unit test
$(MAKE) -C proto check

.PHONY: FORCE # use this to force phony behavior for targets with pattern rules
FORCE:
26 changes: 2 additions & 24 deletions client/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
GO ?= go

# # Setting SHELL to bash allows bash commands to be executed by recipes.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
SHELL = /usr/bin/env bash -o pipefail
Expand All @@ -15,34 +13,14 @@ help: ## Display this help.
##@ Development

.PHONY: generate
generate: crds codegen fmt module vendor ## Run all code generation/modification tools.

.PHONY: test
test: vet ## Run all tests
generate: crds codegen fmt ## Run all code generation/modification tools.

##@ Generators

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

.PHONY:
codegen: ## Generate deepcopy, client, informer, and lister implementations.
./hack/update-codegen.sh

.PHONY: fmt
fmt: ## Run go fmt
$(GO) fmt ./...

.PHONY: module ## Update go mod
$(GO) mod tidy

.PHONY: vendor
vendor: ## Update vendor dir
$(GO) mod vendor

##@ Tests

.PHONY: vet
vet: ## Run go vet
$(GO) vet ./...
33 changes: 18 additions & 15 deletions controller/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,43 +1,46 @@
# NOTE: context is expected to be repo root, not controller/ dir
# DO NOT EDIT! This file is generated by 'hack/gen-dockerfile.sh' from 'Dockerfile.in'

#
# BUILDER
#
FROM docker.io/library/golang:1.22.5 AS builder
FROM docker.io/library/golang:1.22 AS builder

WORKDIR /buildroot

# Cache deps before building and copying source, so that we don't need to re-download
# as much and so that source changes don't invalidate our downloaded layer.
COPY client/ client/
COPY proto/ proto/
COPY internal/ internal/

COPY controller/go.mod controller/go.mod
COPY controller/go.sum controller/go.sum

WORKDIR /buildroot/controller

RUN go mod download
RUN cd controller && go mod download

COPY controller/cmd/ cmd/
COPY controller/pkg/ pkg/
COPY controller/cmd/ controller/cmd/
COPY controller/pkg/ controller/pkg/

ENV CGO_ENABLED=0
WORKDIR /buildroot/controller

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


#
# FINAL IMAGE
#
FROM gcr.io/distroless/static:latest
FROM gcr.io/distroless/static:nonroot

LABEL maintainers="Kubernetes Authors"
LABEL description="COSI Controller"
LABEL description="COSI controller"

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

COPY --from=builder /buildroot/controller/artifacts/controller-manager .
ENTRYPOINT ["/controller-manager"]
WORKDIR /
COPY --from=builder /buildroot/artifacts/controller .
USER 65532:65532

ENTRYPOINT ["/controller"]
38 changes: 0 additions & 38 deletions controller/Makefile

This file was deleted.

44 changes: 44 additions & 0 deletions hack/Dockerfile.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# BUILDER
#
FROM docker.io/library/golang:1.22 AS builder

WORKDIR /buildroot

# Cache deps before building and copying source, so that we don't need to re-download
# as much and so that source changes don't invalidate our downloaded layer.
COPY client/ client/
COPY proto/ proto/
COPY internal/ internal/

COPY {{COMPONENT}}/go.mod {{COMPONENT}}/go.mod
COPY {{COMPONENT}}/go.sum {{COMPONENT}}/go.sum

RUN cd {{COMPONENT}} && go mod download

COPY {{COMPONENT}}/cmd/ {{COMPONENT}}/cmd/
COPY {{COMPONENT}}/pkg/ {{COMPONENT}}/pkg/

WORKDIR /buildroot/{{COMPONENT}}

RUN CGO_ENABLED=0 go build -o /buildroot/artifacts/{{COMPONENT}} cmd/*.go


#
# FINAL IMAGE
#
FROM gcr.io/distroless/static:nonroot

LABEL maintainers="Kubernetes Authors"
LABEL description="COSI {{COMPONENT}}"

LABEL org.opencontainers.image.title="COSI {{COMPONENT}}"
LABEL org.opencontainers.image.description="Container Object Storage Interface (COSI) {{COMPONENT}}"
LABEL org.opencontainers.image.source="https://github.com/kubernetes-sigs/container-object-storage-interface-api/{{COMPONENT}}"
LABEL org.opencontainers.image.licenses="APACHE-2.0"

WORKDIR /
COPY --from=builder /buildroot/artifacts/{{COMPONENT}} .
USER 65532:65532

ENTRYPOINT ["/{{COMPONENT}}"]
12 changes: 12 additions & 0 deletions hack/gen-dockerfile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -o errexit
set -o nounset
# set -o xtrace

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

COMPONENT="$1"

echo "# DO NOT EDIT! This file is generated by '${BASH_SOURCE[0]}' from 'Dockerfile.in'"
echo "" # empty line
sed "s/{{COMPONENT}}/$COMPONENT/g" "$SCRIPT_DIR"/Dockerfile.in
Loading