Skip to content

Commit

Permalink
Log missing mapped ports instead of failing
Browse files Browse the repository at this point in the history
SDNs, overlays, etc often won't need to map ports.
  • Loading branch information
schmichael committed Dec 9, 2017
1 parent 0921368 commit 7dbf0c4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
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

0 comments on commit 7dbf0c4

Please sign in to comment.