Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print generated GatewayClasses, TLSRoutes, TCPRoutes and ReferenceGrants. #110

Merged
merged 1 commit into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 81 additions & 18 deletions cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/client-go/tools/clientcmd"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"

// Call init function for the providers
_ "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/ingressnginx"
Expand Down Expand Up @@ -73,39 +72,103 @@ func (pr *PrintRunner) PrintGatewaysAndHTTPRoutes(cmd *cobra.Command, _ []string
return fmt.Errorf("failed to initialize namespace filter: %w", err)
}

httpRoutes, gateways, err := i2gw.ToGatewayAPIResources(cmd.Context(), pr.namespaceFilter, pr.inputFile, pr.providers)
gatewayResources, err := i2gw.ToGatewayAPIResources(cmd.Context(), pr.namespaceFilter, pr.inputFile, pr.providers)
if err != nil {
return err
}

pr.outputResult(httpRoutes, gateways)
pr.outputResult(gatewayResources)

return nil
}

func (pr *PrintRunner) outputResult(httpRoutes []gatewayv1.HTTPRoute, gateways []gatewayv1.Gateway) {
if len(httpRoutes)+len(gateways) == 0 {
msg := "No resources found"
if pr.namespaceFilter != "" {
msg = fmt.Sprintf("%s in %s namespace", msg, pr.namespaceFilter)
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)
}
}
}
Comment on lines +88 to +97
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a nitpick, but is there any reason you repeated the outer for loop instead of having it once?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept the original logic: print all gateways from all providers, then all HTTPRoutes etc.

If it's in one loop, then the logic would change and we'd print all resources per provider.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100%


for _, r := range gatewayResources {
resourceCount += len(r.Gateways)
for _, gateway := range r.Gateways {
gateway := gateway
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
err := pr.resourcePrinter.PrintObj(&httpRoute, os.Stdout)
if err != nil {
fmt.Printf("# Error printing %s HTTPRoute: %v\n", httpRoute.Name, err)
}
}
fmt.Println(msg)
return
}

for i := range gateways {
err := pr.resourcePrinter.PrintObj(&gateways[i], os.Stdout)
if err != nil {
fmt.Printf("# Error printing %s Gateway: %v\n", gateways[i].Name, err)
for _, r := range gatewayResources {
resourceCount += len(r.TLSRoutes)
for _, tlsRoute := range r.TLSRoutes {
tlsRoute := tlsRoute
err := pr.resourcePrinter.PrintObj(&tlsRoute, os.Stdout)
if err != nil {
fmt.Printf("# Error printing %s TLSRoute: %v\n", tlsRoute.Name, err)
}
}
}

for i := range httpRoutes {
err := pr.resourcePrinter.PrintObj(&httpRoutes[i], os.Stdout)
if err != nil {
fmt.Printf("# Error printing %s HTTPRoute: %v\n", httpRoutes[i].Name, err)
for _, r := range gatewayResources {
resourceCount += len(r.TCPRoutes)
for _, tcpRoute := range r.TCPRoutes {
tcpRoute := tcpRoute
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
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.ReferenceGrants)
for _, referenceGrant := range r.ReferenceGrants {
referenceGrant := referenceGrant
err := pr.resourcePrinter.PrintObj(&referenceGrant, os.Stdout)
if err != nil {
fmt.Printf("# Error printing %s ReferenceGrant: %v\n", referenceGrant.Name, 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
Expand Down
37 changes: 16 additions & 21 deletions pkg/i2gw/ingress2gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,68 +31,63 @@ import (
kubeyaml "k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
)

func ToGatewayAPIResources(ctx context.Context, namespace string, inputFile string, providers []string) ([]gatewayv1.HTTPRoute, []gatewayv1.Gateway, error) {
func ToGatewayAPIResources(ctx context.Context, namespace string, inputFile string, providers []string) ([]GatewayResources, error) {
conf, err := config.GetConfig()
if err != nil {
return nil, nil, fmt.Errorf("failed to get client config: %w", err)
return nil, fmt.Errorf("failed to get client config: %w", err)
}

cl, err := client.New(conf, client.Options{})
if err != nil {
return nil, nil, fmt.Errorf("failed to create client: %w", err)
return nil, fmt.Errorf("failed to create client: %w", err)
}
cl = client.NewNamespacedClient(cl, namespace)

var ingresses networkingv1.IngressList
var gateways []gatewayv1.Gateway
var httpRoutes []gatewayv1.HTTPRoute

providerByName, err := constructProviders(&ProviderConf{
Client: cl,
}, providers)
if err != nil {
return nil, nil, err
return nil, err
}

resources := InputResources{}

if inputFile != "" {
if err = ConstructIngressesFromFile(&ingresses, inputFile, namespace); err != nil {
return nil, nil, fmt.Errorf("failed to read ingresses from file: %w", err)
return nil, fmt.Errorf("failed to read ingresses from file: %w", err)
}
resources.Ingresses = ingresses.Items
if err = readProviderResourcesFromFile(ctx, providerByName, inputFile); err != nil {
return nil, nil, err
return nil, err
}
} else {
if err = ConstructIngressesFromCluster(ctx, cl, &ingresses); err != nil {
return nil, nil, fmt.Errorf("failed to read ingresses from cluster: %w", err)
return nil, fmt.Errorf("failed to read ingresses from cluster: %w", err)
}
resources.Ingresses = ingresses.Items
if err = readProviderResourcesFromCluster(ctx, providerByName); err != nil {
return nil, nil, err
return nil, err
}
}

var errs field.ErrorList
var (
gatewayResources []GatewayResources
errs field.ErrorList
)
for _, provider := range providerByName {
gatewayResources, conversionErrs := provider.ToGatewayAPI(resources)
providerGatewayResources, conversionErrs := provider.ToGatewayAPI(resources)
errs = append(errs, conversionErrs...)
for _, gateway := range gatewayResources.Gateways {
gateways = append(gateways, gateway)
}
for _, route := range gatewayResources.HTTPRoutes {
httpRoutes = append(httpRoutes, route)
}
gatewayResources = append(gatewayResources, providerGatewayResources)
}
if len(errs) > 0 {
return nil, nil, aggregatedErrs(errs)
return nil, aggregatedErrs(errs)
}

return httpRoutes, gateways, nil
return gatewayResources, nil
}

func readProviderResourcesFromFile(ctx context.Context, providerByName map[ProviderName]Provider, inputFile string) error {
Expand Down