-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathMakefile
More file actions
161 lines (130 loc) · 4.74 KB
/
Copy pathMakefile
File metadata and controls
161 lines (130 loc) · 4.74 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
GOMOD ?= on
GO ?= GO111MODULE=$(GOMOD) go
BINPATH := $(abspath ./bin)
#retrieve go version details for version check
GO_VERSION := $(shell $(GO) version | sed -e 's/^[^0-9.]*\([0-9.]*\).*/\1/')
GO_VERSION_MAJ := $(shell echo $(GO_VERSION) | cut -f1 -d'.')
GO_VERSION_MIN := $(shell echo $(GO_VERSION) | cut -f2 -d'.')
GOFMT ?= gofmt
RM = rm
GOBINPATH := $(shell $(GO) env GOPATH)/bin
COMMIT := $(shell git rev-parse HEAD)
DATE_FMT = +%Y%m%d
ifdef SOURCE_DATE_EPOCH
BUILD_DATE ?= $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" $(DATE_FMT))
else
BUILD_DATE ?= $(shell date $(DATE_FMT))
endif
# TAG can be provided as an envvar (provided in the .spec file)
TAG ?= $(shell git describe --tags --exact-match HEAD 2> /dev/null)
# CLOSEST_TAG can be provided as an envvar (provided in the .spec file)
CLOSEST_TAG ?= $(shell git describe --tags)
# VERSION is inferred from CLOSEST_TAG
# It accepts tags of type `vX.Y.Z`, `vX.Y.Z-(alpha|beta|rc|...)` and produces X.Y.Z
VERSION := $(shell echo $(CLOSEST_TAG) | sed -E 's/v(([0-9]\.?)+).*/\1/')
TAGS := development
PROJECT_PATH := github.com/flavio/kuberlr
KUBERLR_LDFLAGS = -ldflags "-X=$(PROJECT_PATH)/pkg/kuberlr.Version=$(VERSION) \
-X=$(PROJECT_PATH)/pkg/kuberlr.BuildDate=$(BUILD_DATE) \
-X=$(PROJECT_PATH)/pkg/kuberlr.Tag=$(TAG) \
-X=$(PROJECT_PATH)/pkg/kuberlr.ClosestTag=$(CLOSEST_TAG)"
KUBERLR_DIRS = cmd pkg internal
# go source files
KUBERLR_SRCS = $(shell find $(KUBERLR_DIRS) -type f -name '*.go')
# Define target platforms, image builder and the fully qualified image name.
TARGET_PLATFORMS ?= linux/amd64,linux/arm64,linux/s390x
RUNNER := docker
IMAGE_BUILDER := $(RUNNER) buildx
MACHINE := kwctl
REPO ?= flavio
IMAGE = $(REPO)/kuberlr:$(TAG)
BUILD_ACTION = --load
.PHONY: all
all: install
.PHONY: build
build: go-version-check
$(GO) build $(KUBERLR_LDFLAGS) -tags $(TAGS) ./cmd/...
.PHONY: install
install: go-version-check
$(GO) install $(KUBERLR_LDFLAGS) -tags $(TAGS) ./cmd/...
.PHONY: clean
clean:
$(GO) clean -i ./...
$(RM) -f ./kuberlr
$(RM) -rf $(BINPATH)
.PHONY: distclean
distclean: clean
$(GO) clean -i -cache -testcache -modcache ./...
.PHONY: staging
staging:
make TAGS=staging install
.PHONY: release
release:
make TAGS=release install
.PHONY: go-version-check
go-version-check:
@[ $(GO_VERSION_MAJ) -ge 2 ] || \
[ $(GO_VERSION_MAJ) -eq 1 -a $(GO_VERSION_MIN) -ge 26 ] || (echo "FATAL: Go version should be >= 1.26.x" ; exit 1 ; )
.PHONY: lint
lint: golangci-lint
GO111MODULE=on go mod tidy && GO111MODULE=on go mod verify
# run go fmt
test -z `$(GOFMT) -l $(KUBERLR_SRCS)` || { $(GOFMT) -d $(KUBERLR_SRCS) && false; }
# run golangci-lint
$(GOLANGCI_LINT) run
.PHONY: lint-fix
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
$(GOLANGCI_LINT) run --fix
# tests
.PHONY: test
test: test-unit test-bench
.PHONY: test-unit
test-unit:
$(GO) test -coverprofile=coverage.out $(PROJECT_PATH)/{cmd,pkg,internal}/...
.PHONY: test-unit-coverage
test-unit-coverage: test-unit
$(GO) tool cover -html=coverage.out
.PHONY: test-bench
test-bench:
$(GO) test -bench=. $(PROJECT_PATH)/{cmd,pkg,internal}/...
buildx-machine: ## create rancher dockerbuildx machine targeting platform defined by DEFAULT_PLATFORMS.
@docker buildx ls | grep $(MACHINE) || \
docker buildx create --name=$(MACHINE) --platform=$(TARGET_PLATFORMS)
image-build: buildx-machine ## build (and load) the container image targeting the current platform.
$(IMAGE_BUILDER) build -f package/Dockerfile \
--progress plain --no-cache \
--builder $(MACHINE) $(IMAGE_ARGS) \
--build-arg PROJECT_PATH=$(PROJECT_PATH) \
--build-arg VERSION=$(VERSION) \
--build-arg BUILD_DATE=$(BUILD_DATE) \
--build-arg TAG=$(TAG) \
-t "$(IMAGE)" $(BUILD_ACTION) .
@echo "Built $(IMAGE)"
generate-mocks:
mockery
##@ Dependencies
## Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)
## Tool Binaries
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-$(GOLANGCI_LINT_VERSION)
## Tool Versions
GOLANGCI_LINT_VERSION ?= v2.12.1
.PHONY: golangci-lint
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
$(GOLANGCI_LINT): $(LOCALBIN)
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,${GOLANGCI_LINT_VERSION})
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
# $1 - target path with name of binary (ideally with version)
# $2 - package url which can be installed
# $3 - specific version of package
define go-install-tool
@[ -f $(1) ] || { \
set -e; \
package=$(2)@$(3) ;\
echo "Downloading $${package}" ;\
GOBIN=$(LOCALBIN) go install $${package} ;\
mv "$$(echo "$(1)" | sed "s/-$(3)$$//")" $(1) ;\
}
endef