-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
220 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
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 openstack | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/gophercloud/gophercloud" | ||
) | ||
|
||
// type linkResp struct { | ||
// Href string `json:"href"` | ||
// Rel string `json:"rel"` | ||
// } | ||
|
||
// type valueResp struct { | ||
// ID string `json:"id"` | ||
// Status string `json:"status"` | ||
// Version string `json:"version"` | ||
// MinVersion string `json:"min_version"` | ||
// Links []linkResp `json:"links"` | ||
// } | ||
|
||
// type response struct { | ||
// Version valueResp `json:"version"` | ||
// } | ||
|
||
// ChooseVersion queries the base endpoint of an API to choose the most recent non-experimental alternative from a service's | ||
// published versions. | ||
// It returns the highest-Priority Version among the alternatives that are provided. | ||
// func ChooseVersion(client *gophercloud.ServiceClient, recognized []*utils.Version) (*utils.Version, error) { | ||
// log := ctrl.LoggerFrom(context.Background()) | ||
// log.Info("Choosing among versions", "num recognized", len(recognized)) | ||
|
||
// // If a full endpoint is specified, check version suffixes for a match first. | ||
// for _, v := range recognized { | ||
// log.Info("Recognized version", "ID", v.ID) | ||
// if strings.HasSuffix(client.Endpoint, v.Suffix) { | ||
// return v, nil | ||
// } | ||
// } | ||
|
||
// var resp response | ||
// log.Info("Getting available versions", "endpoint", client.Endpoint) | ||
// _, err := client.Request("GET", client.Endpoint, &gophercloud.RequestOpts{ | ||
// JSONResponse: &resp, | ||
// OkCodes: []int{200, 300}, | ||
// }) | ||
|
||
// if err != nil { | ||
// return nil, err | ||
// } | ||
|
||
// log.Info("Found available versions", "versions", resp.Version) | ||
|
||
// var minVersion, maxVersion int | ||
|
||
// // Parse the min and max versions. We deal only with the decimal part, so 2.53 -> 53. | ||
// minVersion, err = parseMicroversion(resp.Version.MinVersion) | ||
// if err != nil { | ||
// return nil, err | ||
// } | ||
// maxVersion, err = parseMicroversion(resp.Version.Version) | ||
// if err != nil { | ||
// return nil, err | ||
// } | ||
|
||
// // TODO: Sort by priority so we can return at first match | ||
// for _, v := range recognized { | ||
// version, err := parseMicroversion(v.ID) | ||
// if err != nil { | ||
// return nil, err | ||
// } | ||
|
||
// // Acceptable version | ||
// if (version <= maxVersion) && (version >= minVersion) { | ||
// return v, nil | ||
// } | ||
// } | ||
|
||
// return nil, fmt.Errorf("no supported version available from endpoint %s", client.Endpoint) | ||
// } | ||
|
||
// GetSupportedMicroversions returns the minimum and maximum microversion that is supported by the ServiceClient Endpoint. | ||
func GetSupportedMicroversions(client gophercloud.ServiceClient) (string, string, error) { | ||
type valueResp struct { | ||
ID string `json:"id"` | ||
Status string `json:"status"` | ||
Version string `json:"version"` | ||
MinVersion string `json:"min_version"` | ||
} | ||
|
||
type response struct { | ||
Version valueResp `json:"version"` | ||
} | ||
var resp response | ||
_, err := client.Request("GET", client.Endpoint, &gophercloud.RequestOpts{ | ||
JSONResponse: &resp, | ||
OkCodes: []int{200, 300}, | ||
}) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
return resp.Version.MinVersion, resp.Version.Version, nil | ||
} | ||
|
||
// MicroversionSupported checks if a microversion falls in the supported interval. | ||
// It returns true if the version is within the interval and false otherwise. | ||
func MicroversionSupported(version string, minVersion string, maxVersion string) (bool, error) { | ||
// Parse the version X.Y into X and Y integers that are easier to compare. | ||
vMajor, v, err := parseMicroversion(version) | ||
if err != nil { | ||
return false, err | ||
} | ||
minMajor, min, err := parseMicroversion(minVersion) | ||
if err != nil { | ||
return false, err | ||
} | ||
maxMajor, max, err := parseMicroversion(maxVersion) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
// Check that the major version number is supported. | ||
if (vMajor < minMajor) || (vMajor > maxMajor) { | ||
return false, err | ||
} | ||
|
||
// Check that the minor version number is supported | ||
if (v <= max) && (v >= min) { | ||
return true, nil | ||
} | ||
|
||
return false, nil | ||
} | ||
|
||
// parseMicroversion parses the version X.Y into separate integers X and Y. | ||
// For example, "2.53" becomes 2 and 53. | ||
func parseMicroversion(version string) (int, int, error) { | ||
parts := strings.Split(version, ".") | ||
if len(parts) != 2 { | ||
return 0, 0, fmt.Errorf("invalid microversion format: %q", version) | ||
} | ||
major, err := strconv.Atoi(parts[0]) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
minor, err := strconv.Atoi(parts[1]) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
return major, minor, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters