Fix output extra comma

When running 'kubectl get service', I get a extra comma in result:
'EXTERNAL-IP:,172.16.0.12', but except: 'EXTERNAL-IP:172.16.0.12'.
This commit is contained in:
FengyunPan
2017-06-24 00:10:33 +08:00
parent a97a9f9dd5
commit b9c1848fbd
2 changed files with 121 additions and 6 deletions

View File

@@ -715,8 +715,12 @@ func getServiceExternalIP(svc *api.Service, wide bool) string {
case api.ServiceTypeLoadBalancer: case api.ServiceTypeLoadBalancer:
lbIps := loadBalancerStatusStringer(svc.Status.LoadBalancer, wide) lbIps := loadBalancerStatusStringer(svc.Status.LoadBalancer, wide)
if len(svc.Spec.ExternalIPs) > 0 { if len(svc.Spec.ExternalIPs) > 0 {
result := append(strings.Split(lbIps, ","), svc.Spec.ExternalIPs...) results := []string{}
return strings.Join(result, ",") if len(lbIps) > 0 {
results = append(results, strings.Split(lbIps, ",")...)
}
results = append(results, svc.Spec.ExternalIPs...)
return strings.Join(results, ",")
} }
if len(lbIps) > 0 { if len(lbIps) > 0 {
return lbIps return lbIps

View File

@@ -2320,6 +2320,8 @@ func TestPrintPodShowLabels(t *testing.T) {
} }
func TestPrintService(t *testing.T) { func TestPrintService(t *testing.T) {
single_ExternalIP := []string{"80.11.12.10"}
mul_ExternalIP := []string{"80.11.12.10", "80.11.12.11"}
tests := []struct { tests := []struct {
service api.Service service api.Service
expect string expect string
@@ -2331,8 +2333,10 @@ func TestPrintService(t *testing.T) {
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Type: api.ServiceTypeClusterIP, Type: api.ServiceTypeClusterIP,
Ports: []api.ServicePort{ Ports: []api.ServicePort{
{Protocol: "tcp", {
Port: 2233}, Protocol: "tcp",
Port: 2233,
},
}, },
ClusterIP: "10.9.8.7", ClusterIP: "10.9.8.7",
}, },
@@ -2340,13 +2344,14 @@ func TestPrintService(t *testing.T) {
"test1\tClusterIP\t10.9.8.7\t<none>\t2233/tcp\t<unknown>\n", "test1\tClusterIP\t10.9.8.7\t<none>\t2233/tcp\t<unknown>\n",
}, },
{ {
// Test name, cluster ip, port:nodePort with protocol // Test NodePort service
api.Service{ api.Service{
ObjectMeta: metav1.ObjectMeta{Name: "test2"}, ObjectMeta: metav1.ObjectMeta{Name: "test2"},
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
Type: api.ServiceTypeNodePort, Type: api.ServiceTypeNodePort,
Ports: []api.ServicePort{ Ports: []api.ServicePort{
{Protocol: "tcp", {
Protocol: "tcp",
Port: 8888, Port: 8888,
NodePort: 9999, NodePort: 9999,
}, },
@@ -2356,6 +2361,112 @@ func TestPrintService(t *testing.T) {
}, },
"test2\tNodePort\t10.9.8.7\t<none>\t8888:9999/tcp\t<unknown>\n", "test2\tNodePort\t10.9.8.7\t<none>\t8888:9999/tcp\t<unknown>\n",
}, },
{
// Test LoadBalancer service
api.Service{
ObjectMeta: metav1.ObjectMeta{Name: "test3"},
Spec: api.ServiceSpec{
Type: api.ServiceTypeLoadBalancer,
Ports: []api.ServicePort{
{
Protocol: "tcp",
Port: 8888,
},
},
ClusterIP: "10.9.8.7",
},
},
"test3\tLoadBalancer\t10.9.8.7\t<pending>\t8888/tcp\t<unknown>\n",
},
{
// Test LoadBalancer service with single ExternalIP and no LoadBalancerStatus
api.Service{
ObjectMeta: metav1.ObjectMeta{Name: "test4"},
Spec: api.ServiceSpec{
Type: api.ServiceTypeLoadBalancer,
Ports: []api.ServicePort{
{
Protocol: "tcp",
Port: 8888,
},
},
ClusterIP: "10.9.8.7",
ExternalIPs: single_ExternalIP,
},
},
"test4\tLoadBalancer\t10.9.8.7\t80.11.12.10\t8888/tcp\t<unknown>\n",
},
{
// Test LoadBalancer service with single ExternalIP
api.Service{
ObjectMeta: metav1.ObjectMeta{Name: "test5"},
Spec: api.ServiceSpec{
Type: api.ServiceTypeLoadBalancer,
Ports: []api.ServicePort{
{
Protocol: "tcp",
Port: 8888,
},
},
ClusterIP: "10.9.8.7",
ExternalIPs: single_ExternalIP,
},
Status: api.ServiceStatus{
LoadBalancer: api.LoadBalancerStatus{
Ingress: []api.LoadBalancerIngress{
{
IP: "3.4.5.6",
Hostname: "test.cluster.com",
},
},
},
},
},
"test5\tLoadBalancer\t10.9.8.7\t3.4.5.6,80.11.12.10\t8888/tcp\t<unknown>\n",
},
{
// Test LoadBalancer service with mul ExternalIPs
api.Service{
ObjectMeta: metav1.ObjectMeta{Name: "test6"},
Spec: api.ServiceSpec{
Type: api.ServiceTypeLoadBalancer,
Ports: []api.ServicePort{
{
Protocol: "tcp",
Port: 8888,
},
},
ClusterIP: "10.9.8.7",
ExternalIPs: mul_ExternalIP,
},
Status: api.ServiceStatus{
LoadBalancer: api.LoadBalancerStatus{
Ingress: []api.LoadBalancerIngress{
{
IP: "2.3.4.5",
Hostname: "test.cluster.local",
},
{
IP: "3.4.5.6",
Hostname: "test.cluster.com",
},
},
},
},
},
"test6\tLoadBalancer\t10.9.8.7\t2.3.4.5,3.4.5.6,80.11.12.10,80.11.12.11\t8888/tcp\t<unknown>\n",
},
{
// Test ExternalName service
api.Service{
ObjectMeta: metav1.ObjectMeta{Name: "test7"},
Spec: api.ServiceSpec{
Type: api.ServiceTypeExternalName,
ExternalName: "my.database.example.com",
},
},
"test7\tExternalName\t<none>\tmy.database.example.com\t<none>\t<unknown>\n",
},
} }
buf := bytes.NewBuffer([]byte{}) buf := bytes.NewBuffer([]byte{})