Skip to content

Commit

Permalink
Add missing columns to gwctl get policies (#2808)
Browse files Browse the repository at this point in the history
Signed-off-by: Jongwoo Han <jongwooo.han@gmail.com>
  • Loading branch information
jongwooo authored Mar 6, 2024
1 parent 346e951 commit ef4832a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion gwctl/pkg/cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func runGet(args []string, params *utils.CmdParams, flags *getFlags) {
}
realClock := clock.RealClock{}
gwPrinter := &printer.GatewaysPrinter{Out: params.Out, Clock: realClock}
policiesPrinter := &printer.PoliciesPrinter{Out: params.Out}
policiesPrinter := &printer.PoliciesPrinter{Out: params.Out, Clock: realClock}
httpRoutesPrinter := &printer.HTTPRoutesPrinter{Out: params.Out, Clock: realClock}

switch kind {
Expand Down
17 changes: 13 additions & 4 deletions gwctl/pkg/printer/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ import (
"strings"
"text/tabwriter"

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

"sigs.k8s.io/gateway-api/gwctl/pkg/policymanager"
"k8s.io/apimachinery/pkg/util/duration"
"k8s.io/utils/clock"
)

type PoliciesPrinter struct {
Out io.Writer
Out io.Writer
Clock clock.Clock
}

func (pp *PoliciesPrinter) Print(policies []policymanager.Policy) {
Expand All @@ -40,20 +43,26 @@ func (pp *PoliciesPrinter) Print(policies []policymanager.Policy) {
})

tw := tabwriter.NewWriter(pp.Out, 0, 0, 2, ' ', 0)
row := []string{"POLICY NAME", "POLICY KIND", "TARGET NAME", "TARGET KIND", "POLICY TYPE"}
row := []string{"NAME", "KIND", "TARGET NAME", "TARGET KIND", "POLICY TYPE", "AGE"}
tw.Write([]byte(strings.Join(row, "\t") + "\n"))

for _, policy := range policies {
policyType := "Direct"
if policy.IsInherited() {
policyType = "Inherited"
}

kind := fmt.Sprintf("%v.%v", policy.Unstructured().GroupVersionKind().Kind, policy.Unstructured().GroupVersionKind().Group)

age := duration.HumanDuration(pp.Clock.Since(policy.Unstructured().GetCreationTimestamp().Time))

row := []string{
policy.Unstructured().GetName(),
policy.Unstructured().GroupVersionKind().Kind,
kind,
policy.TargetRef().Name,
policy.TargetRef().Kind,
policyType,
age,
}
tw.Write([]byte(strings.Join(row, "\t") + "\n"))
}
Expand Down
31 changes: 20 additions & 11 deletions gwctl/pkg/printer/policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,23 @@ package printer
import (
"bytes"
"testing"
"time"

"github.com/google/go-cmp/cmp"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
testingclock "k8s.io/utils/clock/testing"

gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
"sigs.k8s.io/gateway-api/gwctl/pkg/cmd/utils"
"sigs.k8s.io/gateway-api/gwctl/pkg/common"
)

func TestPoliciesPrinter_Print_And_PrintDescribeView(t *testing.T) {
fakeClock := testingclock.NewFakeClock(time.Now())
objects := []runtime.Object{
&apiextensionsv1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -55,7 +59,8 @@ func TestPoliciesPrinter_Print_And_PrintDescribeView(t *testing.T) {
"apiVersion": "foo.com/v1",
"kind": "HealthCheckPolicy",
"metadata": map[string]interface{}{
"name": "health-check-gatewayclass",
"name": "health-check-gatewayclass",
"creationTimestamp": fakeClock.Now().Add(-6 * 24 * time.Hour).Format(time.RFC3339),
},
"spec": map[string]interface{}{
"override": map[string]interface{}{
Expand All @@ -80,7 +85,8 @@ func TestPoliciesPrinter_Print_And_PrintDescribeView(t *testing.T) {
"apiVersion": "foo.com/v1",
"kind": "HealthCheckPolicy",
"metadata": map[string]interface{}{
"name": "health-check-gateway",
"name": "health-check-gateway",
"creationTimestamp": fakeClock.Now().Add(-20 * 24 * time.Hour).Format(time.RFC3339),
},
"spec": map[string]interface{}{
"override": map[string]interface{}{
Expand Down Expand Up @@ -122,7 +128,8 @@ func TestPoliciesPrinter_Print_And_PrintDescribeView(t *testing.T) {
"apiVersion": "bar.com/v1",
"kind": "TimeoutPolicy",
"metadata": map[string]interface{}{
"name": "timeout-policy-namespace",
"name": "timeout-policy-namespace",
"creationTimestamp": fakeClock.Now().Add(-5 * time.Minute).Format(time.RFC3339),
},
"spec": map[string]interface{}{
"condition": "path=/abc",
Expand All @@ -139,7 +146,8 @@ func TestPoliciesPrinter_Print_And_PrintDescribeView(t *testing.T) {
"apiVersion": "bar.com/v1",
"kind": "TimeoutPolicy",
"metadata": map[string]interface{}{
"name": "timeout-policy-httproute",
"name": "timeout-policy-httproute",
"creationTimestamp": fakeClock.Now().Add(-13 * time.Minute).Format(time.RFC3339),
},
"spec": map[string]interface{}{
"condition": "path=/def",
Expand All @@ -157,17 +165,18 @@ func TestPoliciesPrinter_Print_And_PrintDescribeView(t *testing.T) {
params := utils.MustParamsForTest(t, common.MustClientsForTest(t, objects...))

pp := &PoliciesPrinter{
Out: &bytes.Buffer{},
Out: &bytes.Buffer{},
Clock: fakeClock,
}

pp.Print(params.PolicyManager.GetPolicies())
got := pp.Out.(*bytes.Buffer).String()
want := `
POLICY NAME POLICY KIND TARGET NAME TARGET KIND POLICY TYPE
health-check-gateway HealthCheckPolicy foo-gateway Gateway Inherited
health-check-gatewayclass HealthCheckPolicy foo-gatewayclass GatewayClass Inherited
timeout-policy-httproute TimeoutPolicy foo-httproute HTTPRoute Direct
timeout-policy-namespace TimeoutPolicy default Namespace Direct
NAME KIND TARGET NAME TARGET KIND POLICY TYPE AGE
health-check-gateway HealthCheckPolicy.foo.com foo-gateway Gateway Inherited 20d
health-check-gatewayclass HealthCheckPolicy.foo.com foo-gatewayclass GatewayClass Inherited 6d
timeout-policy-httproute TimeoutPolicy.bar.com foo-httproute HTTPRoute Direct 13m
timeout-policy-namespace TimeoutPolicy.bar.com default Namespace Direct 5m
`
if diff := cmp.Diff(common.YamlString(want), common.YamlString(got), common.YamlStringTransformer); diff != "" {
t.Errorf("Print: Unexpected diff\ngot=\n%v\nwant=\n%v\ndiff (-want +got)=\n%v", got, want, diff)
Expand Down

0 comments on commit ef4832a

Please sign in to comment.