Skip to content

Commit

Permalink
oVirt: Add missing piece for gathering bootsrap IP info
Browse files Browse the repository at this point in the history
assuming that the engine already got the ip from the  qemu agent.

Signed-off-by: Evgeny Slutsky <eslutsky@redhat.com>
  • Loading branch information
eslutsky committed Jan 18, 2021
1 parent 69f0bbc commit 66eaf1b
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 2 deletions.
6 changes: 5 additions & 1 deletion cmd/openshift-install/gather.go
Expand Up @@ -225,11 +225,15 @@ func extractHostAddresses(config *types.InstallConfig, tfstate *terraform.State)
logrus.Error(err)
}
case ovirttypes.Name:
bootstrap, err := gatherovirt.BootstrapIP(tfstate)
bootstrap, err = gatherovirt.BootstrapIP(tfstate)
if err != nil {
return bootstrap, port, masters, err
}
masters, err = gatherovirt.ControlPlaneIPs(tfstate)
if err != nil {
logrus.Error(err)
}

case vspheretypes.Name:
bootstrap, err = gathervsphere.BootstrapIP(config, tfstate)
if err != nil {
Expand Down
112 changes: 111 additions & 1 deletion pkg/terraform/gather/ovirt/ip.go
Expand Up @@ -2,13 +2,123 @@
package ovirt

import (
//"context"
"fmt"
"net"
"time"

"github.com/openshift/installer/pkg/asset/installconfig/ovirt"
"github.com/openshift/installer/pkg/terraform"
ovirtsdk4 "github.com/ovirt/go-ovirt"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
//"time"
)

func checkPortIsOpen(host string, port string) bool {
timeout := time.Second
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, port), timeout)
if err != nil {
fmt.Println("Connecting error:", err)
}
if conn != nil {
defer conn.Close()
fmt.Println("Opened", net.JoinHostPort(host, port))
return true
}
return false
}

func getreportedDevices(c *ovirtsdk4.Connection, vmID string) (*ovirtsdk4.ReportedDeviceSlice, error) {

vmsService := c.SystemService().VmsService()
//# Look up fot the vm by id:
vmResp, err := vmsService.VmService(vmID).Get().Send()
if err != nil {
return nil, fmt.Errorf("failed to find VM, by id %v, reason: %v", vmID, err)
}
vm := vmResp.MustVm()

// Get the reported-devices service for this vm:
reportedDevicesService := vmsService.VmService(vm.MustId()).ReportedDevicesService()

// Get the guest reported devices
reportedDeviceResp, err := reportedDevicesService.List().Send()
if err != nil {
return nil, fmt.Errorf("Failed to get reported devices list, reason: %v", err)
}
reportedDeviceSlice, _ := reportedDeviceResp.ReportedDevice()

if len(reportedDeviceSlice.Slice()) == 0 {
return nil, fmt.Errorf("Cannot find IPs for vmId: %s", vmID)
}

return reportedDeviceSlice, nil

}

func findVirtualMachineIP(c *ovirtsdk4.Connection, moRefValue string) (string, error) {

ipAddress := ""

reportedDeviceSlice, err := getreportedDevices(c, moRefValue)
if err != nil {
return "", err
}

for _, reportedDevice := range reportedDeviceSlice.Slice() {
ips := reportedDevice.MustIps()

if ips != nil {
for _, ip := range ips.Slice() {
ipres := ip.MustAddress()
if ipres != "" {
if checkPortIsOpen(ipres, "22") {
ipAddress = ipres
}
}

}
}
}

logrus.Debugln(fmt.Sprintf("ovirt vm id: %s , found bootstrap IP %s", moRefValue, ipAddress))
return ipAddress, nil
}

// BootstrapIP returns the ip address for bootstrap host.
// still unsupported, because qemu-ga is not available - see https://bugzilla.redhat.com/show_bug.cgi?id=1764804
func BootstrapIP(tfs *terraform.State) (string, error) {
return "", nil

client, err := ovirt.NewConnection()
if err != nil {
return "", fmt.Errorf("failed to initialize connection to ovirt-engine's %s", err)
}
defer client.Close()

br, err := terraform.LookupResource(tfs, "module.bootstrap", "ovirt_vm", "bootstrap")
if err != nil {
return "", errors.Wrap(err, "failed to lookup bootstrap")
}

if len(br.Instances) == 0 {
return "", errors.New("no bootstrap instance found")
}

vmid, found, err := unstructured.NestedString(br.Instances[0].Attributes, "id")
if err != nil {
return "", errors.Wrap(err, "failed to lookup bootstrap managed object reference")
}
if !found {
return "", errors.Errorf("failed to lookup bootstrap by vmID: %s", vmid)
}

ip, err := findVirtualMachineIP(client, vmid)
if err != nil {
return "", errors.Wrap(err, "failed to lookup bootstrap ipv4 address")
}
return ip, nil
}

// ControlPlaneIPs returns the ip addresses for control plane hosts.
Expand Down

0 comments on commit 66eaf1b

Please sign in to comment.