-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcontroller.go
167 lines (147 loc) · 4.22 KB
/
controller.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
package disruptors
import (
"context"
"fmt"
"sync"
"time"
"github.com/grafana/xk6-disruptor/pkg/internal/version"
"github.com/grafana/xk6-disruptor/pkg/kubernetes/helpers"
corev1 "k8s.io/api/core/v1"
)
// AgentController defines the interface for controlling agents in a set of targets
type AgentController interface {
// InjectDisruptorAgent injects the Disruptor agent in the target pods
InjectDisruptorAgent(ctx context.Context) error
// ExecCommand executes a command in the targets of the AgentController and reports any error
ExecCommand(ctx context.Context, cmd []string) error
// Targets retrieves the names of the target of the controller
Targets(ctx context.Context) ([]string, error)
// Visit allows executing a different command on each target returned by a visiting function
Visit(ctx context.Context, visitor func(target corev1.Pod) ([]string, error)) error
}
// AgentController controls de agents in a set of target pods
type agentController struct {
helper helpers.PodHelper
namespace string
targets []corev1.Pod
timeout time.Duration
}
// InjectDisruptorAgent injects the Disruptor agent in the target pods
func (c *agentController) InjectDisruptorAgent(ctx context.Context) error {
var (
rootUser = int64(0)
rootGroup = int64(0)
runAsNonRoot = false
)
agentContainer := corev1.EphemeralContainer{
EphemeralContainerCommon: corev1.EphemeralContainerCommon{
Name: "xk6-agent",
Image: version.AgentImage(),
ImagePullPolicy: corev1.PullIfNotPresent,
SecurityContext: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{
Add: []corev1.Capability{"NET_ADMIN"},
},
RunAsUser: &rootUser,
RunAsGroup: &rootGroup,
RunAsNonRoot: &runAsNonRoot,
},
TTY: true,
Stdin: true,
},
}
var wg sync.WaitGroup
// ensure errors channel has enough space to avoid blocking gorutines
errors := make(chan error, len(c.targets))
for _, pod := range c.targets {
wg.Add(1)
// attach each container asynchronously
go func(podName string) {
defer wg.Done()
err := c.helper.AttachEphemeralContainer(
ctx,
podName,
agentContainer,
helpers.AttachOptions{
Timeout: c.timeout,
IgnoreIfExists: true,
},
)
if err != nil {
errors <- err
}
}(pod.Name)
}
wg.Wait()
select {
case err := <-errors:
return err
default:
return nil
}
}
// ExecCommand executes a command in the targets of the AgentController and reports any error
func (c *agentController) ExecCommand(ctx context.Context, cmd []string) error {
// visit each target with the same command
return c.Visit(ctx, func(corev1.Pod) ([]string, error) {
return cmd, nil
})
}
// Visit allows executing a different command on each target returned by a visiting function
func (c *agentController) Visit(_ context.Context, visitor func(corev1.Pod) ([]string, error)) error {
var wg sync.WaitGroup
// ensure errors channel has enough space to avoid blocking gorutines
errors := make(chan error, len(c.targets))
for _, pod := range c.targets {
wg.Add(1)
// attach each container asynchronously
go func(pod corev1.Pod) {
// get the command to execute in the target
cmd, err := visitor(pod)
if err != nil {
errors <- fmt.Errorf("error building command for pod %s: %w", pod.Name, err)
}
_, stderr, err := c.helper.Exec(pod.Name, "xk6-agent", cmd, []byte{})
if err != nil {
errors <- fmt.Errorf("error invoking agent: %w \n%s", err, string(stderr))
}
wg.Done()
}(pod)
}
wg.Wait()
select {
case err := <-errors:
return err
default:
return nil
}
}
// Targets retrieves the list of names of the target pods
func (c *agentController) Targets(_ context.Context) ([]string, error) {
names := []string{}
for _, p := range c.targets {
names = append(names, p.Name)
}
return names, nil
}
// NewAgentController creates a new controller for a list of target pods
func NewAgentController(
_ context.Context,
helper helpers.PodHelper,
namespace string,
targets []corev1.Pod,
timeout time.Duration,
) AgentController {
if timeout == 0 {
timeout = 30 * time.Second
}
if timeout < 0 {
timeout = 0
}
return &agentController{
helper: helper,
namespace: namespace,
targets: targets,
timeout: timeout,
}
}