-
Notifications
You must be signed in to change notification settings - Fork 79
/
cuda-vector-add.go
310 lines (272 loc) · 9.61 KB
/
cuda-vector-add.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Package cudavectoradd implements tester for CUDA GPU Test.
package cudavectoradd
import (
"context"
"errors"
"fmt"
"io"
"reflect"
"strings"
"time"
eks_tester "github.com/aws/aws-k8s-tester/eks/tester"
"github.com/aws/aws-k8s-tester/eksconfig"
k8s_client "github.com/aws/aws-k8s-tester/pkg/k8s-client"
"github.com/aws/aws-k8s-tester/pkg/timeutil"
"go.uber.org/zap"
v1 "k8s.io/api/core/v1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/exec"
)
// Config defines Cuda-Vector-Add configuration.
type Config struct {
Logger *zap.Logger
LogWriter io.Writer
Stopc chan struct{}
EKSConfig *eksconfig.Config
K8SClient k8s_client.EKS
}
const (
podName = "cuda-vector-add"
appName = "cuda-vector-add"
)
var pkgName = reflect.TypeOf(tester{}).PkgPath()
func (ts *tester) Name() string { return pkgName }
func New(cfg Config) eks_tester.Tester {
cfg.Logger.Info("creating tester", zap.String("tester", pkgName))
return &tester{cfg: cfg}
}
type tester struct {
cfg Config
}
func (ts *tester) Create() (err error) {
if !ts.cfg.EKSConfig.IsEnabledAddOnCUDAVectorAdd() {
ts.cfg.Logger.Info("skipping tester.Create", zap.String("tester", pkgName))
return nil
}
if ts.cfg.EKSConfig.AddOnCUDAVectorAdd.Created {
ts.cfg.Logger.Info("skipping tester.Create", zap.String("tester", pkgName))
return nil
}
ts.cfg.EKSConfig.AddOnCUDAVectorAdd.Created = true
ts.cfg.EKSConfig.Sync()
createStart := time.Now()
defer func() {
createEnd := time.Now()
ts.cfg.EKSConfig.AddOnCUDAVectorAdd.TimeFrameCreate = timeutil.NewTimeFrame(createStart, createEnd)
ts.cfg.EKSConfig.Sync()
}()
if err = k8s_client.CreateNamespace(
ts.cfg.Logger,
ts.cfg.K8SClient.KubernetesClientSet(),
ts.cfg.EKSConfig.AddOnCUDAVectorAdd.Namespace,
); err != nil {
return err
}
if err = ts.createPod(); err != nil {
return err
}
if err = ts.checkPod(); err != nil {
return err
}
ts.cfg.Logger.Info("successfully created Pod", zap.String("pod-name", podName))
return ts.cfg.EKSConfig.Sync()
}
func (ts *tester) createPod() (err error) {
pod := &v1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Pod",
},
ObjectMeta: metav1.ObjectMeta{
Name: podName,
},
Spec: v1.PodSpec{
RestartPolicy: v1.RestartPolicyOnFailure,
Containers: []v1.Container{
{
Name: appName,
// https://github.com/kubernetes/kubernetes/blob/v1.7.11/test/images/nvidia-cuda/Dockerfile
Image: "k8s.gcr.io/cuda-vector-add:v0.1",
Resources: v1.ResourceRequirements{
Limits: map[v1.ResourceName]resource.Quantity{
v1.ResourceName("nvidia.com/gpu"): resource.MustParse("1"),
},
},
},
},
},
}
ts.cfg.Logger.Info("creating Pod", zap.String("pod-name", podName))
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
_, err = ts.cfg.K8SClient.
KubernetesClientSet().
CoreV1().
Pods(ts.cfg.EKSConfig.AddOnCUDAVectorAdd.Namespace).
Create(ctx, pod, metav1.CreateOptions{})
cancel()
if err != nil {
return fmt.Errorf("failed to create Pod (%v)", err)
}
ts.cfg.Logger.Info("created Pod")
return ts.cfg.EKSConfig.Sync()
}
func (ts *tester) checkPod() error {
descArgsPods := []string{
ts.cfg.EKSConfig.KubectlPath,
"--kubeconfig=" + ts.cfg.EKSConfig.KubeConfigPath,
"--namespace=" + ts.cfg.EKSConfig.AddOnCUDAVectorAdd.Namespace,
"describe",
"pods/" + podName,
}
descCmdPods := strings.Join(descArgsPods, " ")
logArgs := []string{
ts.cfg.EKSConfig.KubectlPath,
"--kubeconfig=" + ts.cfg.EKSConfig.KubeConfigPath,
"--namespace=" + ts.cfg.EKSConfig.AddOnCUDAVectorAdd.Namespace,
"logs",
"pods/" + podName,
"--all-containers=true",
"--timestamps",
}
logsCmd := strings.Join(logArgs, " ")
ts.cfg.Logger.Info("checking Pod",
zap.String("pod-name", podName),
zap.String("container-name", appName),
zap.String("command-describe", descCmdPods),
zap.String("command-logs", logsCmd),
)
succeeded := false
retryStart, waitDur := time.Now(), 5*time.Minute
for time.Now().Sub(retryStart) < waitDur {
select {
case <-ts.cfg.Stopc:
return errors.New("cuda-vector-add pod check aborted")
case <-time.After(5 * time.Second):
}
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
output, err := exec.New().CommandContext(ctx, descArgsPods[0], descArgsPods[1:]...).CombinedOutput()
cancel()
out := string(output)
if err != nil {
ts.cfg.Logger.Warn("'kubectl describe' failed", zap.Error(err))
}
fmt.Fprintf(ts.cfg.LogWriter, "\n'%s' output:\n\n%s\n\n", descCmdPods, out)
ctx, cancel = context.WithTimeout(context.Background(), 15*time.Second)
output, err = exec.New().CommandContext(ctx, logArgs[0], logArgs[1:]...).CombinedOutput()
cancel()
out = string(output)
if err != nil {
ts.cfg.Logger.Warn("'kubectl logs' failed", zap.Error(err))
}
fmt.Fprintf(ts.cfg.LogWriter, "\n'%s' output:\n\n%s\n\n", logsCmd, out)
ctx, cancel = context.WithTimeout(context.Background(), 15*time.Second)
pout, err := ts.cfg.K8SClient.
KubernetesClientSet().
CoreV1().
Pods(ts.cfg.EKSConfig.AddOnCUDAVectorAdd.Namespace).
Get(
ctx,
podName,
metav1.GetOptions{},
)
cancel()
if err != nil {
ts.cfg.Logger.Info("failed to query Pod", zap.String("pod-name", podName), zap.Error(err))
continue
}
if pout.Status.Phase != v1.PodSucceeded {
ts.cfg.Logger.Warn("unexpected Pod phase", zap.String("pod-name", podName), zap.String("pod-phase", fmt.Sprintf("%v", pout.Status.Phase)))
time.Sleep(5 * time.Second)
continue
}
if !strings.Contains(out, "Test PASSED") || !strings.Contains(out, "[Vector addition") {
ts.cfg.Logger.Warn("unexpected logs output")
time.Sleep(5 * time.Second)
continue
}
succeeded = true
ts.cfg.Logger.Info("successfully checked Pod logs",
zap.String("pod-name", podName),
zap.String("container-name", appName),
)
break
}
if !succeeded {
return fmt.Errorf("failed to created Pod %q", podName)
}
return ts.cfg.EKSConfig.Sync()
}
/*
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 40s default-scheduler Successfully assigned eks-2020060315-bluesbw5mxnq-cuda-vector-add/cuda-vector-add to ip-192-168-93-246.us-west-2.compute.internal
Normal Pulling 39s kubelet, ip-192-168-93-246.us-west-2.compute.internal Pulling image "k8s.gcr.io/cuda-vector-add:v0.1"
Normal Pulled 18s kubelet, ip-192-168-93-246.us-west-2.compute.internal Successfully pulled image "k8s.gcr.io/cuda-vector-add:v0.1"
Normal Created 4s kubelet, ip-192-168-93-246.us-west-2.compute.internal Created container cuda-vector-add
Normal Started 3s kubelet, ip-192-168-93-246.us-west-2.compute.internal Started container cuda-vector-add
'/tmp/kubectl-test-v1.16.9 --kubeconfig=/tmp/leegyuho-test-eks.kubeconfig.yaml --namespace=eks-2020060315-bluesbw5mxnq-cuda-vector-add logs pods/cuda-vector-add --timestamps --all-containers=true' output:
2020-06-03T22:47:36.676558366Z [Vector addition of 50000 elements]
2020-06-03T22:47:36.676594518Z Copy input data from the host memory to the CUDA device
2020-06-03T22:47:36.676600024Z CUDA kernel launch with 196 blocks of 256 threads
2020-06-03T22:47:36.676604025Z Copy output data from the CUDA device to the host memory
2020-06-03T22:47:36.67660785Z Test PASSED
2020-06-03T22:47:36.676611883Z Done
Ref: https://docs.deep-hybrid-datacloud.eu/en/latest/technical/kubernetes/gpu-kubernetes-centos7.html#span-style-color-rgb-0-0-0-text-decoration-none-test-1-span-style-color-rgb-0-0-0-text-decoration-none-simple-vector-add-cuda8-span-span
It takes < 10 seconds to complete
$ kubectl apply -f vector-add.yaml
pod "vector-add" created
$ kubectl get pods/vector-add
NAME READY STATUS RESTARTS AGE
vector-add 0/1 Completed 0 4s
*/
func (ts *tester) Delete() error {
if !ts.cfg.EKSConfig.IsEnabledAddOnCUDAVectorAdd() {
ts.cfg.Logger.Info("skipping tester.Delete", zap.String("tester", pkgName))
return nil
}
if !ts.cfg.EKSConfig.AddOnCUDAVectorAdd.Created {
ts.cfg.Logger.Info("skipping tester.Delete", zap.String("tester", pkgName))
return nil
}
deleteStart := time.Now()
defer func() {
deleteEnd := time.Now()
ts.cfg.EKSConfig.AddOnCUDAVectorAdd.TimeFrameDelete = timeutil.NewTimeFrame(deleteStart, deleteEnd)
ts.cfg.EKSConfig.Sync()
}()
var errs []string
ts.cfg.Logger.Info("deleting Pod", zap.String("pod-name", podName))
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
err := ts.cfg.K8SClient.
KubernetesClientSet().
CoreV1().
Pods(ts.cfg.EKSConfig.AddOnCUDAVectorAdd.Namespace).
Delete(
ctx,
podName,
metav1.DeleteOptions{},
)
cancel()
if err != nil && !apierrs.IsNotFound(err) && !strings.Contains(err.Error(), "not found") {
ts.cfg.Logger.Warn("failed to delete", zap.Error(err))
return fmt.Errorf("failed to delete Pod (%v)", err)
}
ts.cfg.Logger.Info("deleted Pod", zap.String("pod-name", podName), zap.Error(err))
if err := k8s_client.DeleteNamespaceAndWait(
ts.cfg.Logger,
ts.cfg.K8SClient.KubernetesClientSet(),
ts.cfg.EKSConfig.AddOnCUDAVectorAdd.Namespace,
k8s_client.DefaultNamespaceDeletionInterval,
k8s_client.DefaultNamespaceDeletionTimeout,
k8s_client.WithForceDelete(true),
); err != nil {
errs = append(errs, fmt.Sprintf("failed to delete pod CudaVectorAdd namespace (%v)", err))
}
if len(errs) > 0 {
return errors.New(strings.Join(errs, ", "))
}
ts.cfg.EKSConfig.AddOnCUDAVectorAdd.Created = false
return ts.cfg.EKSConfig.Sync()
}