API printers

Change-Id: I7a0777bbd0b10e1a849c7891439b00622ec6b09c
This commit is contained in:
Antonio Ojea 2022-08-16 13:55:43 +02:00 committed by Antonio Ojea
parent 7e77e8b21d
commit 55a35bebee
2 changed files with 162 additions and 2 deletions

View File

@ -654,13 +654,23 @@ func AddHandlers(h printers.PrintHandler) {
_ = h.TableHandler(podSchedulingCtxColumnDefinitions, printPodSchedulingContext)
_ = h.TableHandler(podSchedulingCtxColumnDefinitions, printPodSchedulingContextList)
serviceCIDRColumnDefinitions := []metav1.TableColumnDefinition{
{Name: "Name", Type: "string", Format: "name", Description: metav1.ObjectMeta{}.SwaggerDoc()["name"]},
{Name: "IPv4", Type: "string", Description: networkingv1alpha1.ServiceCIDRSpec{}.SwaggerDoc()["ipv4"]},
{Name: "IPv6", Type: "string", Description: networkingv1alpha1.ServiceCIDRSpec{}.SwaggerDoc()["ipv6"]},
{Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]},
}
_ = h.TableHandler(serviceCIDRColumnDefinitions, printServiceCIDR)
_ = h.TableHandler(serviceCIDRColumnDefinitions, printServiceCIDRList)
ipAddressColumnDefinitions := []metav1.TableColumnDefinition{
{Name: "Name", Type: "string", Format: "name", Description: metav1.ObjectMeta{}.SwaggerDoc()["name"]},
{Name: "ParentRef", Type: "string", Description: networkingv1alpha1.IPAddressSpec{}.SwaggerDoc()["parentRef"]},
}
h.TableHandler(ipAddressColumnDefinitions, printIPAddress)
h.TableHandler(ipAddressColumnDefinitions, printIPAddressList)
_ = h.TableHandler(ipAddressColumnDefinitions, printIPAddress)
_ = h.TableHandler(ipAddressColumnDefinitions, printIPAddressList)
}
// Pass ports=nil for all ports.
@ -2838,6 +2848,36 @@ func printPriorityLevelConfigurationList(list *flowcontrol.PriorityLevelConfigur
return rows, nil
}
func printServiceCIDR(obj *networking.ServiceCIDR, options printers.GenerateOptions) ([]metav1.TableRow, error) {
row := metav1.TableRow{
Object: runtime.RawExtension{Object: obj},
}
ipv4 := "<none>"
ipv6 := "<none>"
if obj.Spec.IPv4 != "" {
ipv4 = obj.Spec.IPv4
}
if obj.Spec.IPv6 != "" {
ipv6 = obj.Spec.IPv6
}
row.Cells = append(row.Cells, obj.Name, ipv4, ipv6, translateTimestampSince(obj.CreationTimestamp))
return []metav1.TableRow{row}, nil
}
func printServiceCIDRList(list *networking.ServiceCIDRList, options printers.GenerateOptions) ([]metav1.TableRow, error) {
rows := make([]metav1.TableRow, 0, len(list.Items))
for i := range list.Items {
r, err := printServiceCIDR(&list.Items[i], options)
if err != nil {
return nil, err
}
rows = append(rows, r...)
}
return rows, nil
}
func printIPAddress(obj *networking.IPAddress, options printers.GenerateOptions) ([]metav1.TableRow, error) {
row := metav1.TableRow{
Object: runtime.RawExtension{Object: obj},

View File

@ -6574,3 +6574,123 @@ func TestPrintIPAddressList(t *testing.T) {
}
}
func TestPrintServiceCIDR(t *testing.T) {
ipv4CIDR := "10.1.0.0/16"
ipv6CIDR := "fd00:1:1::/64"
tests := []struct {
ccc networking.ServiceCIDR
options printers.GenerateOptions
expected []metav1.TableRow
}{
{
// Test name, IPv4 only.
ccc: networking.ServiceCIDR{
ObjectMeta: metav1.ObjectMeta{Name: "test1"},
Spec: networking.ServiceCIDRSpec{
IPv4: ipv4CIDR,
},
},
options: printers.GenerateOptions{},
// Columns: Name, IPv4, IPv6, Age.
expected: []metav1.TableRow{{Cells: []interface{}{"test1", ipv4CIDR, "<none>", "<unknown>"}}},
},
{
// Test name, IPv6 only.
ccc: networking.ServiceCIDR{
ObjectMeta: metav1.ObjectMeta{Name: "test5"},
Spec: networking.ServiceCIDRSpec{
IPv6: ipv6CIDR,
},
},
options: printers.GenerateOptions{},
// Columns: Name, PerNodeHostBits, IPv4, IPv6, Age
expected: []metav1.TableRow{{Cells: []interface{}{"test5", "<none>", ipv6CIDR, "<unknown>"}}},
},
{
// Test name, DualStack.
ccc: networking.ServiceCIDR{
ObjectMeta: metav1.ObjectMeta{Name: "test9"},
Spec: networking.ServiceCIDRSpec{
IPv4: ipv4CIDR,
IPv6: ipv6CIDR,
},
},
options: printers.GenerateOptions{},
// Columns: Name, PerNodeHostBits, IPv4, IPv6, Age.
expected: []metav1.TableRow{{Cells: []interface{}{"test9", ipv4CIDR, ipv6CIDR, "<unknown>"}}},
},
}
for i, test := range tests {
rows, err := printServiceCIDR(&test.ccc, test.options)
if err != nil {
t.Fatal(err)
}
for i := range rows {
rows[i].Object.Object = nil
}
if !reflect.DeepEqual(test.expected, rows) {
t.Errorf("%d mismatch: %s", i, cmp.Diff(test.expected, rows))
}
}
}
func TestPrintServiceCIDRList(t *testing.T) {
cccList := networking.ServiceCIDRList{
Items: []networking.ServiceCIDR{
{
ObjectMeta: metav1.ObjectMeta{Name: "ccc1"},
Spec: networking.ServiceCIDRSpec{
IPv4: "10.1.0.0/16",
IPv6: "fd00:1:1::/64",
},
},
{
ObjectMeta: metav1.ObjectMeta{Name: "ccc2"},
Spec: networking.ServiceCIDRSpec{
IPv4: "10.2.0.0/16",
IPv6: "fd00:2:1::/64",
},
},
},
}
tests := []struct {
options printers.GenerateOptions
expected []metav1.TableRow
}{
{
// Test name, DualStack with node selector, wide.
options: printers.GenerateOptions{Wide: false},
expected: []metav1.TableRow{
// Columns: Name, IPv4, IPv6, Age.
{Cells: []interface{}{"ccc1", "10.1.0.0/16", "fd00:1:1::/64", "<unknown>"}},
{Cells: []interface{}{"ccc2", "10.2.0.0/16", "fd00:2:1::/64", "<unknown>"}},
},
},
{
// Test name, DualStack with node selector, wide.
options: printers.GenerateOptions{Wide: true},
expected: []metav1.TableRow{
// Columns: Name, IPv4, IPv6, Age.
{Cells: []interface{}{"ccc1", "10.1.0.0/16", "fd00:1:1::/64", "<unknown>"}},
{Cells: []interface{}{"ccc2", "10.2.0.0/16", "fd00:2:1::/64", "<unknown>"}},
},
},
}
for _, test := range tests {
rows, err := printServiceCIDRList(&cccList, test.options)
if err != nil {
t.Fatalf("Error printing service list: %#v", err)
}
for i := range rows {
rows[i].Object.Object = nil
}
if !reflect.DeepEqual(test.expected, rows) {
t.Errorf("mismatch: %s", cmp.Diff(test.expected, rows))
}
}
}