-
Notifications
You must be signed in to change notification settings - Fork 7
/
Makefile
327 lines (269 loc) · 14.8 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# Copyright (C) Nicolas Lamirault <[email protected]>
#
# 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.
#
# SPDX-License-Identifier: Apache-2.0
include hack/build/commons.mk
-include hack/build/$(CLOUD).$(ENV).mk
# ====================================
# D E V E L O P M E N T
# ====================================
##@ Development
.PHONY: clean
clean: ## Cleanup
@echo -e "$(OK_COLOR)[$(BANNER)] Cleanup$(NO_COLOR)"
@find . -name "*.retry"|xargs rm -f
@rm -fr vendor
@rm -fr venv
@find . -name "Chart.lock" | xargs rm -f
@find gitops/argocd/charts/** -name charts | xargs rm -fr
.PHONY: check
check: check-kubectl check-kustomize check-helm check-flux check-conftest check-kubeval check-popeye ## Check requirements
.PHONY: init
init: ## Initialize environment
@poetry install --no-root
@poetry run pre-commit install
.PHONY: doc
doc: ## Generate documentation
@echo -e "$(OK_COLOR)[$(APP)] Documentation$(NO_COLOR)" >&2
@. $(PYTHON_VENV)/bin/activate && mkdocs serve
.PHONY: diagrams
diagrams: guard-CLOUD_PROVIDER guard-OUTPUT ## Generate diagrams
@poetry run python3 diagrams/kubernetes.py --output=$(OUTPUT) --cloud=$(CLOUD_PROVIDER) \
&& mv *.$(OUTPUT) docs/img \
&& poetry run python3 diagrams/portefaix.py --output=$(OUTPUT) --cloud=$(CLOUD_PROVIDER) \
&& mv *.$(OUTPUT) docs/img
.PHONY: validate
validate: ## Execute git-hooks
@poetry run pre-commit run -a
.PHONY: license
license: guard-ACTION ## Check license (ACTION=xxx : fix or check)
@docker run -it --rm -v $(shell pwd):/github/workspace ghcr.io/apache/skywalking-eyes/license-eye --config /github/workspace/.licenserc.yaml header $(ACTION)
# ====================================
# K U B E R N E T E S
# ====================================
##@ Kubernetes
kubernetes-check-context:
@if [[ "$(KUBE_CONTEXT)" != "$(KUBE_CURRENT_CONTEXT)" ]] ; then \
echo -e "$(ERROR_COLOR)Invalid Kubernetes context$(NO_COLOR): $(KUBE_CONTEXT) vs $(KUBE_CURRENT_CONTEXT)"; \
exit 1; \
fi
.PHONY: kubernetes-switch
kubernetes-switch: guard-ENV ## Switch Kubernetes context (ENV=xxx)
@kubectl config use-context $(KUBE_CONTEXT)
.PHONY: kubernetes-secret
kubernetes-secret: guard-NAMESPACE guard-NAME guard-FILE ## Generate a Kubernetes secret file (NAME=xxxx NAMESPACE=xxxx FILE=xxxx)
@kubectl create secret generic $(NAME) -n $(NAMESPACE) --dry-run=client --from-file=$(FILE) -o yaml
.PHONY: kubernetes-credentials
kubernetes-credentials: guard-ENV guard-CLOUD ## Generate credentials (CLOUD=xxxx ENV=xxx)
make -f hack/build/$(CLOUD).mk $(CLOUD)-kube-credentials ENV=$(ENV)
# ====================================
# H E L M
# ====================================
##@ Helm
.PHONY: helm-flux-chart
helm-flux-chart: guard-CHART ## Display Helm chart informations (CHART=xxx)
@echo -e "$(OK_COLOR)[$(APP)] Helm repository and chart $(CHART)$(NO_COLOR)" >&2
@DEBUG=true . hack/scripts/chart-fluxcd.sh $(CHART)
.PHONY: helm-flux-repo
helm-flux-repo: guard-CHART ## Configure Helm repository and chart (CHART=xxx)
@echo -e "$(OK_COLOR)[$(APP)] Helm repository and chart $(CHART)$(NO_COLOR)" >&2
@DEBUG=$(DEBUG) . hack/scripts/chart-fluxcd.sh $(CHART) \
&& helm repo add $${CHART_REPO_NAME} $${CHART_REPO_URL} --force-update \
&& helm repo update
.PHONY: helm-flux-values
helm-flux-values: guard-CHART ## Display Helm values (CHART=xxx)
@echo -e "$(OK_COLOR)[$(APP)] Helm show values $(CHART)$(NO_COLOR)" >&2
@DEBUG=$(DEBUG) . hack/scripts/chart-fluxcd.sh $(CHART) \
&& helm show values $${CHART_REPO_NAME}/$${CHART_NAME} --version $${CHART_VERSION}
.PHONY: helm-flux-custom
helm-flux-show: guard-CHART guard-CLOUD guard-ENV ## Show Helm chart values set for Flux (CHART=xxx CLOUD=xxx ENV=xxx)
@echo -e "$(OK_COLOR)[$(APP)] Build Helm chart ${CHART}:${ENV}$(NO_COLOR)" >&2
@DEBUG=$(DEBUG) . hack/scripts/chart-fluxcd.sh $(CHART) \
&& export TMPFILE=$$(./hack/scripts/flux-helm.sh "$(CHART)" "$(CLOUD)/$(ENV)") \
&& cat $${TMPFILE}
.PHONY: helm-flux-template
helm-flux-template: guard-CHART guard-CLOUD guard-ENV ## Install Helm chart (CHART=xxx CLOUD=xxx ENV=xxx)
@echo -e "$(OK_COLOR)[$(APP)] Build Helm chart ${CHART}:${ENV}$(NO_COLOR)" >&2
@DEBUG=$(DEBUG) . hack/scripts/chart-fluxcd.sh $(CHART) \
&& export TMPFILE=$$(./hack/scripts/flux-helm.sh "$(CHART)" "$(CLOUD)/$(ENV)") \
&& helm template --debug $${CHART_NAME} $${CHART_REPO_NAME}/$${CHART_NAME} --namespace $${CHART_NAMESPACE} -f $${TMPFILE}
.PHONY: helm-flux-install
helm-flux-install: guard-CHART guard-CLOUD guard-ENV kubernetes-check-context ## Install Helm chart (CHART=xxx CLOUD=xxx ENV=xxx)
@echo -e "$(OK_COLOR)[$(APP)] Install Helm chart ${CHART}:${ENV}$(NO_COLOR)" >&2
@DEBUG=$(DEBUG) . hack/scripts/chart-fluxcd.sh $(CHART) \
&& export TMPFILE=$$(./hack/scripts/flux-helm.sh "$(CHART)" "$(CLOUD)/$(ENV)") \
&& helm install $${CHART_NAME} $${CHART_REPO_NAME}/$${CHART_NAME} --namespace $${CHART_NAMESPACE} -f $${TMPFILE}
.PHONY: helm-flux-upgrade
helm-flux-upgrade: guard-CHART guard-CLOUD guard-ENV kubernetes-check-context ## Upgrade Helm chart (SERVICE=xxx ENV=xxx)
@echo -e "$(OK_COLOR)[$(APP)] Upgrade Helm chart ${CHART}:${ENV}$(NO_COLOR)" >&2
@DEBUG=$(DEBUG) . hack/scripts/chart-fluxcd.sh $(CHART) \
&& export TMPFILE=$$(./hack/scripts/flux-helm.sh "$(CHART)" "$(CLOUD)/$(ENV)") \
&& helm upgrade --install $${CHART_NAME} $${CHART_REPO_NAME}/$${CHART_NAME} --namespace $${CHART_NAMESPACE} -f $${TMPFILE}
.PHONY: helm-flux-uninstall
helm-flux-uninstall: guard-CHART guard-CLOUD guard-ENV kubernetes-check-context ## Uninstall Helm chart (CHART=xxx CLOUD=xxx ENV=xxx)
@echo -e "$(OK_COLOR)[$(APP)] Uninstall Helm chart ${CHART}:${ENV}$(NO_COLOR)" >&2
@DEBUG=$(DEBUG) . hack/scripts/chart-fluxcd.sh $(CHART) \
&& helm uninstall $${CHART_NAME} --namespace $${CHART_NAMESPACE}
.PHONY: helm-argo-repo
helm-argo-repo: guard-CHART ## Configure Helm repository and chart (CHART=xxx)
@echo -e "$(OK_COLOR)[$(APP)] Helm repository and chart $(CHART)$(NO_COLOR)" >&2
@DEBUG=$(DEBUG) . hack/scripts/chart-argocd.sh $(CHART) \
&& helm repo add $${CHART_REPO_NAME} $${CHART_REPO_URL} --force-update \
&& helm repo update
.PHONY: helm-argo-values
helm-argo-values: guard-CHART
@echo -e "$(OK_COLOR)[$(APP)] Helm show values $(CHART)$(NO_COLOR)" >&2
@DEBUG=$(DEBUG) . hack/scripts/chart-argocd.sh $(CHART) \
&& helm show values $${CHART_REPO_NAME}/$${CHART_NAME} --version $${CHART_VERSION}
# .PHONY: helm-argo-template
# helm-argo-template: guard-CHART guard-CLOUD guard-ENV ## Template Helm chart (CHART=xxx CLOUD=xxx ENV=xxx)
# @echo -e "$(OK_COLOR)[$(APP)] Build Helm chart ${CHART}:${ENV}$(NO_COLOR)" >&2
# @DEBUG=$(DEBUG) . hack/scripts/chart-argocd.sh $(CHART) \
# && export CHART_DIR=$$(dirname $(CHART)) \
# && echo helm template --debug $${CHART_NAME} $${CHART_REPO_NAME}/$${CHART_NAME} \
# -f "$${CHART_DIR}/values.yaml" -f "$${CHART_DIR}/values-$(CLOUD)-$(ENV).yaml" -f "$${CHART_DIR}/values-$(CLOUD)-$(ENV)-secret.yaml"
.PHONY: helm-argo-template
helm-argo-template: guard-CHART guard-CLOUD guard-ENV ## Template Helm chart (CHART=xxx CLOUD=xxx ENV=xxx)
@echo -e "$(OK_COLOR)[$(APP)] Build Helm chart ${CHART}:${ENV}$(NO_COLOR)" >&2
@DEBUG=$(DEBUG) pushd $(CHART) > /dev/null \
&& export NAMESPACE=$$(basename $$(dirname $(CHART))) \
&& rm -fr charts Chart.lock \
&& helm dependency build >&2 \
&& helm template portefaix . --debug --namespace $${NAMESPACE} -f ./values.yaml -f "./values-$(CLOUD)-$(ENV).yaml" --api-versions=monitoring.coreos.com/v1 \
&& rm -fr Chart.lock charts \
&& popd > /dev/null
.PHONY: helm-argo-template-crds
helm-argo-template-crds: guard-CHART guard-CLOUD guard-ENV ## Template Helm chart (CHART=xxx CLOUD=xxx ENV=xxx)
@echo -e "$(OK_COLOR)[$(APP)] Build Helm chart ${CHART}:${ENV}$(NO_COLOR)" >&2
@DEBUG=$(DEBUG) pushd $(CHART) > /dev/null \
&& export NAMESPACE=$$(basename $$(dirname $(CHART))) \
&& rm -fr charts Chart.lock \
&& helm dependency build >&2 \
&& helm template portefaix . --debug --include-crds --namespace $${NAMESPACE} -f ./values.yaml -f "./values-$(CLOUD)-$(ENV).yaml" --api-versions=monitoring.coreos.com/v1 \
&& rm -fr Chart.lock charts \
&& popd > /dev/null
.PHONY: helm-argo-install
helm-argo-install: guard-CHART guard-CLOUD guard-ENV kubernetes-check-context ## Install Helm chart (CHART=xxx CLOUD=xxx ENV=xxx)
@echo -e "$(OK_COLOR)[$(APP)] Build Helm chart ${CHART}:${ENV}$(NO_COLOR)" >&2
@DEBUG=$(DEBUG) pushd $(CHART) > /dev/null \
&& export CHART_NAME=$$(basename $(CHART)) \
&& export NAMESPACE=$$(basename $$(dirname $(CHART))) \
&& rm -fr charts Chart.lock \
&& helm dependency build >&2 \
&& helm upgrade --install portefaix-$${CHART_NAME} . --debug --namespace $${NAMESPACE} --create-namespace -f ./values.yaml -f "./values-$(CLOUD)-$(ENV).yaml" \
&& rm -fr Chart.lock charts \
&& popd > /dev/null
.PHONY: helm-argo-uninstall
helm-argo-uninstall: guard-CHART guard-CLOUD guard-ENV kubernetes-check-context ## Install Helm chart (CHART=xxx CLOUD=xxx ENV=xxx)
@echo -e "$(OK_COLOR)[$(APP)] Build Helm chart ${CHART}:${ENV}$(NO_COLOR)" >&2
@DEBUG=$(DEBUG) pushd $(CHART) > /dev/null \
&& export CHART_NAME=$$(basename $(CHART)) \
&& export NAMESPACE=$$(basename $$(dirname $(CHART))) \
&& helm uninstall --namespace $${NAMESPACE} portefaix-$${CHART_NAME}
.PHONY: helm-argo-kubescape
helm-argo-kubescape: guard-CHART guard-CLOUD guard-ENV ## Install Helm chart (CHART=xxx CLOUD=xxx ENV=xxx)
@echo -e "$(OK_COLOR)[$(APP)] Build Helm chart ${CHART}:${ENV}$(NO_COLOR)" >&2
@DEBUG=$(DEBUG) pushd $(CHART) > /dev/null \
&& export CHART_NAME=$$(basename $(CHART)) \
&& export NAMESPACE=$$(basename $$(dirname $(CHART))) \
&& rm -fr charts Chart.lock \
&& helm dependency build >&2 \
&& helm template portefaix . --debug --namespace $${NAMESPACE} -f ./values.yaml -f "./values-$(CLOUD)-$(ENV).yaml" --api-versions=monitoring.coreos.com/v1 > manifests.yaml \
&& kubescape scan manifests.yaml
# .PHONY: helm-argo-kubescape
# helm-argo-kubescape: guard-CHART guard-CLOUD guard-ENV kubernetes ## Install Helm chart (CHART=xxx CLOUD=xxx ENV=xxx)
# @echo -e "$(OK_COLOR)[$(APP)] Build Helm chart ${CHART}:${ENV}$(NO_COLOR)" >&2
# @DEBUG=$(DEBUG) pushd $(CHART) > /dev/null \
# && export CHART_NAME=$$(basename $(CHART)) \
# && export NAMESPACE=$$(basename $$(dirname $(CHART))) \
# && rm -fr charts Chart.lock \
# && helm dependency build >&2 \
# && kubescape scan $(CHART)
# ====================================
# O P A
# ====================================
##@ Opa
.PHONY: opa-deps
opa-deps: ## Setup OPA dependencies
@echo -e "$(OK_COLOR)[$(APP)] Install OPA policies $(POLICY)$(NO_COLOR)" >&2
@conftest pull --policy addons/policies/deprek8ion github.com/swade1987/deprek8ion//policies
@conftest pull --policy addons/policies/portefaix github.com/portefaix/portefaix-policies?ref=v0.3.0//policy
POHONY: opa-test
opa-test: ## Test policies
@opa test addons/policies/core
.PHONY: opa-policy
opa-policy-base: guard-CHART guard-ENV guard-POLICY ## Check OPA policies for a Helm chart (CHART=xxx CLOUD=xxx ENV=xxx POLICY=xxx)
@echo -e "$(OK_COLOR)[$(APP)] Open Policy Agent check policies $(CHART):$(ENV)$(NO_COLOR)" >&2
@DEBUG=$(DEBUG) . hack/scripts/chart-fluxcd.sh $(CHART) \
&& export TMPFILE=$$(./hack/scripts/flux-helm.sh "$(CHART)" "$(CLOUD)/$(ENV)") \
&& helm template $${CHART_NAME} $${CHART_REPO_NAME}/$${CHART_NAME} --namespace $${CHART_NAMESPACE} -f $${TMPFILE} | conftest test --all-namespaces -p $(POLICY) -
# ====================================
# S E C R E T S
# ====================================
##@ Secrets
# .PHONY: kubeseal-encrypt
# kubeseal-encrypt: guard-CLOUD guard-ENV guard-FILE guard-NAME guard-NAMESPACE ## Encrypt a Kubernetes secret file (CLOUD=xxx ENV=xxx FILE=xxx)
# @kubectl create secret -n $(NAMESPACE) generic $(NAME) --dry-run=client -o yaml --from-file=$(FILE) | \
# kubeseal --format yaml --cert .secrets/$(CLOUD)/$(ENV)/sealed-secrets/cert.pm
.PHONY: kubeseal-encrypt
kubeseal-encrypt: guard-CLOUD guard-ENV guard-NAMESPACE kubernetes-check-context ## Encrypt data (CLOUD=xxx ENV=xxx DATA=xxx)
@echo -n "$(DATA)" | kubeseal --raw --from-file=/dev/stdin --scope cluster-wide \
--scope cluster-wide \
--controller-namespace kube-system \
--controller-name sealed-secrets \
--namespace $(NAMESPACE)
.PHONY: kubeseal-secret
kubeseal-secret: guard-CLOUD guard-ENV guard-FILE guard-NAMESPACE kubernetes-check-context ## Encrypt data (CLOUD=xxx ENV=xxx FILE=xxx)
@cat $(FILE) | kubeseal --scope cluster-wide \
--controller-namespace kube-system \
--controller-name sealed-secrets \
--namespace $(NAMESPACE) \
--format yaml
# ====================================
# G I T O P S
# ====================================
##@ Gitops
.PHONY: release-prepare
release-prepare: guard-VERSION ## Update release label (VERSION=xxx)
@./hack/scripts/portefaix-labels.sh gitops yaml $(VERSION)
@./hack/scripts/portefaix-labels.sh terraform tfvars $(VERSION) aws
@./hack/scripts/portefaix-labels.sh terraform tfvars $(VERSION) azure
@./hack/scripts/portefaix-labels.sh terraform tfvars $(VERSION) gcp
@./hack/scripts/portefaix-labels.sh ansible/k3s/machines/ yml $(VERSION)
@./hack/scripts/portefaix-labels.sh . sh $(VERSION)
.PHONY: bootstrap-cilium
bootstrap-cilium: guard-ENV guard-CLOUD kubernetes-check-context ## Bootstrap Cilium
@./hack/scripts/bootstrap-argocd.sh $(CLOUD) $(ENV) cilium
.PHONY: bootstrap-argocd
bootstrap-argocd: guard-ENV guard-CLOUD kubernetes-check-context ## Bootstrap ArgoCD
@./hack/scripts/bootstrap-argocd.sh $(CLOUD) $(ENV) argocd
.PHONY: bootstrap-fluxcd
bootstrap-fluxcd: guard-ENV guard-CLOUD guard-BRANCH kubernetes-check-context ## Bootstrap FluxCD
@./hack/scripts/bootstrap-fluxcd.sh $(CLOUD) $(ENV) $(BRANCH)
.PHONY: bootstrap-crds
bootstrap-crds: guard-ENV guard-CLOUD kubernetes-check-context ## Bootstrap CRDs
@./hack/scripts/bootstrap-argocd.sh $(CLOUD) $(ENV) crds
.PHONY: argocd-stack-install
argocd-stack-install: guard-ENV guard-CLOUD guard-STACK kubernetes-check-context ## Setup ArgoCD applications
@./hack/scripts/argocd-app.sh $(CLOUD) $(ENV) $(STACK) install
.PHONY: argocd-stack-build
argocd-stack-build: guard-ENV guard-CLOUD guard-STACK ## Setup ArgoCD applications
@./hack/scripts/argocd-app.sh $(CLOUD) $(ENV) $(STACK) build
.PHONY: argocd-concurrency
argocd-concurrency: ## Setup Argo-CD concurrency files
@./hack/scripts/argocd-concurrency.sh gitops/argocd
.PHONY: argocd-admin-password
argocd-admin-password: ## Retrieve the Argo-CD Admin password
@kubectl -n gitops get secret argocd-initial-admin-secret \
-o jsonpath="{.data.password}" | base64 -d; echo