-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathkubectl-ingress2gateway
More file actions
311 lines (243 loc) · 9.22 KB
/
Copy pathkubectl-ingress2gateway
File metadata and controls
311 lines (243 loc) · 9.22 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
#!/usr/bin/env python3
"""kubectl plugin for ingress2gateway.
Install by copying to a directory in your PATH and making it executable:
cp kubectl-ingress2gateway /usr/local/bin/
chmod +x /usr/local/bin/kubectl-ingress2gateway
Usage:
kubectl ingress2gateway convert <ingress-name> [-n namespace]
kubectl ingress2gateway convert -f ingress.yaml
kubectl ingress2gateway list [-n namespace]
kubectl ingress2gateway apply <ingress-name> [-n namespace]
"""
import argparse
import json
import subprocess
import sys
from typing import Any
try:
from ingress2gateway import (
convert_ingress_to_gateway,
resources_to_yaml,
apply_provider_defaults,
)
from ingress2gateway.reference_grant import generate_reference_grants
except ImportError:
print("Error: ingress2gateway package not installed.", file=sys.stderr)
print("Install with: pip install ingress2gateway", file=sys.stderr)
sys.exit(1)
def run_kubectl(args: list[str], capture: bool = True) -> subprocess.CompletedProcess:
"""Run kubectl command."""
cmd = ["kubectl"] + args
if capture:
return subprocess.run(cmd, capture_output=True, text=True)
return subprocess.run(cmd)
def get_ingress(name: str, namespace: str) -> dict[str, Any] | None:
"""Get Ingress resource from cluster."""
result = run_kubectl(["get", "ingress", name, "-n", namespace, "-o", "json"])
if result.returncode != 0:
print(f"Error getting Ingress: {result.stderr}", file=sys.stderr)
return None
return json.loads(result.stdout)
def list_ingresses(namespace: str | None) -> list[dict[str, Any]]:
"""List Ingress resources in cluster."""
args = ["get", "ingress", "-o", "json"]
if namespace:
args.extend(["-n", namespace])
else:
args.append("-A")
result = run_kubectl(args)
if result.returncode != 0:
print(f"Error listing Ingresses: {result.stderr}", file=sys.stderr)
return []
data = json.loads(result.stdout)
return data.get("items", [])
def convert_command(args: argparse.Namespace) -> int:
"""Handle convert subcommand."""
ingress = None
if args.filename:
# Read from file
import yaml
with open(args.filename) as f:
ingress = yaml.safe_load(f)
elif args.name:
# Get from cluster
ingress = get_ingress(args.name, args.namespace)
else:
# Read from stdin
import yaml
ingress = yaml.safe_load(sys.stdin)
if not ingress:
print("Error: No Ingress resource provided", file=sys.stderr)
return 1
try:
resources = convert_ingress_to_gateway(ingress)
# Apply provider defaults
if args.provider:
resources["gateway"] = apply_provider_defaults(
resources["gateway"], args.provider
)
# Generate reference grants if needed
if args.reference_grants:
grants = generate_reference_grants(
resources["gateway"], resources["httproutes"]
)
resources["reference_grants"] = grants
# Output
yaml_output = resources_to_yaml(resources)
if args.output:
with open(args.output, "w") as f:
f.write(yaml_output)
print(f"Written to {args.output}")
else:
print(yaml_output)
return 0
except ValueError as e:
print(f"Error converting Ingress: {e}", file=sys.stderr)
return 1
def list_command(args: argparse.Namespace) -> int:
"""Handle list subcommand."""
ingresses = list_ingresses(args.namespace if not args.all_namespaces else None)
if not ingresses:
print("No Ingress resources found")
return 0
# Print table header
print(f"{'NAMESPACE':<20} {'NAME':<30} {'HOSTS':<40} {'CLASS':<15}")
print("-" * 105)
for ing in ingresses:
ns = ing.get("metadata", {}).get("namespace", "default")
name = ing.get("metadata", {}).get("name", "")
hosts = ",".join(
rule.get("host", "*") for rule in ing.get("spec", {}).get("rules", [])
)
ing_class = ing.get("spec", {}).get("ingressClassName", "<none>")
# Truncate long values
if len(hosts) > 38:
hosts = hosts[:35] + "..."
print(f"{ns:<20} {name:<30} {hosts:<40} {ing_class:<15}")
return 0
def apply_command(args: argparse.Namespace) -> int:
"""Handle apply subcommand - convert and apply to cluster."""
ingress = None
if args.filename:
import yaml
with open(args.filename) as f:
ingress = yaml.safe_load(f)
elif args.name:
ingress = get_ingress(args.name, args.namespace)
else:
print("Error: Provide Ingress name or -f filename", file=sys.stderr)
return 1
if not ingress:
return 1
try:
resources = convert_ingress_to_gateway(ingress)
if args.provider:
resources["gateway"] = apply_provider_defaults(
resources["gateway"], args.provider
)
yaml_output = resources_to_yaml(resources)
if args.dry_run:
print("# Dry run - would apply:")
print(yaml_output)
return 0
# Apply via kubectl
result = subprocess.run(
["kubectl", "apply", "-f", "-"],
input=yaml_output,
text=True,
capture_output=True,
)
if result.returncode != 0:
print(f"Error applying resources: {result.stderr}", file=sys.stderr)
return 1
print(result.stdout)
return 0
except ValueError as e:
print(f"Error: {e}", file=sys.stderr)
return 1
def diff_command(args: argparse.Namespace) -> int:
"""Handle diff subcommand - show what would change."""
ingress = None
if args.filename:
import yaml
with open(args.filename) as f:
ingress = yaml.safe_load(f)
elif args.name:
ingress = get_ingress(args.name, args.namespace)
else:
print("Error: Provide Ingress name or -f filename", file=sys.stderr)
return 1
if not ingress:
return 1
try:
resources = convert_ingress_to_gateway(ingress)
if args.provider:
resources["gateway"] = apply_provider_defaults(
resources["gateway"], args.provider
)
yaml_output = resources_to_yaml(resources)
# Use kubectl diff
result = subprocess.run(
["kubectl", "diff", "-f", "-"],
input=yaml_output,
text=True,
)
return result.returncode
except ValueError as e:
print(f"Error: {e}", file=sys.stderr)
return 1
def main() -> int:
"""Main entry point."""
parser = argparse.ArgumentParser(
prog="kubectl-ingress2gateway",
description="Convert Kubernetes Ingress to Gateway API resources",
)
subparsers = parser.add_subparsers(dest="command", help="Commands")
# Convert command
convert_parser = subparsers.add_parser("convert", help="Convert Ingress to Gateway API")
convert_parser.add_argument("name", nargs="?", help="Ingress resource name")
convert_parser.add_argument("-f", "--filename", help="Ingress YAML file")
convert_parser.add_argument(
"-n", "--namespace", default="default", help="Namespace"
)
convert_parser.add_argument("-o", "--output", help="Output file")
convert_parser.add_argument("-p", "--provider", help="Provider preset")
convert_parser.add_argument(
"--reference-grants",
action="store_true",
help="Generate ReferenceGrants for cross-namespace refs",
)
convert_parser.set_defaults(func=convert_command)
# List command
list_parser = subparsers.add_parser("list", help="List Ingress resources")
list_parser.add_argument("-n", "--namespace", help="Namespace")
list_parser.add_argument(
"-A", "--all-namespaces", action="store_true", help="All namespaces"
)
list_parser.set_defaults(func=list_command)
# Apply command
apply_parser = subparsers.add_parser(
"apply", help="Convert and apply Gateway resources"
)
apply_parser.add_argument("name", nargs="?", help="Ingress resource name")
apply_parser.add_argument("-f", "--filename", help="Ingress YAML file")
apply_parser.add_argument("-n", "--namespace", default="default", help="Namespace")
apply_parser.add_argument("-p", "--provider", help="Provider preset")
apply_parser.add_argument(
"--dry-run", action="store_true", help="Print without applying"
)
apply_parser.set_defaults(func=apply_command)
# Diff command
diff_parser = subparsers.add_parser("diff", help="Show diff of converted resources")
diff_parser.add_argument("name", nargs="?", help="Ingress resource name")
diff_parser.add_argument("-f", "--filename", help="Ingress YAML file")
diff_parser.add_argument("-n", "--namespace", default="default", help="Namespace")
diff_parser.add_argument("-p", "--provider", help="Provider preset")
diff_parser.set_defaults(func=diff_command)
args = parser.parse_args()
if not args.command:
parser.print_help()
return 1
return args.func(args)
if __name__ == "__main__":
sys.exit(main())