From 9be7ad1c5376dc46e2d826ea7d00b2887aea0da6 Mon Sep 17 00:00:00 2001 From: Antonio Ojea Date: Mon, 2 Jan 2023 15:27:05 +0000 Subject: [PATCH] kubectl describe IPAddress Change-Id: I201c2980830058c92b66e2386bfe8bedd8f0dcd4 Kubernetes-commit: 53dbf1752b070dcfe11399b3dd874fa8616851b3 --- pkg/describe/describe.go | 41 ++++++++++++++++++++++++++++++ pkg/describe/describe_test.go | 48 +++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/pkg/describe/describe.go b/pkg/describe/describe.go index 32141a7a1..8b134868c 100644 --- a/pkg/describe/describe.go +++ b/pkg/describe/describe.go @@ -216,6 +216,7 @@ func describerMap(clientConfig *rest.Config) (map[schema.GroupKind]ResourceDescr {Group: networkingv1.GroupName, Kind: "Ingress"}: &IngressDescriber{c}, {Group: networkingv1.GroupName, Kind: "IngressClass"}: &IngressClassDescriber{c}, {Group: networkingv1alpha1.GroupName, Kind: "ClusterCIDR"}: &ClusterCIDRDescriber{c}, + {Group: networkingv1alpha1.GroupName, Kind: "IPAddress"}: &IPAddressDescriber{c}, {Group: batchv1.GroupName, Kind: "Job"}: &JobDescriber{c}, {Group: batchv1.GroupName, Kind: "CronJob"}: &CronJobDescriber{c}, {Group: batchv1beta1.GroupName, Kind: "CronJob"}: &CronJobDescriber{c}, @@ -2853,6 +2854,46 @@ func (c *ClusterCIDRDescriber) describeClusterCIDRV1alpha1(cc *networkingv1alpha }) } +// IPAddressDescriber generates information about an IPAddress. +type IPAddressDescriber struct { + client clientset.Interface +} + +func (c *IPAddressDescriber) Describe(namespace, name string, describerSettings DescriberSettings) (string, error) { + var events *corev1.EventList + + ipV1alpha1, err := c.client.NetworkingV1alpha1().IPAddresses().Get(context.TODO(), name, metav1.GetOptions{}) + if err == nil { + if describerSettings.ShowEvents { + events, _ = searchEvents(c.client.CoreV1(), ipV1alpha1, describerSettings.ChunkSize) + } + return c.describeIPAddressV1alpha1(ipV1alpha1, events) + } + return "", err +} + +func (c *IPAddressDescriber) describeIPAddressV1alpha1(ip *networkingv1alpha1.IPAddress, events *corev1.EventList) (string, error) { + return tabbedString(func(out io.Writer) error { + w := NewPrefixWriter(out) + w.Write(LEVEL_0, "Name:\t%v\n", ip.Name) + printLabelsMultiline(w, "Labels", ip.Labels) + printAnnotationsMultiline(w, "Annotations", ip.Annotations) + + if ip.Spec.ParentRef != nil { + w.Write(LEVEL_0, "Parent Reference:\n") + w.Write(LEVEL_1, "Group:\t%v\n", ip.Spec.ParentRef.Group) + w.Write(LEVEL_1, "Resource:\t%v\n", ip.Spec.ParentRef.Resource) + w.Write(LEVEL_1, "Namespace:\t%v\n", ip.Spec.ParentRef.Namespace) + w.Write(LEVEL_1, "Name:\t%v\n", ip.Spec.ParentRef.Name) + } + + if events != nil { + DescribeEvents(events, w) + } + return nil + }) +} + // ServiceDescriber generates information about a service. type ServiceDescriber struct { clientset.Interface diff --git a/pkg/describe/describe_test.go b/pkg/describe/describe_test.go index a4d1669fd..8841c0ef1 100644 --- a/pkg/describe/describe_test.go +++ b/pkg/describe/describe_test.go @@ -5686,6 +5686,54 @@ Events: ` + "\n", } } +func TestDescribeIPAddress(t *testing.T) { + + testcases := map[string]struct { + input *fake.Clientset + output string + }{ + "IPAddress v1alpha1": { + input: fake.NewSimpleClientset(&networkingv1alpha1.IPAddress{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo.123", + }, + Spec: networkingv1alpha1.IPAddressSpec{ + ParentRef: &networkingv1alpha1.ParentReference{ + Group: "mygroup", + Resource: "myresource", + Namespace: "mynamespace", + Name: "myname", + }, + }, + }), + + output: `Name: foo.123 +Labels: +Annotations: +Parent Reference: + Group: mygroup + Resource: myresource + Namespace: mynamespace + Name: myname +Events: ` + "\n", + }, + } + + for name, tc := range testcases { + t.Run(name, func(t *testing.T) { + c := &describeClient{T: t, Namespace: "foo", Interface: tc.input} + d := IPAddressDescriber{c} + out, err := d.Describe("bar", "foo.123", DescriberSettings{ShowEvents: true}) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if out != tc.output { + t.Errorf("expected :\n%s\nbut got output:\n%s diff:\n%s", tc.output, out, cmp.Diff(tc.output, out)) + } + }) + } +} + func TestControllerRef(t *testing.T) { var replicas int32 = 1 f := fake.NewSimpleClientset(