-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
61 lines (50 loc) · 1.23 KB
/
config.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
package forwarder
import (
"strconv"
corev1 "k8s.io/api/core/v1"
"k8s.io/kubectl/pkg/scheme"
)
// Config contains the information required to satisfy a call to Forward.
type Config struct {
Pod *corev1.Pod
Port string
}
// GetLocalPort returns the local ports from the Config port mapping.
func (c Config) GetLocalPort() (port int, err error) {
localStr, _ := splitPort(c.Port)
local, err := strconv.ParseInt(localStr, 10, 64)
if err != nil {
return 0, err
}
return int(local), nil
}
// NewConfig interacts with the Kubernetes API to find a pod and ports suitable for forwarding.
func (c Client) NewConfig(resource string, portMap string) (*Config, error) {
namespace, _, err := c.userConfig.Namespace()
if err != nil {
return nil, err
}
obj, err := c.factory.NewBuilder().
WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).
ContinueOnError().
NamespaceParam(namespace).
DefaultNamespace().
ResourceNames("pods", resource).
Do().
Object()
if err != nil {
return nil, err
}
pod, err := c.getAttachablePod(obj)
if err != nil {
return nil, err
}
port, err := c.translatePorts(obj, pod, portMap)
if err != nil {
return nil, err
}
return &Config{
Pod: pod,
Port: port,
}, nil
}