IPAddress printers

Change-Id: I60f40f4fe20b3ebd61b8ee137e55bcdefc1f2c96
This commit is contained in:
Antonio Ojea 2023-01-02 15:28:13 +00:00
parent 53dbf1752b
commit d06dbadd14
2 changed files with 123 additions and 0 deletions

View File

@ -645,6 +645,14 @@ func AddHandlers(h printers.PrintHandler) {
}
_ = h.TableHandler(podSchedulingCtxColumnDefinitions, printPodSchedulingContext)
_ = h.TableHandler(podSchedulingCtxColumnDefinitions, printPodSchedulingContextList)
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)
}
// Pass ports=nil for all ports.
@ -2779,6 +2787,41 @@ func printClusterCIDRList(list *networking.ClusterCIDRList, options printers.Gen
return rows, nil
}
func printIPAddress(obj *networking.IPAddress, options printers.GenerateOptions) ([]metav1.TableRow, error) {
row := metav1.TableRow{
Object: runtime.RawExtension{Object: obj},
}
parentRefName := "<none>"
if obj.Spec.ParentRef != nil {
gr := schema.GroupResource{
Group: obj.Spec.ParentRef.Group,
Resource: obj.Spec.ParentRef.Resource,
}
parentRefName = strings.ToLower(gr.String())
if obj.Spec.ParentRef.Namespace != "" {
parentRefName += "/" + obj.Spec.ParentRef.Namespace
}
parentRefName += "/" + obj.Spec.ParentRef.Name
}
age := translateTimestampSince(obj.CreationTimestamp)
row.Cells = append(row.Cells, obj.Name, parentRefName, age)
return []metav1.TableRow{row}, nil
}
func printIPAddressList(list *networking.IPAddressList, options printers.GenerateOptions) ([]metav1.TableRow, error) {
rows := make([]metav1.TableRow, 0, len(list.Items))
for i := range list.Items {
r, err := printIPAddress(&list.Items[i], options)
if err != nil {
return nil, err
}
rows = append(rows, r...)
}
return rows, nil
}
func printScale(obj *autoscaling.Scale, options printers.GenerateOptions) ([]metav1.TableRow, error) {
row := metav1.TableRow{
Object: runtime.RawExtension{Object: obj},

View File

@ -6482,3 +6482,83 @@ func TestPrintClusterCIDRList(t *testing.T) {
}
}
}
func TestPrintIPAddress(t *testing.T) {
ip := networking.IPAddress{
ObjectMeta: metav1.ObjectMeta{
Name: "192.168.2.2",
CreationTimestamp: metav1.Time{Time: time.Now().AddDate(-10, 0, 0)},
},
Spec: networking.IPAddressSpec{
ParentRef: &networking.ParentReference{
Group: "mygroup",
Resource: "myresource",
Namespace: "mynamespace",
Name: "myname",
},
},
}
// Columns: Name, ParentRef, Age
expected := []metav1.TableRow{{Cells: []interface{}{"192.168.2.2", "myresource.mygroup/mynamespace/myname", "10y"}}}
rows, err := printIPAddress(&ip, printers.GenerateOptions{})
if err != nil {
t.Fatalf("Error generating table rows for IPAddress: %#v", err)
}
rows[0].Object.Object = nil
if !reflect.DeepEqual(expected, rows) {
t.Errorf("mismatch: %s", diff.ObjectReflectDiff(expected, rows))
}
}
func TestPrintIPAddressList(t *testing.T) {
ipList := networking.IPAddressList{
Items: []networking.IPAddress{
{
ObjectMeta: metav1.ObjectMeta{
Name: "192.168.2.2",
CreationTimestamp: metav1.Time{Time: time.Now().AddDate(-10, 0, 0)},
},
Spec: networking.IPAddressSpec{
ParentRef: &networking.ParentReference{
Group: "mygroup",
Resource: "myresource",
Namespace: "mynamespace",
Name: "myname",
},
},
}, {
ObjectMeta: metav1.ObjectMeta{
Name: "2001:db8::2",
CreationTimestamp: metav1.Time{Time: time.Now().AddDate(-5, 0, 0)},
},
Spec: networking.IPAddressSpec{
ParentRef: &networking.ParentReference{
Group: "mygroup2",
Resource: "myresource2",
Namespace: "mynamespace2",
Name: "myname2",
},
},
},
},
}
// Columns: Name, ParentRef, Age
expected := []metav1.TableRow{
{Cells: []interface{}{"192.168.2.2", "myresource.mygroup/mynamespace/myname", "10y"}},
{Cells: []interface{}{"2001:db8::2", "myresource2.mygroup2/mynamespace2/myname2", "5y1d"}},
}
rows, err := printIPAddressList(&ipList, printers.GenerateOptions{})
if err != nil {
t.Fatalf("Error generating table rows for IPAddress: %#v", err)
}
for i := range rows {
rows[i].Object.Object = nil
}
if !reflect.DeepEqual(expected, rows) {
t.Errorf("mismatch: %s", diff.ObjectReflectDiff(expected, rows))
}
}