-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathbuild-kubeconfig.py
More file actions
446 lines (378 loc) · 15.3 KB
/
Copy pathbuild-kubeconfig.py
File metadata and controls
446 lines (378 loc) · 15.3 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#!/usr/bin/env python3
"""Multi-cluster kubeconfig builder for cluster-report.
Two subcommands:
setup Apply RBAC and extract SA tokens for clusters you're logged into
build Build a merged kubeconfig from a clusters inventory file
Usage:
python3 build-kubeconfig.py setup [--all-contexts] [--contexts ctx1,ctx2]
[--output-inventory <path>]
python3 build-kubeconfig.py build --clusters <clusters.json>
[--output <path>] [--verify]
Requires: oc or kubectl, python3 (stdlib only)
"""
import argparse
import base64
import json
import os
import shutil
import subprocess
import sys
import time
from pathlib import Path
SCRIPT_DIR = Path(__file__).resolve().parent
RBAC_MANIFEST = SCRIPT_DIR / "cluster-reporter-rbac.yaml"
SA_NAMESPACE = "cluster-reporter-system"
SECRET_NAME = "cluster-reporter-token"
DEFAULT_INVENTORY = Path.home() / ".ocp-clusters" / "clusters.json"
DEFAULT_OUTPUT = Path("/tmp/cluster-report-kubeconfig")
def find_kube_cmd():
"""Detect oc (preferred) or kubectl in PATH."""
if shutil.which("oc"):
return "oc"
if shutil.which("kubectl"):
print("WARNING: 'oc' not found – falling back to 'kubectl'. "
"Install the OpenShift CLI (oc) for full compatibility: "
"https://mirror.openshift.com/pub/openshift-v4/clients/ocp/stable/",
file=sys.stderr)
return "kubectl"
print('{"error": "Neither oc nor kubectl found in PATH. '
'Install oc: https://mirror.openshift.com/pub/openshift-v4/clients/ocp/stable/"}',
file=sys.stderr)
sys.exit(1)
# ---------------------------------------------------------------------------
# Setup mode
# ---------------------------------------------------------------------------
def run_setup(args):
kube_cmd = find_kube_cmd()
inventory_file = Path(args.output_inventory)
if not args.skip_rbac and not RBAC_MANIFEST.is_file():
print(f"Error: RBAC manifest not found at {RBAC_MANIFEST}", file=sys.stderr)
sys.exit(1)
try:
all_ctx = subprocess.check_output(
[kube_cmd, "config", "get-contexts", "-o", "name"],
text=True, stderr=subprocess.DEVNULL
).strip().splitlines()
except subprocess.CalledProcessError:
all_ctx = []
if not all_ctx:
print('{"error": "No kubeconfig contexts found. Log in to at least one cluster first."}',
file=sys.stderr)
sys.exit(1)
if args.contexts:
contexts = args.contexts.split(",")
unknown = [c for c in contexts if c not in all_ctx]
if unknown:
print(f"Error: unknown context(s): {', '.join(unknown)}", file=sys.stderr)
print(f"Available: {', '.join(all_ctx)}", file=sys.stderr)
sys.exit(1)
elif args.all_contexts:
contexts = all_ctx
else:
print("Available contexts:")
for i, ctx in enumerate(all_ctx, 1):
print(f" {i}. {ctx}")
print()
print("Run with --all-contexts to setup all, or --contexts ctx1,ctx2 to select specific ones.")
sys.exit(0)
print(f"Pre-flight: checking {len(contexts)} cluster(s)...\n")
reachable = {}
for ctx in contexts:
server = _get_server_url(kube_cmd, ctx)
if not server:
print(f" {ctx}: SKIP (no server URL in kubeconfig)")
continue
try:
subprocess.run(
[kube_cmd, "cluster-info", "--context", ctx],
capture_output=True, text=True, timeout=15, check=True
)
reachable[ctx] = server
print(f" {ctx}: reachable ({server})")
except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
print(f" {ctx}: SKIP (unreachable – try '{kube_cmd} login {server}' first)")
continue
if not args.skip_rbac:
try:
result = subprocess.run(
[kube_cmd, "auth", "can-i", "create", "clusterroles",
"--context", ctx],
capture_output=True, text=True, timeout=10
)
if result.stdout.strip().lower() != "yes":
print(f" {ctx}: SKIP (insufficient permissions – "
f"cluster-admin required for RBAC setup, "
f"or use --skip-rbac if RBAC is already applied)")
del reachable[ctx]
except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
print(f" {ctx}: SKIP (could not verify permissions)")
del reachable[ctx]
if not reachable:
print("\nError: no eligible clusters found. Nothing to do.", file=sys.stderr)
sys.exit(1)
print(f"\n{len(reachable)}/{len(contexts)} cluster(s) ready. "
f"Proceeding with setup...\n")
inventory_file.parent.mkdir(parents=True, exist_ok=True)
existing_by_name = {}
if inventory_file.is_file():
try:
with open(inventory_file) as f:
existing_by_name = {c["name"]: c for c in json.load(f).get("clusters", [])}
except (json.JSONDecodeError, KeyError):
pass
results = {"setup": [], "errors": []}
for ctx, server in reachable.items():
print(f"--- {ctx} ---")
print(f" Server: {server}")
if args.skip_rbac:
print(" Skipping RBAC apply (--skip-rbac)")
else:
print(" Applying RBAC...")
try:
subprocess.run(
[kube_cmd, "apply", "-f", str(RBAC_MANIFEST), "--context", ctx],
capture_output=True, text=True, timeout=30, check=True
)
except subprocess.CalledProcessError as e:
results["errors"].append(f"{ctx}: RBAC apply failed: {e.stderr.strip()}")
print(f" FAIL: RBAC apply failed: {e.stderr.strip()}")
continue
print(" Waiting for token...")
token = _wait_for_token(kube_cmd, ctx)
if not token:
results["errors"].append(f"{ctx}: token not populated after 15s")
print(" FAIL: token Secret not populated")
continue
try:
decoded_token = base64.b64decode(token).decode("utf-8")
except Exception:
decoded_token = token
existing_by_name[ctx] = {"name": ctx, "api_url": server, "token": decoded_token}
results["setup"].append(ctx)
print(" OK: token extracted")
with open(inventory_file, "w") as f:
json.dump({"clusters": list(existing_by_name.values())}, f, indent=2)
os.chmod(inventory_file, 0o600)
print()
print("=" * 50)
print(f"Setup complete: {len(results['setup'])} succeeded, {len(results['errors'])} failed")
if results["errors"]:
print("Errors:")
for e in results["errors"]:
print(f" - {e}")
print(f"Inventory written to: {inventory_file}")
print()
print("Next step:")
print(f" python3 {__file__} build --clusters {inventory_file} --verify")
json.dump(results, sys.stderr, indent=2)
def _get_server_url(kube_cmd, ctx):
"""Resolve the API server URL for a kubeconfig context."""
try:
server = subprocess.check_output(
[kube_cmd, "config", "view", "-o",
f'jsonpath={{.clusters[?(@.name=="{ctx}")].cluster.server}}'],
text=True, stderr=subprocess.DEVNULL
).strip()
if server:
return server
cluster_ref = subprocess.check_output(
[kube_cmd, "config", "view", "-o",
f'jsonpath={{.contexts[?(@.name=="{ctx}")].context.cluster}}'],
text=True, stderr=subprocess.DEVNULL
).strip()
if cluster_ref:
return subprocess.check_output(
[kube_cmd, "config", "view", "-o",
f'jsonpath={{.clusters[?(@.name=="{cluster_ref}")].cluster.server}}'],
text=True, stderr=subprocess.DEVNULL
).strip() or None
except subprocess.CalledProcessError:
pass
return None
def _wait_for_token(kube_cmd, ctx, timeout_secs=15):
"""Poll for the SA token Secret to be populated."""
for _ in range(timeout_secs):
try:
token = subprocess.check_output(
[kube_cmd, "get", "secret", SECRET_NAME,
"-n", SA_NAMESPACE, "--context", ctx,
"-o", "jsonpath={.data.token}"],
text=True, stderr=subprocess.DEVNULL, timeout=10
).strip()
if token:
return token
except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
pass
time.sleep(1)
return None
# ---------------------------------------------------------------------------
# Build mode
# ---------------------------------------------------------------------------
def run_build(args):
kube_cmd = find_kube_cmd()
clusters_file = Path(args.clusters)
output_file = Path(args.output)
if not clusters_file.is_file():
print(f'{{"error": "Clusters file not found: {clusters_file}"}}', file=sys.stderr)
sys.exit(1)
with open(clusters_file) as f:
config = json.load(f)
clusters = config.get("clusters", [])
if not clusters:
print('{"error": "No clusters in inventory file"}', file=sys.stderr)
sys.exit(1)
output_file.unlink(missing_ok=True)
output_file.touch(mode=0o600)
env = {**os.environ, "KUBECONFIG": str(output_file)}
errors = []
success = 0
for c in clusters:
name = c.get("name", "")
api_url = c.get("api_url", "")
if not name or not api_url:
errors.append(f"Entry missing name or api_url: {c}")
continue
token = _resolve_token(c, errors)
if token is None:
continue
ca_args = (["--certificate-authority", c["ca_cert"]]
if c.get("ca_cert")
else ["--insecure-skip-tls-verify=true"])
try:
subprocess.run(
[kube_cmd, "config", "set-cluster", name, "--server", api_url] + ca_args,
check=True, capture_output=True, env=env
)
except subprocess.CalledProcessError as e:
errors.append(f"{name}: set-cluster failed: {e.stderr.decode().strip()}")
continue
try:
subprocess.run(
[kube_cmd, "config", "set-credentials", f"{name}-reporter", "--token", token],
check=True, capture_output=True, env=env
)
except subprocess.CalledProcessError as e:
errors.append(f"{name}: set-credentials failed: {e.stderr.decode().strip()}")
continue
try:
subprocess.run(
[kube_cmd, "config", "set-context", name,
"--cluster", name, "--user", f"{name}-reporter"],
check=True, capture_output=True, env=env
)
except subprocess.CalledProcessError as e:
errors.append(f"{name}: set-context failed: {e.stderr.decode().strip()}")
continue
if success == 0:
subprocess.run(
[kube_cmd, "config", "use-context", name],
check=False, capture_output=True, env=env
)
success += 1
verify_results = {}
if args.verify and success > 0:
print(f"Verifying {success} context(s)...")
for c in clusters:
name = c.get("name", "")
if not name:
continue
try:
subprocess.run(
[kube_cmd, "get", "nodes", "--context", name, "-o", "name", "--no-headers"],
capture_output=True, text=True, timeout=15, check=True, env=env
)
verify_results[name] = "ok"
print(f" {name}: OK")
except subprocess.TimeoutExpired:
verify_results[name] = "timeout"
errors.append(f"{name}: verification timed out")
print(f" {name}: TIMEOUT")
except subprocess.CalledProcessError:
verify_results[name] = "failed"
errors.append(f"{name}: verification failed (likely expired token)")
print(f" {name}: FAILED (re-run setup for this cluster)")
result = {
"clusters_configured": success,
"clusters_failed": len(errors),
"kubeconfig": str(output_file),
"errors": errors,
}
if args.verify:
result["verification"] = verify_results
print()
print(json.dumps(result, indent=2))
print()
print(f"Kubeconfig written to: {output_file}")
print()
print("To use with cluster-report:")
print(f" export KUBECONFIG={output_file}")
if success == 0:
sys.exit(1)
def _resolve_token(cluster_entry, errors):
"""Resolve token from inline value or environment variable. Returns None on failure."""
name = cluster_entry.get("name", "<unknown>")
if "token_env" in cluster_entry:
token = os.environ.get(cluster_entry["token_env"])
if not token:
errors.append(f"{name}: env var {cluster_entry['token_env']} not set")
return None
return token
if "token" in cluster_entry:
return cluster_entry["token"]
errors.append(f"{name}: no token or token_env specified")
return None
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser(
description="Multi-cluster kubeconfig builder for cluster-report",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
subparsers = parser.add_subparsers(dest="command", required=True)
# -- setup --
setup_parser = subparsers.add_parser(
"setup",
help="Apply RBAC to clusters you're logged into, extract SA tokens, "
"and write a clusters inventory file.",
)
setup_parser.add_argument(
"--all-contexts", action="store_true",
help="Setup all kubeconfig contexts without prompting.",
)
setup_parser.add_argument(
"--contexts", type=str, default=None,
help="Comma-separated list of contexts to setup.",
)
setup_parser.add_argument(
"--skip-rbac", action="store_true",
help="Skip RBAC apply and only extract tokens (use when RBAC is already configured).",
)
setup_parser.add_argument(
"--output-inventory", type=str, default=str(DEFAULT_INVENTORY),
help=f"Path for the clusters inventory file (default: {DEFAULT_INVENTORY}).",
)
# -- build --
build_parser = subparsers.add_parser(
"build",
help="Read a clusters inventory file and build a merged kubeconfig.",
)
build_parser.add_argument(
"--clusters", type=str, required=True,
help="Path to the clusters inventory JSON file.",
)
build_parser.add_argument(
"--output", type=str, default=str(DEFAULT_OUTPUT),
help=f"Path for the generated kubeconfig (default: {DEFAULT_OUTPUT}).",
)
build_parser.add_argument(
"--verify", action="store_true",
help="Test each context after building the kubeconfig.",
)
args = parser.parse_args()
if args.command == "setup":
run_setup(args)
elif args.command == "build":
run_build(args)
if __name__ == "__main__":
main()