Skip to content

Commit

Permalink
Use network service client to query source network
Browse files Browse the repository at this point in the history
Related to: harvester/harvester#7432

Signed-off-by: Volker Theile <[email protected]>
  • Loading branch information
votdev committed Jan 27, 2025
1 parent 39daa82 commit 024f725
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
26 changes: 24 additions & 2 deletions pkg/source/openstack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Client struct {
storageClient *gophercloud.ServiceClient
computeClient *gophercloud.ServiceClient
imageClient *gophercloud.ServiceClient
networkClient *gophercloud.ServiceClient
options migration.OpenstackSourceOptions
}

Expand Down Expand Up @@ -135,13 +136,19 @@ func NewClient(ctx context.Context, endpoint string, region string, secret *core
return nil, fmt.Errorf("error generating image client: %v", err)
}

networkClient, err := openstack.NewNetworkV2(client, endPointOpts)
if err != nil {
return nil, fmt.Errorf("error generating network client: %v", err)
}

return &Client{
ctx: ctx,
pClient: client,
opts: endPointOpts,
storageClient: storageClient,
computeClient: computeClient,
imageClient: imageClient,
networkClient: networkClient,
options: options,
}, nil
}
Expand Down Expand Up @@ -178,9 +185,24 @@ func (c *Client) Verify() error {

func (c *Client) PreFlightChecks(vm *migration.VirtualMachineImport) (err error) {
for _, nm := range vm.Spec.Mapping {
_, err := networks.Get(c.computeClient, nm.SourceNetwork).Extract()
logrus.WithFields(logrus.Fields{
"name": vm.Name,
"namespace": vm.Namespace,
"sourceNetwork": nm.SourceNetwork,
}).Info("Searching for the source network in preflight check")

pgr := networks.List(c.networkClient, networks.ListOpts{Name: nm.SourceNetwork})
allPgs, err := pgr.AllPages()
if err != nil {
return fmt.Errorf("error getting source network '%s': %v", nm.SourceNetwork, err)
return fmt.Errorf("error while generating all pages during querying source network '%s': %v", nm.SourceNetwork, err)
}

ok, err := allPgs.IsEmpty()
if err != nil {
return fmt.Errorf("error while checking if pages were empty during querying source network '%s': %v", nm.SourceNetwork, err)
}
if ok {
return fmt.Errorf("source network '%s' not found", nm.SourceNetwork)
}
}
return nil
Expand Down
6 changes: 6 additions & 0 deletions pkg/source/vmware/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ func (c *Client) PreFlightChecks(vm *migration.VirtualMachineImport) (err error)
dc = fmt.Sprintf("/%s", c.dc)
}
for _, nm := range vm.Spec.Mapping {
logrus.WithFields(logrus.Fields{
"name": vm.Name,
"namespace": vm.Namespace,
"sourceNetwork": nm.SourceNetwork,
}).Info("Searching for the source network in preflight check")

// The path looks like `/<datacenter>/network/<network-name>`.
path := filepath.Join(dc, "/network", nm.SourceNetwork)
_, err := f.Network(c.ctx, path)
Expand Down

0 comments on commit 024f725

Please sign in to comment.