Skip to content

Commit

Permalink
added comments and changed table render location
Browse files Browse the repository at this point in the history
  • Loading branch information
Devaansh-Kumar committed Jun 28, 2024
1 parent 2333bec commit 614b901
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
12 changes: 9 additions & 3 deletions cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"slices"
"strings"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
"github.com/samber/lo"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -85,19 +86,24 @@ func (pr *PrintRunner) PrintGatewayAPIObjects(cmd *cobra.Command, _ []string) er
return fmt.Errorf("failed to initialize namespace filter: %w", err)
}

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

pr.outputResult(gatewayResources)
pr.outputResult(gatewayResources, tables)

return nil
}

func (pr *PrintRunner) outputResult(gatewayResources []i2gw.GatewayResources) {
func (pr *PrintRunner) outputResult(gatewayResources []i2gw.GatewayResources, tables []table.Writer) {
resourceCount := 0

for _, t := range tables {
t.SetOutputMirror(os.Stdout)
t.Render()
}

for _, r := range gatewayResources {
resourceCount += len(r.GatewayClasses)
for _, gatewayClass := range r.GatewayClasses {
Expand Down
19 changes: 10 additions & 9 deletions pkg/i2gw/ingress2gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"maps"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/notifications"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/validation/field"
Expand All @@ -31,18 +32,18 @@ import (
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
)

func ToGatewayAPIResources(ctx context.Context, namespace string, inputFile string, providers []string, providerSpecificFlags map[string]map[string]string) ([]GatewayResources, error) {
func ToGatewayAPIResources(ctx context.Context, namespace string, inputFile string, providers []string, providerSpecificFlags map[string]map[string]string) ([]GatewayResources, error, []table.Writer) {
var clusterClient client.Client

if inputFile == "" {
conf, err := config.GetConfig()
if err != nil {
return nil, fmt.Errorf("failed to get client config: %w", err)
return nil, fmt.Errorf("failed to get client config: %w", err), nil
}

cl, err := client.New(conf, client.Options{})
if err != nil {
return nil, fmt.Errorf("failed to create client: %w", err)
return nil, fmt.Errorf("failed to create client: %w", err), nil
}
clusterClient = client.NewNamespacedClient(cl, namespace)
}
Expand All @@ -53,16 +54,16 @@ func ToGatewayAPIResources(ctx context.Context, namespace string, inputFile stri
ProviderSpecificFlags: providerSpecificFlags,
}, providers)
if err != nil {
return nil, err
return nil, err, nil
}

if inputFile != "" {
if err = readProviderResourcesFromFile(ctx, providerByName, inputFile); err != nil {
return nil, err
return nil, err, nil
}
} else {
if err = readProviderResourcesFromCluster(ctx, providerByName); err != nil {
return nil, err
return nil, err, nil
}
}

Expand All @@ -75,12 +76,12 @@ func ToGatewayAPIResources(ctx context.Context, namespace string, inputFile stri
errs = append(errs, conversionErrs...)
gatewayResources = append(gatewayResources, providerGatewayResources)
}
notifications.NotificationAggr.CreateNotificationTables()
tables := notifications.NotificationAggr.CreateNotificationTables()
if len(errs) > 0 {
return nil, aggregatedErrs(errs)
return nil, aggregatedErrs(errs), tables
}

return gatewayResources, nil
return gatewayResources, nil, tables
}

func readProviderResourcesFromFile(ctx context.Context, providerByName map[ProviderName]Provider, inputFile string) error {
Expand Down
4 changes: 1 addition & 3 deletions pkg/i2gw/notifications/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
package notifications

import (
"os"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
)
Expand All @@ -43,7 +41,7 @@ var (
func newTableConfig() table.Writer {
t := table.NewWriter()

t.SetOutputMirror(os.Stdout)
// t.SetOutputMirror(os.Stdout)
t.SetRowPainter(func(row table.Row) text.Colors {
switch notificationType := row[0]; notificationType {
case InfoNotification:
Expand Down
10 changes: 7 additions & 3 deletions pkg/i2gw/notifications/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ func (na *NotificationAggregator) DispatchNotification(notification Notification
na.mutex.Unlock()
}

// CreateNotificationTables takes all generated notifications and displays it in a tabular format based on provider
func (na *NotificationAggregator) CreateNotificationTables() {
// CreateNotificationTables takes all generated notifications and returns an array of
// table.Writers that displays the notifications in a tabular format based on provider
func (na *NotificationAggregator) CreateNotificationTables() []table.Writer {
tables := make([]table.Writer, 0)
for provider, msgs := range na.Notifications {
t := newTableConfig()

Expand All @@ -69,8 +71,10 @@ func (na *NotificationAggregator) CreateNotificationTables() {
t.AppendRow(table.Row{n.Type, n.Message, convertObjectsToStr(n.CallingObjects)})
}

t.Render()
tables = append(tables, t)
}

return tables
}

func convertObjectsToStr(ob []client.Object) string {
Expand Down
2 changes: 2 additions & 0 deletions pkg/i2gw/providers/istio/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ func (c *converter) convert(storage *storage) (i2gw.GatewayResources, field.Erro
Name: vs.Name,
}.String())

// We add Virtual Service to the context in order to reference the calling object during notifications
// generated from functions that do not have access to this object.
c.ctx = context.WithValue(c.ctx, virtualServiceKey, vs)

parentRefs, referenceGrants := c.generateReferences(vs, vsFieldPath)
Expand Down

0 comments on commit 614b901

Please sign in to comment.