You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ke...@apache.org on 2021/08/13 04:12:38 UTC

[skywalking-infra-e2e] branch main updated: Export service host to the environment (#33)

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

kezhenxu94 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 d71d338  Export service host to the environment (#33)
d71d338 is described below

commit d71d3386355480061f660e2d05ff3f8781311f5d
Author: mrproliu <74...@qq.com>
AuthorDate: Fri Aug 13 12:12:33 2021 +0800

    Export service host to the environment (#33)
---
 internal/components/setup/compose.go | 66 ++++++++++++++++++++++++++++--------
 1 file changed, 51 insertions(+), 15 deletions(-)

diff --git a/internal/components/setup/compose.go b/internal/components/setup/compose.go
index 9ba463c..2ccddbb 100644
--- a/internal/components/setup/compose.go
+++ b/internal/components/setup/compose.go
@@ -78,23 +78,36 @@ func ComposeSetup(e2eConfig *config.E2EConfig) error {
 		if err2 != nil {
 			return err2
 		}
+		if len(portList) == 0 {
+			continue
+		}
+
 		containerPorts := container.Ports
 
+		// get real ip address for access and export to env
+		host, err2 := portList[0].target.Host(context.Background())
+		if err2 != nil {
+			return err2
+		}
+		// format: <service_name>_host
+		if err2 := exportComposeEnv(fmt.Sprintf("%s_host", service), host, service); err2 != nil {
+			return err2
+		}
+
 		for inx := range portList {
 			for _, containerPort := range containerPorts {
-				if int(containerPort.PrivatePort) != portList[inx] {
+				if int(containerPort.PrivatePort) != portList[inx].expectPort {
 					continue
 				}
 
 				// expose env config to env
 				// format: <service_name>_<port>
-				envKey := fmt.Sprintf("%s_%d", service, containerPort.PrivatePort)
-				envValue := fmt.Sprintf("%d", containerPort.PublicPort)
-				err2 = os.Setenv(envKey, envValue)
-				if err2 != nil {
-					return fmt.Errorf("could not set env for %s:%d, %v", service, portList[inx], err2)
+				if err2 := exportComposeEnv(
+					fmt.Sprintf("%s_%d", service, containerPort.PrivatePort),
+					fmt.Sprintf("%d", containerPort.PublicPort),
+					service); err2 != nil {
+					return err2
 				}
-				logger.Log.Infof("expose env : %s : %s", envKey, envValue)
 				break
 			}
 		}
@@ -110,7 +123,16 @@ func ComposeSetup(e2eConfig *config.E2EConfig) error {
 	return nil
 }
 
-func bindWaitPort(e2eConfig *config.E2EConfig, compose *testcontainers.LocalDockerCompose) (map[string][]int, error) {
+func exportComposeEnv(key, value, service string) error {
+	err := os.Setenv(key, value)
+	if err != nil {
+		return fmt.Errorf("could not set env for %s, %v", service, err)
+	}
+	logger.Log.Infof("expose env : %s : %s", key, value)
+	return nil
+}
+
+func bindWaitPort(e2eConfig *config.E2EConfig, compose *testcontainers.LocalDockerCompose) (map[string][]*hostPortCachedStrategy, error) {
 	timeout := e2eConfig.Setup.Timeout
 	var waitTimeout time.Duration
 	if timeout <= 0 {
@@ -118,14 +140,14 @@ func bindWaitPort(e2eConfig *config.E2EConfig, compose *testcontainers.LocalDock
 	} else {
 		waitTimeout = time.Duration(timeout) * time.Second
 	}
-	serviceWithPorts := make(map[string][]int)
+	serviceWithPorts := make(map[string][]*hostPortCachedStrategy)
 	for service, content := range compose.Services {
 		serviceConfig := content.(map[interface{}]interface{})
 		ports := serviceConfig["ports"]
 		if ports == nil {
 			continue
 		}
-		serviceWithPorts[service] = []int{}
+		serviceWithPorts[service] = []*hostPortCachedStrategy{}
 
 		portList := ports.([]interface{})
 		for inx := range portList {
@@ -133,12 +155,14 @@ func bindWaitPort(e2eConfig *config.E2EConfig, compose *testcontainers.LocalDock
 			if err != nil {
 				return nil, err
 			}
-			serviceWithPorts[service] = append(serviceWithPorts[service], exportPort)
 
-			compose.WithExposedService(
-				service,
-				exportPort,
-				wait.NewHostPortStrategy(nat.Port(fmt.Sprintf("%d/tcp", exportPort))).WithStartupTimeout(waitTimeout))
+			strategy := &hostPortCachedStrategy{
+				expectPort:       exportPort,
+				HostPortStrategy: *wait.NewHostPortStrategy(nat.Port(fmt.Sprintf("%d/tcp", exportPort))).WithStartupTimeout(waitTimeout),
+			}
+			compose.WithExposedService(service, exportPort, strategy)
+
+			serviceWithPorts[service] = append(serviceWithPorts[service], strategy)
 		}
 	}
 	return serviceWithPorts, nil
@@ -182,3 +206,15 @@ func getInstanceName(serviceName string) string {
 	}
 	return serviceName
 }
+
+// hostPortCachedStrategy cached original target
+type hostPortCachedStrategy struct {
+	wait.HostPortStrategy
+	expectPort int
+	target     wait.StrategyTarget
+}
+
+func (hp *hostPortCachedStrategy) WaitUntilReady(ctx context.Context, target wait.StrategyTarget) error {
+	hp.target = target
+	return hp.HostPortStrategy.WaitUntilReady(ctx, target)
+}