...

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

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

     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package container
    15  
    16  import (
    17  	"context"
    18  
    19  	"go.uber.org/fx"
    20  	v1 "k8s.io/api/core/v1"
    21  	"sigs.k8s.io/controller-runtime/pkg/client"
    22  
    23  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    24  	"github.com/chaos-mesh/chaos-mesh/controllers/config"
    25  	"github.com/chaos-mesh/chaos-mesh/pkg/selector/pod"
    26  )
    27  
    28  type SelectImpl struct {
    29  	c client.Client
    30  	r client.Reader
    31  
    32  	pod.Option
    33  }
    34  
    35  type Container struct {
    36  	v1.Pod
    37  	ContainerName string
    38  }
    39  
    40  func (c *Container) Id() string {
    41  	return c.Pod.Namespace + "/" + c.Pod.Name + "/" + c.ContainerName
    42  }
    43  
    44  func (impl *SelectImpl) Select(ctx context.Context, cs *v1alpha1.ContainerSelector) ([]*Container, error) {
    45  	pods, err := pod.SelectAndFilterPods(ctx, impl.c, impl.r, &cs.PodSelector, impl.ClusterScoped, impl.TargetNamespace, impl.EnableFilterNamespace)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	containerNameMap := make(map[string]struct{})
    51  	for _, name := range cs.ContainerNames {
    52  		containerNameMap[name] = struct{}{}
    53  	}
    54  
    55  	var result []*Container
    56  	for _, pod := range pods {
    57  		if len(cs.ContainerNames) == 0 {
    58  			result = append(result, &Container{
    59  				Pod:           pod,
    60  				ContainerName: pod.Spec.Containers[0].Name,
    61  			})
    62  			continue
    63  		}
    64  
    65  		for _, container := range pod.Spec.Containers {
    66  			if _, ok := containerNameMap[container.Name]; ok {
    67  				result = append(result, &Container{
    68  					Pod:           pod,
    69  					ContainerName: container.Name,
    70  				})
    71  			}
    72  		}
    73  	}
    74  
    75  	return result, nil
    76  }
    77  
    78  type Params struct {
    79  	fx.In
    80  
    81  	Client client.Client
    82  	Reader client.Reader `name:"no-cache"`
    83  }
    84  
    85  func New(params Params) *SelectImpl {
    86  	return &SelectImpl{
    87  		params.Client,
    88  		params.Reader,
    89  		pod.Option{
    90  			ClusterScoped:         config.ControllerCfg.ClusterScoped,
    91  			TargetNamespace:       config.ControllerCfg.TargetNamespace,
    92  			EnableFilterNamespace: config.ControllerCfg.EnableFilterNamespace,
    93  		},
    94  	}
    95  }
    96