Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include Conditions in kubectl describe namespace #106219

Merged
merged 1 commit into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions staging/src/k8s.io/kubectl/pkg/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,14 +451,31 @@ func describeNamespace(namespace *corev1.Namespace, resourceQuotaList *corev1.Re
printLabelsMultiline(w, "Labels", namespace.Labels)
printAnnotationsMultiline(w, "Annotations", namespace.Annotations)
w.Write(LEVEL_0, "Status:\t%s\n", string(namespace.Status.Phase))

if len(namespace.Status.Conditions) > 0 {
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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a quite long message or reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, when leaving a review comment, explain what your concern is, and, if possible, give an example.

There are many functions in this package that use this same formatting. The writer interprets the tabs as separators, not as literal tabs. Here's what the output looks like with a long reasons and message:

Name:         example
        Labels:       <none>
        Annotations:  <none>
        Status:       Terminating
        Conditions:
          Type                             Status  LastTransitionTime               Reason                                                                                                                                                                                                             Message
          ----                             ------  ------------------               ------                                                                                                                                                                                                             -------
          NamespaceDeletionContentFailure  True    Wed, 15 Jan 2014 00:00:00 +0000  eexample reason example reason example reason example reason example reason example reason example reason example reason example reason example reason example reason example reason xample reason example reason  example message example message example message example message example message example message example message example message example message example message example message example message example message

        No resource quota.

        No LimitRange resource.

Copy link
Member

@kerthcet kerthcet Nov 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for your advices @dlipovetsky , the code looks good to me.

for _, c := range namespace.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 resourceQuotaList != nil {
w.Write(LEVEL_0, "\n")
DescribeResourceQuotas(resourceQuotaList, w)
}

if limitRangeList != nil {
w.Write(LEVEL_0, "\n")
DescribeLimitRanges(limitRangeList, w)
}

return nil
})
}
Expand Down
91 changes: 80 additions & 11 deletions staging/src/k8s.io/kubectl/pkg/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,19 +236,88 @@ func TestDescribeSecret(t *testing.T) {
}

func TestDescribeNamespace(t *testing.T) {
fake := fake.NewSimpleClientset(&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "myns",
exampleNamespaceName := "example"

testCases := []struct {
name string
namespace *corev1.Namespace
expect []string
}{
{
name: "no quotas or limit ranges",
namespace: &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: exampleNamespaceName,
},
Status: corev1.NamespaceStatus{
Phase: corev1.NamespaceActive,
},
},
expect: []string{
"Name",
exampleNamespaceName,
"Status",
string(corev1.NamespaceActive),
"No resource quota",
"No LimitRange resource.",
},
},
{
name: "has conditions",
namespace: &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: exampleNamespaceName,
},
Status: corev1.NamespaceStatus{
Phase: corev1.NamespaceTerminating,
Conditions: []corev1.NamespaceCondition{
{
LastTransitionTime: metav1.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)),
Message: "example message",
Reason: "example reason",
Status: corev1.ConditionTrue,
Type: corev1.NamespaceDeletionContentFailure,
},
},
},
},
expect: []string{
"Name",
exampleNamespaceName,
"Status",
string(corev1.NamespaceTerminating),
"Conditions",
"Type",
string(corev1.NamespaceDeletionContentFailure),
"Status",
string(corev1.ConditionTrue),
"Reason",
"example reason",
"Message",
"example message",
"No resource quota",
"No LimitRange resource.",
},
},
})
c := &describeClient{T: t, Namespace: "", Interface: fake}
d := NamespaceDescriber{c}
out, err := d.Describe("", "myns", DescriberSettings{ShowEvents: true})
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !strings.Contains(out, "myns") {
t.Errorf("unexpected out: %s", out)

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
fake := fake.NewSimpleClientset(testCase.namespace)
c := &describeClient{T: t, Namespace: "", Interface: fake}
d := NamespaceDescriber{c}

out, err := d.Describe("", testCase.namespace.Name, DescriberSettings{ShowEvents: true})
if err != nil {
t.Errorf("unexpected error: %v", err)
}

for _, expected := range testCase.expect {
if !strings.Contains(out, expected) {
t.Errorf("expected to find %q in output: %q", expected, out)
}
}
})
}
}

Expand Down