Skip to content

Commit

Permalink
fix enumeration of more than 1000 Nova hypervisors
Browse files Browse the repository at this point in the history
Pagination was introduced in microversion 2.33, so our microversion upgrade
in 2fbae7d (PR #579) broke this
accidentally. This was not caught in QA because the QA regions are not
big enough to reach a 4-digit hypervisor count.
  • Loading branch information
majewsky committed Oct 21, 2024
1 parent b776c3c commit c5ae734
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
74 changes: 74 additions & 0 deletions internal/plugins/nova/gophercloud_fixes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*******************************************************************************
*
* Copyright 2024 SAP SE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You should have received a copy of the License along with this
* program. If not, you may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*******************************************************************************/

package nova

import (
"net/http"

"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/hypervisors"
"github.com/gophercloud/gophercloud/v2/pagination"
)

// Like hypervisors.HypervisorPage, but fixes bug <https://github.com/gophercloud/gophercloud/issues/3222> by being a LinkedPageBase instead of a SinglePageBase.
type hypervisorPage struct {
pagination.LinkedPageBase
}

// IsEmpty is required for LinkedPageBase to work.
func (page hypervisorPage) IsEmpty() (bool, error) {
if page.StatusCode == http.StatusNoContent {
return true, nil
}

var data struct {
Hypervisors []struct{} `json:"hypervisors"`
}
err := page.ExtractInto(&data)
return len(data.Hypervisors) == 0, err
}

// NextPageURL is required for LinkedPageBase to work.
func (page hypervisorPage) NextPageURL() (string, error) {
var s struct {
Links []gophercloud.Link `json:"hypervisors_links"`
}
err := page.ExtractInto(&s)
if err != nil {
return "", err
}
return gophercloud.ExtractNextURL(s.Links)
}

// Like hypervisors.List(), but fixes bug <https://github.com/gophercloud/gophercloud/issues/3222>.
func hypervisorsList(client *gophercloud.ServiceClient, opts hypervisors.ListOptsBuilder) pagination.Pager {
url := client.ServiceURL("os-hypervisors", "detail")
if opts != nil {
query, err := opts.ToHypervisorListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}

return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return hypervisorPage{pagination.LinkedPageBase{PageResult: r}}
})
}
4 changes: 2 additions & 2 deletions internal/plugins/nova/hypervisor_selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ type HypervisorSelection struct {
// HypervisorSelection, and calls the given callback once for each of them.
func (s HypervisorSelection) ForeachHypervisor(ctx context.Context, novaV2, placementV1 *gophercloud.ServiceClient, action func(MatchingHypervisor) error) error {
// enumerate hypervisors
page, err := hypervisors.List(novaV2, nil).AllPages(ctx)
page, err := hypervisorsList(novaV2, nil).AllPages(ctx)
if err != nil {
return fmt.Errorf("while listing hypervisors: %w", err)
}
var hypervisorData struct {
Hypervisors []Hypervisor `json:"hypervisors"`
}
err = page.(hypervisors.HypervisorPage).ExtractInto(&hypervisorData)
err = page.(hypervisorPage).ExtractInto(&hypervisorData)
if err != nil {
return fmt.Errorf("while listing hypervisors: %w", err)
}
Expand Down

0 comments on commit c5ae734

Please sign in to comment.