-
Notifications
You must be signed in to change notification settings - Fork 0
/
ports.go
96 lines (77 loc) · 2.76 KB
/
ports.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
Package forwarder contains a few functions that are borrowed from kubernetes/kubectl repo. They are private and cannot be used directly.
https://github.com/kubernetes/kubectl/blob/0b0920722212395d20fd0eecb5abf45ecb3e0cac/pkg/cmd/portforward/portforward.go
*/
package forwarder
import (
"fmt"
"strconv"
"strings"
corev1 "k8s.io/api/core/v1"
"k8s.io/kubectl/pkg/util"
)
// Translates the passed runtime object port mappings into ports that target the passed pod.
func (c Client) translatePorts(obj interface{}, pod *corev1.Pod, port string) (string, error) {
switch t := obj.(type) {
case *corev1.Service:
return translateServicePortToTargetPort(port, *t, *pod)
default:
return convertPodNamedPortToNumber(port, *pod)
}
}
// splitPort splits port string which is in form of [LOCAL PORT]:REMOTE PORT
// and returns local and remote ports separately.
func splitPort(port string) (local, remote string) {
parts := strings.Split(port, ":")
if len(parts) == 2 {
return parts[0], parts[1]
}
return parts[0], parts[0]
}
// Translates service port to target port
// It rewrites ports as needed if the Service port declares targetPort.
// It returns an error when a named targetPort can't find a match in the pod, or the Service did not declare
// the port.
func translateServicePortToTargetPort(port string, svc corev1.Service, pod corev1.Pod) (string, error) {
localPort, remotePort := splitPort(port)
portnum, err := strconv.ParseInt(remotePort, 10, 32)
if err != nil {
svcPort, err := util.LookupServicePortNumberByName(svc, remotePort)
if err != nil {
return "", err
}
portnum = int64(svcPort)
if localPort == remotePort {
localPort = strconv.FormatInt(portnum, 10)
}
}
containerPort, err := util.LookupContainerPortNumberByServicePort(svc, pod, int32(portnum))
if err != nil {
// can't resolve a named port, or Service did not declare this port, return an error
return "", err
}
// convert the resolved target port back to a string
remotePort = strconv.Itoa(int(containerPort))
if localPort != remotePort {
return fmt.Sprintf("%s:%s", localPort, remotePort), nil
}
return remotePort, nil
}
// convertPodNamedPortToNumber converts named ports into port numbers
// It returns an error when a named port can't be found in the pod containers.
func convertPodNamedPortToNumber(port string, pod corev1.Pod) (string, error) {
localPort, remotePort := splitPort(port)
containerPortStr := remotePort
_, err := strconv.Atoi(remotePort)
if err != nil {
containerPort, err := util.LookupContainerPortNumberByName(pod, remotePort)
if err != nil {
return "", err
}
containerPortStr = strconv.Itoa(int(containerPort))
}
if localPort != remotePort {
return fmt.Sprintf("%s:%s", localPort, containerPortStr), nil
}
return containerPortStr, nil
}