-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
187 lines (165 loc) · 6.92 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
DOCKER ?= docker
JQ ?= jq
SSH ?= ssh
TF ?= tofu
PROFILE ?= k0s
apply: profiles/$(PROFILE)/kubeconfig
init: .terraform/.init
destroy:
$(MAKE) .destroy
PHONY: .destroy
.destroy:
-rm -- 'profiles/$(PROFILE)/profile.tfstate' 'profiles/$(PROFILE)/profile.tfstate.backup'
-rm -- 'profiles/$(PROFILE)/kubeconfig'
-rm '.tf.apply.$(PROFILE)'
[ -s 'profiles/$(PROFILE)/profile.tfvars' ] || rm -f -- 'profiles/$(PROFILE)/profile.tfvars'
TF_STATE_CMDS := apply
TF_PHONY_CMDS := console destroy refresh output
TF_CMDS := $(TF_STATE_CMDS) $(TF_PHONY_CMDS)
TF_CLI_ARGS :=
ifneq ($(firstword $(MAKECMDGOALS)),+)
# If the first goal is a terraform command ...
ifneq ($(filter $(firstword $(MAKECMDGOALS)),$(TF_CMDS)),)
# use all subsequent goals as args to that command
TF_CLI_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
# and turn them into do-nothing targets
$(eval $(TF_CLI_ARGS):;@:)
endif
else
+:;@:
endif
# common args to terraform commands
.tf.%: TF_ARGS += -state='profiles/$(PROFILE)/profile.tfstate'
.tf.apply.% .tf.destroy: TF_ARGS += -auto-approve
.tf.apply.% .tf.console .tf.destroy .tf.refresh: TF_ARGS += -var-file='profiles/$(PROFILE)/profile.tfvars' -var='profile_folder=profiles/$(PROFILE)'
.PHONY: $(addprefix .tf.,$(TF_PHONY_CMDS))
$(addprefix .tf.,$(TF_PHONY_CMDS)): .terraform/.init
$(TF) $(patsubst .tf.%,%,$@) $(TF_ARGS) $(TF_CLI_ARGS)
$(patsubst %,.tf.%.$(PROFILE),$(TF_STATE_CMDS)): .terraform/.init
$(TF) $(patsubst .tf.%.$(PROFILE),%,$@) $(TF_ARGS) $(TF_CLI_ARGS)
touch -- '$@'
.PHONY: $(TF_CMDS)
$(foreach cmd,$(TF_PHONY_CMDS),$(eval $(cmd): .tf.$(cmd)))
$(foreach cmd,$(TF_STATE_CMDS),$(eval $(cmd): .tf.$(cmd).$(PROFILE)))
.tf.apply.$(PROFILE): .alpine-image $(shell find . -type f -name '*.tf') profiles/$(PROFILE)/profile.tfvars
.tf.destroy: .tf.touch-profile
.PHONY: .tf.touch-profile
.tf.touch-profile: | profiles/$(PROFILE)
touch -- 'profiles/$(PROFILE)/profile.tfvars'
.PHONY: .alpine-image
.alpine-image:
$(MAKE) -C alpine-image image.qcow2
ssh.%: ID ?= 0
ssh.%: SSH_CONNECT ?= $(shell $(MAKE) -s output -- -json | $(JQ) -r '@sh "-i \(.ssh.value.key_file) \(.ssh.value.user)@\((.machines.value[] | select(.name | endswith("$(patsubst .%,%,$(suffix $@))-$(ID)")).ipv4))"')
.PHONY: ssh.controller ssh.worker
ssh.controller ssh.worker:
@[ -n '$(SSH_CONNECT)' ] || { echo No machine found.; exit 1; }
$(SSH) -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $(SSH_CONNECT)
profiles/$(PROFILE):
mkdir -p -- '$@'
.PHONY: profiles/$(PROFILE)/airgap-images.tar
profiles/$(PROFILE)/airgap-images.tar: | profiles/$(PROFILE)
@set -e \
&& set -- \
&& images="$$(cat)" \
&& for image in $$images; do \
$(DOCKER) pull -- "$$image" \
&& set -- "$$@" "$$image" \
; done \
&& echo Saving $@ ... \
&& $(DOCKER) image save "$$@" -o '[email protected]' \
&& mv -- '[email protected]' '$@'
.PHONY: kube-env
kube-env:
# use / as a marker for "KUBECONFIG wasn't set", since this is never a valid file name
@echo '# use like so: eval "$$($(MAKE) $@)"'
@echo
@echo 'case "$${__K0S_LIBVIRT_MACHINES_OLD_KUBECONFIG-//}" in'
@echo '//) ;;'
@echo '/) unset KUBECONFIG;;'
@echo '*) export KUBECONFIG="$$__K0S_LIBVIRT_MACHINES_OLD_KUBECONFIG";;'
@echo 'esac'
@echo 'unset __K0S_LIBVIRT_MACHINES_OLD_KUBECONFIG'
@echo
@echo export __K0S_LIBVIRT_MACHINES_OLD_KUBECONFIG="$${KUBECONFIG-/}"
@echo export KUBECONFIG="'$(CURDIR)/profiles/$(PROFILE)/kubeconfig'":'"$${KUBECONFIG-$$HOME/.kube/config}"'
@echo echo KUBECONFIG set.
.terraform/.init: $(shell find . -type f -name 'terraform.tf')
$(TF) init
touch -- '$@'
profiles/$(PROFILE)/profile.tfvars: | profiles/$(PROFILE)
@if [ ! -f '$@' ]; then \
{ \
echo '# Put your variable overrides here ...' \
; echo \
; echo '# Prefix to be prepended to all resource names.' \
; echo resource_name_prefix = '"$(PROFILE)-"'\
; echo \
; echo '# IPv4 CIDR of the libvirt network of the virtual machines.' \
; u4() { od -N1 -tu4 -An < /dev/urandom | tr -d [:space:]; } \
; printf 'libvirt_network_ipv4_cidr = "172.%s.%s.0/24"\n' "$$(($$(u4) % 8 + 24))" "$$(u4)" \
; echo \
; echo '# The k0s version to deploy on the machines. May be an exact version, "stable" or "latest".' \
; echo 'k0s_version = "stable"' \
; echo '# Whether to enable k0s dynamic configuration.' \
; echo 'k0s_dynamic_config = false' \
; echo '# The k0s config spec' \
; echo 'k0s_config_spec = {}' \
; echo '# Path to the k0s binary to use, or null if it should be downloaded.' \
; echo 'k0sctl_k0s_binary = null' \
; echo '# Path to the airgap image bundle to be copied to the worker-enabled nodes, or null.' \
; echo 'k0sctl_airgap_image_bundle = null' \
; echo '# Additional files to be copied over to controller nodes.' \
; echo 'k0sctl_additional_controller_files = []' \
; echo '# Install flags to be passed to k0s.' \
; echo 'k0sctl_k0s_install_flags = []' \
; echo '# Install flags to be passed to k0s controllers.' \
; echo 'k0sctl_k0s_controller_install_flags = []' \
; echo '# K0sctl hooks for k0s controllers.' \
; echo 'k0sctl_k0s_controller_hooks = {}' \
; echo '# Install flags to be passed to k0s workers.' \
; echo 'k0sctl_k0s_worker_install_flags = []' \
; echo '# K0sctl hooks for k0s workers.' \
; echo 'k0sctl_k0s_worker_hooks = {}' \
; echo '' \
; echo '# Whether k0s on the controllers should also schedule workloads.' \
; echo 'controller_k0s_enable_worker = false' \
; echo '# The amount of RAM (in MiB) allocated to a controller node.' \
; echo 'controller_memory = 1024' \
; echo '# The number CPUs allocated to a controller node.' \
; echo 'controller_num_cpus = 1' \
; echo '# The number controller nodes to spin up.' \
; echo 'controller_num_nodes = 1' \
; echo \
; echo '# The amount of RAM (in MiB) allocated to a worker node.' \
; echo 'worker_memory = 1024' \
; echo '# The number CPUs allocated to a worker node.' \
; echo 'worker_num_cpus = 1' \
; echo '# The number worker nodes to spin up.' \
; echo 'worker_num_nodes = 1' \
; echo \
; echo '# Whether to use a load balancer in front of the control plane.' \
; echo 'loadbalancer_enabled = false' \
; } >'$@' \
; echo The file $@ has been created with some local terraform variable overrides. 1>&2 \
; if [ -n "$$EDITOR" ]; then \
read -p 'Please hit enter to edit it right now ... ' \
; "$$EDITOR" '$@' \
; else \
echo Please inspect it and re-run $(MAKE) $(MAKECMDGOALS) ... 1>&2 \
; exit 1 \
; fi \
; else \
touch -- '$@' \
; fi
profiles/$(PROFILE)/kubeconfig: .tf.apply.$(PROFILE) | profiles/$(PROFILE)
$(MAKE) -s output -- -json kubeconfig | $(JQ) -r . >'$@'
clean:
-if [ -f .terraform/init ]; then \
$(MAKE) destroy; \
else \
$(MAKE) .destroy; \
fi
-rm -rf .terraform
-rm -- 'profiles/$(PROFILE)/airgap-images.tar' 'profiles/$(PROFILE)/airgap-images.tar.tmp'
-$(MAKE) -C alpine-image clean