kubectl describe IPAddress

Change-Id: I201c2980830058c92b66e2386bfe8bedd8f0dcd4
This commit is contained in:
Antonio Ojea 2023-01-02 15:27:05 +00:00
parent c36562dfd7
commit 53dbf1752b
2 changed files with 89 additions and 0 deletions

View File

@ -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

View File

@ -5686,6 +5686,54 @@ Events: <none>` + "\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: <none>
Annotations: <none>
Parent Reference:
Group: mygroup
Resource: myresource
Namespace: mynamespace
Name: myname
Events: <none>` + "\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(