Change:
<!-- This form is for bug reports and feature requests ONLY! If you're looking for help check and the . --Is this a BUG REPORT or FEATURE REQUEST?: What happened: when using , the TargetPort is wrong What you expected to happen: if the targetPort is string, show the name correctly. Environment: Kubernetes version (use ): Cloud provider or hardware configuration**: OS (e.g. from /etc/os-release): Kernel (e.g. ): Install tools: Others:
could you paste more details about the Kubernetes Version and the yaml you used to create the service? I tried to reproduce this error but it worked fine in my environment.
use the master branch. yaml output
I'm not sure if the targetPort could be set to string type, looks like the targetPort here makes no sense of port1 for it doesn't point to an actual port. I think we have to validate this value rather than displaying the string. WDYT?
It can be a string, you can see this
thanks for your ref, I will try to fix this and pls let me know if any new progress :)
I have commit a pr
ok, thanks~

Discussion:
if sp.TargetPort.Type == intstr.Type(intstr.Int) { w.Write(LEVEL_0, "TargetPort:t%d/%sn", sp.TargetPort.IntVal, sp.Protocol) } else { <del> w.Write(LEVEL_0, "TargetPort:t%d/%sn", sp.TargetPort.StrVal, sp.Protocol) </del> <ins> w.Write(LEVEL_0, "TargetPort:t%s/%sn", sp.TargetPort.StrVal, sp.Protocol) </ins> } if sp.NodePort != 0 { w.Write(LEVEL_0, "NodePort:t%st%d/%sn", name, sp.NodePort, sp.Protocol)

} func TestDescribeService(t *testing.T) { <del> fake := fake.NewSimpleClientset(&api.Service{ ObjectMeta: metav1.ObjectMeta{ Name:      "bar", Namespace: "foo", </del> <ins> testCases := []struct { service *api.Service expect  []string }{ { service: &api.Service{ ObjectMeta: metav1.ObjectMeta{ 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, }, }, expect: []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", }, </ins> }, <del> 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, </del> <ins> { service: &api.Service{ ObjectMeta: metav1.ObjectMeta{ Name:      "bar", Namespace: "foo", }, Spec: api.ServiceSpec{ Type: api.ServiceTypeLoadBalancer, Ports: []api.ServicePort{{ Name:       "port-tcp", Port:       8080, Protocol:   api.ProtocolTCP, TargetPort: intstr.FromString("targetPort"), NodePort:   31111, }}, Selector:              map[string]string{"blah": "heh"}, ClusterIP:             "1.2.3.4", LoadBalancerIP:        "5.6.7.8", SessionAffinity:       "None", ExternalTrafficPolicy: "Local", HealthCheckNodePort:   32222, }, }, expect: []string{ "Name", "bar", "Namespace", "foo", "Selector", "blah=heh", "Type", "LoadBalancer", "IP", "1.2.3.4", "Port", "port-tcp", "8080/TCP", "TargetPort", "targetPort/TCP", "NodePort", "port-tcp", "31111/TCP", "Session Affinity", "None", "External Traffic Policy", "Local", "HealthCheck NodePort", "32222", }, </ins> }, <del> }) 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) </del> } <del> for _, expected := range expectedElements { if !strings.Contains(out, expected) { t.Errorf("expected to find %q in output: %q", expected, out) </del> <ins> for _, testCase := range testCases { fake := fake.NewSimpleClientset(testCase.service) 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) } for _, expected := range testCase.expect { if !strings.Contains(out, expected) { t.Errorf("expected to find %q in output: %q", expected, out) } </ins> } } }