Skip to content

Commit

Permalink
Update instant backups to include k8up PodConfig if tolerations/affin…
Browse files Browse the repository at this point in the history
…ities present; always update backup values even if app exists; update textual to 0.71.0 (#277)

* allow setting a k8up podConfig during TUI instant backup if argo.path has word 'toleration' in it

* update doc screenshot and poetry.lock

* always declare the backup values ahead of determining if app exists

* closes #276 : update textual to 0.71.0
  • Loading branch information
jessebot committed Jul 2, 2024
1 parent 57538b2 commit e9bcfb5
Show file tree
Hide file tree
Showing 9 changed files with 342 additions and 287 deletions.
164 changes: 82 additions & 82 deletions docs/assets/images/screenshots/help_text.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
405 changes: 214 additions & 191 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "smol_k8s_lab"
version = "5.8.4"
version = "5.9.0"
description = "CLI and TUI to quickly install slimmer Kubernetes distros and then manage apps declaratively using Argo CD"
authors = ["Jesse Hitch <jessebot@linux.com>",
"Max Roby <emax@cloudydev.net>"]
Expand Down Expand Up @@ -43,7 +43,7 @@ requests = "^2.32"
rich = "^13.0"
ruamel-yaml = "^0.18"
ruamel-yaml-string = "^0.1"
textual = "^0.70"
textual = "^0.71"
xdg-base-dirs = "^6.0"
pygame = "^2.5"
python-ulid = "^2.6"
Expand Down
7 changes: 4 additions & 3 deletions smol_k8s_lab/k8s_apps/social/mastodon.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def configure_mastodon(argocd: ArgoCD,
# we need namespace immediately
mastodon_namespace = cfg['argo']['namespace']

if init_enabled:
# backups are their own config.yaml section
backup_vals = process_backup_vals(cfg.get('backups', {}), 'mastodon', argocd)

if init_enabled and not app_installed:
argocd.k8s.create_namespace(mastodon_namespace)

Expand Down Expand Up @@ -90,9 +94,6 @@ def configure_mastodon(argocd: ArgoCD,
# create a local alias to check and make sure mastodon is functional
create_minio_alias("mastodon", s3_endpoint, "mastodon", s3_access_key)

# backups are their own config.yaml section
backup_vals = process_backup_vals(cfg.get('backups', {}), 'mastodon', argocd)

if bitwarden and not restore_enabled:
setup_bitwarden_items(argocd,
mastodon_hostname,
Expand Down
5 changes: 3 additions & 2 deletions smol_k8s_lab/k8s_apps/social/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ def configure_matrix(argocd: ArgoCD,
# always declare matrix namespace immediately
matrix_namespace = cfg['argo']['namespace']

if init_enabled:
backup_vals = process_backup_vals(cfg['backups'], 'matrix', argocd)

# initial secrets to deploy this app from scratch
if init_enabled and not app_installed:
argocd.k8s.create_namespace(matrix_namespace)

backup_vals = process_backup_vals(cfg['backups'], 'matrix', argocd)

# configure s3 credentials
s3_endpoint = secrets.get('s3_endpoint', "")
s3_bucket = secrets.get('s3_bucket', "matrix")
Expand Down
7 changes: 4 additions & 3 deletions smol_k8s_lab/k8s_apps/social/nextcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ def configure_nextcloud(argocd: ArgoCD,
header(f"{header_start} [green]Nextcloud[/], to self host your files",
'🩵')

if init_enabled:
# backups are their own config.yaml section
backup_vals = process_backup_vals(cfg.get('backups', {}), 'nextcloud', argocd)

# if the user has chosen to use smol-k8s-lab initialization
if not app_installed and init_enabled:
nextcloud_namespace = cfg['argo']['namespace']
Expand All @@ -81,9 +85,6 @@ def configure_nextcloud(argocd: ArgoCD,
else:
log.warn("Strange, there's no nextcloud init values...")

# backups are their own config.yaml section
backup_vals = process_backup_vals(cfg.get('backups', {}), 'nextcloud', argocd)

if secrets:
s3_endpoint = secrets.get('s3_endpoint', "")
if s3_endpoint and not restore_enabled:
Expand Down
9 changes: 7 additions & 2 deletions smol_k8s_lab/k8s_tools/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def create_pvc_restic_backup(app: str,
bucket: str,
cnpg_backup: bool = True,
cnpg_s3_endpoint: str = "",
quiet: bool = False) -> None:
quiet: bool = False,
needs_pod_config: bool = False) -> None:
"""
a function to immediately run a restic backup job
unless it's nextcloud, then we put it into maintenance_mode first...
Expand All @@ -39,7 +40,7 @@ def create_pvc_restic_backup(app: str,
"spec": {
"failedJobsHistoryLimit": 5,
"promURL": "push-gateway.prometheus.svc:9091",
"successfulJobsHistoryLimit": 2,
"successfulJobsHistoryLimit": 1,
"backend": {
"repoPasswordSecretRef": {
"key": "resticRepoPassword",
Expand All @@ -63,6 +64,10 @@ def create_pvc_restic_backup(app: str,
}
}

# some apps, like home assistant, often need a tolerations/affinity spec
if needs_pod_config:
backup_yaml['spec']['podConfigRef'] = {"name": f"{app}-podconfig"}

# nextcloud is special and needs to be put into maintenance mode before backups
if app == "nextcloud":
# nextcloud backups need to run as user 82 which is nginx
Expand Down
19 changes: 18 additions & 1 deletion smol_k8s_lab/k8s_tools/k8s_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,24 @@ def update_secret_key(self,
if in_line_key_name is set to a key name, you can specify a base key in a
secret that contains an inline yaml block
"""
secret_data = self.get_secret(secret_name, secret_namespace)['data']

# get current secret data, but catch if there's no secret at all
try:
secret_data = self.get_secret(secret_name, secret_namespace)['data']
except Exception as e:
log.error(f"Error getting secret: {e}")
log.info("creating new secret")
if in_line_key_name:
self.create_secret(secret_name,
secret_namespace,
updated_values_dict,
in_line_key_name)
else:
self.create_secret(secret_name,
secret_namespace,
updated_values_dict)
# return immediately so we don't do the rest of the function
return

# if this is a secret with a filename key and then inline yaml inside...
if in_line_key_name:
Expand Down
9 changes: 8 additions & 1 deletion smol_k8s_lab/tui/app_widgets/backup_and_restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ def trigger_backup(self) -> None:
"""
run backup of an app in a thread so we don't lock up the UI
"""
# some apps, like home assistant need a special podConfig for k8up due
# to needing tolerations/affinity specs
needs_pod_config = False
if "toleration" in self.screen.cfg[self.app_name]['argo']['path']:
needs_pod_config = True

namespace = self.screen.cfg[self.app_name]['argo']['namespace']

self.log(
Expand All @@ -242,7 +248,8 @@ def trigger_backup(self) -> None:
bucket=self.backup_s3_bucket,
cnpg_backup=cnpg_backup,
cnpg_s3_endpoint=cnpg_endpoint,
quiet=True)
quiet=True,
needs_pod_config=needs_pod_config)
self.get_widget_by_id(f"{self.app_name}-backup-button").display = True
self.get_widget_by_id(f"{self.app_name}-backup-running").display = False
self.app.notify("\nSuccessfully backed up! 🎉",
Expand Down

0 comments on commit e9bcfb5

Please sign in to comment.