-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathget_k8_config.py
116 lines (89 loc) · 3.46 KB
/
get_k8_config.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
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
import json
import boto3
from util import validate_unique_cluster_name
import storage
SECRETS_CLIENT = boto3.client('secretsmanager')
def get_all_k8_configs(event, context):
"""Generate k8 config object for all tracked clusters"""
# /get-all-k8s-configs
clusters = _cluster_list()
results = _generate_cluster_config(clusters)
return results
def get_k8_config(event, context):
"""Generate k8 config object from list of clusters as query input"""
# /get-k8-config?cloud-infra.cloud&cloud-infra-2.net
clusters = event['queryStringParameters']
results = _generate_cluster_config(clusters)
return results
def _generate_cluster_config(clusters):
CLUSTER_TABLE = storage.get_cluster_table()
config = {
"apiVersion": "v1",
"kind": "Config",
"preferences": {},
"clusters": [],
"users": [],
"contexts": [],
"current-context": ""
}
for cluster in clusters:
if validate_unique_cluster_name(cluster, CLUSTER_TABLE) is not None:
cluster_item = CLUSTER_TABLE.get_item(Key={"id": cluster})
cluster_item = cluster_item['Item']
# Add certificate-authority-data if available,
# this is optional at the time of adding config
if cluster_item['certificate-authority-data'] != "NA":
config["clusters"].append(
{"cluster":
{"certificate-authority-data":
cluster_item['certificate-authority-data'],
"server": cluster_item['server']
},
"name": cluster_item['id']
})
else:
config["clusters"].append(
{"cluster":
{"server": cluster_item['server']},
"name": cluster_item['id']
})
for user in cluster_item['users_config']:
for user_key, secret in user['user'].items():
print(f'getting secret: {secret}')
secret_response = SECRETS_CLIENT.get_secret_value(
SecretId=secret
)
user['user'][user_key] = secret_response['SecretString']
for user in cluster_item['users_config']:
config["users"].append(user)
config["contexts"].append(
{"context":
{"cluster": cluster_item['id'],
"user": cluster_item['id']
},
"name": cluster_item['id']
})
# Last item processed will become the current-context in response
config["current-context"] = cluster_item['id']
else:
return {
"statusCode": 404,
"body": json.dumps(
{"message": (f'Unable to process cluster config for '
f'{cluster}, confirm cluster is in list '
f'endpoint output')}
)
}
return {
"statusCode": 200,
"body": json.dumps(config)
}
def _cluster_list():
"""Scan for all cluster ids and return list"""
CLUSTER_TABLE = storage.get_cluster_table()
clusters = []
cluster_items = CLUSTER_TABLE.scan()
for cluster in cluster_items['Items']:
clusters.append(cluster['id'])
print(f'tracked clusters: {clusters}')
return clusters