-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
230 lines (171 loc) · 6.07 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
PKG?=github.com/awspca-issuer
BINNAME?=manager
PREFIX?=
# Set V to 1 for verbose output from the Makefile
Q=$(if $V,,@)
# Image URL to use all building/pushing image targets
IMG ?= awspca-issuer:latest
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true"
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif
all: build test lint
.PHONY: all
#########################################
# Bootstrapping
#########################################
bootstra%:
# Using a released version of golangci-lint to take into account custom replacements in their go.mod
$Q curl -sSfL https://raw.githubusercontent.com/smallstep/cli/master/make/golangci-install.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.23.8
.PHONY: bootstra%
#################################################
# Determine the type of `push` and `version`
#################################################
# If TRAVIS_TAG is set then we know this ref has been tagged.
ifdef TRAVIS_TAG
VERSION := $(TRAVIS_TAG)
NOT_RC := $(shell echo $(VERSION) | grep -v -e -rc)
ifeq ($(NOT_RC),)
PUSHTYPE := release-candidate
else
PUSHTYPE := release
endif
else
VERSION ?= $(shell [ -d .git ] && git describe --tags --always --dirty="-dev")
VERSION := $(or $(VERSION),v0.0.0)
PUSHTYPE := master
endif
VERSION := $(shell echo $(VERSION) | sed 's/^v//')
ifdef V
$(info TRAVIS_TAG is $(TRAVIS_TAG))
$(info VERSION is $(VERSION))
$(info PUSHTYPE is $(PUSHTYPE))
endif
#########################################
# Test
#########################################
test: generate fmt vet manifests
$Q go test ./api/... ./controllers/... -coverprofile cover.out
.PHONY: test
#########################################
# Build
#########################################
DATE := $(shell date -u '+%Y-%m-%d %H:%M UTC')
LDFLAGS := -ldflags='-w -X "main.Version=$(VERSION)" -X "main.BuildTime=$(DATE)"'
GOFLAGS := CGO_ENABLED=0
build: $(PREFIX)bin/$(BINNAME)
@echo "Build Complete!"
download:
$Q go mod download
$(PREFIX)bin/$(BINNAME): download generate $(call rwildcard,*.go)
$Q mkdir -p $(@D)
$Q $(GOOS_OVERRIDE) $(GOFLAGS) go build -v -o $(PREFIX)bin/$(BINNAME) $(LDFLAGS) $(PKG)
#########################################
# Generate
#########################################
# Generate code
generate: controller-gen
$Q $(CONTROLLER_GEN) object:headerFile=./hack/boilerplate.go.txt paths=./api/...
# find or download controller-gen
# download controller-gen if necessary
controller-gen:
ifeq (, $(shell which controller-gen))
$Q go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.2.5
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif
#########################################
# Install
#########################################
# Generate manifests e.g. CRD, RBAC etc.
manifests: controller-gen
$Q $(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
# Install CRDs into a cluster
install: manifests
$Q kubectl apply -f config/crd/bases
# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
deploy: manifests
$Q kubectl apply -f config/crd/bases
$Q kustomize build config/default | kubectl apply -f -
#########################################
# Format and Linting
#########################################
# Run go fmt against code
fmt:
$Q go fmt ./...
# Run go vet against code
vet:
$Q go vet ./...
lint:
$Q LOG_LEVEL=error golangci-lint run
.PHONY: fmt vet lint
#########################################
# Dev
#########################################
# Run against the configured Kubernetes cluster in ~/.kube/config
run: generate
$Q go run ./main.go
.PHONY: run
#########################################
# Clean
#########################################
clean:
ifneq ($(BINNAME),"")
$Q rm -f bin/$(BINNAME)
endif
.PHONY: clean
#################################################
# Docker
#################################################
DOCKER_OUTPUT=$(OUTPUT_ROOT)docker/
DOCKER_MAKE=V=$V GOOS_OVERRIDE='GOOS=linux GOARCH=amd64' PREFIX=$(1) make $(1)bin/$(2)
DOCKER_BUILD=$Q docker build -t $(IMG) -f $(2) --build-arg BINPATH=$(DOCKER_OUTPUT)bin/$(1) .
docker: docker-make Dockerfile
$(call DOCKER_BUILD,manager,Dockerfile)
docker-make:
$Q mkdir -p $(DOCKER_OUTPUT)
$(call DOCKER_MAKE,$(DOCKER_OUTPUT),manager)
.PHONY: docker docker-make
# Make sure to run a local registry
# docker run -d -p 5000:5000 --restart=always --name registry registry:2
docker-dev: docker
$Q docker tag ${IMG} localhost:5000/${IMG}
$Q docker push localhost:5000/${IMG}
.PHONY: docker-dev
#################################################
# Releasing Docker Images
#################################################
DOCKER_TAG=docker tag $(1):latest $(1):$(2)
DOCKER_PUSH=docker push $(1):$(2)
docker-tag:
$(call DOCKER_TAG,awspca-issuer,$(VERSION))
docker-push-tag: docker-tag
$(call DOCKER_PUSH,awspca-issuer,$(VERSION))
docker-push-tag-latest:
$(call DOCKER_PUSH,awspca-issuer,latest)
# Rely on DOCKER_USERNAME and DOCKER_PASSWORD being set inside the CI or
# equivalent environment
docker-login:
$Q docker login -u="$(DOCKER_USERNAME)" -p="$(DOCKER_PASSWORD)"
.PHONY: docker-login docker-tag docker-push-tag docker-push-tag-latest
#################################################
# Targets for pushing the docker images
#################################################
# For all builds we build the docker container
docker-master: docker
# For all builds with a release candidate tag
docker-release-candidate: docker-master docker-login docker-push-tag
# For all builds with a release tag
docker-release: docker-release-candidate docker-push-tag-latest
.PHONY: docker-master docker-release-candidate docker-release
#################################################
# Targets for creating artifacts
#################################################
# This command is called by travis directly *after* a successful build
artifacts: docker-$(PUSHTYPE)
.PHONY: artifacts