-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathkubectl-k8spin.py
executable file
·342 lines (268 loc) · 10.7 KB
/
kubectl-k8spin.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env python
import argparse
import json
import subprocess
ORG = {
"Name": ("metadata", "name"),
"CPU": ("spec", "resources", "cpu"),
"Memory": ("spec", "resources", "memory")
}
TENANT = {
"Organization": ("metadata", "labels", "k8spin.cloud/org"),
"Name": ("metadata", "name"),
"CPU": ("spec", "resources", "cpu"),
"Memory": ("spec", "resources", "memory")
}
SPACE = {
"Organization": ("metadata", "labels", "k8spin.cloud/org"),
"Tenant": ("metadata", "labels", "k8spin.cloud/tenant"),
"Name": ("metadata", "name"),
"CPU": ("spec", "resources", "cpu"),
"Memory": ("spec", "resources", "memory"),
"Default CPU": ("spec", "containers", "defaults", "resources", "cpu"),
"Default Memory": ("spec", "containers", "defaults", "resources", "memory")
}
class CommandArguments():
def __init__(self, org=None, tenant=None, space=None, cpu=None, memory=None, def_cpu=None, def_memory=None):
self.org = org
self.tenant = tenant
self.space = space
self.cpu = cpu
self.memory = memory
self.def_cpu = def_cpu
self.def_memory = def_memory
def extract_from_dictionary(dictionary, *keys_or_indexes):
value = dictionary
for key_or_index in keys_or_indexes:
value = value[key_or_index]
return value
def tab_print(values):
line = "{:<20s}"*len(values)
print(line.format(*values))
def print_item(item, kind):
tab_print([extract_from_dictionary(item, *kind[key])
for key in kind.keys()])
def execute_get_command(command, kind):
result = subprocess.run(
filter(None, command.split(" ")), capture_output=True)
if result.returncode == 0:
j = json.loads(result.stdout)
tab_print(list(kind.keys()))
if j.get("kind") == "List":
for item in j.get("items"):
print_item(item, kind)
else:
print_item(j, kind)
else:
print(result.stderr.decode("utf-8"))
def execute_command(command, input=None):
result = subprocess.run(
filter(None, command.split(" ")), capture_output=True, input=input)
if result.returncode == 0:
print(result.stdout.decode("utf-8"))
else:
print(result.stderr.decode("utf-8"))
def execute_apply_command(json_manifest):
command = "kubectl apply -f -"
execute_command(command, json.dumps(json_manifest).encode("utf-8"))
def create_org(arg: CommandArguments):
json_manifest = {
"kind": "Organization",
"apiVersion": "k8spin.cloud/v1",
"metadata": {
"name": arg.org
},
"spec": {
"resources": {
"cpu": arg.cpu,
"memory": arg.memory
}
}
}
execute_apply_command(json_manifest)
def get_org(arg: CommandArguments):
org = arg.org
if not org:
org = ""
command = f"kubectl get org {org} -o json"
execute_get_command(command, ORG)
def delete_org(arg: CommandArguments):
org = arg.org
if not org:
org = ""
command = f"kubectl delete org {org}"
execute_command(command)
def create_tenant(arg: CommandArguments):
json_manifest = {
"kind": "Tenant",
"apiVersion": "k8spin.cloud/v1",
"metadata": {
"name": arg.tenant,
"namespace": f"org-{arg.org}"
},
"spec": {
"resources": {
"cpu": arg.cpu,
"memory": arg.memory
}
}
}
execute_apply_command(json_manifest)
def get_tenant(arg: CommandArguments):
org = arg.org
if org:
opts = f"-n org-{org}"
else:
opts = "--all-namespaces"
tenant = arg.tenant
command = f"kubectl get tenant {tenant} -o json {opts}"
execute_get_command(command, TENANT)
def delete_tenant(arg: CommandArguments):
command = f"kubectl delete tenant {arg.tenant} -n org-{arg.org}"
execute_command(command)
def create_space(arg: CommandArguments):
json_manifest = {
"kind": "Space",
"apiVersion": "k8spin.cloud/v1",
"metadata": {
"name": arg.space,
"namespace": f"org-{arg.org}-tenant-{arg.tenant}"
},
"spec": {
"resources": {
"cpu": arg.cpu,
"memory": arg.memory
},
"containers": {
"defaults": {
"resources": {
"cpu": arg.def_cpu,
"memory": arg.def_memory
}
}
}
}
}
execute_apply_command(json_manifest)
def get_space(arg: CommandArguments):
org = arg.org
tenant = arg.tenant
if org:
opts = f"-n org-{org}-tenant-{tenant}"
else:
opts = "--all-namespaces"
space = arg.space
command = f"kubectl get space {space} -o json {opts}"
execute_get_command(command, SPACE)
def delete_space(arg: CommandArguments):
command = f"kubectl delete space {arg.space} -n org-{arg.org}-tenant-{arg.tenant}"
execute_command(command)
# Parent parser
parser = argparse.ArgumentParser(prog="kubectl k8spin",
description="K8Spin kubectl plugin to manage multi-tenancy concepts")
# Parent debug flag
parser.add_argument("--debug", default=False, required=False,
action="store_true", dest="debug", help="Activate debug mode")
# Parent commands
commands = parser.add_subparsers(title="commands", dest="command")
# Add get parser
get_parser = commands.add_parser(
"get", help="Get K8Spin resources")
# Add create parser
create_parser = commands.add_parser(
"create", help="Create K8Spin resources")
# Add delete parser
delete_parser = commands.add_parser(
"delete", help="Delete K8Spin resources")
version_parser = commands.add_parser("version", help="K8SPin Version")
# create commands
get_commands = get_parser.add_subparsers(
title="Query For", dest="sub_command")
create_commands = create_parser.add_subparsers(
title="Create", dest="sub_command")
delete_commands = delete_parser.add_subparsers(
title="Delete", dest="sub_command")
get_org_parser = get_commands.add_parser(
"org", help="Organization")
get_org_parser.add_argument("org_name", nargs="?", default="")
get_tenant_parser = get_commands.add_parser(
"tenant", help="Tenant")
get_tenant_parser.add_argument("tenant_name", nargs="?", default="")
get_tenant_parser.add_argument("--org", metavar="org", dest="org_name",
help="Filter by Organization")
get_space_parser = get_commands.add_parser(
"space", help="Space")
get_space_parser.add_argument("space_name", nargs="?", default="")
get_space_parser.add_argument("--tenant", metavar="tenant", dest="tenant_name",
help="Filter by Tenant")
get_space_parser.add_argument("--org", metavar="org", dest="org_name",
help="Filter by Organization")
create_org_parser = create_commands.add_parser(
"org", help="Organization")
create_org_parser.add_argument("org_name")
create_org_parser.add_argument("--cpu", metavar="cpu", dest="cpu",
help="CPU Amount", required=True)
create_org_parser.add_argument("--memory", metavar="memory", dest="memory",
help="Memory Amount", required=True)
create_tenant_parser = create_commands.add_parser(
"tenant", help="Tenant")
create_tenant_parser.add_argument("tenant_name")
create_tenant_parser.add_argument("--org", metavar="org", dest="org_name",
help="Filter by Organization", required=True)
create_tenant_parser.add_argument("--cpu", metavar="cpu", dest="cpu",
help="CPU Amount", required=True)
create_tenant_parser.add_argument("--memory", metavar="memory", dest="memory",
help="Memory Amount", required=True)
create_space_parser = create_commands.add_parser(
"space", help="Space")
create_space_parser.add_argument("space_name")
create_space_parser.add_argument("--org", metavar="org", dest="org_name",
help="Filter by Organization", required=True)
create_space_parser.add_argument("--tenant", metavar="tenant", dest="tenant_name",
help="Filter by Tenant", required=True)
create_space_parser.add_argument("--cpu", metavar="cpu", dest="cpu",
help="CPU Amount", required=True)
create_space_parser.add_argument("--memory", metavar="memory", dest="memory",
help="Memory Amount", required=True)
create_space_parser.add_argument("--default-cpu", metavar="cpu", dest="def_cpu",
help="CPU Amount", required=True)
create_space_parser.add_argument("--default-memory", metavar="memory", dest="def_memory",
help="Memory Amount", required=True)
delete_org_parser = delete_commands.add_parser(
"org", help="Organization")
delete_org_parser.add_argument("org_name")
delete_tenant_parser = delete_commands.add_parser(
"tenant", help="Tenant")
delete_tenant_parser.add_argument("tenant_name")
delete_tenant_parser.add_argument("--org", metavar="org", dest="org_name",
help="Filter by Organization", required=True)
delete_space_parser = delete_commands.add_parser(
"space", help="Space")
delete_space_parser.add_argument("space_name")
delete_space_parser.add_argument("--org", metavar="org", dest="org_name",
help="Filter by Organization", required=True)
delete_space_parser.add_argument("--tenant", metavar="tenant", dest="tenant_name",
help="Filter by Tenant", required=True)
# Parsing
main_args = parser.parse_args()
command = main_args.command if hasattr(main_args, "command") else None
sub_command = main_args.sub_command if hasattr(
main_args, "sub_command") else None
if command and sub_command:
org = main_args.org_name if hasattr(main_args, "org_name") else None
tenant = main_args.tenant_name if hasattr(
main_args, "tenant_name") else None
space = main_args.space_name if hasattr(main_args, "space_name") else None
cpu = main_args.cpu if hasattr(main_args, "cpu") else None
memory = main_args.memory if hasattr(main_args, "memory") else None
def_cpu = main_args.def_cpu if hasattr(main_args, "def_cpu") else None
def_memory = main_args.def_memory if hasattr(
main_args, "def_memory") else None
arg = CommandArguments(org=org, tenant=tenant,
space=space, cpu=cpu, memory=memory, def_cpu=def_cpu, def_memory=def_memory)
method_to_call = locals()[f"{command}_{sub_command}"]
method_to_call(arg)
elif command == "version":
print("K8SPin v1.1.0")
else:
parser.print_help()