Skip to content

Commit 29e8a7a

Browse files
authored
Sort gateways by name and gatewayclass (#2837)
1 parent bfac91b commit 29e8a7a

File tree

2 files changed

+115
-10
lines changed

2 files changed

+115
-10
lines changed

gwctl/pkg/printer/gateways.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ package printer
1919
import (
2020
"fmt"
2121
"io"
22+
"sort"
2223
"strings"
2324
"text/tabwriter"
2425

2526
"sigs.k8s.io/gateway-api/gwctl/pkg/policymanager"
2627
"sigs.k8s.io/gateway-api/gwctl/pkg/resourcediscovery"
2728
"sigs.k8s.io/yaml"
28-
29+
2930
"k8s.io/apimachinery/pkg/util/duration"
3031
"k8s.io/utils/clock"
3132
)
@@ -50,7 +51,19 @@ func (gp *GatewaysPrinter) Print(resourceModel *resourcediscovery.ResourceModel)
5051
row := []string{"NAME", "CLASS", "ADDRESSES", "PORTS", "PROGRAMMED", "AGE"}
5152
tw.Write([]byte(strings.Join(row, "\t") + "\n"))
5253

54+
gatewayNodes := make([]*resourcediscovery.GatewayNode, 0, len(resourceModel.Gateways))
5355
for _, gatewayNode := range resourceModel.Gateways {
56+
gatewayNodes = append(gatewayNodes, gatewayNode)
57+
}
58+
59+
sort.Slice(gatewayNodes, func(i, j int) bool {
60+
if gatewayNodes[i].Gateway.GetName() != gatewayNodes[j].Gateway.GetName() {
61+
return gatewayNodes[i].Gateway.GetName() < gatewayNodes[j].Gateway.GetName()
62+
}
63+
return gatewayNodes[i].Gateway.Spec.GatewayClassName < gatewayNodes[j].Gateway.Spec.GatewayClassName
64+
})
65+
66+
for _, gatewayNode := range gatewayNodes {
5467
var addresses []string
5568
for _, address := range gatewayNode.Gateway.Status.Addresses {
5669
addresses = append(addresses, address.Value)

gwctl/pkg/printer/gateways_test.go

Lines changed: 101 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,79 @@ func TestGatewaysPrinter_Print(t *testing.T) {
4040
objects := []runtime.Object{
4141
&gatewayv1.GatewayClass{
4242
ObjectMeta: metav1.ObjectMeta{
43-
Name: "foo-gatewayclass",
43+
Name: "internal-class",
44+
},
45+
Spec: gatewayv1.GatewayClassSpec{
46+
ControllerName: "example.net/gateway-controller",
47+
Description: common.PtrTo("random"),
48+
},
49+
},
50+
&gatewayv1.GatewayClass{
51+
ObjectMeta: metav1.ObjectMeta{
52+
Name: "external-class",
53+
},
54+
Spec: gatewayv1.GatewayClassSpec{
55+
ControllerName: "example.net/gateway-controller",
56+
Description: common.PtrTo("random"),
57+
},
58+
},
59+
&gatewayv1.GatewayClass{
60+
ObjectMeta: metav1.ObjectMeta{
61+
Name: "regional-internal-class",
4462
},
4563
Spec: gatewayv1.GatewayClassSpec{
4664
ControllerName: "example.net/gateway-controller",
4765
Description: common.PtrTo("random"),
4866
},
4967
},
50-
5168
&gatewayv1.Gateway{
5269
ObjectMeta: metav1.ObjectMeta{
53-
Name: "foo-gateway",
70+
Name: "abc-gateway-12345",
5471
CreationTimestamp: metav1.Time{
55-
Time: fakeClock.Now().Add(-time.Second),
72+
Time: fakeClock.Now().Add(-20 * 24 * time.Hour),
5673
},
5774
},
5875
Spec: gatewayv1.GatewaySpec{
59-
GatewayClassName: "foo-gatewayclass",
76+
GatewayClassName: "internal-class",
77+
Listeners: []gatewayv1.Listener{
78+
{
79+
Name: gatewayv1.SectionName("https-443"),
80+
Protocol: gatewayv1.HTTPSProtocolType,
81+
Port: gatewayv1.PortNumber(443),
82+
},
83+
{
84+
Name: gatewayv1.SectionName("http-8080"),
85+
Protocol: gatewayv1.HTTPProtocolType,
86+
Port: gatewayv1.PortNumber(8080),
87+
},
88+
},
89+
},
90+
Status: gatewayv1.GatewayStatus{
91+
Addresses: []gatewayv1.GatewayStatusAddress{
92+
{
93+
Value: "192.168.100.5",
94+
},
95+
},
96+
Conditions: []metav1.Condition{
97+
{
98+
Type: "Programmed",
99+
Status: "False",
100+
},
101+
},
102+
},
103+
},
104+
&gatewayv1.Gateway{
105+
ObjectMeta: metav1.ObjectMeta{
106+
Name: "demo-gateway-2",
107+
CreationTimestamp: metav1.Time{
108+
Time: fakeClock.Now().Add(-5 * 24 * time.Hour),
109+
},
110+
},
111+
Spec: gatewayv1.GatewaySpec{
112+
GatewayClassName: "external-class",
60113
Listeners: []gatewayv1.Listener{
61114
{
62-
Name: gatewayv1.SectionName("http-1"),
115+
Name: gatewayv1.SectionName("http-80"),
63116
Protocol: gatewayv1.HTTPProtocolType,
64117
Port: gatewayv1.PortNumber(80),
65118
},
@@ -70,11 +123,48 @@ func TestGatewaysPrinter_Print(t *testing.T) {
70123
{
71124
Value: "10.0.0.1",
72125
},
126+
{
127+
Value: "10.0.0.2",
128+
},
129+
{
130+
Value: "10.0.0.3",
131+
},
132+
},
133+
Conditions: []metav1.Condition{
134+
{
135+
Type: "Programmed",
136+
Status: "True",
137+
},
138+
},
139+
},
140+
},
141+
&gatewayv1.Gateway{
142+
ObjectMeta: metav1.ObjectMeta{
143+
Name: "random-gateway",
144+
CreationTimestamp: metav1.Time{
145+
Time: fakeClock.Now().Add(-3 * time.Second),
146+
},
147+
},
148+
Spec: gatewayv1.GatewaySpec{
149+
GatewayClassName: "regional-internal-class",
150+
Listeners: []gatewayv1.Listener{
151+
{
152+
Name: gatewayv1.SectionName("http-8443"),
153+
Protocol: gatewayv1.HTTPProtocolType,
154+
Port: gatewayv1.PortNumber(8443),
155+
},
156+
},
157+
},
158+
Status: gatewayv1.GatewayStatus{
159+
Addresses: []gatewayv1.GatewayStatusAddress{
160+
{
161+
Value: "10.11.12.13",
162+
},
73163
},
74164
Conditions: []metav1.Condition{
75165
{
76166
Type: "Programmed",
77-
Status: metav1.ConditionTrue,
167+
Status: "Unknown",
78168
},
79169
},
80170
},
@@ -99,8 +189,10 @@ func TestGatewaysPrinter_Print(t *testing.T) {
99189

100190
got := params.Out.(*bytes.Buffer).String()
101191
want := `
102-
NAME CLASS ADDRESSES PORTS PROGRAMMED AGE
103-
foo-gateway foo-gatewayclass 10.0.0.1 80 True 1s
192+
NAME CLASS ADDRESSES PORTS PROGRAMMED AGE
193+
abc-gateway-12345 internal-class 192.168.100.5 443,8080 False 20d
194+
demo-gateway-2 external-class 10.0.0.1,10.0.0.2 + 1 more 80 True 5d
195+
random-gateway regional-internal-class 10.11.12.13 8443 Unknown 3s
104196
`
105197

106198
if diff := cmp.Diff(common.YamlString(want), common.YamlString(got), common.YamlStringTransformer); diff != "" {

0 commit comments

Comments
 (0)