...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/selector/generic/annotation/selector.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/selector/generic/annotation

     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 annotation
    17  
    18  import (
    19  	"k8s.io/apimachinery/pkg/labels"
    20  	"sigs.k8s.io/controller-runtime/pkg/client"
    21  
    22  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    23  	"github.com/chaos-mesh/chaos-mesh/pkg/label"
    24  	"github.com/chaos-mesh/chaos-mesh/pkg/selector/generic"
    25  )
    26  
    27  const Name = "annotation"
    28  
    29  type annotationSelector struct {
    30  	labels.Selector
    31  }
    32  
    33  var _ generic.Selector = &annotationSelector{}
    34  
    35  func (s *annotationSelector) ListOption() client.ListOption {
    36  	return nil
    37  }
    38  
    39  func (s *annotationSelector) ListFunc(_ client.Reader) generic.ListFunc {
    40  	return nil
    41  }
    42  
    43  func (s *annotationSelector) Match(obj client.Object) bool {
    44  	annotations := labels.Set(obj.GetAnnotations())
    45  	return s.Matches(annotations)
    46  }
    47  
    48  func New(spec v1alpha1.GenericSelectorSpec, _ generic.Option) (generic.Selector, error) {
    49  	selectorStr := label.Label(spec.AnnotationSelectors).String()
    50  	s, err := labels.Parse(selectorStr)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	return &annotationSelector{Selector: s}, nil
    55  }
    56