mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
fix kubectl port-forward for services with explicit local port
This commit is contained in:
parent
672aa55ee4
commit
ef2eaa4f8f
@ -185,7 +185,10 @@ func translateServicePortToTargetPort(ports []string, svc corev1.Service, pod co
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if int32(portnum) != containerPort || localPort == "" {
|
||||
// should fail when localPort is empty (=> use random local port)
|
||||
localportnum, err := strconv.Atoi(localPort)
|
||||
|
||||
if int32(portnum) != containerPort || localPort == "" || (int32(localportnum) != containerPort && err == nil) {
|
||||
translated = append(translated, fmt.Sprintf("%s:%d", localPort, containerPort))
|
||||
} else {
|
||||
translated = append(translated, fmt.Sprintf("%d", containerPort))
|
||||
|
@ -209,6 +209,35 @@ func TestTranslateServicePortToTargetPort(t *testing.T) {
|
||||
translated: []string{":8080"},
|
||||
err: false,
|
||||
},
|
||||
{
|
||||
name: "test success 1 (int port with explicit local port)",
|
||||
svc: corev1.Service{
|
||||
Spec: corev1.ServiceSpec{
|
||||
Ports: []corev1.ServicePort{
|
||||
{
|
||||
Port: 8080,
|
||||
TargetPort: intstr.FromInt(8080),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
pod: corev1.Pod{
|
||||
Spec: corev1.PodSpec{
|
||||
Containers: []corev1.Container{
|
||||
{
|
||||
Ports: []corev1.ContainerPort{
|
||||
{
|
||||
Name: "http",
|
||||
ContainerPort: int32(8080)},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
ports: []string{"8000:8080"},
|
||||
translated: []string{"8000:8080"},
|
||||
err: false,
|
||||
},
|
||||
{
|
||||
name: "test success 2 (clusterIP: None)",
|
||||
svc: corev1.Service{
|
||||
|
Loading…
Reference in New Issue
Block a user