...

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

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

     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 test
    17  
    18  import (
    19  	"fmt"
    20  	"os/exec"
    21  	"path/filepath"
    22  	"strings"
    23  
    24  	apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
    25  	"k8s.io/client-go/kubernetes"
    26  	"k8s.io/klog/v2"
    27  	aggregatorclientset "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset"
    28  	"k8s.io/kubernetes/test/e2e/framework"
    29  )
    30  
    31  const (
    32  	imagePullPolicyIfNotPresent = "IfNotPresent"
    33  )
    34  
    35  // OperatorConfig describe the configuration during installing chaos-mesh
    36  type OperatorConfig struct {
    37  	Namespace       string
    38  	ReleaseName     string
    39  	Manager         ManagerConfig
    40  	Daemon          DaemonConfig
    41  	Tag             string
    42  	DNSImage        string
    43  	EnableDashboard bool
    44  }
    45  
    46  // ManagerConfig describe the chaos-operator configuration during installing chaos-mesh
    47  type ManagerConfig struct {
    48  	ImageRegistry   string
    49  	ImageRepository string
    50  	ImageTag        string
    51  	ImagePullPolicy string
    52  }
    53  
    54  // DaemonConfig describe the chaos-daemon configuration during installing chaos-mesh
    55  type DaemonConfig struct {
    56  	ImageRegistry   string
    57  	ImageRepository string
    58  	ImageTag        string
    59  	Runtime         string
    60  	SocketPath      string
    61  	ImagePullPolicy string
    62  }
    63  
    64  // NewDefaultOperatorConfig create the default configuration for chaos-mesh test
    65  func NewDefaultOperatorConfig() OperatorConfig {
    66  	return OperatorConfig{
    67  		Namespace:   "chaos-mesh",
    68  		ReleaseName: "chaos-mesh",
    69  		Tag:         "e2e",
    70  		Manager: ManagerConfig{
    71  			ImageRegistry:   "ghcr.io",
    72  			ImageRepository: "chaos-mesh/chaos-mesh",
    73  			ImageTag:        "latest",
    74  			ImagePullPolicy: imagePullPolicyIfNotPresent,
    75  		},
    76  		Daemon: DaemonConfig{
    77  			ImageRegistry:   "ghcr.io",
    78  			ImageRepository: "chaos-mesh/chaos-daemon",
    79  			ImageTag:        "latest",
    80  			ImagePullPolicy: imagePullPolicyIfNotPresent,
    81  			Runtime:         "containerd",
    82  			SocketPath:      "/run/containerd/containerd.sock",
    83  		},
    84  		DNSImage: "ghcr.io/chaos-mesh/chaos-coredns:v0.2.6",
    85  	}
    86  }
    87  
    88  type operatorAction struct {
    89  	framework *framework.Framework
    90  	kubeCli   kubernetes.Interface
    91  	aggrCli   aggregatorclientset.Interface
    92  	apiExtCli apiextensionsclientset.Interface
    93  	cfg       *Config
    94  }
    95  
    96  func (oi *OperatorConfig) operatorHelmSetValue() string {
    97  	set := map[string]string{
    98  		"controllerManager.image.registry":   oi.Manager.ImageRegistry,
    99  		"controllerManager.image.repository": oi.Manager.ImageRepository,
   100  		"controllerManager.image.tag":        oi.Manager.ImageTag,
   101  		"controllerManager.imagePullPolicy":  oi.Manager.ImagePullPolicy,
   102  		"chaosDaemon.image.registry":         oi.Daemon.ImageRegistry,
   103  		"chaosDaemon.image.repository":       oi.Daemon.ImageRepository,
   104  		"chaosDaemon.image.tag":              oi.Daemon.ImageTag,
   105  		"chaosDaemon.runtime":                oi.Daemon.Runtime,
   106  		"chaosDaemon.socketPath":             oi.Daemon.SocketPath,
   107  		"chaosDaemon.imagePullPolicy":        oi.Daemon.ImagePullPolicy,
   108  		"dnsServer.create":                   "true",
   109  		"dnsServer.image":                    oi.DNSImage,
   110  		"dashboard.create":                   fmt.Sprintf("%t", oi.EnableDashboard),
   111  	}
   112  	arr := make([]string, 0, len(set))
   113  	for k, v := range set {
   114  		arr = append(arr, fmt.Sprintf("%s=%s", k, v))
   115  	}
   116  	return fmt.Sprintf("\"%s\"", strings.Join(arr, ","))
   117  }
   118  
   119  func (oa *operatorAction) operatorChartPath(tag string) string {
   120  	return oa.chartPath(operatorChartName, tag)
   121  }
   122  
   123  func (oa *operatorAction) chartPath(name string, tag string) string {
   124  	return filepath.Join(oa.cfg.ChartDir, tag, name)
   125  }
   126  
   127  func (oa *operatorAction) manifestPath(tag string) string {
   128  	return filepath.Join(oa.cfg.ManifestDir, tag)
   129  }
   130  
   131  func (oa *operatorAction) runKubectlOrDie(args ...string) string {
   132  	cmd := "kubectl"
   133  	klog.Infof("Running '%s %s'", cmd, strings.Join(args, " "))
   134  	out, err := exec.Command(cmd, args...).CombinedOutput()
   135  	if err != nil {
   136  		klog.Fatalf("Failed to run '%s %s'\nCombined output: %q\nError: %v", cmd, strings.Join(args, " "), string(out), err)
   137  	}
   138  	klog.Infof("Combined output: %q", string(out))
   139  	return string(out)
   140  }
   141  
   142  func (oa *operatorAction) apiVersions() []string {
   143  	stdout := oa.runKubectlOrDie("api-versions")
   144  	return strings.Split(stdout, "\n")
   145  }
   146