Commit 10ebd659 authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

Merge pull request #34922 from zreigz/add-nodeport

Automatic merge from submit-queue Add NodePort value in kubectl output This PR enhances kubectl output after the command execution: `kubectl get svc`. It additionally shows the value of the NodePort. This PR is a response for this issue: https://github.com/kubernetes/kubernetes/issues/34100
parents 74bb64a8 b7da8747
...@@ -1101,6 +1101,9 @@ func makePortString(ports []api.ServicePort) string { ...@@ -1101,6 +1101,9 @@ func makePortString(ports []api.ServicePort) string {
for ix := range ports { for ix := range ports {
port := &ports[ix] port := &ports[ix]
pieces[ix] = fmt.Sprintf("%d/%s", port.Port, port.Protocol) pieces[ix] = fmt.Sprintf("%d/%s", port.Port, port.Protocol)
if port.NodePort > 0 {
pieces[ix] = fmt.Sprintf("%d:%d/%s", port.Port, port.NodePort, port.Protocol)
}
} }
return strings.Join(pieces, ",") return strings.Join(pieces, ",")
} }
......
...@@ -1572,3 +1572,53 @@ func TestPrintPodShowLabels(t *testing.T) { ...@@ -1572,3 +1572,53 @@ func TestPrintPodShowLabels(t *testing.T) {
buf.Reset() buf.Reset()
} }
} }
func TestPrintService(t *testing.T) {
tests := []struct {
service api.Service
expect string
}{
{
// Test name, cluster ip, port with protocol
api.Service{
ObjectMeta: api.ObjectMeta{Name: "test1"},
Spec: api.ServiceSpec{
Type: api.ServiceTypeClusterIP,
Ports: []api.ServicePort{
{Protocol: "tcp",
Port: 2233},
},
ClusterIP: "0.0.0.0",
},
},
"test1\t0.0.0.0\t<none>\t2233/tcp\t<unknown>\n",
},
{
// Test name, cluster ip, port:nodePort with protocol
api.Service{
ObjectMeta: api.ObjectMeta{Name: "test2"},
Spec: api.ServiceSpec{
Type: api.ServiceTypeClusterIP,
Ports: []api.ServicePort{
{Protocol: "tcp",
Port: 8888,
NodePort: 9999,
},
},
ClusterIP: "10.9.8.7",
},
},
"test2\t10.9.8.7\t<none>\t8888:9999/tcp\t<unknown>\n",
},
}
buf := bytes.NewBuffer([]byte{})
for _, test := range tests {
printService(&test.service, buf, PrintOptions{false, false, false, false, true, false, false, "", []string{}})
// We ignore time
if buf.String() != test.expect {
t.Fatalf("Expected: %s, got: %s %d", test.expect, buf.String(), strings.Compare(test.expect, buf.String()))
}
buf.Reset()
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment