Skip to content

Commit

Permalink
Start using new Describer for Gateway and Expand output to include ne…
Browse files Browse the repository at this point in the history
…w fields.
  • Loading branch information
gauravkghildiyal committed Apr 20, 2024
1 parent 1e95a6b commit f45d005
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 39 deletions.
84 changes: 49 additions & 35 deletions gwctl/pkg/printer/gateways.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@ package printer
import (
"fmt"
"io"
"os"
"sort"
"strings"
"text/tabwriter"

"sigs.k8s.io/gateway-api/gwctl/pkg/policymanager"
"sigs.k8s.io/gateway-api/gwctl/pkg/resourcediscovery"
"sigs.k8s.io/yaml"

"k8s.io/apimachinery/pkg/util/duration"
"k8s.io/utils/clock"
Expand All @@ -37,16 +34,6 @@ type GatewaysPrinter struct {
Clock clock.Clock
}

type gatewayDescribeView struct {
// Gateway name
Name string `json:",omitempty"`
// Gateway namespace
Namespace string `json:",omitempty"`
GatewayClass string `json:",omitempty"`
DirectlyAttachedPolicies []policymanager.ObjRef `json:",omitempty"`
EffectivePolicies map[policymanager.PolicyCrdID]policymanager.Policy `json:",omitempty"`
}

func (gp *GatewaysPrinter) Print(resourceModel *resourcediscovery.ResourceModel) {
tw := tabwriter.NewWriter(gp.Out, 0, 0, 2, ' ', 0)
row := []string{"NAME", "CLASS", "ADDRESSES", "PORTS", "PROGRAMMED", "AGE"}
Expand Down Expand Up @@ -107,35 +94,62 @@ func (gp *GatewaysPrinter) PrintDescribeView(resourceModel *resourcediscovery.Re
index := 0
for _, gatewayNode := range resourceModel.Gateways {
index++
views := []gatewayDescribeView{
{
Name: gatewayNode.Gateway.GetName(),
Namespace: gatewayNode.Gateway.GetNamespace(),
},
{
GatewayClass: string(gatewayNode.Gateway.Spec.GatewayClassName),
},

metadata := gatewayNode.Gateway.ObjectMeta.DeepCopy()
metadata.Labels = nil
metadata.Annotations = nil
metadata.Name = ""
metadata.Namespace = ""

pairs := []*DescriberKV{
{Key: "Name", Value: gatewayNode.Gateway.GetName()},
{Key: "Namespace", Value: gatewayNode.Gateway.GetNamespace()},
{Key: "Labels", Value: gatewayNode.Gateway.Labels},
{Key: "Annotations", Value: gatewayNode.Gateway.Annotations},
{Key: "APIVersion", Value: gatewayNode.Gateway.APIVersion},
{Key: "Kind", Value: gatewayNode.Gateway.Kind},
{Key: "Metadata", Value: metadata},
{Key: "Spec", Value: &gatewayNode.Gateway.Spec},
{Key: "Status", Value: &gatewayNode.Gateway.Status},
}
if policyRefs := resourcediscovery.ConvertPoliciesMapToPolicyRefs(gatewayNode.Policies); len(policyRefs) != 0 {
views = append(views, gatewayDescribeView{
DirectlyAttachedPolicies: policyRefs,
})

// AttachedRoutes
attachedRoutes := &Table{
ColumnNames: []string{"Kind", "Name"},
UseSeparator: true,
}
if len(gatewayNode.EffectivePolicies) != 0 {
views = append(views, gatewayDescribeView{
EffectivePolicies: gatewayNode.EffectivePolicies,
})
for _, httpRouteNode := range gatewayNode.HTTPRoutes {
row := []string{
httpRouteNode.HTTPRoute.Kind, // Kind
fmt.Sprintf("%v/%v", httpRouteNode.HTTPRoute.Namespace, httpRouteNode.HTTPRoute.Name), // Name
}
attachedRoutes.Rows = append(attachedRoutes.Rows, row)
}
pairs = append(pairs, &DescriberKV{Key: "AttachedRoutes", Value: attachedRoutes})

for _, view := range views {
b, err := yaml.Marshal(view)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to marshal to yaml: %v\n", err)
os.Exit(1)
// DirectlyAttachedPolicies
if policyRefs := resourcediscovery.ConvertPoliciesMapToPolicyRefs(gatewayNode.Policies); len(policyRefs) != 0 {
directlyAttachedPolicies := &Table{
ColumnNames: []string{"Type", "Name"},
UseSeparator: true,
}
for _, policyRef := range policyRefs {
row := []string{
fmt.Sprintf("%v.%v", policyRef.Kind, policyRef.Group), // Type
fmt.Sprintf("%v/%v", policyRef.Namespace, policyRef.Name), // Name
}
directlyAttachedPolicies.Rows = append(directlyAttachedPolicies.Rows, row)
}
fmt.Fprint(gp.Out, string(b))
pairs = append(pairs, &DescriberKV{Key: "DirectlyAttachedPolicies", Value: directlyAttachedPolicies})
}

// EffectivePolicies
if len(gatewayNode.EffectivePolicies) != 0 {
pairs = append(pairs, &DescriberKV{Key: "EffectivePolicies", Value: gatewayNode.EffectivePolicies})
}

Describe(gp.Out, pairs)

if index+1 <= len(resourceModel.Gateways) {
fmt.Fprintf(gp.Out, "\n\n")
}
Expand Down
41 changes: 37 additions & 4 deletions gwctl/pkg/printer/gateways_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,24 @@ func TestGatewaysPrinter_PrintDescribeView(t *testing.T) {
},
},

&gatewayv1.HTTPRoute{
TypeMeta: metav1.TypeMeta{
Kind: "HTTPRoute",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo-httproute",
},
Spec: gatewayv1.HTTPRouteSpec{
CommonRouteSpec: gatewayv1.CommonRouteSpec{
ParentRefs: []gatewayv1.ParentReference{{
Kind: common.PtrTo(gatewayv1.Kind("Gateway")),
Group: common.PtrTo(gatewayv1.Group("gateway.networking.k8s.io")),
Name: "foo-gateway",
}},
},
},
},

&apiextensionsv1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: "healthcheckpolicies.foo.com",
Expand Down Expand Up @@ -345,11 +363,26 @@ func TestGatewaysPrinter_PrintDescribeView(t *testing.T) {
got := params.Out.(*bytes.Buffer).String()
want := `
Name: foo-gateway
GatewayClass: foo-gatewayclass
Namespace: ""
Labels: null
Annotations: null
APIVersion: ""
Kind: ""
Metadata:
creationTimestamp: null
resourceVersion: "999"
Spec:
gatewayClassName: foo-gatewayclass
listeners: null
Status: {}
AttachedRoutes:
Kind Name
---- ----
HTTPRoute /foo-httproute
DirectlyAttachedPolicies:
- Group: foo.com
Kind: HealthCheckPolicy
Name: health-check-gateway
Type Name
---- ----
HealthCheckPolicy.foo.com /health-check-gateway
EffectivePolicies:
HealthCheckPolicy.foo.com:
key1: value-parent-1
Expand Down

0 comments on commit f45d005

Please sign in to comment.