-
-
Notifications
You must be signed in to change notification settings - Fork 377
/
core.go
254 lines (213 loc) · 6.29 KB
/
core.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Package core provides functionality for managing core functionalities in Keploy.
package core
import (
"context"
"errors"
"fmt"
"sync"
"golang.org/x/sync/errgroup"
"go.keploy.io/server/v2/pkg/core/app"
"go.keploy.io/server/v2/pkg/models"
"go.keploy.io/server/v2/utils"
"go.uber.org/zap"
)
type Core struct {
Proxy // embedding the Proxy interface to transfer the proxy methods to the core object
Hooks // embedding the Hooks interface to transfer the hooks methods to the core object
Tester // embedding the Tester interface to transfer the tester methods to the core object
logger *zap.Logger
id utils.AutoInc
apps sync.Map
proxyStarted bool
}
func New(logger *zap.Logger, hook Hooks, proxy Proxy, tester Tester) *Core {
return &Core{
logger: logger,
Hooks: hook,
Proxy: proxy,
Tester: tester,
}
}
func (c *Core) Setup(ctx context.Context, cmd string, opts models.SetupOptions) (uint64, error) {
// create a new app and store it in the map
id := uint64(c.id.Next())
a := app.NewApp(c.logger, id, cmd, app.Options{
DockerNetwork: opts.DockerNetwork,
Container: opts.Container,
DockerDelay: opts.DockerDelay,
})
c.apps.Store(id, a)
err := a.Setup(ctx)
if err != nil {
utils.LogError(c.logger, err, "failed to setup app")
return 0, err
}
return id, nil
}
func (c *Core) getApp(id uint64) (*app.App, error) {
a, ok := c.apps.Load(id)
if !ok {
return nil, fmt.Errorf("app with id:%v not found", id)
}
// type assertion on the app
h, ok := a.(*app.App)
if !ok {
return nil, fmt.Errorf("failed to type assert app with id:%v", id)
}
return h, nil
}
func (c *Core) Hook(ctx context.Context, id uint64, opts models.HookOptions) error {
hookErr := errors.New("failed to hook into the app")
a, err := c.getApp(id)
if err != nil {
utils.LogError(c.logger, err, "failed to get app")
return hookErr
}
isDocker := false
appKind := a.Kind(ctx)
//check if the app is docker/docker-compose or native
if appKind == utils.Docker || appKind == utils.DockerCompose {
isDocker = true
}
select {
case <-ctx.Done():
return ctx.Err()
default:
}
g, ok := ctx.Value(models.ErrGroupKey).(*errgroup.Group)
if !ok {
return errors.New("failed to get the error group from the context")
}
// create a new error group for the hooks
hookErrGrp, _ := errgroup.WithContext(ctx)
hookCtx := context.WithoutCancel(ctx) //so that main context doesn't cancel the hookCtx to control the lifecycle of the hooks
hookCtx, hookCtxCancel := context.WithCancel(hookCtx)
hookCtx = context.WithValue(hookCtx, models.ErrGroupKey, hookErrGrp)
// create a new error group for the proxy
proxyErrGrp, _ := errgroup.WithContext(ctx)
proxyCtx := context.WithoutCancel(ctx) //so that main context doesn't cancel the proxyCtx to control the lifecycle of the proxy
proxyCtx, proxyCtxCancel := context.WithCancel(proxyCtx)
proxyCtx = context.WithValue(proxyCtx, models.ErrGroupKey, proxyErrGrp)
g.Go(func() error {
<-ctx.Done()
proxyCtxCancel()
err = proxyErrGrp.Wait()
if err != nil {
utils.LogError(c.logger, err, "failed to stop the proxy")
}
hookCtxCancel()
err := hookErrGrp.Wait()
if err != nil {
utils.LogError(c.logger, err, "failed to unload the hooks")
}
return nil
})
//load hooks
err = c.Hooks.Load(hookCtx, id, HookCfg{
AppID: id,
Pid: 0,
IsDocker: isDocker,
KeployIPV4: a.KeployIPv4Addr(),
})
if err != nil {
utils.LogError(c.logger, err, "failed to load hooks")
return hookErr
}
if c.proxyStarted {
c.logger.Debug("Proxy already started")
return nil
}
select {
case <-ctx.Done():
return ctx.Err()
default:
}
// TODO: Hooks can be loaded multiple times but proxy should be started only once
// if there is another containerized app, then we need to pass new (ip:port) of proxy to the eBPF
// as the network namespace is different for each container and so is the keploy/proxy IP to communicate with the app.
// start proxy
err = c.Proxy.StartProxy(proxyCtx, ProxyOptions{
DNSIPv4Addr: a.KeployIPv4Addr(),
//DnsIPv6Addr: ""
})
if err != nil {
utils.LogError(c.logger, err, "failed to start proxy")
return hookErr
}
c.proxyStarted = true
// For keploy test bench
if opts.EnableTesting {
// enable testing in the app
a.EnableTesting = true
a.Mode = opts.Mode
// Setting up the test bench
err := c.Tester.Setup(ctx, models.TestingOptions{Mode: opts.Mode})
if err != nil {
utils.LogError(c.logger, err, "error while setting up the test bench environment")
return errors.New("failed to setup the test bench")
}
}
return nil
}
func (c *Core) Run(ctx context.Context, id uint64, _ models.RunOptions) models.AppError {
a, err := c.getApp(id)
if err != nil {
utils.LogError(c.logger, err, "failed to get app")
return models.AppError{AppErrorType: models.ErrInternal, Err: err}
}
runAppErrGrp, runAppCtx := errgroup.WithContext(ctx)
inodeErrCh := make(chan error, 1)
appErrCh := make(chan models.AppError, 1)
inodeChan := make(chan uint64, 1) //send inode to the hook
defer func() {
err := runAppErrGrp.Wait()
defer close(inodeErrCh)
defer close(inodeChan)
if err != nil {
utils.LogError(c.logger, err, "failed to stop the app")
}
}()
runAppErrGrp.Go(func() error {
defer utils.Recover(c.logger)
if a.Kind(ctx) == utils.Native {
return nil
}
select {
case inode := <-inodeChan:
err := c.Hooks.SendInode(ctx, id, inode)
if err != nil {
utils.LogError(c.logger, err, "")
inodeErrCh <- errors.New("failed to send inode to the kernel")
}
case <-ctx.Done():
return nil
}
return nil
})
runAppErrGrp.Go(func() error {
defer utils.Recover(c.logger)
defer close(appErrCh)
appErr := a.Run(runAppCtx, inodeChan)
if appErr.Err != nil {
utils.LogError(c.logger, appErr.Err, "error while running the app")
appErrCh <- appErr
}
return nil
})
select {
case <-runAppCtx.Done():
return models.AppError{AppErrorType: models.ErrCtxCanceled, Err: nil}
case appErr := <-appErrCh:
return appErr
case inodeErr := <-inodeErrCh:
return models.AppError{AppErrorType: models.ErrInternal, Err: inodeErr}
}
}
func (c *Core) GetAppIP(_ context.Context, id uint64) (string, error) {
a, err := c.getApp(id)
if err != nil {
utils.LogError(c.logger, err, "failed to get app")
return "", err
}
return a.ContainerIPv4Addr(), nil
}