Skip to content

Commit 9019ea3

Browse files
committed
Allow for SSE transport to expose port
Signed-off-by: Juan Antonio Osorio <[email protected]>
1 parent 1b80529 commit 9019ea3

File tree

3 files changed

+97
-5
lines changed

3 files changed

+97
-5
lines changed

cmd/vibetool/run.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,23 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
167167
}
168168
}
169169

170+
// Create container options
171+
containerOptions := container.NewCreateContainerOptions()
172+
173+
// Set options based on transport type
174+
if transportType == transport.TransportTypeSSE {
175+
// For SSE transport, expose the container port
176+
containerPortStr := fmt.Sprintf("%d/tcp", port)
177+
containerOptions.ExposedPorts[containerPortStr] = struct{}{}
178+
fmt.Printf("Exposing container port %d\n", port)
179+
180+
// For SSE transport, we don't need to attach stdio
181+
containerOptions.AttachStdio = false
182+
} else if transportType == transport.TransportTypeStdio {
183+
// For STDIO transport, we need to attach stdio
184+
containerOptions.AttachStdio = true
185+
}
186+
170187
// Create the container
171188
fmt.Printf("Creating container %s from image %s...\n", containerName, image)
172189
containerID, err := runtime.CreateContainer(
@@ -177,6 +194,7 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
177194
envVars,
178195
containerLabels,
179196
containerPermConfig,
197+
containerOptions,
180198
)
181199
if err != nil {
182200
return fmt.Errorf("failed to create container: %v", err)

pkg/container/client.go

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/docker/docker/api/types/mount"
1717
"github.com/docker/docker/api/types/network"
1818
"github.com/docker/docker/client"
19+
"github.com/docker/go-connections/nat"
1920
)
2021

2122
// Common socket paths
@@ -125,12 +126,14 @@ func findContainerSocket() (string, RuntimeType, error) {
125126
}
126127

127128
// CreateContainer creates a container without starting it
129+
// If options is nil, default options will be used
128130
func (c *Client) CreateContainer(
129131
ctx context.Context,
130132
image, name string,
131133
command []string,
132134
envVars, labels map[string]string,
133135
permissionConfig PermissionConfig,
136+
options *CreateContainerOptions,
134137
) (string, error) {
135138
// Convert environment variables to slice
136139
env := make([]string, 0, len(envVars))
@@ -149,19 +152,35 @@ func (c *Client) CreateContainer(
149152
})
150153
}
151154

155+
// Determine if we should attach stdio
156+
// Default to true if options is nil, otherwise use options.AttachStdio
157+
attachStdio := options == nil || options.AttachStdio
158+
152159
// Create container configuration
153160
config := &container.Config{
154161
Image: image,
155162
Cmd: command,
156163
Env: env,
157164
Labels: labels,
158-
AttachStdin: true,
159-
AttachStdout: true,
160-
AttachStderr: true,
161-
OpenStdin: true,
165+
AttachStdin: attachStdio,
166+
AttachStdout: attachStdio,
167+
AttachStderr: attachStdio,
168+
OpenStdin: attachStdio,
162169
Tty: false,
163170
}
164171

172+
// Add exposed ports if provided
173+
if options != nil && len(options.ExposedPorts) > 0 {
174+
config.ExposedPorts = nat.PortSet{}
175+
for port := range options.ExposedPorts {
176+
natPort, err := nat.NewPort("tcp", strings.Split(port, "/")[0])
177+
if err != nil {
178+
return "", NewContainerError(err, "", fmt.Sprintf("failed to parse port: %v", err))
179+
}
180+
config.ExposedPorts[natPort] = struct{}{}
181+
}
182+
}
183+
165184
// Create host configuration
166185
hostConfig := &container.HostConfig{
167186
Mounts: mounts,
@@ -171,6 +190,26 @@ func (c *Client) CreateContainer(
171190
SecurityOpt: permissionConfig.SecurityOpt,
172191
}
173192

193+
// Add port bindings if provided
194+
if options != nil && len(options.PortBindings) > 0 {
195+
hostConfig.PortBindings = nat.PortMap{}
196+
for port, bindings := range options.PortBindings {
197+
natPort, err := nat.NewPort("tcp", strings.Split(port, "/")[0])
198+
if err != nil {
199+
return "", NewContainerError(err, "", fmt.Sprintf("failed to parse port: %v", err))
200+
}
201+
202+
natBindings := make([]nat.PortBinding, len(bindings))
203+
for i, binding := range bindings {
204+
natBindings[i] = nat.PortBinding{
205+
HostIP: binding.HostIP,
206+
HostPort: binding.HostPort,
207+
}
208+
}
209+
hostConfig.PortBindings[natPort] = natBindings
210+
}
211+
}
212+
174213
// Create network configuration
175214
networkConfig := &network.NetworkingConfig{}
176215

pkg/container/types.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ type PortMapping struct {
3939
// Runtime defines the interface for container runtimes
4040
type Runtime interface {
4141
// CreateContainer creates a container without starting it
42-
CreateContainer(ctx context.Context, image, name string, command []string, envVars, labels map[string]string, permissionConfig PermissionConfig) (string, error)
42+
// If options is nil, default options will be used
43+
CreateContainer(ctx context.Context, image, name string, command []string, envVars, labels map[string]string, permissionConfig PermissionConfig, options *CreateContainerOptions) (string, error)
4344

4445
// StartContainer starts a container
4546
StartContainer(ctx context.Context, containerID string) error
@@ -93,6 +94,40 @@ type PermissionConfig struct {
9394
SecurityOpt []string
9495
}
9596

97+
// CreateContainerOptions represents options for creating a container
98+
type CreateContainerOptions struct {
99+
// ExposedPorts is a map of container ports to expose
100+
// The key is in the format "port/protocol" (e.g., "8080/tcp")
101+
// The value is an empty struct (not used)
102+
ExposedPorts map[string]struct{}
103+
104+
// PortBindings is a map of container ports to host ports
105+
// The key is in the format "port/protocol" (e.g., "8080/tcp")
106+
// The value is a slice of host port bindings
107+
PortBindings map[string][]PortBinding
108+
109+
// AttachStdio indicates whether to attach stdin/stdout/stderr
110+
// This is typically set to true for stdio transport
111+
AttachStdio bool
112+
}
113+
114+
// PortBinding represents a host port binding
115+
type PortBinding struct {
116+
// HostIP is the host IP to bind to (empty for all interfaces)
117+
HostIP string
118+
// HostPort is the host port to bind to (empty for random port)
119+
HostPort string
120+
}
121+
122+
// NewCreateContainerOptions creates a new CreateContainerOptions with default values
123+
func NewCreateContainerOptions() *CreateContainerOptions {
124+
return &CreateContainerOptions{
125+
ExposedPorts: make(map[string]struct{}),
126+
PortBindings: make(map[string][]PortBinding),
127+
AttachStdio: false,
128+
}
129+
}
130+
96131
// Mount represents a volume mount
97132
type Mount struct {
98133
// Source is the source path on the host

0 commit comments

Comments
 (0)