Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Fix footloose ssh for Ignite VMs #247

Merged
merged 1 commit into from
Jun 9, 2020
Merged
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
58 changes: 43 additions & 15 deletions pkg/cluster/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,58 @@ func (m *Machine) IsStarted() bool {
}

// HostPort returns the host port corresponding to the given container port.
func (m *Machine) HostPort(containerPort int) (hostPort int, err error) {
// Use the cached version first.
func (m *Machine) HostPort(containerPort int) (int, error) {
// Use the cached version first
if hostPort, ok := m.ports[containerPort]; ok {
return hostPort, nil
}
// retrieve the specific port mapping using docker inspect
lines, err := docker.Inspect(m.ContainerName(), fmt.Sprintf("{{(index (index .NetworkSettings.Ports \"%d/tcp\") 0).HostPort}}", containerPort))
if err != nil {
return -1, errors.Wrapf(err, "hostport: failed to inspect container: %v", lines)
}
if len(lines) != 1 {
return -1, errors.Errorf("hostport: should only be one line, got %d lines", len(lines))

var hostPort int

// Handle Ignite VMs
if m.IsIgnite() {
// Retrieve the machine details
vm, err := ignite.PopulateMachineDetails(m.name)
if err != nil {
return -1, errors.Wrap(err, "failed to populate VM details")
}

// Find the host port for the given VM port
var found = false
for _, p := range vm.Spec.Network.Ports {
if int(p.VMPort) == containerPort {
hostPort = int(p.HostPort)
found = true
break
}
}

if !found {
return -1, fmt.Errorf("invalid VM port queried: %d", containerPort)
}
} else {
// retrieve the specific port mapping using docker inspect
lines, err := docker.Inspect(m.ContainerName(), fmt.Sprintf("{{(index (index .NetworkSettings.Ports \"%d/tcp\") 0).HostPort}}", containerPort))
if err != nil {
return -1, errors.Wrapf(err, "hostport: failed to inspect container: %v", lines)
}
if len(lines) != 1 {
return -1, errors.Errorf("hostport: should only be one line, got %d lines", len(lines))
}

port := strings.Replace(lines[0], "'", "", -1)
if hostPort, err = strconv.Atoi(port); err != nil {
return -1, errors.Wrap(err, "hostport: failed to parse string to int")
}
}

if m.ports == nil {
m.ports = make(map[int]int)
}

port := strings.Replace(lines[0], "'", "", -1)
m.ports[containerPort], err = strconv.Atoi(port)
if err != nil {
return -1, errors.Wrap(err, "hostport: failed to parse string to int")
}
return m.ports[containerPort], nil
// Cache the result
m.ports[containerPort] = hostPort
return hostPort, nil
}

func (m *Machine) networks() ([]*RuntimeNetwork, error) {
Expand Down