Skip to content

Commit

Permalink
hack up port mapping to allow debug port exposure
Browse files Browse the repository at this point in the history
  • Loading branch information
francoposa committed Feb 8, 2024
1 parent db90b84 commit 129bde3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 31 deletions.
6 changes: 4 additions & 2 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ func NewMemcached() *e2e.ConcreteService {
images.Memcached,
nil,
e2e.NewTCPReadinessProbe(MemcachedPort),
MemcachedPort,
[]int{MemcachedPort},
nil,
)
}

Expand All @@ -35,6 +36,7 @@ func NewRedis() *e2e.ConcreteService {
"--appendonly", "no",
),
e2e.NewTCPReadinessProbe(RedisPort),
RedisPort,
[]int{RedisPort},
nil,
)
}
11 changes: 10 additions & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func newMinio(port int, envVars map[string]string, bktNames ...string) *e2e.HTTP
e2e.NewCommandWithoutEntrypoint("sh", "-c", strings.Join(commands, " && ")),
e2e.NewHTTPReadinessProbe(port, "/minio/health/ready", 200, 200),
port,
nil,
nil,
)
envVars["MINIO_ACCESS_KEY"] = MinioAccessKey
envVars["MINIO_SECRET_KEY"] = MinioSecretKey
Expand All @@ -71,6 +73,8 @@ func NewKES(port int, serverName, serverKeyFile, serverCertFile, clientKeyFile,
e2e.NewCommandWithoutEntrypoint("sh", "-c", command),
readinessProbe,
port,
nil,
nil,
), nil
}

Expand All @@ -82,6 +86,8 @@ func NewConsul() *e2e.HTTPService {
e2e.NewCommand("agent", "-server", "-client=0.0.0.0", "-dev", "-log-level=err"),
e2e.NewHTTPReadinessProbe(8500, "/v1/operator/autopilot/health", 200, 200, `"Healthy": true`),
8500,
nil,
nil,
)
}

Expand All @@ -92,7 +98,8 @@ func NewETCD() *e2e.HTTPService {
e2e.NewCommand("/usr/local/bin/etcd", "--listen-client-urls=http://0.0.0.0:2379", "--advertise-client-urls=http://0.0.0.0:2379", "--listen-metrics-urls=http://0.0.0.0:9000", "--log-level=error"),
e2e.NewHTTPReadinessProbe(9000, "/health", 200, 204),
2379,
9000, // Metrics
[]int{9000}, // Metrics
nil,
)
}

Expand All @@ -104,5 +111,7 @@ func NewDynamoDB() *e2e.HTTPService {
// DynamoDB doesn't have a readiness probe, so we check if the / works even if returns 400
e2e.NewHTTPReadinessProbe(8000, "/", 400, 400),
8000,
nil,
nil,
)
}
2 changes: 2 additions & 0 deletions db/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func NewKafka() *KafkaService {
nil, // No custom command.
NewKafkaReadinessProbe(9092),
9092,
nil,
nil,
),
}
}
Expand Down
62 changes: 36 additions & 26 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ var (
// ConcreteService can be reused (started and stopped many time), but it can represent only one running container
// at the time.
type ConcreteService struct {
name string
image string
networkPorts []int
env map[string]string
user string
command *Command
cmd *exec.Cmd
readiness ReadinessProbe
privileged bool
name string
image string
ports []int
env map[string]string
user string
command *Command
cmd *exec.Cmd
readiness ReadinessProbe
privileged bool

// Maps container ports to dynamically binded local ports.
networkPortsContainerToLocal map[int]int
portMapContainerToLocal map[int]int

// Generic retry backoff.
retryBackoff *backoff.Backoff
Expand All @@ -58,15 +58,19 @@ func NewConcreteService(
image string,
command *Command,
readiness ReadinessProbe,
networkPorts ...int,
ports []int,
portMapContainerToLocal map[int]int,
) *ConcreteService {
if portMapContainerToLocal == nil {
portMapContainerToLocal = map[int]int{}
}
return &ConcreteService{
name: name,
image: image,
networkPorts: networkPorts,
command: command,
networkPortsContainerToLocal: map[int]int{},
readiness: readiness,
name: name,
image: image,
ports: ports,
command: command,
portMapContainerToLocal: portMapContainerToLocal,
readiness: readiness,
retryBackoff: backoff.New(context.Background(), backoff.Config{
MinBackoff: 300 * time.Millisecond,
MaxBackoff: 600 * time.Millisecond,
Expand Down Expand Up @@ -123,7 +127,7 @@ func (s *ConcreteService) Start(networkName, sharedDir string) (err error) {
}

// Get the dynamic local ports mapped to the container.
for _, containerPort := range s.networkPorts {
for _, containerPort := range s.ports {
var out []byte

out, err = RunCommandAndGetOutput("docker", "port", s.containerName(), strconv.Itoa(containerPort))
Expand All @@ -140,10 +144,10 @@ func (s *ConcreteService) Start(networkName, sharedDir string) (err error) {
return errors.Wrapf(err, "unable to get mapping for port %d (output: %s); service: %s", containerPort, string(out), s.name)
}

s.networkPortsContainerToLocal[containerPort] = localPort
s.portMapContainerToLocal[containerPort] = localPort
}

logger.Log("Ports for container:", s.containerName(), "Mapping:", s.networkPortsContainerToLocal)
logger.Log("Ports for container:", s.containerName(), "Mapping:", s.portMapContainerToLocal)
return nil
}

Expand Down Expand Up @@ -195,7 +199,7 @@ func (s *ConcreteService) Endpoint(port int) string {
}

// Map the container port to the local port.
localPort, ok := s.networkPortsContainerToLocal[port]
localPort, ok := s.portMapContainerToLocal[port]
if !ok {
return ""
}
Expand Down Expand Up @@ -324,8 +328,13 @@ func (s *ConcreteService) buildDockerRunArgs(networkName, sharedDir string) []st
}

// Published ports
for _, port := range s.networkPorts {
args = append(args, "-p", strconv.Itoa(port))
for _, containerPort := range s.ports {
if localPort, ok := s.portMapContainerToLocal[containerPort]; ok {
args = append(args, "-p", strconv.Itoa(localPort)+":"+strconv.Itoa(containerPort))

} else {
args = append(args, "-p", strconv.Itoa(containerPort))
}
}

// Disable entrypoint if required
Expand Down Expand Up @@ -560,10 +569,11 @@ func NewHTTPService(
command *Command,
readiness ReadinessProbe,
httpPort int,
otherPorts ...int,
otherPorts []int,
portMapContainerToLocal map[int]int,
) *HTTPService {
return &HTTPService{
ConcreteService: NewConcreteService(name, image, command, readiness, append(otherPorts, httpPort)...),
ConcreteService: NewConcreteService(name, image, command, readiness, append(otherPorts, httpPort), portMapContainerToLocal),
metricsTimeout: time.Second,
httpPort: httpPort,
}
Expand All @@ -575,7 +585,7 @@ func (s *HTTPService) SetMetricsTimeout(timeout time.Duration) {

func (s *HTTPService) Metrics() (_ string, err error) {
// Map the container port to the local port
localPort := s.networkPortsContainerToLocal[s.httpPort]
localPort := s.portMapContainerToLocal[s.httpPort]

// Fetch metrics.
// Use an IPv4 address instead of "localhost" hostname because our port mapping assumes IPv4
Expand Down
4 changes: 2 additions & 2 deletions service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ metric_b_summary_count 1
s := &HTTPService{
httpPort: 0,
ConcreteService: &ConcreteService{
networkPortsContainerToLocal: map[int]int{
portMapContainerToLocal: map[int]int{
0: port,
},
},
Expand Down Expand Up @@ -158,7 +158,7 @@ metric_b 1000
s := &HTTPService{
httpPort: 0,
ConcreteService: &ConcreteService{
networkPortsContainerToLocal: map[int]int{
portMapContainerToLocal: map[int]int{
0: port,
},
},
Expand Down

0 comments on commit 129bde3

Please sign in to comment.