-
Notifications
You must be signed in to change notification settings - Fork 1
/
kube_dump.py
163 lines (120 loc) · 4.36 KB
/
kube_dump.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
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
"""
Quick and dirty script to dump services, rcs and secrets from
a given kubernetes namespace into files. This does replicate
some of what is in kubectl, but this removes a few items from
the output to make importing into a new cluster a bit easier.
Information is gathered from the ``~/.kube/config`` file, so
ensure your ``current-context`` is correctly set.
"""
import argparse
import base64
import json
import os
import requests
import sys
import yaml
def get_server(kubeconfig_path):
if kubeconfig_path.startswith('~'):
kubeconfig_path = os.path.expanduser(kubeconfig_path)
if not os.path.exists(os.path.realpath(kubeconfig_path)):
sys.stderr.write("kubeconfig path does not exist!")
exit(1)
with open(kubeconfig_path, 'r') as f:
cfg = yaml.load(f.read())
current_context = cfg.get('current-context')
try:
cluster = [i for i in cfg['clusters'] if i['name'] == current_context][0]
except:
sys.stderr.write(
"Something went wrong getting cluster info. check your kubeconfig")
exit(1)
try:
user = [i for i in cfg['users'] if i['name'] == current_context][0]
except:
sys.stderr.write(
"Something went wrong getting user info. check your kubeconfig")
exit(1)
return {
'url_base': cluster['cluster']['server'],
'username': user['user'].get('username'),
'password': user['user'].get('password'),
}
def alter_service(item):
# remove the status LB things.
item.update({
'status': {'loadBalancer': {}},
})
return remove_metadata(item, extra=('spec', 'clusterIP'))
def alter_rcs(item):
item.update({'status': {}})
return remove_metadata(item)
def remove_metadata(item, extra=()):
to_del = (
('metadata', 'resourceVersion'),
('metadata', 'selfLink'),
('metadata', 'uid'),
('metadata', 'creationTimestamp'),
)
to_del += (extra, )
for part in to_del:
try:
del item[part[0]][part[1]]
except:
pass
return item
def dump(_type, cluster, namespace, outpath):
"""
Dump the specified type to a file or stdout.
"""
url = '{}/api/v1/namespaces/{}/{}'.format(
cluster['url_base'], namespace, _type
)
resp = requests.get(
url, auth=(cluster['username'], cluster['password']), verify=False)
json_resp = resp.json()
for item in json_resp.get('items'):
item.update({
'apiVersion': json_resp.get('apiVersion'),
'kind': json_resp.get('kind').replace('List', ''),
})
# massage some of the output...
if _type == 'services':
output = alter_service(item)
elif _type == 'replicationcontrollers':
output = alter_rcs(item)
elif _type == 'secrets':
output = remove_metadata(item)
if outpath:
out_dir = os.path.join(outpath, _type)
if not os.path.exists(out_dir):
os.makedirs(out_dir)
out_path = os.path.join(
out_dir, '{}.json'.format(item['metadata']['name']))
with open(out_path, 'w') as f_:
json.dump(output, f_, indent=2)
else:
sys.stdout.write(json.dumps(output, indent=2))
sys.stdout.write('\n\n')
def main():
parser = argparse.ArgumentParser(description='Dump a k8s namespace')
parser.add_argument('--namespace', type=str, default='default',
help='k8s namespace.')
parser.add_argument('--kubeconfig', type=str, required=True,
help='path to kubeconfig file.')
parser.add_argument('--outpath', type=str,
help="Path to write files to. " \
"If none is specified, print to stdout")
args = parser.parse_args()
output_path = args.outpath
if output_path:
if output_path.startswith('~'):
output_path = os.expanduser(output_path)
output = os.path.realpath(output_path)
if not os.path.exists(output_path):
os.makedirs(output_path)
cluster = get_server(args.kubeconfig)
dump('secrets', cluster, args.namespace, output_path)
dump('services', cluster, args.namespace, output_path)
dump('replicationcontrollers', cluster, args.namespace, output_path)
if __name__ == '__main__':
main()