Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use network service client to query source network #62

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
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
28 changes: 26 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 @@ -177,12 +184,29 @@ func (c *Client) Verify() error {
}

func (c *Client) PreFlightChecks(vm *migration.VirtualMachineImport) (err error) {
// Check the source network mappings.
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("Checking the source network as part of the preflight checks")

pgr := networks.List(c.networkClient, networks.ListOpts{Name: nm.SourceNetwork})
starbops marked this conversation as resolved.
Show resolved Hide resolved
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
8 changes: 8 additions & 0 deletions pkg/source/vmware/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,27 @@ func (c *Client) Verify() error {
}

func (c *Client) PreFlightChecks(vm *migration.VirtualMachineImport) (err error) {
// Check the source network mappings.
f := find.NewFinder(c.Client.Client, true)
dc := c.dc
if !strings.HasPrefix(c.dc, "/") {
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("Checking the source network as part of the preflight checks")

// The path looks like `/<datacenter>/network/<network-name>`.
path := filepath.Join(dc, "/network", nm.SourceNetwork)
_, err := f.Network(c.ctx, path)
if err != nil {
return fmt.Errorf("error getting source network '%s': %v", nm.SourceNetwork, err)
}
}

return nil
}

Expand Down
Loading