forked from githedgehog/k8s-tpm-device-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
137 lines (110 loc) · 6.16 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Copyright 2023 Hedgehog SONiC Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
SHELL := bash
.SHELLFLAGS := -e -c
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules
MKFILE_DIR := $(shell echo $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) | sed 'sA/$$AA')
BUILD_DIR := $(MKFILE_DIR)/build
BUILD_ARTIFACTS_DIR := $(BUILD_DIR)/artifacts
BUILD_COVERAGE_DIR := $(BUILD_DIR)/coverage
SRC_FILES := $(shell find $(MKFILE_DIR)/cmd -type f -name "*.go")
SRC_FILES += $(shell find $(MKFILE_DIR)/internal -type f -name "*.go")
SRC_FILES += $(shell find $(MKFILE_DIR)/pkg -type f -name "*.go")
# golang requires for modules that their tags have the 'v' prefixed which is not semver 2 compliant
# however, the rest of a 'git describe --tags --dirty' output is, so this does the trick for us internally
VERSION ?= $(shell git describe --tags --dirty)
DOCKER_BUILDX_FLAGS ?=
#DOCKER_PLATFORMS ?= linux/amd64,linux/arm64
DOCKER_PLATFORMS ?= linux/amd64
DOCKER_TAG ?= ghcr.io/keylime/k8s-tpm-device-plugin:$(VERSION)
# helm chart version must be semver 2 compliant
HELM_CHART_VERSION ?= $(shell echo $(VERSION) | sed 's/^v//')
HELM_CHART_DIR := $(BUILD_DIR)/helm/k8s-tpm-device-plugin
HELM_CHART_FILES := $(shell find $(HELM_CHART_DIR) -type f)
HELM_CHART_REPO ?= ghcr.io/keylime/k8s-tpm-device-plugin/helm-charts
##@ General
# The help target prints out all targets with their descriptions organized
# beneath their categories. The categories are represented by '##@' and the
# target descriptions by '##'. The awk commands is responsible for reading the
# entire set of makefiles included in this invocation, looking for lines of the
# file as xyz: ## something, and then pretty-format the target and help. Then,
# if there's a line with ##@ something, that gets pretty-printed as a category.
# More info on the usage of ANSI control characters for terminal formatting:
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
# More info on the awk command:
# http://linuxcommand.org/lc3_adv_awk.php
.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)
all: build
##@ Development
build: k8s-tpm-device-plugin ## Builds the k8s-tpm-device-plugin for amd64 and arm64 architectures
clean: k8s-tpm-device-plugin-clean helm-clean ## Cleans the k8s-tpm-device-plugin builds as well as helm
k8s-tpm-device-plugin: $(BUILD_ARTIFACTS_DIR)/k8s-tpm-device-plugin-amd64 $(BUILD_ARTIFACTS_DIR)/k8s-tpm-device-plugin-arm64
.PHONY: k8s-tpm-device-plugin-clean
k8s-tpm-device-plugin-clean:
rm -v $(BUILD_ARTIFACTS_DIR)/k8s-tpm-device-plugin-amd64 2>/dev/null || true
rm -v $(BUILD_ARTIFACTS_DIR)/k8s-tpm-device-plugin-arm64 2>/dev/null || true
$(BUILD_ARTIFACTS_DIR)/k8s-tpm-device-plugin-amd64: $(SRC_FILES)
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o $(BUILD_ARTIFACTS_DIR)/k8s-tpm-device-plugin-amd64 -ldflags="-w -s -X 'go.githedgehog.com/k8s-tpm-device-plugin/pkg/version.Version=$(VERSION)'" ./cmd/k8s-tpm-device-plugin
$(BUILD_ARTIFACTS_DIR)/k8s-tpm-device-plugin-arm64: $(SRC_FILES)
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o $(BUILD_ARTIFACTS_DIR)/k8s-tpm-device-plugin-arm64 -ldflags="-w -s -X 'go.githedgehog.com/k8s-tpm-device-plugin/pkg/version.Version=$(VERSION)'" ./cmd/k8s-tpm-device-plugin
# Use this target only for local linting. In CI we use a dedicated github action
.PHONY: lint
lint: ## Runs golangci-lint (NOTE: target for local development only, used through github action in CI)
golangci-lint run --verbose ./...
test: test-race test-cover ## Runs golang unit tests twice: for code coverage, and the second time with race detector
.PHONY: test-race
test-race: ## Runs golang unit tests with race detector
@echo "Running tests with race detector..."
go test -race ./cmd/... ./pkg/...
@echo
.PHONY: test-cover
test-cover: ## Runs golang unit tests and generates code coverage information
@echo "Running tests for code coverage..."
go test -cover -covermode=count -coverprofile $(BUILD_COVERAGE_DIR)/coverage.profile ./cmd/... ./pkg/...
go tool cover -func=$(BUILD_COVERAGE_DIR)/coverage.profile -o=$(BUILD_COVERAGE_DIR)/coverage.out
go tool cover -html=$(BUILD_COVERAGE_DIR)/coverage.profile -o=$(BUILD_COVERAGE_DIR)/coverage.html
@echo
@echo -n "Total Code Coverage: "; tail -n 1 $(BUILD_COVERAGE_DIR)/coverage.out | awk '{ print $$3 }'
@echo
##@ Build
.PHONY: docker-build
docker-build:
## Builds the application in a docker container and creates a docker image
docker buildx build \
-f $(MKFILE_DIR)/build/docker/k8s-tpm-device-plugin/Dockerfile \
-t $(DOCKER_TAG) \
--progress=plain \
--build-arg APPVERSION=$(VERSION) \
--build-arg TARGETHOSTARCH=x86_64 \
--build-arg MKIMAGEARCH=x86_64 \
--build-arg GPG_PUBKEY=$(GPG_PUBKEY) \
--platform=$(DOCKER_PLATFORMS) $(DOCKER_BUILDX_FLAGS) \
. 2>&1
.PHONY: docker-push
docker-push: ## Pushes a previously built docker container
docker push $(DOCKER_TAG)
helm: $(BUILD_ARTIFACTS_DIR)/k8s-tpm-device-plugin-$(HELM_CHART_VERSION).tgz ## Builds a helm chart
$(BUILD_ARTIFACTS_DIR)/k8s-tpm-device-plugin-$(HELM_CHART_VERSION).tgz: $(HELM_CHART_FILES)
helm lint $(HELM_CHART_DIR)
helm package $(HELM_CHART_DIR) --version $(HELM_CHART_VERSION) --app-version $(VERSION) -d $(BUILD_ARTIFACTS_DIR)
.PHONY: helm-clean
helm-clean: ## Cleans the packaged helm chart
rm -v $(BUILD_ARTIFACTS_DIR)/k8s-tpm-device-plugin-$(HELM_CHART_VERSION).tgz 2>/dev/null || true
.PHONY: helm-push
helm-push: helm ## Builds AND pushes the helm chart
helm push $(BUILD_ARTIFACTS_DIR)/k8s-tpm-device-plugin-$(HELM_CHART_VERSION).tgz oci://$(HELM_CHART_REPO)