Merge pull request #50071 from xiangpengzhao/fix-des-svc

Automatic merge from submit-queue

Display healthcheck nodeport and other fields in describe service

**What this PR does / why we need it**:
Some fields such as `HealthCheckNodePort` are not displayed currently. This PR fixes it.

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #
Update: found this when tracing #49999

**Special notes for your reviewer**:
/sig cli network

**Release note**:

```release-note
NONE
```
This commit is contained in:
Kubernetes Submit Queue 2017-08-05 20:53:19 -07:00 committed by GitHub
commit 9a277fba7d
2 changed files with 50 additions and 2 deletions

View File

@ -2006,6 +2006,9 @@ func describeService(service *api.Service, endpoints *api.Endpoints, events *api
if len(service.Spec.ExternalIPs) > 0 {
w.Write(LEVEL_0, "External IPs:\t%v\n", strings.Join(service.Spec.ExternalIPs, ","))
}
if service.Spec.LoadBalancerIP != "" {
w.Write(LEVEL_0, "IP:\t%s\n", service.Spec.LoadBalancerIP)
}
if service.Spec.ExternalName != "" {
w.Write(LEVEL_0, "External Name:\t%s\n", service.Spec.ExternalName)
}
@ -2021,12 +2024,26 @@ func describeService(service *api.Service, endpoints *api.Endpoints, events *api
name = "<unset>"
}
w.Write(LEVEL_0, "Port:\t%s\t%d/%s\n", name, sp.Port, sp.Protocol)
if sp.TargetPort.Type == intstr.Type(intstr.Int) {
w.Write(LEVEL_0, "TargetPort:\t%d/%s\n", sp.TargetPort.IntVal, sp.Protocol)
} else {
w.Write(LEVEL_0, "TargetPort:\t%d/%s\n", sp.TargetPort.StrVal, sp.Protocol)
}
if sp.NodePort != 0 {
w.Write(LEVEL_0, "NodePort:\t%s\t%d/%s\n", name, sp.NodePort, sp.Protocol)
}
w.Write(LEVEL_0, "Endpoints:\t%s\n", formatEndpoints(endpoints, sets.NewString(sp.Name)))
}
w.Write(LEVEL_0, "Session Affinity:\t%s\n", service.Spec.SessionAffinity)
if service.Spec.ExternalTrafficPolicy != "" {
w.Write(LEVEL_0, "External Traffic Policy:\t%s\n", service.Spec.ExternalTrafficPolicy)
}
if service.Spec.HealthCheckNodePort != 0 {
w.Write(LEVEL_0, "HealthCheck NodePort:\t%d\n", service.Spec.HealthCheckNodePort)
}
if len(service.Spec.LoadBalancerSourceRanges) > 0 {
w.Write(LEVEL_0, "LoadBalancer Source Ranges:\t%v\n", strings.Join(service.Spec.LoadBalancerSourceRanges, ","))
}
if events != nil {
DescribeEvents(events, w)
}

View File

@ -172,15 +172,46 @@ func TestDescribeService(t *testing.T) {
Name: "bar",
Namespace: "foo",
},
Spec: api.ServiceSpec{
Type: api.ServiceTypeLoadBalancer,
Ports: []api.ServicePort{{
Name: "port-tcp",
Port: 8080,
Protocol: api.ProtocolTCP,
TargetPort: intstr.FromInt(9527),
NodePort: 31111,
}},
Selector: map[string]string{"blah": "heh"},
ClusterIP: "1.2.3.4",
LoadBalancerIP: "5.6.7.8",
SessionAffinity: "None",
ExternalTrafficPolicy: "Local",
HealthCheckNodePort: 32222,
},
})
expectedElements := []string{
"Name", "bar",
"Namespace", "foo",
"Selector", "blah=heh",
"Type", "LoadBalancer",
"IP", "1.2.3.4",
"Port", "port-tcp", "8080/TCP",
"TargetPort", "9527/TCP",
"NodePort", "port-tcp", "31111/TCP",
"Session Affinity", "None",
"External Traffic Policy", "Local",
"HealthCheck NodePort", "32222",
}
c := &describeClient{T: t, Namespace: "foo", Interface: fake}
d := ServiceDescriber{c}
out, err := d.Describe("foo", "bar", printers.DescriberSettings{ShowEvents: true})
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !strings.Contains(out, "Labels:") || !strings.Contains(out, "bar") {
t.Errorf("unexpected out: %s", out)
for _, expected := range expectedElements {
if !strings.Contains(out, expected) {
t.Errorf("expected to find %q in output: %q", expected, out)
}
}
}