|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Microsoft Corporation. |
| 3 | +# Licensed under the MIT license. |
| 4 | + |
| 5 | +""" |
| 6 | +Iterates through each API group folder and runs controller-gen to produce |
| 7 | +per-group webhook configurations (<group>-mwh.yaml / <group>-vwh.yaml), |
| 8 | +avoiding a single massive manifest that risks exceeding etcd size limits. |
| 9 | +
|
| 10 | +Usage: generate-webhooks-per-group.py --api-dir <API_DIR> --output-dir <OUTPUT_DIR> |
| 11 | +""" |
| 12 | + |
| 13 | +import argparse |
| 14 | +import os |
| 15 | +import shutil |
| 16 | +import subprocess |
| 17 | +import sys |
| 18 | +import tempfile |
| 19 | +from concurrent.futures import ThreadPoolExecutor, as_completed |
| 20 | +from pathlib import Path |
| 21 | + |
| 22 | +import yaml |
| 23 | + |
| 24 | +KIND_SUFFIXES = { |
| 25 | + "MutatingWebhookConfiguration": "mwh", |
| 26 | + "ValidatingWebhookConfiguration": "vwh", |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +def has_webhook_markers(group_dir: Path) -> bool: |
| 31 | + """Check if any Go file in the directory tree contains a kubebuilder:webhook marker.""" |
| 32 | + for f in group_dir.rglob("*.go"): |
| 33 | + if "kubebuilder:webhook" in f.read_text(errors="replace"): |
| 34 | + return True |
| 35 | + return False |
| 36 | + |
| 37 | + |
| 38 | +def run_controller_gen(api_dir: Path, group: str, output_dir: Path) -> None: |
| 39 | + """Run controller-gen webhook for a single group.""" |
| 40 | + subprocess.run( |
| 41 | + [ |
| 42 | + "controller-gen", |
| 43 | + "webhook", |
| 44 | + f"output:webhook:artifacts:config={output_dir}", |
| 45 | + f"paths=./{group}/...", |
| 46 | + ], |
| 47 | + cwd=api_dir, |
| 48 | + check=True, |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def split_manifests(manifests_path: Path, group: str, bases_dir: Path) -> list[str]: |
| 53 | + """Split a multi-doc manifests.yaml into per-kind files with renamed metadata.name.""" |
| 54 | + generated = [] |
| 55 | + with open(manifests_path) as f: |
| 56 | + docs = list(yaml.safe_load_all(f)) |
| 57 | + |
| 58 | + for doc in docs: |
| 59 | + if doc is None: |
| 60 | + continue |
| 61 | + kind = doc.get("kind", "") |
| 62 | + suffix = KIND_SUFFIXES.get(kind) |
| 63 | + if suffix is None: |
| 64 | + print(f" WARNING: unexpected kind '{kind}' in {group}, skipping") |
| 65 | + continue |
| 66 | + |
| 67 | + doc["metadata"]["name"] = f"{group}-{suffix}" |
| 68 | + out_file = bases_dir / f"{group}-{suffix}.yaml" |
| 69 | + with open(out_file, "w") as f: |
| 70 | + yaml.dump(doc, f, default_flow_style=False, sort_keys=False) |
| 71 | + |
| 72 | + rel = f"bases/{out_file.name}" |
| 73 | + generated.append(rel) |
| 74 | + print(f" Generated: {out_file.name}") |
| 75 | + |
| 76 | + return generated |
| 77 | + |
| 78 | + |
| 79 | +def write_kustomization(output_dir: Path, resources: list[str]) -> None: |
| 80 | + """Write a kustomization.yaml listing all generated resources.""" |
| 81 | + content = { |
| 82 | + "resources": resources, |
| 83 | + } |
| 84 | + kustomization_path = output_dir / "kustomization.yaml" |
| 85 | + with open(kustomization_path, "w") as f: |
| 86 | + f.write("# This file is auto-generated by generate-webhooks-per-group.py\n") |
| 87 | + f.write("# DO NOT EDIT manually\n") |
| 88 | + yaml.dump(content, f, default_flow_style=False, sort_keys=False) |
| 89 | + |
| 90 | + |
| 91 | +def main() -> None: |
| 92 | + parser = argparse.ArgumentParser(description=__doc__) |
| 93 | + parser.add_argument("--api-dir", required=True, help="Path to the api directory (e.g. v2/api)") |
| 94 | + parser.add_argument("--output-dir", required=True, help="Webhook output directory (e.g. v2/config/webhook/generated)") |
| 95 | + args = parser.parse_args() |
| 96 | + |
| 97 | + api_dir = Path(args.api_dir).resolve() |
| 98 | + output_dir = Path(args.output_dir).resolve() |
| 99 | + bases_dir = output_dir / "bases" |
| 100 | + |
| 101 | + # Clean and create output directories |
| 102 | + if bases_dir.exists(): |
| 103 | + shutil.rmtree(bases_dir) |
| 104 | + bases_dir.mkdir(parents=True) |
| 105 | + |
| 106 | + print("Generating per-group webhook configurations...") |
| 107 | + print(f" API directory: {api_dir}") |
| 108 | + print(f" Output directory: {output_dir}") |
| 109 | + |
| 110 | + all_resources: list[str] = [] |
| 111 | + |
| 112 | + with tempfile.TemporaryDirectory() as tmp: |
| 113 | + tmp_path = Path(tmp) |
| 114 | + |
| 115 | + # Phase 1: discover groups with webhook markers |
| 116 | + groups: list[str] = [] |
| 117 | + group_tmp_dirs: dict[str, Path] = {} |
| 118 | + for group_dir in sorted(api_dir.iterdir()): |
| 119 | + if not group_dir.is_dir(): |
| 120 | + continue |
| 121 | + if not has_webhook_markers(group_dir): |
| 122 | + continue |
| 123 | + |
| 124 | + group = group_dir.name |
| 125 | + groups.append(group) |
| 126 | + group_tmp = tmp_path / group |
| 127 | + group_tmp.mkdir() |
| 128 | + group_tmp_dirs[group] = group_tmp |
| 129 | + |
| 130 | + # Phase 2: run controller-gen concurrently for all groups |
| 131 | + print(f" Running controller-gen for {len(groups)} groups concurrently...") |
| 132 | + with ThreadPoolExecutor() as executor: |
| 133 | + futures = { |
| 134 | + executor.submit(run_controller_gen, api_dir, group, group_tmp_dirs[group]): group |
| 135 | + for group in groups |
| 136 | + } |
| 137 | + for future in as_completed(futures): |
| 138 | + group = futures[future] |
| 139 | + future.result() # raises on failure |
| 140 | + print(f" Completed: {group}") |
| 141 | + |
| 142 | + # Phase 3: split and rename manifests |
| 143 | + for group in groups: |
| 144 | + manifests = tmp_path / group / "manifests.yaml" |
| 145 | + if not manifests.exists(): |
| 146 | + print(f" WARNING: no manifests.yaml for {group}, skipping") |
| 147 | + continue |
| 148 | + all_resources.extend(split_manifests(manifests, group, bases_dir)) |
| 149 | + |
| 150 | + # Phase 4: write kustomization.yaml |
| 151 | + print(f"\nWriting kustomization.yaml with {len(all_resources)} resources...") |
| 152 | + write_kustomization(output_dir, all_resources) |
| 153 | + print(f"Done. Generated {len(all_resources)} webhook configuration files.") |
| 154 | + |
| 155 | + |
| 156 | +if __name__ == "__main__": |
| 157 | + main() |
0 commit comments