1
2
3
4
5
6
7
8
9
10
11
12
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
41 type OperatorAction interface {
42 CleanCRDOrDie()
43 DeployOperator(config OperatorConfig) error
44 InstallCRD(config OperatorConfig) error
45 }
46
47
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
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