Merge pull request #33552 from floreks/kubectl-node-external-ip

Automatic merge from submit-queue

kubectl: Add external ip information to node when '-o wide' is used

<!--  Thanks for sending a pull request!  Here are some tips for you:
1. If this is your first time, read our contributor guidelines https://github.com/kubernetes/kubernetes/blob/master/CONTRIBUTING.md and developer guide https://github.com/kubernetes/kubernetes/blob/master/docs/devel/development.md
2. If you want *faster* PR reviews, read how: https://github.com/kubernetes/kubernetes/blob/master/docs/devel/faster_reviews.md
3. Follow the instructions for writing a release note: https://github.com/kubernetes/kubernetes/blob/master/docs/devel/pull-requests.md#release-notes
-->

**Which issue this PR fixes**: fixes #33457

**Special notes for your reviewer**: 
1. Is it possible to expose multiple external ips on the node?
2. Should this be supported or first one be taken like now? 
3. Should more node address types be shown?

I'll add tests if solution is approved.

**Release note**:
<!--  Steps to write your release note:
1. Use the release-note-* labels to set the release note state (if you have access) 
2. Enter your extended release note in the below block; leaving it blank means using the PR title as the release note. If no release note is required, just write `NONE`. 
-->
```release-note
kubectl: Add external ip information to node when '-o wide' is used
```
This commit is contained in:
Kubernetes Submit Queue 2016-10-07 21:10:27 -07:00 committed by GitHub
commit 882d431f96
2 changed files with 69 additions and 0 deletions

View File

@ -1474,6 +1474,12 @@ func printNode(node *api.Node, w io.Writer, options PrintOptions) error {
if _, err := fmt.Fprintf(w, "%s\t%s\t%s", name, strings.Join(status, ","), translateTimestamp(node.CreationTimestamp)); err != nil {
return err
}
if options.Wide {
if _, err := fmt.Fprintf(w, "\t%s", getNodeExternalIP(node)); err != nil {
return err
}
}
// Display caller specify column labels first.
if _, err := fmt.Fprint(w, AppendLabels(node.Labels, options.ColumnLabels)); err != nil {
return err
@ -1482,6 +1488,17 @@ func printNode(node *api.Node, w io.Writer, options PrintOptions) error {
return err
}
// Returns first external ip of the node or "<none>" if none is found.
func getNodeExternalIP(node *api.Node) string {
for _, address := range node.Status.Addresses {
if address.Type == api.NodeExternalIP {
return address.Address
}
}
return "<none>"
}
func printNodeList(list *api.NodeList, w io.Writer, options PrintOptions) error {
for _, node := range list.Items {
if err := printNode(&node, w, options); err != nil {
@ -2179,6 +2196,9 @@ func formatWideHeaders(wide bool, t reflect.Type) []string {
if t.String() == "*extensions.ReplicaSet" || t.String() == "*extensions.ReplicaSetList" {
return []string{"CONTAINER(S)", "IMAGE(S)", "SELECTOR"}
}
if t.String() == "*api.Node" || t.String() == "*api.NodeList" {
return []string{"EXTERNAL-IP"}
}
}
return nil
}

View File

@ -692,6 +692,55 @@ func TestPrintNodeStatus(t *testing.T) {
}
}
func TestPrintNodeExternalIP(t *testing.T) {
printer := NewHumanReadablePrinter(PrintOptions{
ColumnLabels: []string{},
Wide: true,
})
table := []struct {
node api.Node
externalIP string
}{
{
node: api.Node{
ObjectMeta: api.ObjectMeta{Name: "foo1"},
Status: api.NodeStatus{Addresses: []api.NodeAddress{{Type: api.NodeExternalIP, Address: "1.1.1.1"}}},
},
externalIP: "1.1.1.1",
},
{
node: api.Node{
ObjectMeta: api.ObjectMeta{Name: "foo2"},
Status: api.NodeStatus{Addresses: []api.NodeAddress{{Type: api.NodeInternalIP, Address: "1.1.1.1"}}},
},
externalIP: "<none>",
},
{
node: api.Node{
ObjectMeta: api.ObjectMeta{Name: "foo3"},
Status: api.NodeStatus{Addresses: []api.NodeAddress{
{Type: api.NodeLegacyHostIP, Address: "1.1.1.1"},
{Type: api.NodeExternalIP, Address: "2.2.2.2"},
{Type: api.NodeInternalIP, Address: "3.3.3.3"},
{Type: api.NodeExternalIP, Address: "4.4.4.4"},
}},
},
externalIP: "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.externalIP) {
t.Fatalf("Expect printing node %s with external ip %#v, got: %#v", test.node.Name, test.externalIP, buffer.String())
}
}
}
func contains(fields []string, field string) bool {
for _, v := range fields {
if v == field {