-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
67 lines (54 loc) · 2.04 KB
/
main.py
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
import os
import argparse
from glueops.setup_logging import configure as go_configure_logging
from kubernetes import (
config as k8s_config
)
from backup import backup
from restore import restore
output_file = "secrets.yaml"
# configure logger
logger = go_configure_logging(
name='CERT_BACKUP_RESTORE',
level=os.getenv('PYTHON_LOG_LEVEL', 'INFO')
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="CLI used to backup and restore k8s tls secrets")
parser.add_argument("--backup", action="store_true", help="backs up tls secrets in s3")
parser.add_argument("--restore", action="store_true", help="restore tls secrets from s3 in k8s")
args = parser.parse_args()
# setting cluster config
try:
k8s_config.load_incluster_config()
logger.info("Loaded incluster kubeconfig")
except Exception as e:
logger.warning(f'Error loading in-cluster k8s config: {e}')
try:
logger.info('Using local Kubeconfig (not in-cluster)')
k8s_config.load_kube_config()
except Exception:
logger.exception('Failed to load Kubeconfig from cluster, local file')
if args.backup:
try:
all_tls_secrets = backup.get_tls_secrets()
if all_tls_secrets:
backup.write_secrets_to_file(all_tls_secrets, output_file)
logger.info(f"TLS secrets retrieved and stored in {output_file}")
else:
logger.info("No TLS secrets found.")
backup.upload_secrets_to_s3()
except Exception as e:
logger.error(f"Error backing up secrets: {e}")
if args.restore:
try:
latest_backup = restore.get_latest_backup()
if latest_backup:
restore.restore_tls_secrets()
else:
logger.info("No backups found.")
except Exception as e:
logger.error(f"Error restoring secrets: {e}")
else:
exit()
if os.path.exists(output_file):
os.remove(output_file)