From 17964a895f45fbbc19c876309b25f1abcfa886ab Mon Sep 17 00:00:00 2001 From: Antonio Ojea Date: Mon, 15 Aug 2022 19:53:39 +0200 Subject: [PATCH 1/2] kubectl describe Change-Id: I0664e11a3a5549e1cc9602b22dcaf294200792a4 Kubernetes-commit: 7e77e8b21d1e26b0ee2e4f54684ddd80e4d29983 --- pkg/describe/describe.go | 56 +++++++++++++++++++++++++++++++++++ pkg/describe/describe_test.go | 41 +++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/pkg/describe/describe.go b/pkg/describe/describe.go index 2e0e2ec69..0dc009073 100644 --- a/pkg/describe/describe.go +++ b/pkg/describe/describe.go @@ -215,6 +215,7 @@ func describerMap(clientConfig *rest.Config) (map[schema.GroupKind]ResourceDescr {Group: networkingv1beta1.GroupName, Kind: "IngressClass"}: &IngressClassDescriber{c}, {Group: networkingv1.GroupName, Kind: "Ingress"}: &IngressDescriber{c}, {Group: networkingv1.GroupName, Kind: "IngressClass"}: &IngressClassDescriber{c}, + {Group: networkingv1alpha1.GroupName, Kind: "ServiceCIDR"}: &ServiceCIDRDescriber{c}, {Group: networkingv1alpha1.GroupName, Kind: "IPAddress"}: &IPAddressDescriber{c}, {Group: batchv1.GroupName, Kind: "Job"}: &JobDescriber{c}, {Group: batchv1.GroupName, Kind: "CronJob"}: &CronJobDescriber{c}, @@ -2844,6 +2845,61 @@ func (i *IngressClassDescriber) describeIngressClassV1(ic *networkingv1.IngressC }) } +// ServiceCIDRDescriber generates information about a ServiceCIDR. +type ServiceCIDRDescriber struct { + client clientset.Interface +} + +func (c *ServiceCIDRDescriber) Describe(namespace, name string, describerSettings DescriberSettings) (string, error) { + var events *corev1.EventList + + svcV1alpha1, err := c.client.NetworkingV1alpha1().ServiceCIDRs().Get(context.TODO(), name, metav1.GetOptions{}) + if err == nil { + if describerSettings.ShowEvents { + events, _ = searchEvents(c.client.CoreV1(), svcV1alpha1, describerSettings.ChunkSize) + } + return c.describeServiceCIDRV1alpha1(svcV1alpha1, events) + } + return "", err +} + +func (c *ServiceCIDRDescriber) describeServiceCIDRV1alpha1(svc *networkingv1alpha1.ServiceCIDR, events *corev1.EventList) (string, error) { + return tabbedString(func(out io.Writer) error { + w := NewPrefixWriter(out) + w.Write(LEVEL_0, "Name:\t%v\n", svc.Name) + printLabelsMultiline(w, "Labels", svc.Labels) + printAnnotationsMultiline(w, "Annotations", svc.Annotations) + + if svc.Spec.IPv4 != "" { + w.Write(LEVEL_0, "IPv4:\t%s\n", svc.Spec.IPv4) + } + + if svc.Spec.IPv6 != "" { + w.Write(LEVEL_0, "IPv6:\t%s\n", svc.Spec.IPv6) + } + + if len(svc.Status.Conditions) > 0 { + w.Write(LEVEL_0, "Status:\n") + w.Write(LEVEL_0, "Conditions:\n") + w.Write(LEVEL_1, "Type\tStatus\tLastTransitionTime\tReason\tMessage\n") + w.Write(LEVEL_1, "----\t------\t------------------\t------\t-------\n") + for _, c := range svc.Status.Conditions { + w.Write(LEVEL_1, "%v\t%v\t%s\t%v\t%v\n", + c.Type, + c.Status, + c.LastTransitionTime.Time.Format(time.RFC1123Z), + c.Reason, + c.Message) + } + } + + if events != nil { + DescribeEvents(events, w) + } + return nil + }) +} + // IPAddressDescriber generates information about an IPAddress. type IPAddressDescriber struct { client clientset.Interface diff --git a/pkg/describe/describe_test.go b/pkg/describe/describe_test.go index f78d961fd..93d711cd3 100644 --- a/pkg/describe/describe_test.go +++ b/pkg/describe/describe_test.go @@ -5932,6 +5932,47 @@ Events: ` + "\n", } } +func TestDescribeServiceCIDR(t *testing.T) { + + testcases := map[string]struct { + input *fake.Clientset + output string + }{ + "ServiceCIDR v1alpha1": { + input: fake.NewSimpleClientset(&networkingv1alpha1.ServiceCIDR{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo.123", + }, + Spec: networkingv1alpha1.ServiceCIDRSpec{ + IPv4: "10.1.0.0/16", + IPv6: "fd00:1:1::/64", + }, + }), + + output: `Name: foo.123 +Labels: +Annotations: +IPv4: 10.1.0.0/16 +IPv6: fd00:1:1::/64 +Events: ` + "\n", + }, + } + + for name, tc := range testcases { + t.Run(name, func(t *testing.T) { + c := &describeClient{T: t, Namespace: "foo", Interface: tc.input} + d := ServiceCIDRDescriber{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 TestDescribeIPAddress(t *testing.T) { testcases := map[string]struct { From 931e86795387fccbf2532731010f53a9bdce52e5 Mon Sep 17 00:00:00 2001 From: Antonio Ojea Date: Sun, 29 Oct 2023 17:55:03 +0000 Subject: [PATCH 2/2] describer: use new ServiceCIDR API Change-Id: Iff11c70f20bab3e55e4e569fb110ef25dd6dd97e Kubernetes-commit: 513fdb5422932e3234590d03c4b8d417e7544796 --- pkg/describe/describe.go | 8 +------- pkg/describe/describe_test.go | 38 +++++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/pkg/describe/describe.go b/pkg/describe/describe.go index 0dc009073..53ba5dfc9 100644 --- a/pkg/describe/describe.go +++ b/pkg/describe/describe.go @@ -2870,13 +2870,7 @@ func (c *ServiceCIDRDescriber) describeServiceCIDRV1alpha1(svc *networkingv1alph printLabelsMultiline(w, "Labels", svc.Labels) printAnnotationsMultiline(w, "Annotations", svc.Annotations) - if svc.Spec.IPv4 != "" { - w.Write(LEVEL_0, "IPv4:\t%s\n", svc.Spec.IPv4) - } - - if svc.Spec.IPv6 != "" { - w.Write(LEVEL_0, "IPv6:\t%s\n", svc.Spec.IPv6) - } + w.Write(LEVEL_0, "CIDRs:\t%v\n", strings.Join(svc.Spec.CIDRs, ", ")) if len(svc.Status.Conditions) > 0 { w.Write(LEVEL_0, "Status:\n") diff --git a/pkg/describe/describe_test.go b/pkg/describe/describe_test.go index 93d711cd3..09673bf0b 100644 --- a/pkg/describe/describe_test.go +++ b/pkg/describe/describe_test.go @@ -5944,16 +5944,46 @@ func TestDescribeServiceCIDR(t *testing.T) { Name: "foo.123", }, Spec: networkingv1alpha1.ServiceCIDRSpec{ - IPv4: "10.1.0.0/16", - IPv6: "fd00:1:1::/64", + CIDRs: []string{"10.1.0.0/16", "fd00:1:1::/64"}, }, }), output: `Name: foo.123 Labels: Annotations: -IPv4: 10.1.0.0/16 -IPv6: fd00:1:1::/64 +CIDRs: 10.1.0.0/16, fd00:1:1::/64 +Events: ` + "\n", + }, + "ServiceCIDR v1alpha1 IPv4": { + input: fake.NewSimpleClientset(&networkingv1alpha1.ServiceCIDR{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo.123", + }, + Spec: networkingv1alpha1.ServiceCIDRSpec{ + CIDRs: []string{"10.1.0.0/16"}, + }, + }), + + output: `Name: foo.123 +Labels: +Annotations: +CIDRs: 10.1.0.0/16 +Events: ` + "\n", + }, + "ServiceCIDR v1alpha1 IPv6": { + input: fake.NewSimpleClientset(&networkingv1alpha1.ServiceCIDR{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo.123", + }, + Spec: networkingv1alpha1.ServiceCIDRSpec{ + CIDRs: []string{"fd00:1:1::/64"}, + }, + }), + + output: `Name: foo.123 +Labels: +Annotations: +CIDRs: fd00:1:1::/64 Events: ` + "\n", }, }