...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/selector/pod/node_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  	"context"
    20  
    21  	v1 "k8s.io/api/core/v1"
    22  	"k8s.io/apimachinery/pkg/labels"
    23  	"k8s.io/apimachinery/pkg/types"
    24  	"sigs.k8s.io/controller-runtime/pkg/client"
    25  
    26  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    27  	"github.com/chaos-mesh/chaos-mesh/pkg/selector/generic"
    28  )
    29  
    30  const nodeSelectorName = "node"
    31  
    32  type nodeSelector struct {
    33  	nodes []v1.Node
    34  	// empty avoids the situation that v1alpha1.PodSelectorSpec.NodeSelectors does not match any nodes, which should not skip the selector check
    35  	// empty is true only when v1alpha1.PodSelectorSpec.Nodes and v1alpha1.PodSelectorSpec.NodeSelectors are both empty
    36  	empty bool
    37  }
    38  
    39  var _ generic.Selector = &nodeSelector{}
    40  
    41  func (s *nodeSelector) ListOption() client.ListOption {
    42  	return nil
    43  }
    44  
    45  func (s *nodeSelector) ListFunc(_ client.Reader) generic.ListFunc {
    46  	return nil
    47  }
    48  
    49  func (s *nodeSelector) Match(obj client.Object) bool {
    50  	if s.empty {
    51  		return true
    52  	}
    53  
    54  	pod := obj.(*v1.Pod)
    55  	for _, node := range s.nodes {
    56  		if node.Name == pod.Spec.NodeName {
    57  			return true
    58  		}
    59  	}
    60  	return false
    61  }
    62  
    63  // if both setting Nodes and NodeSelectors, the node list will be combined.
    64  func newNodeSelector(ctx context.Context, c client.Client, spec v1alpha1.PodSelectorSpec) (generic.Selector, error) {
    65  	if len(spec.Nodes) == 0 && len(spec.NodeSelectors) == 0 {
    66  		return &nodeSelector{empty: true}, nil
    67  	}
    68  	var nodes []v1.Node
    69  	if len(spec.Nodes) > 0 {
    70  		for _, name := range spec.Nodes {
    71  			var node v1.Node
    72  			if err := c.Get(ctx, types.NamespacedName{Name: name}, &node); err != nil {
    73  				return nil, err
    74  			}
    75  			nodes = append(nodes, node)
    76  		}
    77  	}
    78  	if len(spec.NodeSelectors) > 0 {
    79  		var nodeList v1.NodeList
    80  		if err := c.List(ctx, &nodeList, &client.ListOptions{
    81  			LabelSelector: labels.SelectorFromSet(spec.NodeSelectors),
    82  		}); err != nil {
    83  			return nil, err
    84  		}
    85  		nodes = append(nodes, nodeList.Items...)
    86  	}
    87  	return &nodeSelector{nodes: nodes}, nil
    88  }
    89