diff --git a/staging/src/k8s.io/apimachinery/pkg/version/helpers.go b/staging/src/k8s.io/apimachinery/pkg/version/helpers.go new file mode 100644 index 00000000000..5e041d6f3fa --- /dev/null +++ b/staging/src/k8s.io/apimachinery/pkg/version/helpers.go @@ -0,0 +1,88 @@ +/* +Copyright 2018 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 version + +import ( + "regexp" + "strconv" + "strings" +) + +type versionType int + +const ( + // Bigger the version type number, higher priority it is + versionTypeAlpha versionType = iota + versionTypeBeta + versionTypeGA +) + +var kubeVersionRegex = regexp.MustCompile("^v([\\d]+)(?:(alpha|beta)([\\d]+))?$") + +func parseKubeVersion(v string) (majorVersion int, vType versionType, minorVersion int, ok bool) { + var err error + submatches := kubeVersionRegex.FindStringSubmatch(v) + if len(submatches) != 4 { + return 0, 0, 0, false + } + switch submatches[2] { + case "alpha": + vType = versionTypeAlpha + case "beta": + vType = versionTypeBeta + case "": + vType = versionTypeGA + default: + return 0, 0, 0, false + } + if majorVersion, err = strconv.Atoi(submatches[1]); err != nil { + return 0, 0, 0, false + } + if vType != versionTypeGA { + if minorVersion, err = strconv.Atoi(submatches[3]); err != nil { + return 0, 0, 0, false + } + } + return majorVersion, vType, minorVersion, true +} + +// CompareKubeAwareVersionStrings compares two kube-like version strings. +// Kube-like version strings are starting with a v, followed by a major version, optional "alpha" or "beta" strings +// followed by a minor version (e.g. v1, v2beta1). Versions will be sorted based on GA/alpha/beta first and then major +// and minor versions. e.g. v2, v1, v1beta2, v1beta1, v1alpha1. +func CompareKubeAwareVersionStrings(v1, v2 string) int { + if v1 == v2 { + return 0 + } + v1major, v1type, v1minor, ok1 := parseKubeVersion(v1) + v2major, v2type, v2minor, ok2 := parseKubeVersion(v2) + switch { + case !ok1 && !ok2: + return strings.Compare(v2, v1) + case !ok1 && ok2: + return -1 + case ok1 && !ok2: + return 1 + } + if v1type != v2type { + return int(v1type) - int(v2type) + } + if v1major != v2major { + return v1major - v2major + } + return v1minor - v2minor +} diff --git a/staging/src/k8s.io/apimachinery/pkg/version/helpers_test.go b/staging/src/k8s.io/apimachinery/pkg/version/helpers_test.go new file mode 100644 index 00000000000..b473eae0ebc --- /dev/null +++ b/staging/src/k8s.io/apimachinery/pkg/version/helpers_test.go @@ -0,0 +1,52 @@ +/* +Copyright 2018 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 version + +import ( + "testing" +) + +func TestCompareKubeAwareVersionStrings(t *testing.T) { + tests := []*struct { + v1, v2 string + expectedGreater bool + }{ + {"v1", "v2", false}, + {"v2", "v1", true}, + {"v10", "v2", true}, + {"v1", "v2alpha1", true}, + {"v1", "v2beta1", true}, + {"v1alpha2", "v1alpha1", true}, + {"v1beta1", "v2alpha3", true}, + {"v1alpha10", "v1alpha2", true}, + {"v1beta10", "v1beta2", true}, + {"foo", "v1beta2", false}, + {"bar", "foo", true}, + {"version1", "version2", true}, // Non kube-like versions are sorted alphabetically + {"version1", "version10", true}, // Non kube-like versions are sorted alphabetically + } + + for _, tc := range tests { + if e, a := tc.expectedGreater, CompareKubeAwareVersionStrings(tc.v1, tc.v2) > 0; e != a { + if e { + t.Errorf("expected %s to be greater than %s", tc.v1, tc.v2) + } else { + t.Errorf("expected %s to be less than than %s", tc.v1, tc.v2) + } + } + } +} diff --git a/staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/helpers.go b/staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/helpers.go index 5e36e7db26e..ab1f40cdc1f 100644 --- a/staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/helpers.go +++ b/staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/helpers.go @@ -22,6 +22,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/apimachinery/pkg/version" ) func SortedByGroupAndVersion(servers []*APIService) [][]*APIService { @@ -76,7 +77,7 @@ func (s ByVersionPriority) Less(i, j int) bool { if s[i].Spec.VersionPriority != s[j].Spec.VersionPriority { return s[i].Spec.VersionPriority > s[j].Spec.VersionPriority } - return s[i].Name < s[j].Name + return version.CompareKubeAwareVersionStrings(s[i].Spec.Version, s[j].Spec.Version) > 0 } // APIServiceNameToGroupVersion returns the GroupVersion for a given apiServiceName. The name diff --git a/staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/helpers_test.go b/staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/helpers_test.go new file mode 100644 index 00000000000..7bca228bdc0 --- /dev/null +++ b/staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/helpers_test.go @@ -0,0 +1,71 @@ +/* +Copyright 2018 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 apiregistration + +import ( + "reflect" + "testing" +) + +func TestSortedAPIServicesByVersion(t *testing.T) { + tests := []*struct { + name string + versions []string + expected []string + }{ + { + name: "case1", + versions: []string{"v1", "v2"}, + expected: []string{"v2", "v1"}, + }, + { + name: "case2", + versions: []string{"v2", "v10"}, + expected: []string{"v10", "v2"}, + }, + { + name: "case3", + versions: []string{"v2", "v2beta1", "v10beta2", "v10beta1", "v10alpha1", "v1"}, + expected: []string{"v2", "v1", "v10beta2", "v10beta1", "v2beta1", "v10alpha1"}, + }, + { + name: "case4", + versions: []string{"v1", "v2", "test", "foo10", "final", "foo2", "foo1"}, + expected: []string{"v2", "v1", "final", "foo1", "foo10", "foo2", "test"}, + }, + { + name: "case5_from_documentation", + versions: []string{"v12alpha1", "v10", "v11beta2", "v10beta3", "v3beta1", "v2", "v11alpha2", "foo1", "v1", "foo10"}, + expected: []string{"v10", "v2", "v1", "v11beta2", "v10beta3", "v3beta1", "v12alpha1", "v11alpha2", "foo1", "foo10"}, + }, + } + + for _, tc := range tests { + apiServices := []*APIService{} + for _, v := range tc.versions { + apiServices = append(apiServices, &APIService{Spec: APIServiceSpec{Version: v, VersionPriority: 100}}) + } + sortedServices := SortedByGroupAndVersion(apiServices) + actual := []string{} + for _, s := range sortedServices[0] { + actual = append(actual, s.Spec.Version) + } + if !reflect.DeepEqual(tc.expected, actual) { + t.Errorf("expected %s, actual %s", tc.expected, actual) + } + } +} diff --git a/staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/types.go b/staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/types.go index d052361c73b..36bbd6243ca 100644 --- a/staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/types.go +++ b/staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/types.go @@ -66,8 +66,13 @@ type APIServiceSpec struct { // VersionPriority controls the ordering of this API version inside of its group. Must be greater than zero. // The primary sort is based on VersionPriority, ordered highest to lowest (20 before 10). - // The secondary sort is based on the alphabetical comparison of the name of the object. (v1.bar before v1.foo) // Since it's inside of a group, the number can be small, probably in the 10s. + // In case of equal version priorities, the version string will be used to compute the order inside a group. + // If the version string is "kube-like", it will sort above non "kube-like" version strings, which are ordered + // lexicographically. "Kube-like" versions start with a "v", then are followed by a number (the major version), + // then optionally the string "alpha" or "beta" and another number (the minor version). These are sorted first + // by GA > beta > alpha, and then by comparing major version, then minor version. An example sorted list of + // versions: v10, v2, v1, v11beta2, v10beta3, v3beta1, v12alpha1, v11alpha2, foo1, foo10. VersionPriority int32 } diff --git a/staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/v1/types.go b/staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/v1/types.go index 4e2a9318255..0f746657d8a 100644 --- a/staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/v1/types.go +++ b/staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/v1/types.go @@ -66,8 +66,13 @@ type APIServiceSpec struct { // VersionPriority controls the ordering of this API version inside of its group. Must be greater than zero. // The primary sort is based on VersionPriority, ordered highest to lowest (20 before 10). - // The secondary sort is based on the alphabetical comparison of the name of the object. (v1.bar before v1.foo) // Since it's inside of a group, the number can be small, probably in the 10s. + // In case of equal version priorities, the version string will be used to compute the order inside a group. + // If the version string is "kube-like", it will sort above non "kube-like" version strings, which are ordered + // lexicographically. "Kube-like" versions start with a "v", then are followed by a number (the major version), + // then optionally the string "alpha" or "beta" and another number (the minor version). These are sorted first + // by GA > beta > alpha, and then by comparing major version, then minor version. An example sorted list of + // versions: v10, v2, v1, v11beta2, v10beta3, v3beta1, v12alpha1, v11alpha2, foo1, foo10. VersionPriority int32 `json:"versionPriority" protobuf:"varint,8,opt,name=versionPriority"` // leaving this here so everyone remembers why proto index 6 is skipped diff --git a/staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/v1beta1/types.go b/staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/v1beta1/types.go index 0652d509049..26e408446d9 100644 --- a/staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/v1beta1/types.go +++ b/staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/v1beta1/types.go @@ -66,8 +66,13 @@ type APIServiceSpec struct { // VersionPriority controls the ordering of this API version inside of its group. Must be greater than zero. // The primary sort is based on VersionPriority, ordered highest to lowest (20 before 10). - // The secondary sort is based on the alphabetical comparison of the name of the object. (v1.bar before v1.foo) // Since it's inside of a group, the number can be small, probably in the 10s. + // In case of equal version priorities, the version string will be used to compute the order inside a group. + // If the version string is "kube-like", it will sort above non "kube-like" version strings, which are ordered + // lexicographically. "Kube-like" versions start with a "v", then are followed by a number (the major version), + // then optionally the string "alpha" or "beta" and another number (the minor version). These are sorted first + // by GA > beta > alpha, and then by comparing major version, then minor version. An example sorted list of + // versions: v10, v2, v1, v11beta2, v10beta3, v3beta1, v12alpha1, v11alpha2, foo1, foo10. VersionPriority int32 `json:"versionPriority" protobuf:"varint,8,opt,name=versionPriority"` // leaving this here so everyone remembers why proto index 6 is skipped diff --git a/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_apis_test.go b/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_apis_test.go index 76d4680abe9..31dc80702f9 100644 --- a/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_apis_test.go +++ b/staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_apis_test.go @@ -219,18 +219,18 @@ func TestAPIs(t *testing.T) { { Name: "bar", Versions: []metav1.GroupVersionForDiscovery{ - { - GroupVersion: "bar/v1", - Version: "v1", - }, { GroupVersion: "bar/v2", Version: "v2", }, + { + GroupVersion: "bar/v1", + Version: "v1", + }, }, PreferredVersion: metav1.GroupVersionForDiscovery{ - GroupVersion: "bar/v1", - Version: "v1", + GroupVersion: "bar/v2", + Version: "v2", }, }, },