Skip to content

Commit ef4832a

Browse files
authored
Add missing columns to gwctl get policies (#2808)
Signed-off-by: Jongwoo Han <[email protected]>
1 parent 346e951 commit ef4832a

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

gwctl/pkg/cmd/get/get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func runGet(args []string, params *utils.CmdParams, flags *getFlags) {
6464
}
6565
realClock := clock.RealClock{}
6666
gwPrinter := &printer.GatewaysPrinter{Out: params.Out, Clock: realClock}
67-
policiesPrinter := &printer.PoliciesPrinter{Out: params.Out}
67+
policiesPrinter := &printer.PoliciesPrinter{Out: params.Out, Clock: realClock}
6868
httpRoutesPrinter := &printer.HTTPRoutesPrinter{Out: params.Out, Clock: realClock}
6969

7070
switch kind {

gwctl/pkg/printer/policies.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ import (
2323
"strings"
2424
"text/tabwriter"
2525

26+
"sigs.k8s.io/gateway-api/gwctl/pkg/policymanager"
2627
"sigs.k8s.io/yaml"
2728

28-
"sigs.k8s.io/gateway-api/gwctl/pkg/policymanager"
29+
"k8s.io/apimachinery/pkg/util/duration"
30+
"k8s.io/utils/clock"
2931
)
3032

3133
type PoliciesPrinter struct {
32-
Out io.Writer
34+
Out io.Writer
35+
Clock clock.Clock
3336
}
3437

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

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

4649
for _, policy := range policies {
4750
policyType := "Direct"
4851
if policy.IsInherited() {
4952
policyType = "Inherited"
5053
}
54+
55+
kind := fmt.Sprintf("%v.%v", policy.Unstructured().GroupVersionKind().Kind, policy.Unstructured().GroupVersionKind().Group)
56+
57+
age := duration.HumanDuration(pp.Clock.Since(policy.Unstructured().GetCreationTimestamp().Time))
58+
5159
row := []string{
5260
policy.Unstructured().GetName(),
53-
policy.Unstructured().GroupVersionKind().Kind,
61+
kind,
5462
policy.TargetRef().Name,
5563
policy.TargetRef().Kind,
5664
policyType,
65+
age,
5766
}
5867
tw.Write([]byte(strings.Join(row, "\t") + "\n"))
5968
}

gwctl/pkg/printer/policies_test.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,23 @@ package printer
1919
import (
2020
"bytes"
2121
"testing"
22+
"time"
2223

2324
"github.com/google/go-cmp/cmp"
25+
2426
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2527
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2628
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2729
"k8s.io/apimachinery/pkg/runtime"
28-
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
30+
testingclock "k8s.io/utils/clock/testing"
2931

32+
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
3033
"sigs.k8s.io/gateway-api/gwctl/pkg/cmd/utils"
3134
"sigs.k8s.io/gateway-api/gwctl/pkg/common"
3235
)
3336

3437
func TestPoliciesPrinter_Print_And_PrintDescribeView(t *testing.T) {
38+
fakeClock := testingclock.NewFakeClock(time.Now())
3539
objects := []runtime.Object{
3640
&apiextensionsv1.CustomResourceDefinition{
3741
ObjectMeta: metav1.ObjectMeta{
@@ -55,7 +59,8 @@ func TestPoliciesPrinter_Print_And_PrintDescribeView(t *testing.T) {
5559
"apiVersion": "foo.com/v1",
5660
"kind": "HealthCheckPolicy",
5761
"metadata": map[string]interface{}{
58-
"name": "health-check-gatewayclass",
62+
"name": "health-check-gatewayclass",
63+
"creationTimestamp": fakeClock.Now().Add(-6 * 24 * time.Hour).Format(time.RFC3339),
5964
},
6065
"spec": map[string]interface{}{
6166
"override": map[string]interface{}{
@@ -80,7 +85,8 @@ func TestPoliciesPrinter_Print_And_PrintDescribeView(t *testing.T) {
8085
"apiVersion": "foo.com/v1",
8186
"kind": "HealthCheckPolicy",
8287
"metadata": map[string]interface{}{
83-
"name": "health-check-gateway",
88+
"name": "health-check-gateway",
89+
"creationTimestamp": fakeClock.Now().Add(-20 * 24 * time.Hour).Format(time.RFC3339),
8490
},
8591
"spec": map[string]interface{}{
8692
"override": map[string]interface{}{
@@ -122,7 +128,8 @@ func TestPoliciesPrinter_Print_And_PrintDescribeView(t *testing.T) {
122128
"apiVersion": "bar.com/v1",
123129
"kind": "TimeoutPolicy",
124130
"metadata": map[string]interface{}{
125-
"name": "timeout-policy-namespace",
131+
"name": "timeout-policy-namespace",
132+
"creationTimestamp": fakeClock.Now().Add(-5 * time.Minute).Format(time.RFC3339),
126133
},
127134
"spec": map[string]interface{}{
128135
"condition": "path=/abc",
@@ -139,7 +146,8 @@ func TestPoliciesPrinter_Print_And_PrintDescribeView(t *testing.T) {
139146
"apiVersion": "bar.com/v1",
140147
"kind": "TimeoutPolicy",
141148
"metadata": map[string]interface{}{
142-
"name": "timeout-policy-httproute",
149+
"name": "timeout-policy-httproute",
150+
"creationTimestamp": fakeClock.Now().Add(-13 * time.Minute).Format(time.RFC3339),
143151
},
144152
"spec": map[string]interface{}{
145153
"condition": "path=/def",
@@ -157,17 +165,18 @@ func TestPoliciesPrinter_Print_And_PrintDescribeView(t *testing.T) {
157165
params := utils.MustParamsForTest(t, common.MustClientsForTest(t, objects...))
158166

159167
pp := &PoliciesPrinter{
160-
Out: &bytes.Buffer{},
168+
Out: &bytes.Buffer{},
169+
Clock: fakeClock,
161170
}
162171

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

0 commit comments

Comments
 (0)