Skip to content

Commit

Permalink
Add tests and related cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravkghildiyal committed Jul 3, 2024
1 parent fb4c165 commit f27a963
Show file tree
Hide file tree
Showing 10 changed files with 380 additions and 164 deletions.
2 changes: 1 addition & 1 deletion gwctl/cmd/subcommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func runGetOrDescribeHTTPRoutes(f cmdutils.Factory, o *getOrDescribeOptions) {
handleErrOrExitWithMsg(err, "failed to discover HTTPRoute resources")

realClock := clock.RealClock{}
httpRoutesPrinter := &printer.HTTPRoutesPrinter{Writer: o.out, Clock: realClock}
httpRoutesPrinter := &printer.HTTPRoutesPrinter{Writer: o.out, Clock: realClock, EventFetcher: discoverer}
if o.cmdName == commandNameGet {
printer.Print(httpRoutesPrinter, resourceModel, o.outputFormat)
} else {
Expand Down
4 changes: 2 additions & 2 deletions gwctl/pkg/printer/backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ func (bp *BackendsPrinter) PrintDescribeView(resourceModel *resourcediscovery.Re
pairs = append(pairs, &DescriberKV{Key: "ReferencedByRoutes", Value: routes})

// DirectlyAttachedPolicies
policyRefs := resourcediscovery.ConvertPoliciesMapToPolicyRefs(backendNode.Policies)
pairs = append(pairs, &DescriberKV{Key: "DirectlyAttachedPolicies", Value: convertPolicyRefsToTable(policyRefs)})
policies := SortByString(maps.Values(backendNode.Policies))
pairs = append(pairs, &DescriberKV{Key: "DirectlyAttachedPolicies", Value: convertPoliciesToRefsTable(policies, false)})

// EffectivePolicies
if len(backendNode.EffectivePolicies) != 0 {
Expand Down
38 changes: 30 additions & 8 deletions gwctl/pkg/printer/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"

"sigs.k8s.io/gateway-api/gwctl/pkg/common"
"sigs.k8s.io/gateway-api/gwctl/pkg/resourcediscovery"
)

// DescriberKV stores key-value pairs that are used with Describing a resource.
Expand Down Expand Up @@ -154,20 +154,42 @@ func convertEventsSliceToTable(events []corev1.Event, clock clock.Clock) *Table
return table
}

func convertPolicyRefsToTable(policyRefs []common.ObjRef) *Table {
func convertPoliciesToRefsTable(policies []*resourcediscovery.PolicyNode, includeTarget bool) *Table {
table := &Table{
ColumnNames: []string{"Type", "Name"},
UseSeparator: true,
}
for _, policyRef := range policyRefs {
name := policyRef.Name
if policyRef.Namespace != "" {
name = fmt.Sprintf("%v/%v", policyRef.Namespace, name)
if includeTarget {
table.ColumnNames = append(table.ColumnNames, "Target Kind", "Target Name")
}

for _, policyNode := range policies {
policyType := fmt.Sprintf("%v.%v", policyNode.Policy.Unstructured().GroupVersionKind().Kind, policyNode.Policy.Unstructured().GroupVersionKind().Group)

policyName := policyNode.Policy.Unstructured().GetName()
if ns := policyNode.Policy.Unstructured().GetNamespace(); ns != "" {
policyName = fmt.Sprintf("%v/%v", ns, policyName)
}

targetKind := policyNode.Policy.TargetRef().Kind

targetName := policyNode.Policy.TargetRef().Name
if ns := policyNode.Policy.TargetRef().Namespace; ns != "" {
targetName = fmt.Sprintf("%v/%v", ns, targetName)
}

row := []string{
fmt.Sprintf("%v.%v", policyRef.Kind, policyRef.Group), // Type
name, // Name
policyType, // Type
policyName, // Name
}

if includeTarget {
row = append(row,
targetKind, // Target Kind
targetName, // Target Name
)
}

table.Rows = append(table.Rows, row)
}
return table
Expand Down
4 changes: 2 additions & 2 deletions gwctl/pkg/printer/gatewayclasses.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ func (gcp *GatewayClassesPrinter) PrintDescribeView(resourceModel *resourcedisco
}

// DirectlyAttachedPolicies
policyRefs := resourcediscovery.ConvertPoliciesMapToPolicyRefs(gatewayClassNode.Policies)
pairs = append(pairs, &DescriberKV{Key: "DirectlyAttachedPolicies", Value: convertPolicyRefsToTable(policyRefs)})
policies := SortByString(maps.Values(gatewayClassNode.Policies))
pairs = append(pairs, &DescriberKV{Key: "DirectlyAttachedPolicies", Value: convertPoliciesToRefsTable(policies, false)})

// Events
eventList := gcp.EventFetcher.FetchEventsFor(context.Background(), gatewayClassNode.GatewayClass)
Expand Down
4 changes: 2 additions & 2 deletions gwctl/pkg/printer/gateways.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ func (gp *GatewaysPrinter) PrintDescribeView(resourceModel *resourcediscovery.Re
pairs = append(pairs, &DescriberKV{Key: "AttachedRoutes", Value: attachedRoutes})

// DirectlyAttachedPolicies
policyRefs := resourcediscovery.ConvertPoliciesMapToPolicyRefs(gatewayNode.Policies)
pairs = append(pairs, &DescriberKV{Key: "DirectlyAttachedPolicies", Value: convertPolicyRefsToTable(policyRefs)})
policies := SortByString(maps.Values(gatewayNode.Policies))
pairs = append(pairs, &DescriberKV{Key: "DirectlyAttachedPolicies", Value: convertPoliciesToRefsTable(policies, false)})

// EffectivePolicies
if len(gatewayNode.EffectivePolicies) != 0 {
Expand Down
99 changes: 15 additions & 84 deletions gwctl/pkg/printer/httproutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ limitations under the License.
package printer

import (
"context"
"fmt"
"io"
"sort"
"strings"

"golang.org/x/exp/maps"
Expand All @@ -33,7 +33,8 @@ var _ Printer = (*HTTPRoutesPrinter)(nil)

type HTTPRoutesPrinter struct {
io.Writer
Clock clock.Clock
Clock clock.Clock
EventFetcher eventFetcher
}

func (hp *HTTPRoutesPrinter) GetPrintableNodes(resourceModel *resourcediscovery.ResourceModel) []NodeResource {
Expand Down Expand Up @@ -95,13 +96,15 @@ func (hp *HTTPRoutesPrinter) PrintDescribeView(resourceModel *resourcediscovery.
index++

metadata := httpRouteNode.HTTPRoute.ObjectMeta.DeepCopy()
resetMetadataFields(metadata)

namespace := handleDefaultNamespace(httpRouteNode.HTTPRoute.Namespace)
metadata.Labels = nil
metadata.Annotations = nil
metadata.Name = ""
metadata.Namespace = ""
metadata.ManagedFields = nil

pairs := []*DescriberKV{
{"Name", httpRouteNode.HTTPRoute.GetName()},
{"Namespace", namespace},
{"Namespace", httpRouteNode.HTTPRoute.Namespace},
{"Label", httpRouteNode.HTTPRoute.Labels},
{"Annotations", httpRouteNode.HTTPRoute.Annotations},
{"APIVersion", httpRouteNode.HTTPRoute.APIVersion},
Expand All @@ -112,85 +115,12 @@ func (hp *HTTPRoutesPrinter) PrintDescribeView(resourceModel *resourcediscovery.
}

// DirectlyAttachedPolicies
directlyAttachedPolicies := &Table{
ColumnNames: []string{"Type", "Name"},
UseSeparator: true,
}

for _, policyNode := range httpRouteNode.Policies {
if policyNode.Policy.IsDirect() {
policyNamespace := handleDefaultNamespace(policyNode.Policy.TargetRef().Namespace)

row := []string{
// Type
fmt.Sprintf("%v.%v", policyNode.Policy.Unstructured().GroupVersionKind().Kind, policyNode.Policy.Unstructured().GroupVersionKind().Group),
// Name
fmt.Sprintf("%v/%v", policyNamespace, policyNode.Policy.Unstructured().GetName()),
}

directlyAttachedPolicies.Rows = append(directlyAttachedPolicies.Rows, row)
}
}

if len(directlyAttachedPolicies.Rows) != 0 {
pairs = append(pairs, &DescriberKV{Key: "DirectlyAttachedPolicies", Value: directlyAttachedPolicies})
}
policies := SortByString(maps.Values(httpRouteNode.Policies))
pairs = append(pairs, &DescriberKV{Key: "DirectlyAttachedPolicies", Value: convertPoliciesToRefsTable(policies, false)})

// InheritedPolicies
if len(httpRouteNode.InheritedPolicies) != 0 {
inheritedPolicies := &Table{
ColumnNames: []string{"Type", "Name", "Target Kind", "Target Name"},
UseSeparator: true,
}

for _, policyNode := range httpRouteNode.InheritedPolicies {
policyNamespace := handleDefaultNamespace(policyNode.Policy.Unstructured().GetNamespace())

row := []string{
// Type
fmt.Sprintf(
"%v.%v",
policyNode.Policy.Unstructured().GroupVersionKind().Kind,
policyNode.Policy.Unstructured().GroupVersionKind().Group,
),
// Name
fmt.Sprintf("%v/%v", policyNamespace, policyNode.Policy.Unstructured().GetName()),
// Target Kind
policyNode.Policy.TargetRef().Kind,
}

// Target Name
switch policyNode.Policy.TargetRef().Kind {

case "Namespace":
row = append(row, handleDefaultNamespace(policyNode.Policy.TargetRef().Name))

case "GatewayClass":
row = append(row, policyNode.Policy.TargetRef().Name)

default:
// handle namespaced objects
targetRefNamespace := handleDefaultNamespace(policyNode.Policy.TargetRef().Namespace)
name := fmt.Sprintf("%v/%v", targetRefNamespace, policyNode.Policy.TargetRef().Name)

row = append(row, name)
}

// Sort inheritedPolices on the basis of Type and Name
sort.Slice(inheritedPolicies.Rows, func(i, j int) bool {
// Compare the Type of inheritedPolicies
if inheritedPolicies.Rows[i][0] != inheritedPolicies.Rows[j][0] {
return inheritedPolicies.Rows[i][0] < inheritedPolicies.Rows[j][0]
}
// If inheritedPolicies are of same Type, compare Names
return inheritedPolicies.Rows[i][1] < inheritedPolicies.Rows[j][1]
})

inheritedPolicies.Rows = append(inheritedPolicies.Rows, row)
}

pairs = append(pairs, &DescriberKV{Key: "InheritedPolicies", Value: inheritedPolicies})
}
inheritedPolicies := SortByString(maps.Values(httpRouteNode.InheritedPolicies))
pairs = append(pairs, &DescriberKV{Key: "InheritedPolicies", Value: convertPoliciesToRefsTable(inheritedPolicies, true)})

// EffectivePolices
if len(httpRouteNode.EffectivePolicies) != 0 {
Expand All @@ -203,7 +133,8 @@ func (hp *HTTPRoutesPrinter) PrintDescribeView(resourceModel *resourcediscovery.
}

// Events
pairs = append(pairs, &DescriberKV{Key: "Events", Value: convertEventsSliceToTable(httpRouteNode.Events, hp.Clock)})
eventList := hp.EventFetcher.FetchEventsFor(context.Background(), httpRouteNode.HTTPRoute)
pairs = append(pairs, &DescriberKV{Key: "Events", Value: convertEventsSliceToTable(eventList.Items, hp.Clock)})

Describe(hp, pairs)

Expand Down
38 changes: 27 additions & 11 deletions gwctl/pkg/printer/httproutes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ func TestHTTPRoutesPrinter_PrintDescribeView(t *testing.T) {
fakeClock := testingclock.NewFakeClock(time.Now())
objects := []runtime.Object{
&gatewayv1.GatewayClass{
TypeMeta: metav1.TypeMeta{
APIVersion: gatewayv1.GroupVersion.String(),
Kind: "GatewayClass",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo-gatewayclass",
},
Expand Down Expand Up @@ -330,6 +334,10 @@ func TestHTTPRoutesPrinter_PrintDescribeView(t *testing.T) {
},

&gatewayv1.Gateway{
TypeMeta: metav1.TypeMeta{
APIVersion: gatewayv1.GroupVersion.String(),
Kind: "Gateway",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo-gateway",
Namespace: "default",
Expand All @@ -343,7 +351,8 @@ func TestHTTPRoutesPrinter_PrintDescribeView(t *testing.T) {
"apiVersion": "foo.com/v1",
"kind": "HealthCheckPolicy",
"metadata": map[string]interface{}{
"name": "health-check-gateway",
"name": "health-check-gateway",
"namespace": "default",
},
"spec": map[string]interface{}{
"override": map[string]interface{}{
Expand All @@ -364,8 +373,13 @@ func TestHTTPRoutesPrinter_PrintDescribeView(t *testing.T) {
},

&gatewayv1.HTTPRoute{
TypeMeta: metav1.TypeMeta{
APIVersion: gatewayv1.GroupVersion.String(),
Kind: "HTTPRoute",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo-httproute",
Name: "foo-httproute",
Namespace: "default",
},
Spec: gatewayv1.HTTPRouteSpec{
CommonRouteSpec: gatewayv1.CommonRouteSpec{
Expand All @@ -382,7 +396,8 @@ func TestHTTPRoutesPrinter_PrintDescribeView(t *testing.T) {
"apiVersion": "bar.com/v1",
"kind": "TimeoutPolicy",
"metadata": map[string]interface{}{
"name": "timeout-policy-httproute",
"name": "timeout-policy-httproute",
"namespace": "default",
},
"spec": map[string]interface{}{
"condition": "path=/def",
Expand Down Expand Up @@ -463,8 +478,9 @@ func TestHTTPRoutesPrinter_PrintDescribeView(t *testing.T) {
}

hp := &HTTPRoutesPrinter{
Writer: buff,
Clock: fakeClock,
Writer: buff,
Clock: fakeClock,
EventFetcher: discoverer,
}
hp.PrintDescribeView(resourceModel)

Expand All @@ -474,8 +490,8 @@ Name: foo-httproute
Namespace: default
Label: null
Annotations: null
APIVersion: ""
Kind: ""
APIVersion: gateway.networking.k8s.io/v1
Kind: HTTPRoute
Metadata:
creationTimestamp: null
resourceVersion: "999"
Expand All @@ -491,10 +507,10 @@ DirectlyAttachedPolicies:
---- ----
TimeoutPolicy.bar.com default/timeout-policy-httproute
InheritedPolicies:
Type Name Target Kind Target Name
---- ---- ----------- -----------
HealthCheckPolicy.foo.com default/health-check-gatewayclass GatewayClass foo-gatewayclass
HealthCheckPolicy.foo.com default/health-check-gateway Gateway default/foo-gateway
Type Name Target Kind Target Name
---- ---- ----------- -----------
HealthCheckPolicy.foo.com health-check-gatewayclass GatewayClass foo-gatewayclass
HealthCheckPolicy.foo.com default/health-check-gateway Gateway default/foo-gateway
EffectivePolicies:
default/foo-gateway:
HealthCheckPolicy.foo.com:
Expand Down
4 changes: 2 additions & 2 deletions gwctl/pkg/printer/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func (nsp *NamespacesPrinter) PrintDescribeView(resourceModel *resourcediscovery
}

// DirectlyAttachedPolicies
policyRefs := resourcediscovery.ConvertPoliciesMapToPolicyRefs(namespaceNode.Policies)
pairs = append(pairs, &DescriberKV{Key: "DirectlyAttachedPolicies", Value: convertPolicyRefsToTable(policyRefs)})
policies := SortByString(maps.Values(namespaceNode.Policies))
pairs = append(pairs, &DescriberKV{Key: "DirectlyAttachedPolicies", Value: convertPoliciesToRefsTable(policies, false)})

// Events
eventList := nsp.EventFetcher.FetchEventsFor(context.Background(), namespaceNode.Namespace)
Expand Down
Loading

0 comments on commit f27a963

Please sign in to comment.