mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 23:37:01 +00:00
Merge pull request #85511 from oke-py/port-forward-named-port
Fix a bug in port-forward: named port not working with service
This commit is contained in:
commit
92a14f4f7e
@ -184,10 +184,10 @@ func translateServicePortToTargetPort(ports []string, svc corev1.Service, pod co
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if int32(portnum) != containerPort {
|
||||
if int32(portnum) != containerPort || localPort == "" {
|
||||
translated = append(translated, fmt.Sprintf("%s:%d", localPort, containerPort))
|
||||
} else {
|
||||
translated = append(translated, port)
|
||||
translated = append(translated, fmt.Sprintf("%d", containerPort))
|
||||
}
|
||||
}
|
||||
return translated, nil
|
||||
|
@ -413,6 +413,36 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
|
||||
translated: []string{":8080", ":8443"},
|
||||
err: false,
|
||||
},
|
||||
{
|
||||
name: "test success 4 (named service port and named pod container port)",
|
||||
svc: corev1.Service{
|
||||
Spec: corev1.ServiceSpec{
|
||||
Ports: []corev1.ServicePort{
|
||||
{
|
||||
Port: 80,
|
||||
Name: "http",
|
||||
TargetPort: intstr.FromString("http"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
pod: corev1.Pod{
|
||||
Spec: corev1.PodSpec{
|
||||
Containers: []corev1.Container{
|
||||
{
|
||||
Ports: []corev1.ContainerPort{
|
||||
{
|
||||
Name: "http",
|
||||
ContainerPort: int32(80)},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
ports: []string{"http"},
|
||||
translated: []string{"80"},
|
||||
err: false,
|
||||
},
|
||||
{
|
||||
name: "test success (targetPort omitted)",
|
||||
svc: corev1.Service{
|
||||
|
Loading…
Reference in New Issue
Block a user