...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/schedule/utils/list.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/schedule/utils

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    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  // TODO: use another easy-to-used type for describing ChaosList
    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