-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMakefile
237 lines (206 loc) · 6.29 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
231
232
233
234
235
236
237
VERSION = $(shell git describe --tags --dirty --always)
GIT_COMMIT = $(shell git rev-parse HEAD)
REPO = github.com/leg100/etok
RANDOM_SUFFIX := $(shell cat /dev/urandom | tr -dc 'a-z0-9' | head -c5)
BUILD_BIN ?= ./etok
KUBECTL = kubectl --context=$(KUBECTX)
KUBE_VERSION = v0.18.2
LD_FLAGS = " \
-X '$(REPO)/pkg/version.Version=$(VERSION)' \
-X '$(REPO)/pkg/version.Commit=$(GIT_COMMIT)' \
" \
ENV ?= kind
IMG ?= etok
TAG ?= $(VERSION)-$(RANDOM_SUFFIX)
KUBECTX=""
# Override vars if ENV=gke
ifeq ($(ENV),gke)
IMG = $(GKE_IMAGE)
DEPLOY_FLAGS = --sa-annotations iam.gke.io/gcp-service-account=$(BACKUP_SERVICE_ACCOUNT)
KUBECTX = $(GKE_KUBE_CONTEXT)
endif
# Override vars if ENV=kind
ifeq ($(ENV),kind)
KUBECTX=kind-kind
endif
# Development-mode
ifeq ($(BINDATA_DEBUG),true)
BINDATA_FLAGS=-debug
endif
# 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
# Even though operator runs outside the cluster, it still creates pods. So an image still needs to
# be built and pushed/loaded first.
.PHONY: local
local: image push
ETOK_IMAGE=$(IMG):$(TAG) $(BUILD_BIN) operator --context $(KUBECTX)
# Same as above - image still needs to be built and pushed/loaded
.PHONY: deploy
deploy: image push deploy-operator-secret
$(BUILD_BIN) install --context $(KUBECTX) --image $(IMG):$(TAG) $(DEPLOY_FLAGS) \
--backup-provider=gcs --gcs-bucket=$(BACKUP_BUCKET)
# Deploy github webhook server
.PHONY: deploy-github
deploy-github: image push
$(BUILD_BIN) github deploy --context $(KUBECTX) --image $(IMG):$(TAG) --url foo.bar
# Tail operator logs
.PHONY: logs
logs:
$(KUBECTL) --namespace=etok logs -f deploy/etok
# Deploy only CRDs
.PHONY: crds
crds: build
$(BUILD_BIN) install --context $(KUBECTX) --crds-only
.PHONY: undeploy
undeploy: build delete-operator-secret
$(BUILD_BIN) install --dry-run | $(KUBECTL) delete -f - --wait --ignore-not-found=true
# Deploy a secret containing GCP svc acc key, on kind, for the operator to use
.PHONY: deploy-operator-secret
deploy-operator-secret: delete-operator-secret create-operator-namespace
ifeq ($(ENV),kind)
$(KUBECTL) --namespace=etok create secret generic etok --from-file=GOOGLE_CREDENTIALS=$(GOOGLE_APPLICATION_CREDENTIALS)
endif
.PHONY: delete-operator-secret
delete-operator-secret:
ifeq ($(ENV),kind)
$(KUBECTL) --namespace=etok delete secret etok --ignore-not-found
endif
# Create operator namespace, ignore already exists errors
.PHONY: create-operator-namespace
create-operator-namespace:
ifeq ($(ENV),kind)
$(KUBECTL) create namespace etok 2>/dev/null || true
endif
.PHONY: e2e
e2e: image push deploy
go test -v ./test/e2e -failfast -context $(KUBECTX)
.PHONY: unit
unit:
go test ./ ./cmd/... ./pkg/...
.PHONY: build
build:
CGO_ENABLED=0 go build -o $(BUILD_BIN) -ldflags $(LD_FLAGS) github.com/leg100/etok
.PHONY: install
install:
go install -ldflags $(LD_FLAGS) github.com/leg100/etok
.PHONY: install-latest-release
install-latest-release:
{ \
set -e ;\
ZIP_FILE=$$(tempfile --prefix=etok --suffix=.zip) ;\
RELEASE_URL=$$(curl -s https://api.github.com/repos/leg100/etok/releases/latest | \
jq -r '.assets[] | select(.name | test(".*_linux_amd64.zip$$")) | .browser_download_url') ;\
curl -Lo $$ZIP_FILE $$RELEASE_URL ;\
unzip -o -d $(GOBIN) $$ZIP_FILE ;\
}
.PHONY: image
image: build
docker build -f build/Dockerfile -t $(IMG):$(TAG) .
.PHONY: push
push:
ifeq ($(ENV),kind)
kind load docker-image $(IMG):$(TAG)
else
docker push $(IMG):$(TAG)
endif
# Generate manifests e.g. CRD, RBAC etc.
.PHONY: manifests
manifests: controller-gen
$(CONTROLLER_GEN) crd \
paths=./api/etok.dev/v1alpha1/run_types.go \
paths=./api/etok.dev/v1alpha1/workspace_types.go \
paths=./api/etok.dev/v1alpha1/groupversion_info.go \
output:artifacts:config=config/operator/
$(CONTROLLER_GEN) rbac:roleName=etok \
paths=./pkg/controllers/run_controller.go \
paths=./pkg/controllers/workspace_controller.go \
output:artifacts:config=config/operator/
$(CONTROLLER_GEN) crd \
paths=./api/etok.dev/v1alpha1/check_suite_types.go \
paths=./api/etok.dev/v1alpha1/check_run_types.go \
paths=./api/etok.dev/v1alpha1/groupversion_info.go \
output:artifacts:config=config/webhook/
$(CONTROLLER_GEN) rbac:roleName=webhook \
paths=./cmd/github/... \
output:artifacts:config=config/webhook/
# Run go fmt against code
.PHONY: fmt
fmt:
go fmt ./...
# Run go vet against code
.PHONY: vet
vet:
go vet ./...
# Generate code (deep-copy funcs)
.PHONY: generate
generate: controller-gen
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
# find or download controller-gen
# download controller-gen if necessary
.PHONY: controller-gen
controller-gen:
ifeq (, $(shell which controller-gen))
@{ \
set -e ;\
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$CONTROLLER_GEN_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.4.0 ;\
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
}
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif
.PHONY: generate-clientset
generate-clientset: client-gen
@{ \
set -e ;\
rm -rf pkg/k8s/etokclient ;\
$(CLIENT_GEN) \
--clientset-name etokclient \
--input-base github.com/leg100/etok/api \
--input etok.dev/v1alpha1 \
-h hack/boilerplate.go.txt \
-p github.com/leg100/etok/pkg/k8s ;\
mv github.com/leg100/etok/pkg/k8s/etokclient pkg/k8s/ ;\
rm -rf github.com ;\
}
# find or download client-gen
# download client-gen if necessary
.PHONY: client-gen
client-gen:
ifeq (, $(shell which client-gen))
@{ \
set -e ;\
CLIENT_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$CLIENT_GEN_TMP_DIR ;\
go mod init tmp ;\
go get k8s.io/code-generator/cmd/client-gen@$(KUBE_VERSION) ;\
rm -rf $$CLIENT_GEN_TMP_DIR ;\
}
CLIENT_GEN=$(GOBIN)/client-gen
else
CLIENT_GEN=$(shell which client-gen)
endif
.PHONY: kustomize
kustomize:
ifeq (, $(shell which kustomize))
@{ \
set -e ;\
KUSTOMIZE_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$KUSTOMIZE_GEN_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/kustomize/kustomize/v3@v3.5.4 ;\
rm -rf $$KUSTOMIZE_GEN_TMP_DIR ;\
}
KUSTOMIZE=$(GOBIN)/kustomize
else
KUSTOMIZE=$(shell which kustomize)
endif