-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_set.go
260 lines (226 loc) · 6.85 KB
/
node_set.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
255
256
257
258
259
260
package dispatcher
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"sync"
"github.com/celestiaorg/knuu/pkg/names"
"github.com/celestiaorg/testwave/pkg/constants"
"github.com/celestiaorg/testwave/pkg/message"
"github.com/celestiaorg/testwave/pkg/playbook"
"github.com/celestiaorg/testwave/pkg/worker"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
)
type dependentServicesResponse struct {
redisPort int32
redisIP, minioEndpoint string
}
func (d *Dispatcher) DeployNodeSet(ctx context.Context, wg *sync.WaitGroup, ns *playbook.NodeSet) {
defer wg.Done()
pod, err := d.createPodForNodeSet(ctx, ns)
if err != nil {
logrus.Errorf("Error creating pod for NodeSet %s: %v", ns.UID, err)
return
}
_, err = d.Clientset.CoreV1().Pods(d.Namespace).Create(ctx, pod, metav1.CreateOptions{})
if err != nil {
logrus.Errorf("Error deploying pod for NodeSet %s : %v", ns.UID, err)
return
}
logrus.Infof("NodeSet `%s` pod deployed", ns.UID)
}
func (d *Dispatcher) setupDependentServices(ctx context.Context) (*dependentServicesResponse, error) {
var (
res = &dependentServicesResponse{}
wg sync.WaitGroup
minioErr, minioEpErr error
redisErr, redisIpErr error
)
// run these two independent tasks in two different go routines
// for sake of optimization
func() {
wg.Add(1)
defer wg.Done()
if redisErr = d.deployRedis(ctx); redisErr != nil {
return
}
res.redisIP, res.redisPort, redisIpErr = d.RedisIPPort(ctx)
}()
func() {
wg.Add(1)
defer wg.Done()
if minioErr = d.deployMinio(ctx); minioErr != nil {
return
}
res.minioEndpoint, minioEpErr = d.getMinioEndpoint(ctx)
}()
wg.Wait()
for _, err := range []error{redisErr, minioErr, minioEpErr, redisIpErr} {
if err != nil {
return nil, err
}
}
return res, nil
}
func (d *Dispatcher) createPodForNodeSet(ctx context.Context, nodeSet *playbook.NodeSet) (*v1.Pod, error) {
if nodeSet == nil {
return nil, ErrNilNodeSet
}
if len(nodeSet.Workers) == 0 {
return nil, ErrNoWorkersInNodeSet.Wrap(fmt.Errorf("NodeSet %s", nodeSet.UID))
}
depServices, err := d.setupDependentServices(ctx)
if err != nil {
return nil, err
}
defaultEnvs := []v1.EnvVar{
{Name: worker.EnvTestPlan, Value: d.Playbook.Name()},
{Name: worker.EnvPodUID, Value: nodeSet.UID},
{Name: worker.EnvMinioEndpoint, Value: depServices.minioEndpoint},
{Name: worker.EnvMinioAccessKey, Value: minioAccessKey},
{Name: worker.EnvMinioSecretKey, Value: minioSecretKey},
{Name: message.EnvRedisAddr, Value: fmt.Sprintf("%s:%d", depServices.redisIP, depServices.redisPort)},
{Name: message.EnvRedisDB, Value: RedisDB},
{Name: message.EnvRedisPassword, Value: RedisPassword},
}
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: nodeSet.UID,
Namespace: d.Namespace,
},
Spec: v1.PodSpec{
RestartPolicy: v1.RestartPolicyNever,
},
}
for i, w := range nodeSet.Workers {
if w.UID == "" {
w.UID = fmt.Sprintf("%s-w%d", nodeSet.UID, i)
}
wEnvs := defaultEnvs
wEnvs = append(wEnvs, v1.EnvVar{Name: worker.EnvWorkerUID, Value: w.UID})
for k, v := range w.Envs {
wEnvs = append(wEnvs, v1.EnvVar{
Name: k,
Value: v,
})
}
pod, vols, err := d.AddFilesToPod(ctx, pod, w.Files)
if err != nil {
return nil, ErrAddingFileToPod.Wrap(err)
}
wc := v1.Container{
Name: w.UID,
Image: d.WorkerImage, // All workers in a nodeset share the same image
Env: wEnvs,
Args: []string{constants.DefaultBinPath, worker.Cmd},
VolumeMounts: vols,
// We need these privileges to allow BitTwister to shape the traffic
SecurityContext: &v1.SecurityContext{
Capabilities: &v1.Capabilities{Add: []v1.Capability{"NET_ADMIN"}},
Privileged: ptr.To[bool](true),
},
}
pod.Spec.Containers = append(pod.Spec.Containers, wc)
}
return pod, nil
}
// AddFilesToPod adds files to a pod and returns the updated pod and the volume mounts.
// The files are added to the pod as a ConfigMap.
// The mount points are determined by the `values` of the filesMap.
// Please note that, the files must be small in size, as they are stored in the ConfigMap.
// For large files, we should use either Minio or copy them directly into the docker image.
func (m *Dispatcher) AddFilesToPod(ctx context.Context, pod *v1.Pod, filesMap map[string]string) (*v1.Pod, []v1.VolumeMount, error) {
// first group files by their mount points to cover
// multiple files in the same mount directory
gFilesMap := groupMountPoints(filesMap)
vols := []v1.VolumeMount{}
for mountDir, fMaps := range gFilesMap {
configMapMountPoint := mountDir
configMapName, err := names.NewRandomK8(pod.Name + "-confmap")
if err != nil {
return nil, nil, err
}
configMap := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: configMapName,
Namespace: m.Namespace,
},
BinaryData: map[string][]byte{},
}
for srcPath, mountPath := range fMaps {
filename := filepath.Base(mountPath)
content, err := fileContent(srcPath)
if err != nil {
return nil, nil, err
}
configMap.BinaryData[filename] = content
}
_, err = m.Clientset.CoreV1().ConfigMaps(m.Namespace).Create(ctx, configMap, metav1.CreateOptions{})
if err != nil {
return nil, nil, err
}
pod.Spec.Volumes = append(pod.Spec.Volumes,
v1.Volume{
Name: configMapName, // we might change this in the future
VolumeSource: v1.VolumeSource{
ConfigMap: &v1.ConfigMapVolumeSource{
LocalObjectReference: v1.LocalObjectReference{
Name: configMapName,
},
},
},
})
vols = append(vols, v1.VolumeMount{
Name: configMapName,
MountPath: configMapMountPoint,
})
}
return pod, vols, nil
}
func fileContent(path string) ([]byte, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return io.ReadAll(f)
}
// groupMountPoints groups mount points by their parent directory.
// This is useful for creating a single volume for each directory.
// For example, if we have the following files:
//
// {
// "/home/user1/file1.txt": "/opt/file1.txt",
// "/home/user1/file2.txt": "/opt/file2.txt",
// "/home/user2/file3.txt": "/opt/file3.txt",
// "/home/user2/file4.txt": "/etc/file4.txt",
// }
//
// Then the result will be:
//
// {
// "/opt/": {
// "/home/user1/file1.txt": "/opt/file1.txt",
// "/home/user1/file2.txt": "/opt/file2.txt",
// "/home/user2/file3.txt": "/opt/file3.txt",
// },
// "/etc/": {
// "/home/user2/file4.txt": "/etc/file4.txt",
// },
// }
func groupMountPoints(filesMap map[string]string) map[string]map[string]string {
resMap := make(map[string]map[string]string)
for srcPath, mountPath := range filesMap {
mountDir := filepath.Dir(mountPath)
if resMap[mountDir] == nil {
resMap[mountDir] = make(map[string]string)
}
resMap[mountDir][srcPath] = mountPath
}
return resMap
}