mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-31 16:46:54 +00:00
kubectl: Add external ip information to node when '-o wide' is used
This commit is contained in:
@@ -1468,6 +1468,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
|
||||
@@ -1476,6 +1482,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 {
|
||||
@@ -2173,6 +2190,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
|
||||
}
|
||||
|
@@ -691,6 +691,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 {
|
||||
|
Reference in New Issue
Block a user