Merge pull request #57623 from dixudx/kubectl_node_internalip

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

include node internal ip as additional information for kubectl

**What this PR does / why we need it**:
Node external ip is only available for cloud-based cluster.
This PR does extend showing node internal ip for baremental clusters, where external ip is always shown as `<none>`.

Before the change,

```
$ kubectl get node -o wide
NAME        STATUS    AGE       VERSION   EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION
server-01   Ready     21d       v1.9.0    <none>        Ubuntu 16.04.2 LTS   4.4.0-83-generic
server-02   Ready     21d       v1.9.0    <none>        Ubuntu 16.04.2 LTS   4.4.0-83-generic
```

After the change,

```
$ kubectl get node -o wide
NAME        STATUS    ROLES     AGE       VERSION   INTERNAL-IP      EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION     CONTAINER-RUNTIME
server-01   Ready     master    21d       v1.9.0    192.168.31.100   <none>        Ubuntu 16.04.2 LTS   4.4.0-83-generic   docker://1.13.1
server-02   Ready     <none>    21d       v1.9.0    192.168.31.101   <none>        Ubuntu 16.04.2 LTS   4.4.0-83-generic   docker://1.13.1
```

**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
Fixes #

**Special notes for your reviewer**:
/cc @kubernetes/sig-cli-pr-reviews 
**Release note**:

```release-note
include node internal ip as additional information for kubectl
```
This commit is contained in:
Kubernetes Submit Queue 2018-03-24 09:38:03 -07:00 committed by GitHub
commit 008dafe05a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 1 deletions

View File

@ -220,6 +220,7 @@ func AddHandlers(h printers.PrintHandler) {
{Name: "Roles", Type: "string", Description: "The roles of the node"},
{Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]},
{Name: "Version", Type: "string", Description: apiv1.NodeSystemInfo{}.SwaggerDoc()["kubeletVersion"]},
{Name: "Internal-IP", Type: "string", Priority: 1, Description: apiv1.NodeStatus{}.SwaggerDoc()["addresses"]},
{Name: "External-IP", Type: "string", Priority: 1, Description: apiv1.NodeStatus{}.SwaggerDoc()["addresses"]},
{Name: "OS-Image", Type: "string", Priority: 1, Description: apiv1.NodeSystemInfo{}.SwaggerDoc()["osImage"]},
{Name: "Kernel-Version", Type: "string", Priority: 1, Description: apiv1.NodeSystemInfo{}.SwaggerDoc()["kernelVersion"]},
@ -1138,7 +1139,7 @@ func printNode(obj *api.Node, options printers.PrintOptions) ([]metav1beta1.Tabl
if crVersion == "" {
crVersion = "<unknown>"
}
row.Cells = append(row.Cells, getNodeExternalIP(obj), osImage, kernelVersion, crVersion)
row.Cells = append(row.Cells, getNodeInternalIP(obj), getNodeExternalIP(obj), osImage, kernelVersion, crVersion)
}
return []metav1beta1.TableRow{row}, nil
@ -1155,6 +1156,17 @@ func getNodeExternalIP(node *api.Node) string {
return "<none>"
}
// Returns the internal IP of the node or "<none>" if none is found.
func getNodeInternalIP(node *api.Node) string {
for _, address := range node.Status.Addresses {
if address.Type == api.NodeInternalIP {
return address.Address
}
}
return "<none>"
}
// findNodeRoles returns the roles of a given node.
// The roles are determined by looking for:
// * a node-role.kubernetes.io/<role>="" label

View File

@ -1118,6 +1118,54 @@ func TestPrintNodeExternalIP(t *testing.T) {
}
}
func TestPrintNodeInternalIP(t *testing.T) {
printer := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{
Wide: true,
})
AddHandlers(printer)
table := []struct {
node api.Node
internalIP string
}{
{
node: api.Node{
ObjectMeta: metav1.ObjectMeta{Name: "foo1"},
Status: api.NodeStatus{Addresses: []api.NodeAddress{{Type: api.NodeInternalIP, Address: "1.1.1.1"}}},
},
internalIP: "1.1.1.1",
},
{
node: api.Node{
ObjectMeta: metav1.ObjectMeta{Name: "foo2"},
Status: api.NodeStatus{Addresses: []api.NodeAddress{{Type: api.NodeExternalIP, Address: "1.1.1.1"}}},
},
internalIP: "<none>",
},
{
node: api.Node{
ObjectMeta: metav1.ObjectMeta{Name: "foo3"},
Status: api.NodeStatus{Addresses: []api.NodeAddress{
{Type: api.NodeInternalIP, Address: "2.2.2.2"},
{Type: api.NodeExternalIP, Address: "3.3.3.3"},
{Type: api.NodeInternalIP, Address: "4.4.4.4"},
}},
},
internalIP: "2.2.2.2",
},
}
for _, test := range table {
buffer := &bytes.Buffer{}
err := printer.PrintObj(&test.node, buffer)
if err != nil {
t.Fatalf("An error occurred printing Node: %#v", err)
}
if !contains(strings.Fields(buffer.String()), test.internalIP) {
t.Fatalf("Expect printing node %s with internal ip %#v, got: %#v", test.node.Name, test.internalIP, buffer.String())
}
}
}
func contains(fields []string, field string) bool {
for _, v := range fields {
if v == field {