-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
158 lines (130 loc) · 6.44 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
SHELL := /bin/bash
### the location where the necessary cli binaries are stored
binary_location = ${HOME}/.fks
### gitops repository and branch to play with
gitops_repo = $(shell git config --get remote.origin.url)
gitops_branch = $(shell git branch --show-current)
### used as folder within the repo to contain the root kustomization "flux-system" as well as the kind cluster name
cluster_name = local-cluster
### operating system, options are (darwin|linux)
os = $(shell uname -s | awk '{print tolower($$0)}')
### operating system, options are (amd64|arm64)
arch = $(shell [[ "$$(uname -m)" = x86_64 ]] && echo "amd64" || echo "$$(uname -m)")
### versions
# https://kubernetes.io/releases/
kubectl_version = v1.31.1
# https://github.com/kubernetes-sigs/kind/releases
kind_version = v0.24.0
# https://github.com/fluxcd/flux2/releases
flux_version = v2.4.0
# https://hub.docker.com/r/kindest/node/tags
kindest_node_version = v1.31.1
###
kubectl_arch = $(os)/$(arch)
kubectl_location = $(binary_location)/kubectl
kind_arch = $(os)-$(arch)
kind_location = $(binary_location)/kind
flux_arch = $(os)_$(arch)
flux_location = $(binary_location)/flux
kindest_node_image = kindest/node:$(kindest_node_version)
### leave empty for enforcing docker even if podman was available, or set env NO_PODMAN=1
# kind_podman =
kind_podman = $(shell [[ "$$NO_PODMAN" -ne 1 ]] && which podman > /dev/null && echo "KIND_EXPERIMENTAL_PROVIDER=podman" || echo "")
kind_cmd = $(kind_podman) $(kind_location)
wait_timeout= "60s"
.PHONY: pre-check
pre-check: # validate required tools
### Checking installed tooling
# Podman or Docker
@if [ -z "$(kind_podman)" ]; then \
docker version -f 'docker client version {{.Client.Version}}, server version {{.Server.Version}}'; \
else \
podman -v; \
fi
#
# Kubectl ($(kubectl_location))
@$(kubectl_location) version --client=true --output=json | jq -r '"kubectl version "+ .clientVersion.gitVersion'
#
# Kind ($(kind_location))
@$(kind_location) --version
#
# Flux ($(flux_location))
@$(flux_location) --version
#
gitops_repo_owner = $(shell [[ "$(gitops_repo)" = http* ]] && echo $(gitops_repo) | cut -d/ -f4 || echo $(gitops_repo) | cut -d: -f2 | cut -d/ -f1)
gitops_repo_name = $(shell [[ "$(gitops_repo)" = http* ]] && echo $(gitops_repo) | cut -d/ -f5 | cut -d. -f1 || echo $(gitops_repo) | cut -d/ -f2 | cut -d. -f1)
.PHONY: check
check: pre-check # validate prerequisites
### Checking prerequisites
# Kube Context
@$(kubectl_location) cluster-info --context kind-$(cluster_name) | grep 127.0.0.1
#
# GitOps-Repository-Url: $(gitops_repo)
# Repo-Owner: $(gitops_repo_owner)
# Repo-Name: $(gitops_repo_name)
# GitOps-Branch: $(gitops_branch)
# Everything is fine, lets get bootstrapped
#
kind_version_number = $(shell echo $(kind_version) | cut -c 2-)
flux_version_number = $(shell echo $(flux_version) | cut -c 2-)
kubectl_version_number = $(shell echo $(kubectl_version) | cut -c 2-)
.PHONY: prepare
prepare: # install prerequisites
# Creating $(binary_location)
@mkdir -p $(binary_location)
# Install or update kind $(kind_version_number) for $(kind_arch) into $(kind_location)
@curl -sSLfo $(kind_location) "https://github.com/kubernetes-sigs/kind/releases/download/v$(kind_version_number)/kind-$(kind_arch)"
@chmod a+x $(kind_location)
# Install or update flux $(flux_version_number) for $(flux_arch) into $(flux_location)
@curl -sSLfo $(flux_location).tgz https://github.com/fluxcd/flux2/releases/download/v$(flux_version_number)/flux_$(flux_version_number)_$(flux_arch).tar.gz
@tar xf $(flux_location).tgz -C $(binary_location) && rm -f $(flux_location).tgz
@chmod a+x $(flux_location)
# Install or update kubectl $(kubectl_version_number) for $(kubectl_arch) into $(kubectl_location)
@curl -sSLfo $(kubectl_location) https://dl.k8s.io/release/$(kubectl_version)/bin/$(kubectl_arch)/kubectl
@chmod a+x $(kubectl_location)
.PHONY: new
new: # create fresh kind cluster
# Creating kind cluster named '$(cluster_name)'
@$(kind_cmd) create cluster -n $(cluster_name) --config .kind/config.yaml --image $(kindest_node_image)
@$(kind_cmd) export kubeconfig -n $(cluster_name) --kubeconfig ${HOME}/.kube/config
.PHONY: kube-ctx
kube-ctx: # create fresh kind cluster
@$(kind_cmd) export kubeconfig -n $(cluster_name) --kubeconfig ${HOME}/.kube/config
.PHONY: clean
clean: # remove kind cluster
# Removing kind cluster named '$(cluster_name)'
@$(kind_cmd) delete cluster -n $(cluster_name)
.PHONY: bootstrap
bootstrap: check kube-ctx # install and configure flux
ifndef GITHUB_TOKEN
@echo "!!! please set GITHUB_TOKEN env to bootstrap flux"
exit 1
endif
### Bootstrapping flux from GitHub repo $(gitops_repo_owner)/$(gitops_repo_name) branch $(gitops_branch)
$(flux_location) bootstrap github \
--components-extra=image-reflector-controller,image-automation-controller \
--read-write-key=true \
--owner=$(gitops_repo_owner) \
--repository=$(gitops_repo_name) \
--branch=$(gitops_branch) \
--path=$(cluster_name)
#
# Configuring GitHub commit status notification
@$(kubectl_location) create secret generic -n flux-system github --from-literal token=${GITHUB_TOKEN} --save-config --dry-run=client -o yaml | $(kubectl_location) apply -f -
@$(flux_location) create alert-provider github -n flux-system --type github --address "https://github.com/$(gitops_repo_owner)/$(gitops_repo_name)" --secret-ref github
@$(flux_location) create alert -n flux-system --provider-ref github --event-source "Kustomization/*" flux-system
@$(kubectl_location) get kustomization -n flux-system
#
.PHONY: reconcile
reconcile: # reconsule flux-system kustomization
@$(flux_location) reconcile kustomization flux-system --with-source --timeout=$(wait_timeout)
@$(flux_location) reconcile kustomization infrastructure --timeout=$(wait_timeout)
@$(flux_location) reconcile kustomization apps --timeout=$(wait_timeout)
@$(kubectl_location) get kustomization -n flux-system
.PHONY: wait
wait: # wait for reconciliation complete
@$(kubectl_location) wait --for=condition=ready --timeout=$(wait_timeout) kustomization -n flux-system flux-system
@$(kubectl_location) wait --for=condition=ready --timeout=$(wait_timeout) kustomization -n flux-system infrastructure
@$(kubectl_location) wait --for=condition=ready --timeout=$(wait_timeout) helmrelease -n ingress ingress-nginx
@$(kubectl_location) wait --for=condition=ready --timeout=$(wait_timeout) helmrelease -n dashboard kubernetes-dashboard
@$(kubectl_location) wait --for=condition=ready --timeout=$(wait_timeout) kustomization -n flux-system apps