...

Source file src/github.com/chaos-mesh/chaos-mesh/test/action.go

Documentation: github.com/chaos-mesh/chaos-mesh/test

     1  // Copyright 2020 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 test
    15  
    16  import (
    17  	"fmt"
    18  	"os/exec"
    19  	"strings"
    20  	"time"
    21  
    22  	corev1 "k8s.io/api/core/v1"
    23  	"k8s.io/apimachinery/pkg/util/wait"
    24  
    25  	apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/apimachinery/pkg/labels"
    28  	"k8s.io/client-go/kubernetes"
    29  	"k8s.io/klog"
    30  	aggregatorclientset "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset"
    31  
    32  	"github.com/chaos-mesh/chaos-mesh/test/e2e/e2econst"
    33  	e2eutil "github.com/chaos-mesh/chaos-mesh/test/e2e/util"
    34  )
    35  
    36  const (
    37  	operatorChartName = "chaos-mesh"
    38  )
    39  
    40  // OperatorAction describe the common operation during test (e2e/stability/etc..)
    41  type OperatorAction interface {
    42  	CleanCRDOrDie()
    43  	DeployOperator(config OperatorConfig) error
    44  	InstallCRD(config OperatorConfig) error
    45  }
    46  
    47  // NewOperatorAction create an OperatorAction interface instance
    48  func NewOperatorAction(
    49  	kubeCli kubernetes.Interface,
    50  	aggrCli aggregatorclientset.Interface,
    51  	apiExtCli apiextensionsclientset.Interface,
    52  	cfg *Config) OperatorAction {
    53  
    54  	oa := &operatorAction{
    55  		kubeCli:   kubeCli,
    56  		aggrCli:   aggrCli,
    57  		apiExtCli: apiExtCli,
    58  		cfg:       cfg,
    59  	}
    60  	return oa
    61  }
    62  
    63  func (oa *operatorAction) DeployOperator(info OperatorConfig) error {
    64  	klog.Infof("create namespace chaos-testing")
    65  	cmd := fmt.Sprintf(`kubectl create ns %s`, e2econst.ChaosMeshNamespace)
    66  	klog.Infof(cmd)
    67  	output, err := exec.Command("/bin/sh", "-c", cmd).CombinedOutput()
    68  	if err != nil {
    69  		return fmt.Errorf("failed to create namespace chaos-testing: %v %s", err, string(output))
    70  	}
    71  	klog.Infof("deploying chaos-mesh:%v", info.ReleaseName)
    72  	cmd = fmt.Sprintf(`helm install %s %s --namespace %s --set %s`,
    73  		info.ReleaseName,
    74  		oa.operatorChartPath(info.Tag),
    75  		info.Namespace,
    76  		info.operatorHelmSetValue())
    77  	klog.Info(cmd)
    78  	res, err := exec.Command("/bin/sh", "-c", cmd).CombinedOutput()
    79  	if err != nil {
    80  		return fmt.Errorf("failed to deploy operator: %v, %s", err, string(res))
    81  	}
    82  	klog.Infof("start to waiting chaos-mesh ready")
    83  	err = wait.Poll(5*time.Second, 5*time.Minute, func() (done bool, err error) {
    84  
    85  		ls := &metav1.LabelSelector{
    86  			MatchLabels: map[string]string{
    87  				"app.kubernetes.io/instance": "chaos-mesh",
    88  			},
    89  		}
    90  		l, err := metav1.LabelSelectorAsSelector(ls)
    91  		if err != nil {
    92  			klog.Errorf("failed to get selector, err:%v", err)
    93  			return false, nil
    94  		}
    95  		pods, err := oa.kubeCli.CoreV1().Pods(info.Namespace).List(metav1.ListOptions{LabelSelector: l.String()})
    96  		if err != nil {
    97  			klog.Errorf("failed to get chaos-mesh pods, err:%v", err)
    98  			return false, nil
    99  		}
   100  		for _, pod := range pods.Items {
   101  			if pod.Status.Phase != corev1.PodRunning {
   102  				return false, nil
   103  			}
   104  		}
   105  		return true, nil
   106  	})
   107  	if err != nil {
   108  		return err
   109  	}
   110  	return e2eutil.WaitForAPIServicesAvailable(oa.aggrCli, labels.Everything())
   111  }
   112  
   113  func (oa *operatorAction) InstallCRD(info OperatorConfig) error {
   114  	klog.Infof("deploying chaos-mesh crd :%v", info.ReleaseName)
   115  	oa.runKubectlOrDie("apply", "-f", oa.manifestPath("e2e/crd.yaml"), "--validate=false")
   116  	e2eutil.WaitForCRDsEstablished(oa.apiExtCli, labels.Everything())
   117  	// workaround for https://github.com/kubernetes/kubernetes/issues/65517
   118  	klog.Infof("force sync kubectl cache")
   119  	cmdArgs := []string{"sh", "-c", "rm -rf ~/.kube/cache ~/.kube/http-cache"}
   120  	_, err := exec.Command(cmdArgs[0], cmdArgs[1:]...).CombinedOutput()
   121  	if err != nil {
   122  		klog.Fatalf("Failed to run '%s': %v", strings.Join(cmdArgs, " "), err)
   123  	}
   124  	return nil
   125  }
   126  
   127  func (oa *operatorAction) CleanCRDOrDie() {
   128  	oa.runKubectlOrDie("delete", "crds", "--all")
   129  }
   130