Skip to content

Commit 62eb4ae

Browse files
author
Gunther Klessinger
committed
feat: flux
1 parent e7ff989 commit 62eb4ae

File tree

10 files changed

+428
-131
lines changed

10 files changed

+428
-131
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ wheels/
1010
.venv
1111
README.md
1212
mysecrets.py
13+
tmp

environ

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ HK_LOCATION="fsn1"
2424
GITOPS_BRANCH="main"
2525
GITOPS_HOST="py:keyval"
2626
GITOPS_OWNER="company"
27-
GITOPS_PATH="clusters/staging"
27+
GITOPS_PATH="clusters/production"
2828
GITOPS_REPO="k8s"
2929
GITOPS_TOKEN="py:keyval"
3030
GITOPS_FLUX_PRIV_SECRET="py:keyval"

justfile

+8-4
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ pyhk3-config *ARGS:
2828
just p hk3s render_config
2929
just p do show_env {{ARGS}}
3030

31+
env *ARGS:
32+
just p do show_env {{ARGS}}
3133

3234
[confirm('Sure to destroy all servers of the cluster?')]
3335
rm:
34-
just p do remove all
35-
36+
just p do delete all
3637

3738

3839
[confirm('Sure to destroy proxy (if existing) and recreate it?')]
@@ -53,10 +54,13 @@ port-forward:
5354
just p do port_forward
5455

5556

56-
install-flux:
57-
just p flux prepare_repo 'gh:/fluxcd/flux2-kustomize-helm-example'
57+
flux-install:
5858
just p flux install
59+
just p flux add_sops_secret
60+
just p flux add_tmpl 'gh:/fluxcd/flux2-kustomize-helm-example'
5961

62+
flux-uninstall:
63+
just p flux uninstall
6064

6165
test:
6266
just pyhk3-config

src/pyhk3/create.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,13 @@ def configure_caddy():
178178

179179
class tools:
180180
def ensure_ssh_key_known_to_hetzner():
181+
"""We return the name of any key which matches the fingerprint of the local key
182+
183+
If None: we create a new one and upload
184+
"""
181185
have = hapi.get('ssh_keys') or []
182186
fps = set([k['fingerprint'] for k in have])
183-
fp = E('FN_SSH_KEY') + '.pub'
187+
fp = E('FN_SSH_KEY', _home_repl=True) + '.pub'
184188
r = run(
185189
['ssh-keygen', '-l', '-E', 'md5', '-f', fp],
186190
capture_output=True,
@@ -192,8 +196,7 @@ def ensure_ssh_key_known_to_hetzner():
192196
n = E('NAME')
193197
if n in [k['name'] for k in have]:
194198
msg = f'proceed to delete and re-add the SSH key {n}'
195-
if not confirm(msg, default=False):
196-
die('unconfirmed')
199+
confirm(msg, default=False)
197200
f = [k['id'] for k in have if k['name'] == n]
198201
r = hapi.delete('ssh_keys', f[0])
199202
r = hapi.post('ssh_keys', data={'name': n, 'public_key': open(fp).read()})
@@ -312,7 +315,8 @@ def install():
312315
ip = ips('proxy')['pub']
313316
v = os.environ.get('HCLOUD_TOKEN')
314317
os.environ['HCLOUD_TOKEN'] = E('HCLOUD_TOKEN_WRITE')
315-
cmd = 'kubectl get nodes 2>/dev/null && echo "Skipping install - already running" || '
318+
cmd = 'mkdir -p /root/.kube && '
319+
cmd += 'kubectl get nodes 2>/dev/null && echo "Skipping install - already running" || '
316320
cmd += 'hetzner-k3s create --config /root/config.yml'
317321
ssh(ip, cmd=cmd, capture_output=False, send_env=['HCLOUD_TOKEN'])
318322
os.environ['HCLOUD_TOKEN'] = v

src/pyhk3/do.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import yaml
2+
import os
23
from .defaults import envdefaults
3-
from .tools import env
4+
from .tools import env, confirm
45
from .create import hk3s, tools, local
56
from .hapi import by_name, hapi, ips, need_env
67
from .tools import die, log, shw, ssh
@@ -12,6 +13,12 @@
1213

1314
def delete(name):
1415
"""Deleting objects by name, via hapi"""
16+
if name == 'all':
17+
N = E('NAME')
18+
r = [x['name'] for x in hapi.get('servers') if x['name'].startswith(N + '-')]
19+
confirm(f'Delete {r}')
20+
return [delete(i) for i in r]
21+
1522
S = by_name('servers', name)
1623
if not S:
1724
return log.info('Not found', name=name)
@@ -68,9 +75,26 @@ def show_env(match=''):
6875
print(f'{k}={v}')
6976

7077

78+
def namespace_force_delete(namespace):
79+
"""Force delete a namespace. Last result when delete ns got stuck"""
80+
kw = {}
81+
if namespace == 'flux-system':
82+
kw['hint'] = 'use "flux uninstall" instead'
83+
cmd = f"kubectl get namespace {namespace} -o json | jq '.spec.finalizers=[]' | kubectl replace --raw /api/v1/namespaces/{namespace}/finalize -f -"
84+
log.info('About brutally delete a namespace', cmd=cmd, **kw)
85+
confirm(f'Force delete ns {namespace}?', default=False)
86+
os.system(cmd)
87+
log.info('Namespace deleted brutally')
88+
cmd = f'kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get -n {namespace}'
89+
log.info(cmd, wait_confirm=True, hint='possible long list upcoming')
90+
confirm('Listing now all remaining resources', default=True)
91+
os.system(cmd)
92+
93+
7194
class do:
7295
ssh = run_remote
7396
delete = delete
7497
download_kubectl = local.download_kubectl
7598
port_forward = port_forward
7699
show_env = show_env
100+
ns_del_force = namespace_force_delete

src/pyhk3/flux.py

-93
This file was deleted.

0 commit comments

Comments
 (0)