/
server.go
83 lines (73 loc) · 2.12 KB
/
server.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
package delve
import (
"context"
"fmt"
"os"
"github.com/foomo/gograpple/internal/exec"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type KubeDelveServer struct {
host string
port int
kubeCmd *exec.KubectlCmd
process *os.Process
}
func (kds KubeDelveServer) Host() string {
return kds.host
}
func (kds KubeDelveServer) Port() int {
return kds.port
}
func NewKubeDelveServer(l *logrus.Entry, namespace, host string, port int) *KubeDelveServer {
kubectl := exec.NewKubectlCommand()
kubectl.Logger(l).Quiet().Args("-n", namespace)
return &KubeDelveServer{host, port, kubectl, nil}
}
func (kds *KubeDelveServer) StartNoWait(ctx context.Context, pod, container string,
binDest string, binArgs []string, doContinue bool) {
cmd := kds.kubeCmd.ExecPod(pod, container, kds.getRunCmd(binDest, binArgs, doContinue))
cmd.PostStart(
func(p *os.Process) error {
kds.process = p
return nil
}).NoWait().Run(ctx)
<-cmd.Started()
}
func (kds *KubeDelveServer) Start(ctx context.Context, pod, container string,
binDest string, binArgs []string, doContinue bool) error {
cmd := kds.kubeCmd.ExecPod(pod, container, kds.getRunCmd(binDest, binArgs, doContinue))
// execute command to run dlv on container
out, err := cmd.PostStart(
func(p *os.Process) error {
kds.process = p
return nil
}).Run(ctx)
return errors.WithMessage(err, out)
}
// doContinue will start the execution without waiting for a client connection
func (kds KubeDelveServer) getRunCmd(binDest string, binArgs []string, doContinue bool) []string {
cmd := []string{
"dlv", "exec", binDest, "--headless", "--api-version=2", "--accept-multiclient",
"-r", "stdout:/proc/1/fd/1", "-r", "stderr:/proc/1/fd/1",
fmt.Sprintf("--listen=:%v", kds.port),
}
if doContinue {
cmd = append(cmd, "--continue")
}
// cmd = append(cmd, "--log")
if len(binArgs) > 0 {
cmd = append(cmd, "--")
cmd = append(cmd, binArgs...)
}
return cmd
}
func (kds *KubeDelveServer) Stop() error {
if kds.process == nil {
return fmt.Errorf("no process found, run Start first")
}
if err := kds.process.Release(); err != nil {
return err
}
return kds.process.Kill()
}