From 98f7c3919e12d7596f54c93d256a53d4f38e54b1 Mon Sep 17 00:00:00 2001 From: Justin Santa Barbara Date: Mon, 31 Oct 2016 09:55:37 -0400 Subject: [PATCH] kubectl: show node label if defined We are moving towards marking master nodes as tainted, and not necessarily unschedulable. Further now we encourage users to cordon nodes, marking them unschedulable. Thus the reliance on "Unschedulable" is not really a great indicator for the master. So, recognize the existing node 'role' markers, and surface them where Unschedulable is (in the status). We recognize: * a kubernetes.io/role label * a kubeadm.alpha.kubernetes.io/role label Fix #33533 --- pkg/kubectl/describe.go | 1 + pkg/kubectl/resource_printer.go | 20 +++++++++++++++++++ pkg/kubectl/resource_printer_test.go | 30 ++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/pkg/kubectl/describe.go b/pkg/kubectl/describe.go index 98281ad122ba8..1bb649c90f171 100644 --- a/pkg/kubectl/describe.go +++ b/pkg/kubectl/describe.go @@ -1789,6 +1789,7 @@ func (d *NodeDescriber) Describe(namespace, name string, describerSettings Descr func describeNode(node *api.Node, nodeNonTerminatedPodsList *api.PodList, events *api.EventList, canViewPods bool) (string, error) { return tabbedString(func(out io.Writer) error { fmt.Fprintf(out, "Name:\t%s\n", node.Name) + fmt.Fprintf(out, "Role:\t%s\n", findNodeRole(node)) printLabelsMultiline(out, "Labels", node.Labels) printTaintsInAnnotationMultiline(out, "Taints", node.Annotations) fmt.Fprintf(out, "CreationTimestamp:\t%s\n", node.CreationTimestamp.Time.Format(time.RFC1123Z)) diff --git a/pkg/kubectl/resource_printer.go b/pkg/kubectl/resource_printer.go index cb136f8d192fc..ab3cbb03597cb 100644 --- a/pkg/kubectl/resource_printer.go +++ b/pkg/kubectl/resource_printer.go @@ -1491,6 +1491,10 @@ func printNode(node *api.Node, w io.Writer, options PrintOptions) error { if node.Spec.Unschedulable { status = append(status, "SchedulingDisabled") } + role := findNodeRole(node) + if role != "" { + status = append(status, role) + } if _, err := fmt.Fprintf(w, "%s\t%s\t%s", name, strings.Join(status, ","), translateTimestamp(node.CreationTimestamp)); err != nil { return err @@ -1520,6 +1524,22 @@ func getNodeExternalIP(node *api.Node) string { return "" } +// findNodeRole returns the role of a given node, or "" if none found. +// The role is determined by looking in order for: +// * a kubernetes.io/role label +// * a kubeadm.alpha.kubernetes.io/role label +// If no role is found, ("", nil) is returned +func findNodeRole(node *api.Node) string { + if role := node.Labels[unversioned.NodeLabelRole]; role != "" { + return role + } + if role := node.Labels[unversioned.NodeLabelKubeadmAlphaRole]; role != "" { + return role + } + // No role found + return "" +} + func printNodeList(list *api.NodeList, w io.Writer, options PrintOptions) error { for _, node := range list.Items { if err := printNode(&node, w, options); err != nil { diff --git a/pkg/kubectl/resource_printer_test.go b/pkg/kubectl/resource_printer_test.go index c18ead9e242f3..08e71b289b3b0 100644 --- a/pkg/kubectl/resource_printer_test.go +++ b/pkg/kubectl/resource_printer_test.go @@ -678,6 +678,36 @@ func TestPrintNodeStatus(t *testing.T) { }, status: "Unknown,SchedulingDisabled", }, + { + node: api.Node{ + ObjectMeta: api.ObjectMeta{ + Name: "foo10", + Labels: map[string]string{"kubernetes.io/role": "master"}, + }, + Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: api.NodeReady, Status: api.ConditionTrue}}}, + }, + status: "Ready,master", + }, + { + node: api.Node{ + ObjectMeta: api.ObjectMeta{ + Name: "foo11", + Labels: map[string]string{"kubernetes.io/role": "node"}, + }, + Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: api.NodeReady, Status: api.ConditionTrue}}}, + }, + status: "Ready,node", + }, + { + node: api.Node{ + ObjectMeta: api.ObjectMeta{ + Name: "foo12", + Labels: map[string]string{"kubeadm.alpha.kubernetes.io/role": "node"}, + }, + Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: api.NodeReady, Status: api.ConditionTrue}}}, + }, + status: "Ready,node", + }, } for _, test := range table {