-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from elezar/build-images
Align image builds with other projects
- Loading branch information
Showing
7 changed files
with
256 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Copyright (c) NVIDIA CORPORATION. All rights reserved. | ||
# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
|
@@ -12,113 +12,96 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
DOCKER ?= docker | ||
MKDIR ?= mkdir | ||
DIST_DIR ?= $(CURDIR)/dist | ||
|
||
include $(CURDIR)/versions.mk | ||
|
||
GIT_COMMIT := $(shell git describe --tags --dirty --always) | ||
ifeq ($(IMAGE_NAME),) | ||
REGISTRY ?= nvidia | ||
IMAGE_NAME = $(REGISTRY)/$(NAME) | ||
endif | ||
|
||
##### Go variables ##### | ||
MODULE := github.com/NVIDIA/k8s-kata-manager | ||
GOOS ?= linux | ||
GO_CMD ?= go | ||
GO_FMT ?= gofmt | ||
GO_TEST_FLAGS ?= -race | ||
LDFLAGS = -ldflags "-s -w -X github.com/NVIDIA/k8s-kata-manager/internal/version.version=$(GIT_COMMIT)" | ||
EXAMPLES := $(patsubst ./examples/%/,%,$(sort $(dir $(wildcard ./examples/*/)))) | ||
EXAMPLE_TARGETS := $(patsubst %,example-%, $(EXAMPLES)) | ||
|
||
##### General make targets ##### | ||
CMDS := $(patsubst ./cmd/%/,%,$(sort $(dir $(wildcard ./cmd/*/)))) | ||
CMD_TARGETS := $(patsubst %,cmd-%, $(CMDS)) | ||
|
||
CHECK_TARGETS := assert-fmt lint | ||
MAKE_TARGETS := cmds build install fmt test coverage generate $(CHECK_TARGETS) | ||
CHECK_TARGETS := lint | ||
MAKE_TARGETS := binaries build check fmt lint-internal test examples cmds coverage generate vendor check-vendor $(CHECK_TARGETS) | ||
|
||
TARGETS := $(MAKE_TARGETS) | ||
DOCKER_TARGETS := $(patsubst %, docker-%, $(TARGETS)) | ||
TARGETS := $(MAKE_TARGETS) $(EXAMPLE_TARGETS) $(CMD_TARGETS) | ||
|
||
DOCKER_TARGETS := $(patsubst %,docker-%, $(TARGETS)) | ||
.PHONY: $(TARGETS) $(DOCKER_TARGETS) | ||
|
||
##### Container image variables ##### | ||
BUILD_MULTI_ARCH_IMAGES ?= no | ||
DOCKER ?= docker | ||
BUILDX = | ||
ifeq ($(BUILD_MULTI_ARCH_IMAGES),true) | ||
BUILDX = buildx | ||
ifeq ($(VERSION),) | ||
CLI_VERSION = $(LIB_VERSION)$(if $(LIB_TAG),-$(LIB_TAG)) | ||
else | ||
CLI_VERSION = $(VERSION) | ||
endif | ||
CLI_VERSION_PACKAGE = github.com/NVIDIA/$(NAME)/internal/info | ||
|
||
ifeq ($(IMAGE_NAME),) | ||
REGISTRY ?= nvidia | ||
IMAGE_NAME := $(REGISTRY)/k8s-kata-manager | ||
binaries: cmds | ||
ifneq ($(PREFIX),) | ||
cmd-%: COMMAND_BUILD_OPTIONS = -o $(PREFIX)/$(*) | ||
endif | ||
ifneq ($(shell uname),Darwin) | ||
EXTLDFLAGS = -Wl,--export-dynamic -Wl,--unresolved-symbols=ignore-in-object-files | ||
else | ||
EXTLDFLAGS = -Wl,-undefined,dynamic_lookup | ||
endif | ||
BUILDFLAGS = -ldflags "-s -w '-extldflags=$(EXTLDFLAGS)' -X $(CLI_VERSION_PACKAGE).gitCommit=$(GIT_COMMIT) -X $(CLI_VERSION_PACKAGE).version=$(CLI_VERSION)" | ||
build: | ||
go build $(BUILDFLAGS) ./... | ||
|
||
IMAGE_VERSION := $(VERSION) | ||
|
||
DIST ?= ubi8 | ||
|
||
# Note: currently there is no need to build images for different distributions, | ||
# so the distribution is omitted from the tag | ||
#IMAGE_TAG ?= $(IMAGE_VERSION)-$(DIST) | ||
IMAGE_TAG ?= $(IMAGE_VERSION) | ||
IMAGE = $(IMAGE_NAME):$(IMAGE_TAG) | ||
|
||
OUT_IMAGE_NAME ?= $(IMAGE_NAME) | ||
OUT_IMAGE_VERSION ?= $(IMAGE_VERSION) | ||
#OUT_IMAGE_TAG = $(OUT_IMAGE_VERSION)-$(DIST) | ||
OUT_IMAGE_TAG = $(OUT_IMAGE_VERSION) | ||
OUT_IMAGE = $(OUT_IMAGE_NAME):$(OUT_IMAGE_TAG) | ||
|
||
##### Container image make targets ##### | ||
# Note: currently there is no need to build images for different distributions. | ||
IMAGE_BUILD_TARGETS := build-image | ||
IMAGE_PUSH_TARGETS := push-image | ||
IMAGE_PULL_TARGETS := pull-image | ||
.PHONY: $(IMAGE_BUILD_TARGETS) $(IMAGE_PUSH_TARGETS) | ||
|
||
###### Target definitions ##### | ||
cmds: $(CMD_TARGETS) | ||
$(CMD_TARGETS): cmd-%: | ||
@mkdir -p bin | ||
GOOS=$(GOOS) $(GO_CMD) build -v -o bin $(LDFLAGS) $(COMMAND_BUILD_OPTIONS) $(MODULE)/cmd/$(*) | ||
go build $(BUILDFLAGS) $(COMMAND_BUILD_OPTIONS) $(MODULE)/cmd/$(*) | ||
|
||
build: | ||
GOOS=$(GOOS) $(GO_CMD) build -v $(LDFLAGS) $(MODULE)/... | ||
examples: $(EXAMPLE_TARGETS) | ||
$(EXAMPLE_TARGETS): example-%: | ||
go build $(BUILDFLAGS) $(COMMAND_BUILD_OPTIONS) ./examples/$(*) | ||
|
||
install: | ||
$(GO_CMD) install -v $(LDFLAGS) $(MODULE)/cmd/... | ||
all: check test build binary | ||
check: $(CHECK_TARGETS) | ||
|
||
# Apply go fmt to the codebase | ||
fmt: | ||
$(GO_CMD) list -f '{{.Dir}}' $(MODULE)/... \ | ||
| xargs $(GO_FMT) -s -l -w | ||
|
||
assert-fmt: | ||
$(GO_CMD) list -f '{{.Dir}}' $(MODULE)/... \ | ||
| xargs $(GO_FMT) -s -l | ( grep -v /vendor/ || true ) > fmt.out | ||
@if [ -s fmt.out ]; then \ | ||
echo "\nERROR: The following files are not formatted:\n"; \ | ||
cat fmt.out; \ | ||
rm fmt.out; \ | ||
exit 1; \ | ||
else \ | ||
rm fmt.out; \ | ||
fi | ||
go list -f '{{.Dir}}' $(MODULE)/... \ | ||
| xargs gofmt -s -l -w | ||
|
||
goimports: | ||
go list -f {{.Dir}} $(MODULE)/... \ | ||
| xargs goimports -local $(MODULE) -w | ||
|
||
lint: | ||
golangci-lint run ./... | ||
|
||
vendor: | ||
go mod tidy | ||
go mod vendor | ||
go mod verify | ||
|
||
check-vendor: vendor | ||
git diff --quiet HEAD -- go.mod go.sum vendor | ||
|
||
COVERAGE_FILE := coverage.out | ||
test: build | ||
$(GO_CMD) test -v -coverprofile=$(COVERAGE_FILE) $(MODULE)/... | ||
test: build cmds | ||
go test -coverprofile=$(COVERAGE_FILE) $(MODULE)/cmd/... $(MODULE)/internal/... $(MODULE)/api/... | ||
|
||
coverage: test | ||
cat $(COVERAGE_FILE) | grep -v "_mock.go" > $(COVERAGE_FILE).no-mocks | ||
$(GO_CMD) tool cover -func=$(COVERAGE_FILE).no-mocks | ||
go tool cover -func=$(COVERAGE_FILE).no-mocks | ||
|
||
# Generate code | ||
generate: controller-gen | ||
$(CONTROLLER_GEN) object object:headerFile="hack/boilerplate.go.txt" paths="./api/..." | ||
generate: .generate-api | ||
go generate $(MODULE)/... | ||
|
||
# Download controller-gen locally if necessary | ||
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST)))) | ||
CONTROLLER_GEN = $(PROJECT_DIR)/bin/controller-gen | ||
controller-gen: | ||
@GOBIN=$(PROJECT_DIR)/bin GO111MODULE=on $(GO_CMD) install sigs.k8s.io/controller-tools/cmd/[email protected] | ||
CONTROLLER_GEN ?= controller-gen | ||
.generate-api: | ||
$(CONTROLLER_GEN) object object:headerFile="hack/boilerplate.go.txt" paths="./api/..." | ||
|
||
$(DOCKER_TARGETS): docker-%: | ||
@echo "Running 'make $(*)' in container image $(BUILDIMAGE)" | ||
|
@@ -132,25 +115,15 @@ $(DOCKER_TARGETS): docker-%: | |
$(BUILDIMAGE) \ | ||
make $(*) | ||
|
||
|
||
##### Image build and push targets ##### | ||
build-image: | ||
DOCKER_BUILDKIT=1 \ | ||
$(DOCKER) $(BUILDX) build --pull \ | ||
$(DOCKER_BUILD_OPTIONS) \ | ||
$(DOCKER_BUILD_PLATFORM_OPTIONS) \ | ||
--tag $(IMAGE) \ | ||
--build-arg BASE_DIST="$(DIST)" \ | ||
--build-arg CUDA_VERSION="$(CUDA_VERSION)" \ | ||
--build-arg GOLANG_VERSION="$(GOLANG_VERSION)" \ | ||
--build-arg VERSION="$(VERSION)" \ | ||
--build-arg CVE_UPDATES="$(CVE_UPDATES)" \ | ||
--file Dockerfile.ubi8 \ | ||
$(CURDIR) | ||
|
||
push-image: | ||
$(DOCKER) tag "$(IMAGE)" "$(OUT_IMAGE)" | ||
$(DOCKER) push "$(OUT_IMAGE)" | ||
|
||
pull-image: | ||
$(DOCKER) pull "$(IMAGE)" | ||
# Start an interactive shell using the development image. | ||
PHONY: .shell | ||
.shell: | ||
$(DOCKER) run \ | ||
--rm \ | ||
-ti \ | ||
-e GOCACHE=/tmp/.cache/go \ | ||
-e GOMODCACHE=/tmp/.cache/gomod \ | ||
-v $(PWD):/work \ | ||
-w /work \ | ||
--user $$(id -u):$$(id -g) \ | ||
$(BUILDIMAGE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.