-
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.
WIP: Require specific microversions based on features
This is an attempt to set the microversion based on what features are needed. For example, if tags are defined for the server, then we need a microversion that supports tags. If no special features are used/needed then we use the fixed minimum version
- Loading branch information
Showing
7 changed files
with
296 additions
and
9 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
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
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,97 @@ | ||
/* | ||
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" | ||
) | ||
|
||
// 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
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 "testing" | ||
|
||
type microversionSupported struct { | ||
Version string | ||
MinVersion string | ||
MaxVersion string | ||
Supported bool | ||
Error bool | ||
} | ||
|
||
func TestMicroversionSupported(t *testing.T) { | ||
tests := []microversionSupported{ | ||
{ | ||
// Checking min version | ||
Version: "2.1", | ||
MinVersion: "2.1", | ||
MaxVersion: "2.90", | ||
Supported: true, | ||
Error: false, | ||
}, | ||
{ | ||
// Checking max version | ||
Version: "2.90", | ||
MinVersion: "2.1", | ||
MaxVersion: "2.90", | ||
Supported: true, | ||
Error: false, | ||
}, | ||
{ | ||
// Checking too high version | ||
Version: "2.95", | ||
MinVersion: "2.1", | ||
MaxVersion: "2.90", | ||
Supported: false, | ||
Error: false, | ||
}, | ||
{ | ||
// Checking too low version | ||
Version: "2.1", | ||
MinVersion: "2.53", | ||
MaxVersion: "2.90", | ||
Supported: false, | ||
Error: false, | ||
}, | ||
{ | ||
// Invalid version | ||
Version: "2.1.53", | ||
MinVersion: "2.53", | ||
MaxVersion: "2.90", | ||
Supported: false, | ||
Error: true, | ||
}, | ||
{ | ||
// No microversions supported | ||
Version: "2.1", | ||
MinVersion: "", | ||
MaxVersion: "", | ||
Supported: false, | ||
Error: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
supported, err := MicroversionSupported(test.Version, test.MinVersion, test.MaxVersion) | ||
if test.Error { | ||
if err == nil { | ||
t.Error("Expected error but got none!") | ||
} | ||
} else { | ||
if err != nil { | ||
t.Errorf("Expected no error but got %s", err.Error()) | ||
} | ||
} | ||
if test.Supported != supported { | ||
t.Errorf("Expected supported=%t to be %t, when version=%s, min=%s and max=%s", | ||
supported, test.Supported, test.Version, test.MinVersion, test.MaxVersion) | ||
} | ||
} | ||
} |
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