...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/selector/pod/phase_selector.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/selector/pod

     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 pod
    17  
    18  import (
    19  	"strings"
    20  
    21  	"github.com/pkg/errors"
    22  	v1 "k8s.io/api/core/v1"
    23  	"k8s.io/apimachinery/pkg/labels"
    24  	"k8s.io/apimachinery/pkg/selection"
    25  	"sigs.k8s.io/controller-runtime/pkg/client"
    26  
    27  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    28  	"github.com/chaos-mesh/chaos-mesh/pkg/selector/generic"
    29  )
    30  
    31  const phaseSelectorName = "phase"
    32  
    33  type phaseSelector struct {
    34  	reqIncl []labels.Requirement
    35  	reqExcl []labels.Requirement
    36  }
    37  
    38  var _ generic.Selector = &phaseSelector{}
    39  
    40  func (s *phaseSelector) ListOption() client.ListOption {
    41  	return nil
    42  }
    43  
    44  func (s *phaseSelector) ListFunc(_ client.Reader) generic.ListFunc {
    45  	return nil
    46  }
    47  
    48  func (s *phaseSelector) Match(obj client.Object) bool {
    49  	included := len(s.reqIncl) == 0
    50  	pod := obj.(*v1.Pod)
    51  	selector := labels.Set{string(pod.Status.Phase): ""}
    52  
    53  	// include pod if one including requirement matches
    54  	for _, req := range s.reqIncl {
    55  		if req.Matches(selector) {
    56  			included = true
    57  			break
    58  		}
    59  	}
    60  
    61  	// exclude pod if it is filtered out by at least one excluding requirement
    62  	for _, req := range s.reqExcl {
    63  		if !req.Matches(selector) {
    64  			return false
    65  		}
    66  	}
    67  
    68  	return included
    69  }
    70  
    71  func newPhaseSelector(spec v1alpha1.PodSelectorSpec) (generic.Selector, error) {
    72  	selectorStr := strings.Join(spec.PodPhaseSelectors, ",")
    73  	selector, err := labels.Parse(selectorStr)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	reqs, _ := selector.Requirements()
    79  	var (
    80  		reqIncl []labels.Requirement
    81  		reqExcl []labels.Requirement
    82  	)
    83  
    84  	for _, req := range reqs {
    85  		switch req.Operator() {
    86  		case selection.Exists:
    87  			reqIncl = append(reqIncl, req)
    88  		case selection.DoesNotExist:
    89  			reqExcl = append(reqExcl, req)
    90  		default:
    91  			return nil, errors.Errorf("unsupported operator: %s", req.Operator())
    92  		}
    93  	}
    94  
    95  	return &phaseSelector{
    96  		reqIncl: reqIncl,
    97  		reqExcl: reqExcl,
    98  	}, nil
    99  }
   100