Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Log missing mapped ports instead of failing #3637

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ IMPROVEMENTS:
* driver/docker: Adds support for `ulimit` and `sysctl` options [GH-3568]
* driver/docker: Adds support for StopTimeout (set to the same value as
kill_timeout [GH-3601]
* driver/rkt: Don't fail on unmapped ports [GH-3637]
* driver/rkt: Add support for passing through user [GH-3612]
* driver/qemu: Support graceful shutdowns on unix platforms [GH-3411]
* template: Updated to consul template 0.19.4 [GH-3543]
Expand Down
36 changes: 19 additions & 17 deletions client/driver/rkt.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,41 +535,43 @@ func (d *RktDriver) Start(ctx *ExecContext, task *structs.Task) (*StartResponse,
} else {
// TODO add support for more than one network
network := task.Resources.Networks[0]
unmappedPorts := make([]string, 0, len(network.ReservedPorts)+len(network.DynamicPorts))
for _, port := range network.ReservedPorts {
var containerPort string

mapped, ok := driverConfig.PortMap[port.Label]
if !ok {
// If the user doesn't have a mapped port using port_map, driver stops running container.
return nil, fmt.Errorf("port_map is not set. When you defined port in the resources, you need to configure port_map.")
unmappedPorts = append(unmappedPorts, port.Label)
continue
}
containerPort = mapped

hostPortStr := strconv.Itoa(port.Value)

d.logger.Printf("[DEBUG] driver.rkt: exposed port %s", containerPort)
d.logger.Printf("[DEBUG] driver.rkt: alloc %s task %s mapped reserved port %s:%s",
d.DriverContext.allocID, task.Name, mapped, hostPortStr)

// Add port option to rkt run arguments. rkt allows multiple port args
prepareArgs = append(prepareArgs, fmt.Sprintf("--port=%s:%s", containerPort, hostPortStr))
prepareArgs = append(prepareArgs, fmt.Sprintf("--port=%s:%s", mapped, hostPortStr))
}

for _, port := range network.DynamicPorts {
// By default we will map the allocated port 1:1 to the container
var containerPort string

if mapped, ok := driverConfig.PortMap[port.Label]; ok {
containerPort = mapped
} else {
// If the user doesn't have mapped a port using port_map, driver stops running container.
return nil, fmt.Errorf("port_map is not set. When you defined port in the resources, you need to configure port_map.")
mapped, ok := driverConfig.PortMap[port.Label]
if !ok {
unmappedPorts = append(unmappedPorts, port.Label)
continue
}

hostPortStr := strconv.Itoa(port.Value)

d.logger.Printf("[DEBUG] driver.rkt: exposed port %s", containerPort)
d.logger.Printf("[DEBUG] driver.rkt: alloc %s task %s mapped dynamic port %s:%s",
d.DriverContext.allocID, task.Name, mapped, hostPortStr)

// Add port option to rkt run arguments. rkt allows multiple port args
prepareArgs = append(prepareArgs, fmt.Sprintf("--port=%s:%s", containerPort, hostPortStr))
prepareArgs = append(prepareArgs, fmt.Sprintf("--port=%s:%s", mapped, hostPortStr))
}

if len(unmappedPorts) > 0 {
d.logger.Printf("[INFO] driver.rkt: alloc %s task %s did not map these ports: %s",
d.DriverContext.allocID, task.Name, strings.Join(unmappedPorts, ", "))
}
}

// If a user has been specified for the task, pass it through to the user
Expand Down
9 changes: 6 additions & 3 deletions client/driver/rkt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ func TestRktTaskValidate(t *testing.T) {
}

// TODO: Port Mapping test should be ran with proper ACI image and test the port access.
func TestRktDriver_PortsMapping(t *testing.T) {
func TestRktDriver_PortMapping(t *testing.T) {
if !testutil.IsTravis() {
t.Parallel()
}
Expand Down Expand Up @@ -483,8 +483,11 @@ func TestRktDriver_PortsMapping(t *testing.T) {
CPU: 512,
Networks: []*structs.NetworkResource{
{
IP: "127.0.0.1",
ReservedPorts: []structs.Port{{Label: "main", Value: 8080}},
IP: "127.0.0.1",
ReservedPorts: []structs.Port{
{Label: "main", Value: 8080},
{Label: "unmapped", Value: 9999},
},
},
},
},
Expand Down