/
utils.go
118 lines (106 loc) · 3.04 KB
/
utils.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package local
import (
"context"
"fmt"
"os"
"os/exec"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/solo-io/solo-kit/pkg/api/v1/clients"
v1 "github.com/solo-io/squash/pkg/api/v1"
"github.com/solo-io/squash/pkg/utils"
)
func GetPortForwardCmd(targetName, targetNamespace string, localPort, targetRemotePort int) *exec.Cmd {
portSpec := fmt.Sprintf("%v:%v", localPort, targetRemotePort)
cmd := exec.Command("kubectl", "port-forward", targetName, portSpec, "-n", targetNamespace)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
return cmd
}
func getPortForwardWithRandomLocalCmd(targetName, targetNamespace string, targetRemotePort int) string {
portSpec := fmt.Sprintf(":%v", targetRemotePort)
return strings.Join([]string{"kubectl", "port-forward", targetName, portSpec, "-n", targetNamespace}, " ")
}
func GetParticularDebugger(dbgtype string) Local {
var g GdbInterface
var d DLV
var j JavaInterface
var jp JavaPortInterface
var p PythonInterface
var n NodeJsDebugger
switch dbgtype {
case "dlv":
return &d
case "gdb":
return &g
case "java":
return &j
case "java-port":
return &jp
case "nodejs":
return &n
case "nodejs8":
return &n
case "python":
return &p
default:
return nil
}
}
func GetDebugPortFromCrd(daName, daNamespace string) (int, error) {
// TODO - all of our ports should be gotten from the crd. As is, it is possible that the random port chosen from ip_addr:0 could return 1236 - slim chance but may as well handle it
da, err := waitForDebugServerAddress(daName, daNamespace)
if err != nil {
return 0, fmt.Errorf("Could not read debug attachment %v in namespace %v: %v", daName, daNamespace, err)
}
port, err := da.GetPortFromDebugServerAddress()
if err != nil {
return 0, err
}
return port, nil
}
func waitForDebugServerAddress(daName, daNamespace string) (*v1.DebugAttachment, error) {
// TODO(mitchdraft) - pass this (and all ctx's from startup)
ctx := context.Background()
daClient, err := utils.GetBasicDebugAttachmentClient(ctx)
if err != nil {
log.WithField("err", err).Error("getting debug attachment client")
return &v1.DebugAttachment{}, err
}
dac, errc, err := daClient.Watch(daNamespace, clients.WatchOpts{Ctx: ctx})
if err != nil {
return &v1.DebugAttachment{}, err
}
// TODO - make timeout configurable
ctx, cancel := context.WithTimeout(ctx, 300*time.Second)
defer cancel()
for {
select {
case err, _ := <-errc:
return &v1.DebugAttachment{}, err
case <-ctx.Done():
return &v1.DebugAttachment{}, fmt.Errorf("Could not find debug spec in the allotted time.")
case das, ok := <-dac:
if !ok {
return &v1.DebugAttachment{}, fmt.Errorf("could not read watch channel")
}
if len(das) == 0 {
continue
}
da := checkDebugAttachmentsForAddress(das, daName)
if da != nil {
return da, nil
}
}
}
}
func checkDebugAttachmentsForAddress(das v1.DebugAttachmentList, daName string) *v1.DebugAttachment {
for _, da := range das {
if da.Metadata.Name == daName && da.DebugServerAddress != "" {
return da
}
}
return nil
}