...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/ctrl/server/k8s-utils.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/ctrl/server

     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 server
    17  
    18  import (
    19  	"context"
    20  	"strings"
    21  
    22  	"github.com/pkg/errors"
    23  	v1 "k8s.io/api/core/v1"
    24  	"k8s.io/apimachinery/pkg/types"
    25  	"sigs.k8s.io/controller-runtime/pkg/client"
    26  
    27  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    28  	ctrlconfig "github.com/chaos-mesh/chaos-mesh/controllers/config"
    29  	"github.com/chaos-mesh/chaos-mesh/pkg/ctrl/server/model"
    30  	"github.com/chaos-mesh/chaos-mesh/pkg/selector/pod"
    31  )
    32  
    33  const DefaultNamespace = "default"
    34  
    35  func componentLabels(component model.Component) map[string]string {
    36  	var componentLabel string
    37  	switch component {
    38  	case model.ComponentManager:
    39  		componentLabel = "controller-manager"
    40  	case model.ComponentDaemon:
    41  		componentLabel = "chaos-daemon"
    42  	case model.ComponentDashboard:
    43  		componentLabel = "chaos-dashboard"
    44  	case model.ComponentDNSServer:
    45  		componentLabel = "chaos-dns-server"
    46  	default:
    47  		return nil
    48  	}
    49  	return map[string]string{
    50  		"app.kubernetes.io/component": componentLabel,
    51  	}
    52  }
    53  
    54  func parseNamespacedName(namespacedName string) types.NamespacedName {
    55  	parts := strings.Split(namespacedName, "/")
    56  	return types.NamespacedName{
    57  		Namespace: parts[0],
    58  		Name:      parts[1],
    59  	}
    60  }
    61  
    62  // GetPods returns pod list and corresponding chaos daemon
    63  func GetPods(ctx context.Context, status v1alpha1.ChaosStatus, selectorSpec v1alpha1.PodSelectorSpec, c client.Client) ([]v1.Pod, []v1.Pod, error) {
    64  	pods, err := pod.SelectPods(ctx, c, c, selectorSpec, ctrlconfig.ControllerCfg.ClusterScoped, ctrlconfig.ControllerCfg.TargetNamespace, false)
    65  	if err != nil {
    66  		return nil, nil, errors.Wrap(err, "failed to SelectPods")
    67  	}
    68  	if len(pods) == 0 {
    69  		return nil, nil, nil
    70  	}
    71  
    72  	daemonMap, err := getDaemonMap(ctx, c)
    73  	if err != nil {
    74  		return nil, nil, errors.Wrap(err, "get daemon map")
    75  	}
    76  
    77  	var chaosDaemons []v1.Pod
    78  	// get chaos daemon
    79  	for _, chaosPod := range pods {
    80  		daemon, exist := daemonMap[chaosPod.Spec.NodeName]
    81  		if !exist {
    82  			return nil, nil, errors.Errorf("no daemons found for pod %s", chaosPod.GetName())
    83  		}
    84  		chaosDaemons = append(chaosDaemons, daemon)
    85  	}
    86  
    87  	return pods, chaosDaemons, nil
    88  }
    89  
    90  // GetDaemonMap returns a map of node name to daemon pod
    91  func getDaemonMap(ctx context.Context, c client.Client) (map[string]v1.Pod, error) {
    92  	var list v1.PodList
    93  	labels := componentLabels(model.ComponentDaemon)
    94  	if err := c.List(ctx, &list, client.MatchingLabels(labels)); err != nil {
    95  		return nil, errors.Wrapf(err, "list daemons by label %v", labels)
    96  	}
    97  
    98  	daemonMap := map[string]v1.Pod{}
    99  	for _, d := range list.Items {
   100  		if d.Spec.NodeName != "" {
   101  			daemonMap[d.Spec.NodeName] = d
   102  		}
   103  	}
   104  	return daemonMap, nil
   105  }
   106