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

[skywalking-infra-e2e] branch docker updated: Export host

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

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


The following commit(s) were added to refs/heads/docker by this push:
     new d7fe760  Export host
d7fe760 is described below

commit d7fe760843aa7661150ca1257a1981e209ab07e6
Author: mrproliu <74...@qq.com>
AuthorDate: Thu Aug 12 23:44:28 2021 +0800

    Export host
---
 internal/components/setup/compose.go | 41 ++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 11 deletions(-)

diff --git a/internal/components/setup/compose.go b/internal/components/setup/compose.go
index 9ba463c..0a4edf0 100644
--- a/internal/components/setup/compose.go
+++ b/internal/components/setup/compose.go
@@ -62,7 +62,8 @@ func ComposeSetup(e2eConfig *config.E2EConfig) error {
 	compose := testcontainers.NewLocalDockerCompose(composeFilePaths, identifier)
 
 	// bind wait port
-	serviceWithPorts, err := bindWaitPort(e2eConfig, compose)
+	cachedTarget := make(map[nat.Port]*wait.StrategyTarget)
+	serviceWithPorts, err := bindWaitPort(e2eConfig, compose, cachedTarget)
 	if err != nil {
 		return fmt.Errorf("bind wait ports error: %v", err)
 	}
@@ -82,14 +83,19 @@ func ComposeSetup(e2eConfig *config.E2EConfig) error {
 
 		for inx := range portList {
 			for _, containerPort := range containerPorts {
-				if int(containerPort.PrivatePort) != portList[inx] {
+				if int(containerPort.PrivatePort) != portList[inx].expectPort {
 					continue
 				}
 
+				host, err := portList[inx].target.Host(context.Background())
+				if err != nil {
+					return err
+				}
+
 				// expose env config to env
 				// format: <service_name>_<port>
 				envKey := fmt.Sprintf("%s_%d", service, containerPort.PrivatePort)
-				envValue := fmt.Sprintf("%d", containerPort.PublicPort)
+				envValue := fmt.Sprintf("%s:%d", host, 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)
@@ -110,7 +116,7 @@ func ComposeSetup(e2eConfig *config.E2EConfig) error {
 	return nil
 }
 
-func bindWaitPort(e2eConfig *config.E2EConfig, compose *testcontainers.LocalDockerCompose) (map[string][]int, error) {
+func bindWaitPort(e2eConfig *config.E2EConfig, compose *testcontainers.LocalDockerCompose, cached map[nat.Port]*wait.StrategyTarget) (map[string][]*composeHostPortCachedStrategy, error) {
 	timeout := e2eConfig.Setup.Timeout
 	var waitTimeout time.Duration
 	if timeout <= 0 {
@@ -118,14 +124,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][]*composeHostPortCachedStrategy)
 	for service, content := range compose.Services {
 		serviceConfig := content.(map[interface{}]interface{})
 		ports := serviceConfig["ports"]
 		if ports == nil {
 			continue
 		}
-		serviceWithPorts[service] = []int{}
+		serviceWithPorts[service] = []*composeHostPortCachedStrategy{}
 
 		portList := ports.([]interface{})
 		for inx := range portList {
@@ -133,12 +139,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 := &composeHostPortCachedStrategy{
+				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 +190,14 @@ func getInstanceName(serviceName string) string {
 	}
 	return serviceName
 }
+
+type composeHostPortCachedStrategy struct {
+	wait.HostPortStrategy
+	expectPort int
+	target     wait.StrategyTarget
+}
+
+func (hp *composeHostPortCachedStrategy) WaitUntilReady(ctx context.Context, target wait.StrategyTarget) error {
+	hp.target = target
+	return hp.HostPortStrategy.WaitUntilReady(ctx, target)
+}