/
cluster_k8s.go
1250 lines (1042 loc) 路 34.4 KB
/
cluster_k8s.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package runner
import (
"bufio"
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"math/rand"
"net"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/testground/sdk-go/ptypes"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/testground/sdk-go/runtime"
ss "github.com/testground/sdk-go/sync"
"github.com/testground/testground/pkg/api"
"github.com/testground/testground/pkg/aws"
"github.com/testground/testground/pkg/conv"
"github.com/testground/testground/pkg/healthcheck"
"github.com/testground/testground/pkg/logging"
"github.com/testground/testground/pkg/rpc"
"github.com/testground/testground/pkg/task"
"golang.org/x/sync/errgroup"
v1 "k8s.io/api/core/v1"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
lru "github.com/hashicorp/golang-lru"
"github.com/msoap/byline"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/remotecommand"
)
var (
_ api.Runner = (*ClusterK8sRunner)(nil)
_ api.Terminatable = (*ClusterK8sRunner)(nil)
_ api.Healthchecker = (*ClusterK8sRunner)(nil)
mu = sync.Mutex{}
errSyncClient = errors.New("failed to start sync client")
)
const (
defaultK8sNetworkAnnotation = "aws-cni"
// collect-outputs pod is used to compress outputs at the end of a testplan run
// as well as to copy archives from it, since it has EFS attached to it
collectOutputsPodName = "collect-outputs"
// number of CPUs allocated to each Sidecar. should be same as what is set in sidecar.yaml
sidecarCPUs = 0.2
// utilisation is how many CPUs from the remainder shall we allocate to Testground
// note that there are other services running on the Kubernetes cluster such as
// api proxy, node_exporter, dummy, etc.
utilisation = 0.85
// magic values that we monitor on the Testground runner side to detect when Testground
// testplan instances are initialised and at the stage of actually running a test
// check sdk/sync for more information
NetworkInitialisationSuccessful = "network initialisation successful"
NetworkInitialisationFailed = "network initialisation failed"
)
var k8sSubnetIdx uint64 = 0
func init() {
// Avoid collisions in picking up subnets
rand.Seed(time.Now().UnixNano())
k8sSubnetIdx = rand.Uint64() % 4096
}
func nextK8sSubnet() (*net.IPNet, error) {
subnet, _, err := nextDataNetwork(int(atomic.AddUint64(&k8sSubnetIdx, 1) % 4096))
if err != nil {
return nil, err
}
return subnet, err
}
func homeDir() string {
home, _ := os.UserHomeDir()
return home
}
// ClusterK8sRunnerConfig is the configuration object of this runner. Boolean
// values are expressed in a way that zero value (false) is the default setting.
type ClusterK8sRunnerConfig struct {
// LogLevel sets the log level in the test containers (default: not set).
LogLevel string `toml:"log_level"`
KeepService bool `toml:"keep_service"`
// Provider is the infrastructure provider to use
Provider string `toml:"provider"`
// Whether Kubernetes cluster has an autoscaler running
AutoscalerEnabled bool `toml:"autoscaler_enabled"`
// Resources requested for each testplan pod from the Kubernetes cluster
TestplanPodMemory string `toml:"testplan_pod_memory"`
TestplanPodCPU string `toml:"testplan_pod_cpu"`
// Resources requested for the `collect-outputs` pod from the Kubernetes cluster
CollectOutputsPodMemory string `toml:"collect_outputs_pod_memory"`
CollectOutputsPodCPU string `toml:"collect_outputs_pod_cpu"`
ExposedPorts ExposedPorts `toml:"exposed_ports"`
RunTimeoutMin int `toml:"run_timeout_min"`
Sysctls []string `toml:"sysctls"`
}
// ClusterK8sRunner is a runner that creates a Docker service to launch as
// many replicated instances of a container as the run job indicates.
type ClusterK8sRunner struct {
initialized bool
config KubernetesConfig
pool *pool
imagesLRU *lru.Cache
syncClient *ss.DefaultClient
}
type Journal struct {
Events map[string]string `json:"events"`
PodsStatuses map[string]struct{} `json:"pods_statuses"`
}
func (r *Result) String() string {
return fmt.Sprintf("outcome = %s (%s)", r.Outcome, r.StringOutcomes())
}
func (r *Result) StringOutcomes() string {
groups := fmt.Sprintf("%v", r.Outcomes) // map[k:v, x:y]
return groups[4 : len(groups)-1] // remove the `map[` and `]` parts
}
type GroupOutcome struct {
Ok int `json:"ok"`
Total int `json:"total"`
}
func (g *GroupOutcome) String() string {
return fmt.Sprintf("%d/%d", g.Ok, g.Total)
}
type KubernetesConfig struct {
// KubeConfigPath is the path to your kubernetes configuration path
KubeConfigPath string `json:"kubeConfigPath"`
// Namespace is the kubernetes namespaces where the pods should be running
Namespace string `json:"namespace"`
}
// defaultKubernetesConfig uses the default ~/.kube/config
// to discover the kubernetes clusters. It also uses the "default" namespace.
func defaultKubernetesConfig() KubernetesConfig {
kubeconfig := filepath.Join(homeDir(), ".kube", "config")
if _, err := os.Stat(kubeconfig); os.IsNotExist(err) {
kubeconfig = ""
}
return KubernetesConfig{
KubeConfigPath: kubeconfig,
Namespace: "default",
}
}
func (c *ClusterK8sRunner) Run(ctx context.Context, input *api.RunInput, ow *rpc.OutputWriter) (runoutput *api.RunOutput, runerr error) {
if err := c.initPool(); err != nil {
return nil, fmt.Errorf("could not init pool: %w", err)
}
result := newResult(input)
runoutput = &api.RunOutput{
RunID: input.RunID,
Result: result,
}
defer func() {
if ctx.Err() == context.Canceled {
result.Outcome = task.OutcomeCanceled
}
}()
ow = ow.With("runner", "cluster:k8s", "run_id", input.RunID)
cfg := *input.RunnerConfig.(*ClusterK8sRunnerConfig)
// if `provider` is set, we have to push to a docker registry
if cfg.Provider != "" {
err := c.pushImagesToDockerRegistry(ctx, ow, input)
if err != nil {
runerr = fmt.Errorf("failed to push images to %s; err: %w", cfg.Provider, err)
return
}
}
defaultCPU, err := resource.ParseQuantity(cfg.TestplanPodCPU)
if err != nil {
runerr = fmt.Errorf("couldn't parse default test plan pod CPU request; make sure you have specified `testplan_pod_cpu` in .env.toml; err: %w", err)
return
}
defaultMemory, err := resource.ParseQuantity(cfg.TestplanPodMemory)
if err != nil {
runerr = fmt.Errorf("couldn't parse default test plan pod Memory request; make sure you have specified `testplan_pod_memory` in .env.toml; err: %w", err)
return
}
template := runtime.RunParams{
TestPlan: input.TestPlan,
TestCase: input.TestCase,
TestRun: input.RunID,
TestInstanceCount: input.TotalInstances,
TestDisableMetrics: input.DisableMetrics,
TestSidecar: true,
TestOutputsPath: "/outputs",
TestStartTime: time.Now(),
}
// currently weave is not releaasing IP addresses upon container deletion - we get errors back when trying to
// use an already used IP address, even if the container has been removed
// this functionality should be refactored asap, when we understand how weave releases IPs (or why it doesn't release
// them when a container is removed/ and as soon as we decide how to manage `networks in-use` so that there are no
// collisions in concurrent testplan runs
subnet, err := nextK8sSubnet()
if err != nil {
runerr = err
return
}
template.TestSubnet = &ptypes.IPNet{IPNet: *subnet}
enoughResources, err := c.checkClusterResources(ow, input.Groups, defaultMemory, defaultCPU)
if err != nil {
runerr = fmt.Errorf("couldn't check cluster resources: %v", err)
return
}
if !enoughResources {
if cfg.AutoscalerEnabled {
ow.Warnw("too many test instances requested, will have to wait for cluster autoscaler to kick in")
} else {
runerr = errors.New("too many test instances requested, resize cluster if you need more capacity")
return
}
}
jobName := fmt.Sprintf("tg-%s", input.TestPlan)
ow.Infow("deploying testground testplan run on k8s", "job-name", jobName)
var eg errgroup.Group
eg.Go(func() error {
ctxContainers, cancel := context.WithCancel(ctx)
defer cancel()
outcomesDoneCh, err := c.collectOutcomes(ctxContainers, result, &template)
if err != nil {
ow.Errorw("could not start collecting outcomes", "err", err)
}
err = c.watchRunPods(ctx, ow, input, result, &template)
if err != nil {
return err
}
cancel()
<-outcomesDoneCh
return nil
})
sem := make(chan struct{}, 30) // limit the number of concurrent k8s api calls
for _, g := range input.Groups {
runenv := template
runenv.TestGroupID = g.ID
runenv.TestGroupInstanceCount = g.Instances
runenv.TestInstanceParams = g.Parameters
runenv.TestCaptureProfiles = g.Profiles
result.Outcomes[g.ID] = &GroupOutcome{
Total: g.Instances,
}
env := conv.ToEnvVar(runenv.ToEnvVars())
env = append(env, v1.EnvVar{Name: "REDIS_HOST", Value: "testground-infra-redis"})
env = append(env, v1.EnvVar{Name: "SYNC_SERVICE_HOST", Value: "testground-sync-service"})
env = append(env, v1.EnvVar{Name: "INFLUXDB_URL", Value: "http://influxdb:8086"})
// This subnet should correspond to the secondary CNI's IP range (usually Weave)
env = append(env, v1.EnvVar{Name: "TEST_SUBNET", Value: "10.32.0.0/12"})
// Set the log level if provided in cfg.
if cfg.LogLevel != "" {
env = append(env, v1.EnvVar{Name: "LOG_LEVEL", Value: cfg.LogLevel})
}
env = append(env, v1.EnvVar{Name: "POD_IP", ValueFrom: &v1.EnvVarSource{FieldRef: &v1.ObjectFieldSelector{FieldPath: "status.podIP"}}})
env = append(env, v1.EnvVar{Name: "HOST_IP", ValueFrom: &v1.EnvVarSource{FieldRef: &v1.ObjectFieldSelector{FieldPath: "status.hostIP"}}})
// Inject exposed ports.
for name, value := range cfg.ExposedPorts.ToEnvVars() {
env = append(env, v1.EnvVar{Name: name, Value: value})
}
podCPU := defaultCPU
if g.Resources.CPU != "" {
var err error
podCPU, err = resource.ParseQuantity(g.Resources.CPU)
if err != nil {
runerr = err
return
}
}
podMemory := defaultMemory
if g.Resources.Memory != "" {
var err error
podMemory, err = resource.ParseQuantity(g.Resources.Memory)
if err != nil {
runerr = err
return
}
}
for i := 0; i < g.Instances; i++ {
i := i
g := g
sem <- struct{}{}
podName := fmt.Sprintf("%s-%s-%s-%d", jobName, input.RunID, g.ID, i)
defer func() {
if cfg.KeepService {
return
}
client := c.pool.Acquire()
defer c.pool.Release(client)
ow.Debugw("deleting pod", "pod", podName)
err = client.CoreV1().Pods(c.config.Namespace).Delete(ctx, podName, metav1.DeleteOptions{})
if err != nil {
ow.Errorw("couldn't remove pod", "pod", podName, "err", err)
}
}()
eg.Go(func() error {
defer func() { <-sem }()
currentEnv := make([]v1.EnvVar, len(env))
copy(currentEnv, env)
currentEnv = append(currentEnv, v1.EnvVar{
Name: "TEST_OUTPUTS_PATH",
Value: fmt.Sprintf("/outputs/%s/%s/%d", input.RunID, g.ID, i),
})
return c.createTestplanPod(ctx, podName, input, runenv, currentEnv, g, i, podMemory, podCPU)
})
}
}
// we want to fetch logs even in an event of error
defer func() {
if input.TotalInstances <= 200 {
var gg errgroup.Group
for _, g := range input.Groups {
for i := 0; i < g.Instances; i++ {
i := i
g := g
sem <- struct{}{}
gg.Go(func() error {
defer func() { <-sem }()
podName := fmt.Sprintf("%s-%s-%s-%d", jobName, input.RunID, g.ID, i)
ow.Debugw("fetching logs", "pod", podName)
logs, err := c.getPodLogs(ow, podName)
if err != nil {
return err
}
ow.Debugw("got logs", "pod", podName, "len", len(logs))
_, err = ow.WriteProgress([]byte(logs))
return err
})
}
}
err = gg.Wait()
if err != nil {
ow.Errorw("error while fetching logs", "err", err.Error())
}
ow.Debugw("done getting logs")
}
}()
err = eg.Wait()
if err != nil {
runerr = err
return
}
if !cfg.KeepService {
ow.Info("cleaning up finished pods...")
}
runerr = nil
return
}
func (*ClusterK8sRunner) ID() string {
return "cluster:k8s"
}
func (c *ClusterK8sRunner) Healthcheck(ctx context.Context, engine api.Engine, ow *rpc.OutputWriter, fix bool) (*api.HealthcheckReport, error) {
// Ignore sync client error as we may start the redis pod below.
if err := c.initPool(); err != nil && !errors.Is(err, errSyncClient) {
return nil, err
}
client := c.pool.Acquire()
defer c.pool.Release(client)
// How many plan worker nodes are there?
res, err := client.CoreV1().Nodes().List(ctx, metav1.ListOptions{
LabelSelector: "testground.node.role.plan=true",
})
if err != nil {
return nil, err
}
planNodes := res.Items
hh := &healthcheck.Helper{}
hh.Enlist("efs pod",
healthcheck.CheckK8sPods(ctx, client, "app=efs-provisioner", c.config.Namespace, 1),
healthcheck.NotImplemented(),
)
hh.Enlist("redis pod",
healthcheck.CheckK8sPods(ctx, client, "app=redis", c.config.Namespace, 1),
healthcheck.NotImplemented(),
)
hh.Enlist("sync service pod",
healthcheck.CheckK8sPods(ctx, client, "name=testground-sync-service", c.config.Namespace, 1),
healthcheck.NotImplemented(),
)
hh.Enlist("prometheus pod",
healthcheck.CheckK8sPods(ctx, client, "app=prometheus", c.config.Namespace, 1),
healthcheck.NotImplemented(),
)
hh.Enlist("grafana pod",
healthcheck.CheckK8sPods(ctx, client, "app.kubernetes.io/name=grafana", c.config.Namespace, 1),
healthcheck.NotImplemented(),
)
hh.Enlist("sidecar pods",
healthcheck.CheckK8sPods(ctx, client, "name=testground-sidecar", c.config.Namespace, len(planNodes)),
healthcheck.NotImplemented(),
)
return hh.RunChecks(ctx, fix)
}
func (*ClusterK8sRunner) ConfigType() reflect.Type {
return reflect.TypeOf(ClusterK8sRunnerConfig{})
}
func (*ClusterK8sRunner) CompatibleBuilders() []string {
return []string{"docker:go", "docker:generic"}
}
func (c *ClusterK8sRunner) Enabled() bool {
_ = c.initPool()
return c.pool != nil
}
func (c *ClusterK8sRunner) initPool() error {
mu.Lock()
defer mu.Unlock()
if c.initialized {
return nil
}
c.config = defaultKubernetesConfig()
c.imagesLRU, _ = lru.New(256)
var err error
workers := 20
c.pool, err = newPool(workers, c.config)
if err != nil {
return err
}
c.syncClient, err = ss.NewGenericClient(context.Background(), logging.S())
if err != nil {
return fmt.Errorf("%w: %s", errSyncClient, err)
}
c.initialized = true
return nil
}
func (c *ClusterK8sRunner) CollectOutputs(ctx context.Context, input *api.CollectionInput, ow *rpc.OutputWriter) error {
if err := c.initPool(); err != nil {
return fmt.Errorf("could not init pool: %w", err)
}
log := ow.With("runner", "cluster:k8s", "run_id", input.RunID)
err := c.ensureCollectOutputsPod(ctx, input)
if err != nil {
return err
}
client := c.pool.Acquire()
defer c.pool.Release(client)
// This is the same line found in client_pool.go...
// I need the restCfg, for remotecommand.
// TODO: Reorganize not to repeat ourselves.
k8sCfg, err := clientcmd.BuildConfigFromFlags("", c.config.KubeConfigPath)
if err != nil {
return err
}
// This request is sent to the collect-outputs pod
// tar, compress, and write to stdout.
// stdout will remain connected so we can read it later.
log.Info("collecting outputs")
req := client.
CoreV1().
RESTClient().
Post().
Resource("pods").
Name("collect-outputs").
Namespace("default").
SubResource("exec").
Param("container", "collect-outputs").
VersionedParams(&v1.PodExecOptions{
Container: "collect-outputs",
Command: []string{
"tar",
"-C",
"/outputs",
"-czf",
"-",
input.RunID,
},
Stdin: false,
Stderr: false,
Stdout: true,
}, scheme.ParameterCodec)
log.Debug("sending command to remote server: ", req.URL())
exec, err := remotecommand.NewSPDYExecutor(k8sCfg, "POST", req.URL())
if err != nil {
log.Warnf("failed to send remote collection command: %v", err)
return err
}
// Connect stdout of the above command to the output file
// Connect stderr to a buffer which we can read from to display any errors to the user.
outbuf := bufio.NewWriter(ow.BinaryWriter())
defer outbuf.Flush()
err = exec.Stream(remotecommand.StreamOptions{
Stdout: outbuf,
})
if err != nil {
log.Warnf("failed to collect results from remote collection command: %v", err)
return err
}
return nil
}
// waitForPod waits until a given pod reaches the desired `phase` or the context is canceled
func (c *ClusterK8sRunner) waitForPod(ctx context.Context, podName string, phase string) error {
client := c.pool.Acquire()
defer c.pool.Release(client)
var p string
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
if p == phase {
return nil
}
res, err := client.CoreV1().Pods(c.config.Namespace).List(ctx, metav1.ListOptions{
FieldSelector: "metadata.name=" + podName,
})
if err != nil {
return err
}
if len(res.Items) != 1 {
continue
}
pod := res.Items[0]
p = string(pod.Status.Phase)
time.Sleep(1 * time.Second)
}
}
}
// ensureCollectOutputsPod ensures that we have a collect-outputs pod running
func (c *ClusterK8sRunner) ensureCollectOutputsPod(ctx context.Context, input *api.CollectionInput) error {
client := c.pool.Acquire()
defer c.pool.Release(client)
res, err := client.CoreV1().Pods(c.config.Namespace).List(ctx, metav1.ListOptions{
FieldSelector: "metadata.name=" + collectOutputsPodName,
})
if err != nil {
return err
}
if len(res.Items) == 0 {
err = c.createCollectOutputsPod(ctx, input)
if err != nil {
return err
}
err = c.waitForPod(ctx, collectOutputsPodName, "Running")
if err != nil {
return err
}
} else if len(res.Items) > 1 {
return errors.New("unexpected number of pods for outputs collection")
}
return nil
}
func (c *ClusterK8sRunner) getPodLogs(ow *rpc.OutputWriter, podName string) (string, error) {
client := c.pool.Acquire()
defer c.pool.Release(client)
podLogOpts := v1.PodLogOptions{
LimitBytes: int64Ptr(10000000000), // 100mb
}
var podLogs io.ReadCloser
var err error
err = retry(5, 5*time.Second, func() error {
req := client.CoreV1().Pods(c.config.Namespace).GetLogs(podName, &podLogOpts)
podLogs, err = req.Stream(context.TODO())
if err != nil {
ow.Warnw("got error when trying to fetch pod logs", "err", err.Error())
}
return err
})
if err != nil {
return "", fmt.Errorf("error in opening stream: %v", err)
}
defer podLogs.Close()
lr := byline.NewReader(podLogs)
lr.MapString(func(line string) string { return podName + " | " + line })
buf := &bytes.Buffer{}
_, err = io.Copy(buf, lr)
if err != nil {
return "", fmt.Errorf("error in copy information from podLogs to buf: %v", err)
}
return buf.String(), nil
}
func (c *ClusterK8sRunner) watchRunPods(ctx context.Context, ow *rpc.OutputWriter, input *api.RunInput, result *Result, rp *runtime.RunParams) error {
client := c.pool.Acquire()
defer c.pool.Release(client)
cfg := *input.RunnerConfig.(*ClusterK8sRunnerConfig)
runTimeout := 10 * time.Minute
if cfg.RunTimeoutMin != 0 {
runTimeout = time.Duration(cfg.RunTimeoutMin) * time.Minute
}
fieldSelector := "type!=Normal"
opts := metav1.ListOptions{
FieldSelector: fieldSelector,
}
eventsWatcher, err := client.CoreV1().Events(c.config.Namespace).Watch(ctx, opts)
if err != nil {
ow.Errorw("k8s client pods list error", "err", err.Error())
return err
}
defer eventsWatcher.Stop()
eventsChan := eventsWatcher.ResultChan()
go func() {
for ge := range eventsChan {
e, ok := ge.Object.(*v1.Event)
if ok && strings.Contains(e.InvolvedObject.Name, input.RunID) {
id := e.ObjectMeta.Name
event := fmt.Sprintf("obj<%s> type<%s> reason<%s> message<%s> type<%s> count<%d> lastTimestamp<%s>", e.InvolvedObject.Name, ge.Type, e.Reason, e.Message, e.Type, e.Count, e.LastTimestamp)
ow.Warnw("testplan received event", "event", event)
result.Journal.Events[id] = event
}
}
}()
podsByState := make(map[string]*v1.PodList)
var countersMu sync.Mutex
start := time.Now()
allRunningStage := false
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if time.Since(start) > runTimeout {
return fmt.Errorf("run timeout reached. make sure your plan execution completes within %s.", runTimeout)
}
time.Sleep(2000 * time.Millisecond)
countPodsByState := func(state string) int {
fieldSelector := fmt.Sprintf("status.phase=%s", state)
opts := metav1.ListOptions{
LabelSelector: fmt.Sprintf("testground.run_id=%s", input.RunID),
FieldSelector: fieldSelector,
}
res, err := client.CoreV1().Pods(c.config.Namespace).List(ctx, opts)
if err != nil {
ow.Warnw("k8s client pods list error", "err", err.Error())
return -1
}
countersMu.Lock()
podsByState[state] = res
countersMu.Unlock()
return len(res.Items)
}
counters := map[string]int{}
states := []string{"Pending", "Running", "Succeeded", "Failed", "Unknown"}
var wg sync.WaitGroup
wg.Add(len(states))
for _, state := range states {
state := state
go func() {
defer wg.Done()
n := countPodsByState(state)
countersMu.Lock()
counters[state] = n
countersMu.Unlock()
}()
}
wg.Wait()
ow.Debugw("testplan pods state", "running_for", time.Since(start).Truncate(time.Second), "succeeded", counters["Succeeded"], "running", counters["Running"], "pending", counters["Pending"], "failed", counters["Failed"], "unknown", counters["Unknown"])
if counters["Failed"] > 0 {
for _, p := range podsByState["Failed"].Items {
if !strings.Contains(p.ObjectMeta.Name, input.RunID) {
continue
}
for _, st := range p.Status.ContainerStatuses {
event := fmt.Sprintf("pod status <failed> obj<%s> reason<%s> started_at<%s> finished_at<%s> exitcode<%d>", st.Name, st.State.Terminated.Reason, st.State.Terminated.StartedAt, st.State.Terminated.FinishedAt, st.State.Terminated.ExitCode)
ow.Warnw("testplan received status", "status", event)
result.Journal.PodsStatuses[event] = struct{}{}
}
}
}
if counters["Running"] == input.TotalInstances && !allRunningStage {
allRunningStage = true
ow.Infow("all testplan instances in `Running` state", "took", time.Since(start).Truncate(time.Second))
}
if counters["Succeeded"] == input.TotalInstances {
ow.Infow("all testplan instances in `Succeeded` state", "took", time.Since(start).Truncate(time.Second))
return nil
}
if (counters["Succeeded"] + counters["Failed"]) == input.TotalInstances {
ow.Warnw("all testplan instances in `Succeeded` or `Failed` state", "took", time.Since(start).Truncate(time.Second))
return nil
}
}
}
func (c *ClusterK8sRunner) createTestplanPod(ctx context.Context, podName string, input *api.RunInput, runenv runtime.RunParams, env []v1.EnvVar, g *api.RunGroup, i int, podResourceMemory resource.Quantity, podResourceCPU resource.Quantity) error {
client := c.pool.Acquire()
defer c.pool.Release(client)
cfg := *input.RunnerConfig.(*ClusterK8sRunnerConfig)
var sysctls []v1.Sysctl
for _, v := range cfg.Sysctls {
sysctl := strings.Split(v, "=")
sysctls = append(sysctls, v1.Sysctl{Name: sysctl[0], Value: sysctl[1]})
}
var ports []v1.ContainerPort
cnt := 0
for _, p := range cfg.ExposedPorts {
port, err := strconv.ParseInt(p, 10, 32)
if err != nil {
return err
}
ports = append(ports, v1.ContainerPort{Name: fmt.Sprintf("port%d", cnt), ContainerPort: int32(port)})
cnt++
}
mountPropagationMode := v1.MountPropagationHostToContainer
sharedVolumeName := "efs-shared"
podRequest := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Labels: map[string]string{
"testground.plan": input.TestPlan,
"testground.testcase": runenv.TestCase,
"testground.run_id": input.RunID,
"testground.groupid": g.ID,
"testground.purpose": "plan",
},
Annotations: map[string]string{"cni": defaultK8sNetworkAnnotation, "k8s.v1.cni.cncf.io/networks": "weave"},
},
Spec: v1.PodSpec{
Volumes: []v1.Volume{
{
Name: sharedVolumeName,
VolumeSource: v1.VolumeSource{
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
ClaimName: "efs",
},
},
},
},
SecurityContext: &v1.PodSecurityContext{
Sysctls: sysctls,
},
RestartPolicy: v1.RestartPolicyNever,
InitContainers: []v1.Container{
{
Name: "wait-for-sidecar",
Image: "busybox",
ImagePullPolicy: v1.PullIfNotPresent,
Args: []string{"-c", "until nc -vz $HOST_IP 6060; do echo \"Waiting for local sidecar to listen to $HOST_IP:6060\"; sleep 2; done;"},
Command: []string{"sh"},
Env: env,
Resources: v1.ResourceRequirements{
Limits: v1.ResourceList{
v1.ResourceMemory: resource.MustParse("10Mi"),
v1.ResourceCPU: resource.MustParse("10m"),
},
},
},
{
Name: "mkdir-outputs",
Image: "busybox",
ImagePullPolicy: v1.PullIfNotPresent,
Args: []string{"-c", "mkdir -p $TEST_OUTPUTS_PATH"},
Command: []string{"sh"},
Env: env,
VolumeMounts: []v1.VolumeMount{
{
Name: sharedVolumeName,
MountPath: "/outputs",
MountPropagation: &mountPropagationMode,
},
},
Resources: v1.ResourceRequirements{
Limits: v1.ResourceList{
v1.ResourceMemory: resource.MustParse("10Mi"),
v1.ResourceCPU: resource.MustParse("10m"),
},
},
},
},
Containers: []v1.Container{
{
Name: podName,
Image: g.ArtifactPath,
ImagePullPolicy: v1.PullIfNotPresent,
Args: []string{},
Env: env,
Ports: ports,
VolumeMounts: []v1.VolumeMount{
{
Name: sharedVolumeName,
MountPath: "/outputs",
MountPropagation: &mountPropagationMode,
},
},
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceMemory: podResourceMemory,
v1.ResourceCPU: podResourceCPU,
},
Limits: v1.ResourceList{
v1.ResourceMemory: podResourceMemory,
},
},
},
},
NodeSelector: map[string]string{"testground.node.role.plan": "true"},
},
}
_, err := client.CoreV1().Pods(c.config.Namespace).Create(ctx, podRequest, metav1.CreateOptions{})
return err
}
func int64Ptr(i int64) *int64 { return &i }
type FakeWriterAt struct {
w io.Writer
}
func (fw FakeWriterAt) WriteAt(p []byte, offset int64) (n int, err error) {
// ignore 'offset' because we forced sequential downloads
return fw.w.Write(p)
}
// checkClusterResources returns whether we can fit the input groups in the current cluster
func (c *ClusterK8sRunner) checkClusterResources(ow *rpc.OutputWriter, groups []*api.RunGroup, fallbackMemory resource.Quantity, fallbackCPU resource.Quantity) (bool, error) {
neededCPUs := 0.0
defaultPodCPU, err := strconv.ParseFloat(fallbackCPU.AsDec().String(), 64)
if err != nil {
return false, err
}
client := c.pool.Acquire()
defer c.pool.Release(client)
res, err := client.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{
LabelSelector: "testground.node.role.plan=true",
})
if err != nil {
return false, err
}
nodes := len(res.Items)
// all worker nodes are the same, so just take allocatable CPU from the first
item := res.Items[0].Status.Allocatable["cpu"]
nodeCPUs := item.ToDec().Value()
totalCPUs := nodes * int(nodeCPUs)
availableCPUs := float64(totalCPUs) - float64(nodes)*sidecarCPUs
for _, g := range groups {
var podCPU float64
if g.Resources.CPU != "" {
cpu, err := resource.ParseQuantity(g.Resources.CPU)
if err != nil {
return false, err
}
podCPU, err = strconv.ParseFloat(cpu.AsDec().String(), 64)
if err != nil {
return false, err
}
} else {
podCPU = defaultPodCPU
}