forked from kubernetes-sigs/ingress2gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint.go
More file actions
479 lines (407 loc) · 16.8 KB
/
print.go
File metadata and controls
479 lines (407 loc) · 16.8 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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"fmt"
"io"
"os"
"path/filepath"
"slices"
"strings"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/notifications"
"github.com/samber/lo"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/client-go/tools/clientcmd"
// Call init function for the providers
_ "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/apisix"
_ "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/cilium"
_ "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/gce"
_ "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/ingressnginx"
_ "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/istio"
_ "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/kong"
_ "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/nginx"
_ "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/openapi3"
// Call init for emitters
_ "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/emitters/agentgateway"
_ "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/emitters/envoygateway"
_ "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/emitters/gce"
_ "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/emitters/kgateway"
_ "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/emitters/standard"
)
type PrintRunner struct {
// outputFormat contains currently set output format. Value assigned via --output/-o flag.
// Defaults to YAML.
outputFormat string
// inputFile contains the paths to YAML manifest files to process. Value assigned via --input-file flag.
inputFile []string
// The namespace used to query Gateway API objects. Value assigned via
// --namespace/-n flag.
// On absence, the current user active namespace is used.
namespace string
// allNamespaces indicates whether all namespaces should be used. Value assigned via
// --all-namespaces/-A flag.
allNamespaces bool
// resourcePrinter determines how resource objects are printed out
resourcePrinter printers.ResourcePrinter
// Only resources that matches this filter will be processed.
namespaceFilter string
// providers indicates which providers are used to execute convert action.
providers []string
// Provider specific flags --<provider>-<flag>.
providerSpecificFlags map[string]*string
// emitter indicates which emitter is used to generate the Gateway API resources.
// Defaults to "standard".
emitter string
// allowExperimentalGatewayAPI indicates whether Experimental Gateway API features (like URLRewrite) should be included in the output.
allowExperimentalGatewayAPI bool
}
// PrintGatewayAPIObjects performs necessary steps to digest and print
// converted Gateway API objects. The steps include reading from the source,
// construct ingresses and provider-specific resources, convert them, then print
// the Gateway API objects out.
func (pr *PrintRunner) PrintGatewayAPIObjects(cmd *cobra.Command, _ []string) error {
err := pr.initializeResourcePrinter()
if err != nil {
return fmt.Errorf("failed to initialize resource printer: %w", err)
}
err = pr.initializeNamespaceFilter()
if err != nil {
return fmt.Errorf("failed to initialize namespace filter: %w", err)
}
allFiles := []string{}
for _, path := range pr.inputFile {
// Check if path is a directory
info, statErr := os.Stat(path)
if statErr != nil {
return fmt.Errorf("input path does not exist: %s", path)
}
if info.IsDir() {
return fmt.Errorf("provided input path %s is a directory", path)
}
allFiles = append(allFiles, path)
}
var gatewayResources []i2gw.GatewayResources
var report *notifications.Report
var inputReader io.Reader
if len(allFiles) > 0 {
var readers []io.Reader
for i, file := range allFiles {
cleanPath := filepath.Clean(file)
f, openErr := os.Open(cleanPath)
if openErr != nil {
return fmt.Errorf("error reading file %s: %w", file, openErr)
}
readers = append(readers, f)
if i < len(allFiles)-1 {
readers = append(readers, strings.NewReader("\n---\n"))
}
defer func(f *os.File, path string) {
if closeErr := f.Close(); closeErr != nil {
fmt.Fprintf(os.Stderr, "warning: failed to close file %s: %v\n", path, closeErr)
}
}(f, cleanPath)
}
if len(readers) == 1 {
inputReader = readers[0]
} else {
inputReader = io.MultiReader(readers...)
}
}
gatewayResources, report, err = i2gw.ToGatewayAPIResources(cmd.Context(), pr.namespaceFilter, inputReader, pr.providers, pr.emitter, pr.getProviderSpecificFlags(), pr.allowExperimentalGatewayAPI, noColor)
if err != nil {
return err
}
fmt.Fprint(os.Stderr, report.Render())
pr.outputResult(gatewayResources)
return nil
}
func (pr *PrintRunner) outputResult(gatewayResources []i2gw.GatewayResources) {
resourceCount := 0
for _, r := range gatewayResources {
resourceCount += len(r.GatewayClasses)
for _, gatewayClass := range r.GatewayClasses {
gatewayClass := gatewayClass
err := pr.resourcePrinter.PrintObj(&gatewayClass, os.Stdout)
if err != nil {
fmt.Printf("# Error printing %s GatewayClass: %v\n", gatewayClass.Name, err)
}
}
}
for _, r := range gatewayResources {
resourceCount += len(r.Gateways)
for _, gateway := range r.Gateways {
gateway := gateway
if gateway.Annotations == nil {
gateway.Annotations = make(map[string]string)
}
gateway.Annotations[i2gw.GeneratorAnnotationKey] = fmt.Sprintf("ingress2gateway-%s", i2gw.Version)
err := pr.resourcePrinter.PrintObj(&gateway, os.Stdout)
if err != nil {
fmt.Printf("# Error printing %s Gateway: %v\n", gateway.Name, err)
}
}
}
for _, r := range gatewayResources {
resourceCount += len(r.HTTPRoutes)
for _, httpRoute := range r.HTTPRoutes {
httpRoute := httpRoute
if httpRoute.Annotations == nil {
httpRoute.Annotations = make(map[string]string)
}
httpRoute.Annotations[i2gw.GeneratorAnnotationKey] = fmt.Sprintf("ingress2gateway-%s", i2gw.Version)
err := pr.resourcePrinter.PrintObj(&httpRoute, os.Stdout)
if err != nil {
fmt.Printf("# Error printing %s HTTPRoute: %v\n", httpRoute.Name, err)
}
}
}
for _, r := range gatewayResources {
resourceCount += len(r.GRPCRoutes)
for _, grpcRoute := range r.GRPCRoutes {
grpcRoute := grpcRoute
if grpcRoute.Annotations == nil {
grpcRoute.Annotations = make(map[string]string)
}
grpcRoute.Annotations[i2gw.GeneratorAnnotationKey] = fmt.Sprintf("ingress2gateway-%s", i2gw.Version)
err := pr.resourcePrinter.PrintObj(&grpcRoute, os.Stdout)
if err != nil {
fmt.Printf("# Error printing %s GRPCRoute: %v\n", grpcRoute.Name, err)
}
}
}
for _, r := range gatewayResources {
resourceCount += len(r.TLSRoutes)
for _, tlsRoute := range r.TLSRoutes {
tlsRoute := tlsRoute
if tlsRoute.Annotations == nil {
tlsRoute.Annotations = make(map[string]string)
}
tlsRoute.Annotations[i2gw.GeneratorAnnotationKey] = fmt.Sprintf("ingress2gateway-%s", i2gw.Version)
err := pr.resourcePrinter.PrintObj(&tlsRoute, os.Stdout)
if err != nil {
fmt.Printf("# Error printing %s TLSRoute: %v\n", tlsRoute.Name, err)
}
}
}
for _, r := range gatewayResources {
resourceCount += len(r.TCPRoutes)
for _, tcpRoute := range r.TCPRoutes {
tcpRoute := tcpRoute
if tcpRoute.Annotations == nil {
tcpRoute.Annotations = make(map[string]string)
}
tcpRoute.Annotations[i2gw.GeneratorAnnotationKey] = fmt.Sprintf("ingress2gateway-%s", i2gw.Version)
err := pr.resourcePrinter.PrintObj(&tcpRoute, os.Stdout)
if err != nil {
fmt.Printf("# Error printing %s TCPRoute: %v\n", tcpRoute.Name, err)
}
}
}
for _, r := range gatewayResources {
resourceCount += len(r.UDPRoutes)
for _, udpRoute := range r.UDPRoutes {
udpRoute := udpRoute
if udpRoute.Annotations == nil {
udpRoute.Annotations = make(map[string]string)
}
udpRoute.Annotations[i2gw.GeneratorAnnotationKey] = fmt.Sprintf("ingress2gateway-%s", i2gw.Version)
err := pr.resourcePrinter.PrintObj(&udpRoute, os.Stdout)
if err != nil {
fmt.Printf("# Error printing %s UDPRoute: %v\n", udpRoute.Name, err)
}
}
}
for _, r := range gatewayResources {
resourceCount += len(r.BackendTLSPolicies)
for _, backendTLSPolicy := range r.BackendTLSPolicies {
backendTLSPolicy := backendTLSPolicy
if backendTLSPolicy.Annotations == nil {
backendTLSPolicy.Annotations = make(map[string]string)
}
backendTLSPolicy.Annotations[i2gw.GeneratorAnnotationKey] = fmt.Sprintf("ingress2gateway-%s", i2gw.Version)
err := pr.resourcePrinter.PrintObj(&backendTLSPolicy, os.Stdout)
if err != nil {
fmt.Printf("# Error printing %s BackendTLSPolicy: %v\n", backendTLSPolicy.Name, err)
}
}
}
for _, r := range gatewayResources {
resourceCount += len(r.ReferenceGrants)
for _, referenceGrant := range r.ReferenceGrants {
referenceGrant := referenceGrant
if referenceGrant.Annotations == nil {
referenceGrant.Annotations = make(map[string]string)
}
referenceGrant.Annotations[i2gw.GeneratorAnnotationKey] = fmt.Sprintf("ingress2gateway-%s", i2gw.Version)
err := pr.resourcePrinter.PrintObj(&referenceGrant, os.Stdout)
if err != nil {
fmt.Printf("# Error printing %s ReferenceGrant: %v\n", referenceGrant.Name, err)
}
}
}
for _, r := range gatewayResources {
resourceCount += len(r.GatewayExtensions)
for _, gatewayExtension := range r.GatewayExtensions {
gatewayExtension := gatewayExtension
fmt.Println("---")
if err := PrintUnstructuredAsYaml(&gatewayExtension); err != nil {
fmt.Printf("# Error printing %s gatewayExtension: %v\n", gatewayExtension.GetName(), err)
}
}
}
if resourceCount == 0 {
msg := "No resources found"
if pr.namespaceFilter != "" {
msg = fmt.Sprintf("%s in %s namespace", msg, pr.namespaceFilter)
}
fmt.Println(msg)
}
}
// initializeResourcePrinter assign a specific type of printers.ResourcePrinter
// based on the outputFormat of the printRunner struct.
func (pr *PrintRunner) initializeResourcePrinter() error {
switch pr.outputFormat {
case "yaml", "":
pr.resourcePrinter = &printers.YAMLPrinter{}
return nil
case "kyaml":
pr.resourcePrinter = &printers.KYAMLPrinter{}
return nil
case "json":
pr.resourcePrinter = &printers.JSONPrinter{}
return nil
default:
return fmt.Errorf("%s is not a supported output format", pr.outputFormat)
}
}
// initializeNamespaceFilter initializes the correct namespace filter for resource processing with these scenarios:
// 1. If the --all-namespaces flag is used, it processes all resources, regardless of whether they are from the cluster or file.
// 2. If an input-file is used and no namespace is added then all namespaces are selected.
// 3. If namespace is specified, it filters resources based on that namespace.
// 4. If no namespace is specified and reading from the cluster, it attempts to get the namespace from the cluster; if unsuccessful, initialization fails.
func (pr *PrintRunner) initializeNamespaceFilter() error {
hasFileInput := len(pr.inputFile) > 0
// When we should use all namespaces, empty string is used as the filter.
if pr.allNamespaces || (hasFileInput && pr.namespace == "") {
pr.namespaceFilter = ""
return nil
}
// If namespace flag is not specified, try to use the default namespace from the cluster
if pr.namespace == "" {
ns, err := getNamespaceInCurrentContext()
if err != nil && !hasFileInput {
// When asked to read from the cluster, but getting the current namespace
// failed for whatever reason - do not process the request.
return err
}
// If err is nil we got the right filtered namespace.
// If the input file is specified, and we failed to get the namespace, use all namespaces.
pr.namespaceFilter = ns
return nil
}
pr.namespaceFilter = pr.namespace
return nil
}
func newPrintCommand() *cobra.Command {
pr := &PrintRunner{}
// printCmd represents the print command. It prints Gateway API resources
// generated from Ingress resources.
var cmd = &cobra.Command{
Use: "print",
Short: "Prints Gateway API objects generated from ingress and provider-specific resources.",
RunE: pr.PrintGatewayAPIObjects,
PreRunE: func(cmd *cobra.Command, _ []string) error {
openAPIExist := slices.Contains(pr.providers, "openapi3")
if openAPIExist && len(pr.providers) != 1 {
return fmt.Errorf("openapi3 must be the only provider when specified")
}
// Auto-set emitter for GCE provider
gceProviderUsed := slices.Contains(pr.providers, "gce")
emitterFlagChanged := cmd.Flags().Changed("emitter")
if gceProviderUsed {
if emitterFlagChanged && pr.emitter != "gce" {
return fmt.Errorf("when using the gce provider, the emitter must be 'gce' (got '%s')", pr.emitter)
}
pr.emitter = "gce"
}
return nil
},
}
cmd.Flags().StringVarP(&pr.outputFormat, "output", "o", "yaml",
"Output format. One of: (yaml, json, kyaml).")
cmd.Flags().StringSliceVar(&pr.inputFile, "input-file", []string{},
`Path to manifest files. When set, the tool will read ingresses from the files instead of reading from the cluster. Supported files are yaml and json.`)
cmd.Flags().StringVarP(&pr.namespace, "namespace", "n", "",
`If present, the namespace scope for this CLI request.`)
cmd.Flags().BoolVarP(&pr.allNamespaces, "all-namespaces", "A", false,
`If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even
if specified with --namespace.`)
cmd.Flags().StringVar(&pr.emitter, "emitter", "standard",
fmt.Sprintf("If present, the tool will try to use the specified emitter to generate the Gateway API resources, supported values are %v. The `standard` emitter will only output Gateway API", i2gw.GetSupportedEmitters()))
cmd.Flags().StringSliceVar(&pr.providers, "providers", []string{},
fmt.Sprintf("If present, the tool will try to convert only resources related to the specified providers, supported values are %v.", i2gw.GetSupportedProviders()))
cmd.Flags().BoolVar(&pr.allowExperimentalGatewayAPI, "allow-experimental-gw-api", false, "If present, the tool will include Experimental Gateway API fields (e.g. URLRewrite) in the output. Default is false.")
pr.providerSpecificFlags = make(map[string]*string)
for provider, flags := range i2gw.GetProviderSpecificFlagDefinitions() {
for _, flag := range flags {
flagName := fmt.Sprintf("%s-%s", provider, flag.Name)
pr.providerSpecificFlags[flagName] = cmd.Flags().String(flagName, flag.DefaultValue, fmt.Sprintf("Provider-specific: %s. %s", provider, flag.Description))
}
}
_ = cmd.MarkFlagRequired("providers")
cmd.MarkFlagsMutuallyExclusive("namespace", "all-namespaces")
return cmd
}
// getNamespaceInCurrentContext returns the namespace in the current active context of the user.
func getNamespaceInCurrentContext() (string, error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{})
currentNamespace, _, err := kubeConfig.Namespace()
return currentNamespace, err
}
// getProviderSpecificFlags returns the provider specific flags input by the user.
// The flags are returned in a map where the key is the provider name and the value is a map of flag name to flag value.
func (pr *PrintRunner) getProviderSpecificFlags() map[string]map[string]string {
providerSpecificFlags := make(map[string]map[string]string)
for flagName, value := range pr.providerSpecificFlags {
provider, found := lo.Find(pr.providers, func(p string) bool { return strings.HasPrefix(flagName, fmt.Sprintf("%s-", p)) })
if !found {
continue
}
flagNameWithoutProvider := strings.TrimPrefix(flagName, fmt.Sprintf("%s-", provider))
if providerSpecificFlags[provider] == nil {
providerSpecificFlags[provider] = make(map[string]string)
}
providerSpecificFlags[provider][flagNameWithoutProvider] = *value
}
return providerSpecificFlags
}
func PrintUnstructuredAsYaml(obj *unstructured.Unstructured) error {
// Create a YAML serializer
serializer := json.NewSerializerWithOptions(json.DefaultMetaFactory, nil, nil,
json.SerializerOptions{
Yaml: true,
Pretty: true, // Optional: for better readability
Strict: true,
})
// Encode the unstructured object to YAML
err := serializer.Encode(obj, os.Stdout)
if err != nil {
return err
}
return nil
}