-
Notifications
You must be signed in to change notification settings - Fork 8
/
kubeproxy.go
59 lines (50 loc) · 1.14 KB
/
kubeproxy.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
package kubexp
import (
"bytes"
"fmt"
"os"
"time"
)
type containerPort struct {
name string
port int
}
type portMapping struct {
destPort int
containerPort containerPort
}
type portforwardProxy struct {
mapping portMapping
pod string
namespace string
startTime time.Time
logChannel chan string
log bytes.Buffer
}
func newPortforwardProxy(namespace, podName string, ports portMapping) *portforwardProxy {
p := &portforwardProxy{namespace: namespace, pod: podName, mapping: ports, logChannel: make(chan string), startTime: time.Now()}
return p
}
func (p *portforwardProxy) execute() error {
go func() {
cmd := kubectl(backend.context.Name, "port-forward", "-n", p.namespace, p.pod, fmt.Sprintf("%v:%v", p.mapping.destPort, p.mapping.containerPort.port))
cmd.Output()
}()
return nil
}
func (p *portforwardProxy) getPid() int {
return pidOfPort(p.mapping.destPort)
}
func (p *portforwardProxy) stop() error {
pid := pidOfPort(p.mapping.destPort)
if pid != -1 {
process, err := os.FindProcess(pid)
if err != nil {
return err
}
if err := process.Kill(); err != nil {
return err
}
}
return nil
}