...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package utils
17
18 import (
19 "context"
20
21 "github.com/go-logr/logr"
22 "github.com/pkg/errors"
23 "sigs.k8s.io/controller-runtime/pkg/client"
24
25 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
26 )
27
28 type ActiveLister struct {
29 client.Client
30 Log logr.Logger
31 }
32
33
34 func (lister *ActiveLister) ListActiveJobs(ctx context.Context, schedule *v1alpha1.Schedule) (v1alpha1.GenericChaosList, error) {
35 kind, ok := v1alpha1.AllScheduleItemKinds()[string(schedule.Spec.Type)]
36 if !ok {
37 lister.Log.Info("unknown kind", "kind", schedule.Spec.Type)
38 return nil, errors.Errorf("Unknown type: %s", schedule.Spec.Type)
39 }
40
41 list := kind.SpawnList()
42 err := lister.List(ctx, list, client.MatchingLabels{v1alpha1.LabelManagedBy: schedule.Name})
43 if err != nil {
44 lister.Log.Error(err, "fail to list chaos")
45 return nil, nil
46 }
47
48 return list, nil
49 }
50
51 func NewActiveLister(c client.Client, logger logr.Logger) *ActiveLister {
52 return &ActiveLister{
53 Client: c,
54 Log: logger.WithName("activeLister"),
55 }
56 }
57