You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by zh...@apache.org on 2021/03/25 06:56:04 UTC

[skywalking-infra-e2e] branch main updated: Refactor setup part (#15)

This is an automated email from the ASF dual-hosted git repository.

zhangke pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-infra-e2e.git


The following commit(s) were added to refs/heads/main by this push:
     new 3ea8e50  Refactor setup part (#15)
3ea8e50 is described below

commit 3ea8e501eb5e29176432a58ac0f40846be6fc816
Author: Humbert Zhang <50...@qq.com>
AuthorDate: Thu Mar 25 14:55:56 2021 +0800

    Refactor setup part (#15)
    
    * refactor setup part
    
    * fix bugs and polish codes
    
    * polish comment
---
 commands/cleanup/cleanup.go         |   4 +-
 commands/run/run.go                 |  22 ++++++++
 commands/verify/verify.go           |   6 +--
 examples/simple/e2e.yaml            |  41 +++++++--------
 internal/components/setup/common.go |  17 +++---
 internal/components/setup/kind.go   | 100 ++++++++++++++++++++----------------
 internal/config/e2eConfig.go        |  19 +++----
 internal/constant/kind.go           |  13 +++++
 8 files changed, 130 insertions(+), 92 deletions(-)

diff --git a/commands/cleanup/cleanup.go b/commands/cleanup/cleanup.go
index d5d2024..dd87482 100644
--- a/commands/cleanup/cleanup.go
+++ b/commands/cleanup/cleanup.go
@@ -34,7 +34,7 @@ var Cleanup = &cobra.Command{
 	Use:   "cleanup",
 	Short: "",
 	RunE: func(cmd *cobra.Command, args []string) error {
-		err := cleanupAccordingE2E()
+		err := DoCleanupAccordingE2E()
 		if err != nil {
 			err = fmt.Errorf("[Cleanup] %s", err)
 			return err
@@ -43,7 +43,7 @@ var Cleanup = &cobra.Command{
 	},
 }
 
-func cleanupAccordingE2E() error {
+func DoCleanupAccordingE2E() error {
 	e2eConfig := config.GlobalConfig.E2EConfig
 	if e2eConfig.Setup.Env == constant.Kind {
 		err := cleanup.KindCleanUp(&e2eConfig)
diff --git a/commands/run/run.go b/commands/run/run.go
index 5827d9d..2a32006 100644
--- a/commands/run/run.go
+++ b/commands/run/run.go
@@ -18,7 +18,10 @@
 package run
 
 import (
+	"github.com/apache/skywalking-infra-e2e/commands/cleanup"
 	"github.com/apache/skywalking-infra-e2e/commands/setup"
+	"github.com/apache/skywalking-infra-e2e/commands/trigger"
+	"github.com/apache/skywalking-infra-e2e/commands/verify"
 	"github.com/apache/skywalking-infra-e2e/internal/config"
 	"github.com/apache/skywalking-infra-e2e/internal/logger"
 
@@ -50,9 +53,28 @@ func runAccordingE2E() error {
 	}
 	logger.Log.Infof("setup part finished successfully")
 
+	// cleanup part
+	defer func() {
+		err = cleanup.DoCleanupAccordingE2E()
+		if err != nil {
+			logger.Log.Errorf("cleanup part error: %s", err)
+		}
+		logger.Log.Infof("cleanup part finished successfully")
+	}()
+
 	// trigger part
+	err = trigger.DoActionAccordingE2E()
+	if err != nil {
+		return err
+	}
+	logger.Log.Infof("trigger part finished successfully")
 
 	// verify part
+	err = verify.DoVerifyAccordingConfig()
+	if err != nil {
+		return err
+	}
+	logger.Log.Infof("verify part finished successfully")
 
 	return nil
 }
diff --git a/commands/verify/verify.go b/commands/verify/verify.go
index 0e700b9..290cf26 100644
--- a/commands/verify/verify.go
+++ b/commands/verify/verify.go
@@ -48,7 +48,7 @@ var Verify = &cobra.Command{
 			return verifySingleCase(expected, actual, query)
 		}
 		// If there is no given flags.
-		return verifyAccordingConfig()
+		return DoVerifyAccordingConfig()
 	},
 }
 
@@ -84,8 +84,8 @@ func verifySingleCase(expectedFile, actualFile, query string) error {
 	return nil
 }
 
-// verifyAccordingConfig reads cases from the config file and verifies them.
-func verifyAccordingConfig() error {
+// DoVerifyAccordingConfig reads cases from the config file and verifies them.
+func DoVerifyAccordingConfig() error {
 	if config.GlobalConfig.Error != nil {
 		return config.GlobalConfig.Error
 	}
diff --git a/examples/simple/e2e.yaml b/examples/simple/e2e.yaml
index e486887..f71d3a8 100644
--- a/examples/simple/e2e.yaml
+++ b/examples/simple/e2e.yaml
@@ -18,38 +18,33 @@
 setup:
   env: kind
   file: kind.yaml
-  manifests:
-    - path: busybox1.yaml
-      wait: 
-        - namespace: default
-          resource: pod/busybox1 
-          for: condition=Ready
-    - path: manifests,manifests2
+  steps:
+    - command: |
+        if test -d "$TMPDIR"; then
+            :
+        else
+            TMPDIR=/tmp
+        fi
+
+        cp $TMPDIR/e2e-k8s.config ~/.kube/config
+        export ISTIO_VERSION=1.9.1
+        istioctl version || (curl -L https://istio.io/downloadIstio | sh - && sudo mv $PWD/istio-$ISTIO_VERSION/bin/istioctl /usr/local/bin/)
+        istioctl install -y --set profile=demo \
+            --set meshConfig.defaultConfig.envoyMetricsService.address=skywalking-oap.istio-system:11800 \
+            --set values.telemetry.v2.enabled=false
+        kubectl label namespace default istio-injection=enabled
+    - path: busybox1.yaml,manifests
       wait:
         - namespace: default
           resource: pod
-          label-selector: app=busybox2
-          for: condition=Ready
-        - namespace: default
-          resource: pod
           for: condition=Ready
-  runs: # commands are serial within one code block and parallel between code blocks
-    - command: | # it can be a shell script or anything executable
-        cp $TMPDIR/e2e-k8s.config ~/.kube/config
+    - command: |
         kubectl create deployment nginx1 --image=nginx
       wait:
         - namespace: default
           resource: deployment/nginx1
           for: condition=Available
-    - command: |
-        cp $TMPDIR/e2e-k8s.config ~/.kube/config
-        kubectl create deployment nginx2 \
-                                  --image=nginx
-      wait:
-        - namespace: default
-          resource: deployment/nginx2
-          for: condition=Available
-  timeout: 600
+  timeout: 1200
 
 trigger:
   action: http
diff --git a/internal/components/setup/common.go b/internal/components/setup/common.go
index cae40c4..bf3aa9b 100644
--- a/internal/components/setup/common.go
+++ b/internal/components/setup/common.go
@@ -29,20 +29,17 @@ import (
 )
 
 // RunCommandsAndWait Concurrently run commands and wait for conditions.
-func RunCommandsAndWait(runs []config.Run, timeout time.Duration) error {
+func RunCommandsAndWait(run config.Run, timeout time.Duration) error {
 	waitSet := util.NewWaitSet(timeout)
 
-	for idx := range runs {
-		run := runs[idx]
-		commands := run.Command
-		if len(commands) < 1 {
-			continue
-		}
-
-		waitSet.WaitGroup.Add(1)
-		go executeCommandsAndWait(commands, run.Waits, waitSet)
+	commands := run.Command
+	if len(commands) < 1 {
+		return nil
 	}
 
+	waitSet.WaitGroup.Add(1)
+	go executeCommandsAndWait(commands, run.Waits, waitSet)
+
 	go func() {
 		waitSet.WaitGroup.Wait()
 		close(waitSet.FinishChan)
diff --git a/internal/components/setup/kind.go b/internal/components/setup/kind.go
index c4c4f27..915a207 100644
--- a/internal/components/setup/kind.go
+++ b/internal/components/setup/kind.go
@@ -58,10 +58,10 @@ func KindSetup(e2eConfig *config.E2EConfig) error {
 		return fmt.Errorf("no kind config file was provided")
 	}
 
-	manifests := e2eConfig.Setup.Manifests
+	manifests := e2eConfig.Setup.Steps
 	// if no manifests was provided, then no need to create the cluster.
 	if manifests == nil {
-		logger.Log.Info("no manifests is provided")
+		logger.Log.Info("no steps is provided")
 		return nil
 	}
 
@@ -75,6 +75,7 @@ func KindSetup(e2eConfig *config.E2EConfig) error {
 		return err
 	}
 
+	// run commands and manifests
 	timeout := e2eConfig.Setup.Timeout
 	var waitTimeout time.Duration
 	if timeout <= 0 {
@@ -82,28 +83,40 @@ func KindSetup(e2eConfig *config.E2EConfig) error {
 	} else {
 		waitTimeout = time.Duration(timeout) * time.Second
 	}
-
 	logger.Log.Debugf("wait timeout is %d seconds", int(waitTimeout.Seconds()))
 
 	// record time now
 	timeNow := time.Now()
 
-	err = createManifestsAndWait(c, dc, manifests, waitTimeout)
-	if err != nil {
-		return err
-	}
+	for _, step := range e2eConfig.Setup.Steps {
+		if step.Path != "" && step.Command == "" {
+			manifest := config.Manifest{
+				Path:  step.Path,
+				Waits: step.Waits,
+			}
+			err = createManifestAndWait(c, dc, manifest, waitTimeout)
+			if err != nil {
+				return err
+			}
+		} else if step.Command != "" && step.Path == "" {
+			command := config.Run{
+				Command: step.Command,
+				Waits:   step.Waits,
+			}
 
-	// calculates new timeout. manifests and run of setup uses the same countdown.
-	runWaitTimeout := NewTimeout(timeNow, waitTimeout)
-	if runWaitTimeout <= 0 {
-		return fmt.Errorf("kind setup timeout")
-	}
+			err := RunCommandsAndWait(command, waitTimeout)
+			if err != nil {
+				return err
+			}
+		} else {
+			return fmt.Errorf("step parameter error, one Path or one Command should be specified, but got %+v", step)
+		}
 
-	if len(e2eConfig.Setup.Runs) > 0 {
-		logger.Log.Debugf("executing commands...")
-		err := RunCommandsAndWait(e2eConfig.Setup.Runs, runWaitTimeout)
-		if err != nil {
-			return err
+		waitTimeout = NewTimeout(timeNow, waitTimeout)
+		timeNow = time.Now()
+
+		if waitTimeout <= 0 {
+			return fmt.Errorf("kind setup timeout")
 		}
 	}
 	return nil
@@ -123,8 +136,8 @@ func createKindCluster(kindConfigPath string) error {
 	return nil
 }
 
-// createManifestsAndWait creates manifests in k8s cluster and concurrent waits according to the manifests' wait conditions.
-func createManifestsAndWait(c *kubernetes.Clientset, dc dynamic.Interface, manifests []config.Manifest, timeout time.Duration) error {
+// createManifestAndWait creates manifests in k8s cluster and concurrent waits according to the manifests' wait conditions.
+func createManifestAndWait(c *kubernetes.Clientset, dc dynamic.Interface, manifest config.Manifest, timeout time.Duration) error {
 	waitSet := util.NewWaitSet(timeout)
 
 	kubeConfigYaml, err := ioutil.ReadFile(kubeConfigPath)
@@ -132,32 +145,29 @@ func createManifestsAndWait(c *kubernetes.Clientset, dc dynamic.Interface, manif
 		return err
 	}
 
-	for idx := range manifests {
-		manifest := manifests[idx]
-		waits := manifest.Waits
-		err := createByManifest(c, dc, manifest)
-		if err != nil {
-			return err
-		}
-
-		// len() for nil slices is defined as zero
-		if len(waits) == 0 {
-			logger.Log.Info("no wait-for strategy is provided")
-			continue
-		}
+	waits := manifest.Waits
+	err = createByManifest(c, dc, manifest)
+	if err != nil {
+		return err
+	}
 
-		for idx := range waits {
-			wait := waits[idx]
-			logger.Log.Infof("waiting for %+v", wait)
+	// len() for nil slices is defined as zero
+	if len(waits) == 0 {
+		logger.Log.Info("no wait-for strategy is provided")
+		return nil
+	}
 
-			options, err := getWaitOptions(kubeConfigYaml, &wait)
-			if err != nil {
-				return err
-			}
+	for idx := range waits {
+		wait := waits[idx]
+		logger.Log.Infof("waiting for %+v", wait)
 
-			waitSet.WaitGroup.Add(1)
-			go concurrentlyWait(&wait, options, waitSet)
+		options, err := getWaitOptions(kubeConfigYaml, &wait)
+		if err != nil {
+			return err
 		}
+
+		waitSet.WaitGroup.Add(1)
+		go concurrentlyWait(&wait, options, waitSet)
 	}
 
 	go func() {
@@ -167,12 +177,12 @@ func createManifestsAndWait(c *kubernetes.Clientset, dc dynamic.Interface, manif
 
 	select {
 	case <-waitSet.FinishChan:
-		logger.Log.Infof("create and wait for manifests ready success")
+		logger.Log.Infof("create and wait for manifest ready success")
 	case err := <-waitSet.ErrChan:
-		logger.Log.Errorf("failed to wait for manifests to be ready")
+		logger.Log.Errorf("failed to wait for manifest to be ready")
 		return err
 	case <-time.After(waitSet.Timeout):
-		return fmt.Errorf("wait for manifests ready timeout after %d seconds", int(timeout.Seconds()))
+		return fmt.Errorf("wait for manifest ready timeout after %d seconds", int(timeout.Seconds()))
 	}
 
 	return nil
@@ -214,7 +224,7 @@ func getWaitOptions(kubeConfigYaml []byte, wait *config.Wait) (options *ctlwait.
 }
 
 func createByManifest(c *kubernetes.Clientset, dc dynamic.Interface, manifest config.Manifest) error {
-	files, err := util.GetManifests(manifest.GetPath())
+	files, err := util.GetManifests(manifest.Path)
 	if err != nil {
 		logger.Log.Error("get manifests failed")
 		return err
diff --git a/internal/config/e2eConfig.go b/internal/config/e2eConfig.go
index af90953..23620f1 100644
--- a/internal/config/e2eConfig.go
+++ b/internal/config/e2eConfig.go
@@ -28,11 +28,16 @@ type E2EConfig struct {
 }
 
 type Setup struct {
-	Env       string     `yaml:"env"`
-	File      string     `yaml:"file"`
-	Manifests []Manifest `yaml:"manifests"`
-	Runs      []Run      `yaml:"runs"`
-	Timeout   int        `yaml:"timeout"`
+	Env     string `yaml:"env"`
+	File    string `yaml:"file"`
+	Steps   []Step `yaml:"steps"`
+	Timeout int    `yaml:"timeout"`
+}
+
+type Step struct {
+	Path    string `yaml:"path"`
+	Command string `yaml:"command"`
+	Waits   []Wait `yaml:"wait"`
 }
 
 func (s *Setup) GetFile() string {
@@ -44,10 +49,6 @@ type Manifest struct {
 	Waits []Wait `yaml:"wait"`
 }
 
-func (m Manifest) GetPath() string {
-	return m.Path
-}
-
 type Run struct {
 	Command string `yaml:"command"`
 	Waits   []Wait `yaml:"wait"`
diff --git a/internal/constant/kind.go b/internal/constant/kind.go
index b6bb7cb..9f0ec8a 100644
--- a/internal/constant/kind.go
+++ b/internal/constant/kind.go
@@ -32,8 +32,21 @@ const (
 	K8sClusterConfigFileName = "e2e-k8s.config"
 	DefaultWaitTimeout       = 600 * time.Second
 	SingleDefaultWaitTimeout = 30 * 60 * time.Second
+	StepTypeManifest         = "manifest"
+	StepTypeCommand          = "command"
 )
 
+func init() {
+	tmpDirEnv := os.Getenv("TMPDIR")
+	// TMPDIR maybe "", try to set tmpdir here, so that user can get kubeconfig from TMPDIR.
+	if tmpDirEnv == "" {
+		err := os.Setenv("TMPDIR", "/tmp")
+		if err != nil {
+			panic(err)
+		}
+	}
+}
+
 var (
 	True                     = true
 	False                    = false