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 11, 2021
1 parent 69f0bbc commit 72bf1c3
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 3 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
102 changes: 100 additions & 2 deletions pkg/terraform/gather/ovirt/ip.go
Expand Up @@ -2,13 +2,111 @@
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 findVirtualMachineIP(c *ovirtsdk4.Connection, moRefValue string) (string, error) {

vmsService := c.SystemService().VmsService()
//# Look up fot the vm by id:
vmResp, err := vmsService.List().Search(fmt.Sprintf("id=%s", moRefValue)).Send()
if err != nil {
return "", fmt.Errorf("failed to search vm list, reason: %v", err)
}
vmSlice, _ := vmResp.Vms()
vm := vmSlice.Slice()[0]
ipAddress := ""
// 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 "", fmt.Errorf("failed to get reported devices list, reason: %v", err)
}
reportedDeviceSlice, _ := reportedDeviceResp.ReportedDevice()

if len(reportedDeviceSlice.Slice()) == 0 {
return "", fmt.Errorf("IPS not found")
}

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 managed object reference")
}

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 72bf1c3

Please sign in to comment.