Skip to content

Commit 4aac9f7

Browse files
committed
Fix server port when network mode is host
This is a hack.
1 parent 4628dde commit 4aac9f7

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

pkg/container/docker/client.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -291,20 +291,22 @@ func (c *Client) DeployWorkload(
291291
return 0, nil
292292
}
293293

294+
firstPortInt, err := extractFirstPort(options)
295+
if err != nil {
296+
return 0, err // extractFirstPort already wraps the error with context.
297+
}
294298
if isolateNetwork {
295299
// just extract the first exposed port
296-
firstPortInt, err := extractFirstPort(options)
297-
if err != nil {
298-
return 0, err // extractFirstPort already wraps the error with context.
299-
}
300300
hostPort, err = c.ops.createIngressContainer(ctx, name, firstPortInt, attachStdio, externalEndpointsConfig,
301301
permissionProfile.Network)
302302
if err != nil {
303303
return 0, fmt.Errorf("failed to create ingress container: %v", err)
304304
}
305305
}
306306

307-
return hostPort, nil
307+
finalPort := calculateFinalPort(hostPort, firstPortInt, permissionConfig.NetworkMode)
308+
309+
return finalPort, nil
308310
}
309311

310312
// ListWorkloads lists workloads
@@ -1314,6 +1316,9 @@ func (c *Client) createContainer(
13141316
if err != nil {
13151317
return "", NewContainerError(err, "", fmt.Sprintf("failed to create container: %v", err))
13161318
}
1319+
if resp.Warnings != nil {
1320+
logger.Debugf("Container creation warnings: %v", resp.Warnings)
1321+
}
13171322

13181323
// Start the container
13191324
err = c.api.ContainerStart(ctx, resp.ID, container.StartOptions{})
@@ -1386,10 +1391,21 @@ func (c *Client) createDnsContainer(ctx context.Context, dnsContainerName string
13861391
return dnsContainerId, dnsContainerIP, nil
13871392
}
13881393

1389-
func (c *Client) createMcpContainer(ctx context.Context, name string, networkName string, image string, command []string,
1390-
envVars map[string]string, labels map[string]string, attachStdio bool, permissionConfig *runtime.PermissionConfig,
1391-
additionalDNS string, exposedPorts map[string]struct{}, portBindings map[string][]runtime.PortBinding,
1392-
isolateNetwork bool) error {
1394+
func (c *Client) createMcpContainer(
1395+
ctx context.Context,
1396+
name string,
1397+
networkName string,
1398+
image string,
1399+
command []string,
1400+
envVars map[string]string,
1401+
labels map[string]string,
1402+
attachStdio bool,
1403+
permissionConfig *runtime.PermissionConfig,
1404+
additionalDNS string,
1405+
exposedPorts map[string]struct{},
1406+
portBindings map[string][]runtime.PortBinding,
1407+
isolateNetwork bool,
1408+
) error {
13931409
// Create container configuration
13941410
config := &container.Config{
13951411
Image: image,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//go:build linux
2+
3+
package docker
4+
5+
func calculateFinalPort(hostPort int, firstPortInt int, networkName string) int {
6+
if networkName == "host" {
7+
return firstPortInt
8+
}
9+
return hostPort
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//go:build !linux
2+
3+
package docker
4+
5+
func calculateFinalPort(hostPort int, _ int, _ string) int {
6+
return hostPort
7+
}

pkg/transport/http.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,12 @@ func (t *HTTPTransport) Start(ctx context.Context) error {
321321

322322
// Create the transparent proxy
323323
t.proxy = transparent.NewTransparentProxy(
324-
t.host, t.proxyPort, t.containerName, targetURI,
325-
t.prometheusHandler, t.authInfoHandler,
324+
t.host,
325+
t.proxyPort,
326+
t.containerName,
327+
targetURI,
328+
t.prometheusHandler,
329+
t.authInfoHandler,
326330
t.remoteURL == "",
327331
t.remoteURL != "",
328332
string(t.transportType),

0 commit comments

Comments
 (0)