|
1 | 1 | import os
|
| 2 | +import subprocess |
2 | 3 | from pathlib import Path
|
3 | 4 |
|
4 | 5 | import click
|
@@ -33,3 +34,132 @@ def init():
|
33 | 34 | f"[green]Copied network and namespace example files to {Path(current_dir) / NETWORK_DIR.name}[/green]"
|
34 | 35 | )
|
35 | 36 | richprint(f"[green]Created warnet project structure in {current_dir}[/green]")
|
| 37 | + |
| 38 | + |
| 39 | +# Get kubectl values |
| 40 | +def get_kubectl_value(jsonpath): |
| 41 | + return subprocess.check_output( |
| 42 | + ["kubectl", "config", "view", "--minify", "-o", f"jsonpath={jsonpath}"] |
| 43 | + ).decode("utf-8") |
| 44 | + |
| 45 | + |
| 46 | +# Get all namespaces that start with "warnet-" |
| 47 | +def get_warnet_namespaces(): |
| 48 | + namespaces = ( |
| 49 | + subprocess.check_output( |
| 50 | + ["kubectl", "get", "namespaces", "-o", "jsonpath={.items[*].metadata.name}"] |
| 51 | + ) |
| 52 | + .decode("utf-8") |
| 53 | + .split() |
| 54 | + ) |
| 55 | + return [ns for ns in namespaces if ns.startswith("warnet-")] |
| 56 | + |
| 57 | + |
| 58 | +# Get all service accounts for a given namespace |
| 59 | +def get_service_accounts(namespace): |
| 60 | + return ( |
| 61 | + subprocess.check_output( |
| 62 | + [ |
| 63 | + "kubectl", |
| 64 | + "get", |
| 65 | + "serviceaccounts", |
| 66 | + "-n", |
| 67 | + namespace, |
| 68 | + "-o", |
| 69 | + "jsonpath={.items[*].metadata.name}", |
| 70 | + ] |
| 71 | + ) |
| 72 | + .decode("utf-8") |
| 73 | + .split() |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +@admin.command() |
| 78 | +@click.option( |
| 79 | + "--kubeconfig-dir", |
| 80 | + default="kubeconfigs", |
| 81 | + help="Directory to store kubeconfig files (default: kubeconfigs)", |
| 82 | +) |
| 83 | +@click.option( |
| 84 | + "--token-duration", |
| 85 | + default=172800, |
| 86 | + type=int, |
| 87 | + help="Duration of the token in seconds (default: 48 hours)", |
| 88 | +) |
| 89 | +def create_kubeconfigs(kubeconfig_dir, token_duration): |
| 90 | + """Create kubeconfig files for all ServiceAccounts in namespaces starting with 'warnet-'.""" |
| 91 | + kubeconfig_dir = os.path.expanduser(kubeconfig_dir) |
| 92 | + |
| 93 | + cluster_name = get_kubectl_value("{.clusters[0].name}") |
| 94 | + cluster_server = get_kubectl_value("{.clusters[0].cluster.server}") |
| 95 | + cluster_ca = get_kubectl_value("{.clusters[0].cluster.certificate-authority-data}") |
| 96 | + |
| 97 | + os.makedirs(kubeconfig_dir, exist_ok=True) |
| 98 | + |
| 99 | + # Get all namespaces that start with "warnet-" |
| 100 | + warnet_namespaces = get_warnet_namespaces() |
| 101 | + |
| 102 | + for namespace in warnet_namespaces: |
| 103 | + click.echo(f"Processing namespace: {namespace}") |
| 104 | + service_accounts = get_service_accounts(namespace) |
| 105 | + |
| 106 | + for sa in service_accounts: |
| 107 | + click.echo(f"Processing ServiceAccount: {sa}") |
| 108 | + |
| 109 | + # Create a token for the ServiceAccount with specified duration |
| 110 | + try: |
| 111 | + token = ( |
| 112 | + subprocess.check_output( |
| 113 | + [ |
| 114 | + "kubectl", |
| 115 | + "create", |
| 116 | + "token", |
| 117 | + sa, |
| 118 | + "-n", |
| 119 | + namespace, |
| 120 | + f"--duration={token_duration}s", |
| 121 | + ] |
| 122 | + ) |
| 123 | + .decode("utf-8") |
| 124 | + .strip() |
| 125 | + ) |
| 126 | + except subprocess.CalledProcessError: |
| 127 | + click.echo(f"Failed to create token for ServiceAccount {sa}. Skipping...") |
| 128 | + continue |
| 129 | + |
| 130 | + # Create a kubeconfig file for the user |
| 131 | + kubeconfig_file = os.path.join(kubeconfig_dir, f"{sa}-{namespace}-kubeconfig") |
| 132 | + |
| 133 | + kubeconfig_content = f"""apiVersion: v1 |
| 134 | +kind: Config |
| 135 | +clusters: |
| 136 | +- name: {cluster_name} |
| 137 | + cluster: |
| 138 | + server: {cluster_server} |
| 139 | + certificate-authority-data: {cluster_ca} |
| 140 | +users: |
| 141 | +- name: {sa} |
| 142 | + user: |
| 143 | + token: {token} |
| 144 | +contexts: |
| 145 | +- name: {sa}-{namespace} |
| 146 | + context: |
| 147 | + cluster: {cluster_name} |
| 148 | + namespace: {namespace} |
| 149 | + user: {sa} |
| 150 | +current-context: {sa}-{namespace} |
| 151 | +""" |
| 152 | + with open(kubeconfig_file, "w") as f: |
| 153 | + f.write(kubeconfig_content) |
| 154 | + |
| 155 | + click.echo(f"Created kubeconfig file for {sa}: {kubeconfig_file}") |
| 156 | + click.echo(f"Token duration: {token_duration} seconds") |
| 157 | + click.echo(f"To use this config, run: kubectl --kubeconfig={kubeconfig_file} get pods") |
| 158 | + click.echo("---") |
| 159 | + |
| 160 | + click.echo(f"All kubeconfig files have been created in the '{kubeconfig_dir}' directory.") |
| 161 | + click.echo("Distribute these files to the respective users.") |
| 162 | + click.echo( |
| 163 | + "Users can then use them with kubectl by specifying the --kubeconfig flag or by setting the KUBECONFIG environment variable." |
| 164 | + ) |
| 165 | + click.echo(f"Note: The tokens will expire after {token_duration} seconds.") |
0 commit comments