-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
263 lines (225 loc) · 10.7 KB
/
Makefile
File metadata and controls
263 lines (225 loc) · 10.7 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
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
.PHONY : mise-install \
configure start stop info \
deleteCluster deleteProfile mrproper \
client \
dashboard
#########################
# Default Configuration #
#########################
k8scluster=pg-test
kubeconfig=${PWD}/generated/k8s/$(k8scluster).kubeconfig
kubeversion=$(shell grep "kubectl " .tool-versions | cut -d " " -f 2)
namespace=pg-test
install_prometheus=true
with_monitoring=true
# https://github.com/cloudnative-pg/cloudnative-pg/tags
cnpgVersion=1.28.0
cnpgOperatorChartVersion=0.27.0 # OperatorVersion=1.25.0 # https://github.com/cloudnative-pg/charts/blob/main/charts/cloudnative-pg/Chart.yaml
cnpgClusterChartVersion=0.5.0 # https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/Chart.yaml
postgresqlInstance=postgresql-testing
postgresqlVersion=18.1
# postgresqlExtension=hypopg-hll-cron
postgresqlDiskSize=50Gi
postgresqlNodes=3
postgresqlNodeMem=2Gi
postgresqlNodeCpu=1000m
# postgresqlImage=ghcr.io/wanix/postgresql:$(postgresqlVersion)-$(postgresqlExtension)
postgresqlImage=ghcr.io/cloudnative-pg/postgresql:$(postgresqlVersion)
minikube=true
minikubeResources=--memory 6144 --cpus 4
storage_class=manual
# https://minikube.sigs.k8s.io/docs/drivers/
minikubeDriver=podman
minikubePersistantPath=${PWD}/persistentVolumesData/$(k8scluster).d/$(postgresqlInstance)
minikubeNodes=3
generated_k8s_path=generated/k8s/$(k8scluster).d/$(postgresqlInstance)
generated_helm_values_path=generated/helm-values/$(k8scluster).d/$(postgresqlInstance)
generated_cfg_path=generated/cfg/$(k8scluster).d/$(postgresqlInstance)
kubeMountPath=/tmp/hostpath_pv_data/$(postgresqlInstance)
grafana_port=8080
#######################
# Makefile operations #
#######################
export KUBECONFIG := $(kubeconfig)
export KUBEMOUNTPATH := $(kubeMountPath)
export KSNAMESPACE := $(namespace)
export KSSHAREDSPACE := $(minikubePersistantPath)
export PGVERSION := $(postgresqlVersion)
export PGINSTANCENAME := $(postgresqlInstance)
export PGMAINPASSWORD := $(postgresqlMainPassword)
export PGUSERNAME := $(postgresqlUser)
export PGUSERPASSWORD := $(postgresqlPassword)
export PGMAINDB := $(postgresqlDb)
export PGDISKSIZE := $(postgresqlDiskSize)
export PGINSTANCESNUMBER := $(postgresqlNodes)
export PGINSTANCEMEM := $(postgresqlNodeMem)
export PGINSTANCECPU := $(postgresqlNodeCpu)
export PGSTORAGECLASS := $(storage_class)
export PGCONTAINERIMAGE := $(postgresqlImage)
export PGPROMMONITORING := $(with_monitoring)
export PGUSERUID := $(shell id -u)
export PGUSERGID := $(shell id -g)
mise-install :
# https://asdf-vm.com/guide/getting-started.html
@mise install
@mise trust
configure:
@echo "-- Creating configuration files and needed directories"
@test -d $(minikubePersistantPath) || mkdir -p $(minikubePersistantPath)/postgresql \
$(minikubePersistantPath)/psql
@test -d $(generated_k8s_path) || mkdir -p $(generated_k8s_path)
@test -d $(generated_helm_values_path) || mkdir -p $(generated_helm_values_path)
@test -d $(generated_cfg_path) || mkdir -p $(generated_cfg_path)
@cat kubernetes/cm-postgresql-client.yml.tpl | envsubst > $(generated_k8s_path)/cm-postgresql-client.yml
@cat kubernetes/pod-postgresql-client.yml.tpl | envsubst > $(generated_k8s_path)/pod-postgresql-client.yml
@cat kubernetes/pv-postgresql-client.yml.tpl | envsubst > $(generated_k8s_path)/pv-postgresql-client.yml
@cat kubernetes/pvc-postgresql-client.yml.tpl | envsubst > $(generated_k8s_path)/pvc-postgresql-client.yml
@cat kubernetes/pv-postgresql-data.yml.tpl | envsubst > $(generated_k8s_path)/pv-postgresql-data.yml
@for i in $(shell seq -w 1 $(postgresqlNodes)); do \
mkdir -p $(minikubePersistantPath)/postgresql/data-node-$$i; \
chmod 777 $(minikubePersistantPath)/postgresql/data-node-$$i; \
export PGNODE=$$i; \
cat kubernetes/pv-postgresql-data.yml.tpl | envsubst > $(generated_k8s_path)/pv-postgresql-data-$$i.yml; \
done
@cat kubernetes/helm-cnpg-operator.yml.tpl | envsubst > $(generated_helm_values_path)/helm-cnpg-operator.yml
@cat kubernetes/helm-cnpg-cluster.yml.tpl | envsubst > $(generated_helm_values_path)/helm-cnpg-cluster.yml
start : configure \
start_minikube \
install_monitoring \
install_cloudnative_pg_helm_chart_repo \
install_cloudnative_pg \
install_postgresql_cluster \
forward_grafana info
start_minikube :
ifeq ($(minikube), true)
@echo "-- Starting Minikube"
ifeq ($(minikubeDriver), $(filter $(minikubeDriver), podman docker))
@minikube config set rootless false
@minikube start -p $(k8scluster) $(minikubeResources) \
--kubernetes-version=$(kubeversion) \
--driver=$(minikubeDriver) \
--nodes $(minikubeNodes) \
--mount --mount-string $(minikubePersistantPath):$(kubeMountPath)
else
@minikube start -p $(k8scluster) $(minikubeResources) \
--kubernetes-version=$(kubeversion) \
--driver=$(minikubeDriver) \
--nodes $(minikubeNodes)
endif
endif
install_cloudnative_pg_helm_chart_repo :
@echo "-- installing helm chart repo"
@helm repo add cnpg https://cloudnative-pg.github.io/charts
@helm repo update cnpg
install_cloudnative_pg :
@echo "-- Installing cloudnative-pg operator"
@helm upgrade --install cnpg --namespace cnpg-system --create-namespace --version "$(cnpgOperatorChartVersion)" -f $(generated_helm_values_path)/helm-cnpg-operator.yml cnpg/cloudnative-pg
@kubectl wait pod --timeout 120s --for=condition=Ready -n cnpg-system -l app.kubernetes.io/name=cloudnative-pg
@test -f $(minikubePersistantPath)/postgresql/flag-cnpg.flag || (sleep 10s && touch $(minikubePersistantPath)/postgresql/flag-cnpg.flag)
install_postgresql_cluster :
@echo "-- Creating PostgreSQL Cluster $(postgresqlInstance)"
@helm upgrade --install cnpg --namespace $(namespace) --create-namespace --version "$(cnpgClusterChartVersion)" -f $(generated_helm_values_path)/helm-cnpg-cluster.yml cnpg/cluster
@echo "-- Creating PostgreSQL Cluster PVs"
@for i in $(shell seq -w 1 $(postgresqlNodes)); do \
export PGNODE=$$i; \
kubectl apply -n $(namespace) -f $(generated_k8s_path)/pv-postgresql-data-$$i.yml; \
done
@echo "-- Waiting for cluster $(namespace) to be ready"
@./helpers/wait_for_pods_to_exist.sh 5 300 ' waiting pod creation' -n $(namespace) -l cnpg.io/cluster=pg-cluster-$(postgresqlInstance) -l cnpg.io/instanceRole=primary
@echo " waiting pod availability" && kubectl wait pod --timeout 120s --for=condition=Ready -n $(namespace) -l cnpg.io/cluster=pg-cluster-$(postgresqlInstance) -l cnpg.io/instanceRole=primary
install_monitoring :
ifeq ($(install_prometheus), true)
@echo "-- Install monitoring"
ifeq ($(minikube), true)
@kubectl get ns monitoring 2>&1 >/dev/null || kubectl create ns monitoring
@helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
@helm upgrade --install prometheus-community --namespace monitoring -f kubernetes/helm-prometheus-community-kube-prometheus-stack.yml prometheus-community/kube-prometheus-stack
endif
endif
forward_grafana :
ifeq ($(install_prometheus), true)
ifeq ($(minikube), true)
@echo "-- Waiting Grafana to be available"
@./helpers/wait_for_pods_to_exist.sh 5 60 ' waiting pod creation' -n monitoring -l app.kubernetes.io/instance=prometheus-community -l app.kubernetes.io/name=grafana
@echo " waiting grafana availability" && kubectl wait pod --timeout 120s --for=condition=Ready -n monitoring -l app.kubernetes.io/instance=prometheus-community -l app.kubernetes.io/name=grafana
@test $(shell ps -ef | grep "kubectl port-forward -n monitoring svc/prometheus-community-grafana $(grafana_port):80" | grep -vc grep) -eq 0 \
&& echo "-- Forwarding Grafana" \
&& nohup kubectl port-forward -n monitoring svc/prometheus-community-grafana $(grafana_port):80&
endif
endif
stop_forward_grafana :
ifeq ($(minikube), true)
ifeq ($(install_prometheus), true)
@test $(shell ps -ef | grep "kubectl port-forward -n monitoring svc/prometheus-community-grafana $(grafana_port):80" | grep -vc grep) -gt 0 \
&& echo "-- Stopping Grafana port-forward" \
&& ps -ef | grep "kubectl port-forward -n monitoring svc/prometheus-community-grafana $(grafana_port):80" \
| grep -v grep | sed 's/\s\+/ /g' | cut -d ' ' -f 2 | xargs kill 2> /dev/null \
|| true
@rm -f nohup.out
endif
endif
info : status
@echo
@echo You can now set your env:
@echo export KUBECONFIG="$(kubeconfig)"
ifeq ($(install_prometheus), true)
ifeq ($(minikube), true)
$(eval grafana_user := $(shell export KUBECONFIG="$(kubeconfig)"; kubectl get secret -n monitoring prometheus-community-grafana -o jsonpath='{.data.admin-user}' | base64 -d))
$(eval grafana_pass := $(shell export KUBECONFIG="$(kubeconfig)"; kubectl get secret -n monitoring prometheus-community-grafana -o jsonpath='{.data.admin-password}' | base64 -d))
@echo "-- grafana credentials:"
@echo " $(grafana_user) // $(grafana_pass)"
@echo " grafana URL: http://localhost:$(grafana_port)/d/cloudnative-pg/cloudnativepg?orgId=1&refresh=30s"
endif
endif
status :
ifeq ($(minikube), true)
@echo "-- Minikube status"
@minikube status -p $(k8scluster) -l cluster
else
@echo This operation is for minikube driver only !
endif
stop : stop_forward_grafana
ifeq ($(minikube), true)
@echo "-- Stopping Minikube cluster"
-@minikube stop -p $(k8scluster)
@rm -f $(minikubePersistantPath)/postgresql/*.flag
else
@echo This operation is for minikube driver only !
endif
deleteCluster : stop_forward_grafana
ifeq ($(minikube), true)
@echo "-- Deleting Minikube cluster"
-@minikube delete -p $(k8scluster)
@rm -f $(minikubePersistantPath)/postgresql/*.flag
endif
mrproper: deleteCluster
rm -f cfg/helm-conf--*.yml
rm -Rf generated/k8s/*.d
rm -Rf generated/cfg/*.d
rm -f nohup.out
rm -Rf persistentVolumesData/*.d
deleteProfile : deleteCluster
rm -f $(generated_k8s_path)/*.yml
-rmdir $(generated_k8s_path)
rm -f $(generated_helm_values_path)/*.yml
-rmdir $(generated_helm_values_path)
rm -f $(generated_cfg_path)/*.yml
-rmdir $(generated_cfg_path)
-rm -Rf $(minikubePersistantPath)
client :
@echo "-- Creating client pod and resources"
@kubectl apply -n $(namespace) -f $(generated_k8s_path)/cm-postgresql-client.yml
@kubectl apply -n $(namespace) -f $(generated_k8s_path)/pv-postgresql-client.yml
@kubectl apply -n $(namespace) -f $(generated_k8s_path)/pvc-postgresql-client.yml
@kubectl apply -n $(namespace) -f $(generated_k8s_path)/pod-postgresql-client.yml
@./helpers/wait_for_pods_to_exist.sh 5 60 ' waiting for $(postgresqlInstance)-client' -n $(namespace) $(postgresqlInstance)-client
@kubectl wait pod --timeout 120s --for=condition=Ready -n $(namespace) $(postgresqlInstance)-client
@echo "-- Connecting client pod"
@kubectl exec -n $(namespace) -it $(postgresqlInstance)-client -- /bin/bash
dashboard:
ifeq ($(minikube), true)
@echo "-- Launching Minikube dashboard"
@minikube dashboard -p $(k8scluster)
else
@echo This operation is for minikube driver only !
endif