Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
97ada15f
Commit
97ada15f
authored
May 17, 2018
by
Mehdy Bohlool
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Sort API Services by Kube-Version order
parent
f86ec3f7
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
237 additions
and
10 deletions
+237
-10
helpers.go
staging/src/k8s.io/apimachinery/pkg/version/helpers.go
+88
-0
helpers_test.go
staging/src/k8s.io/apimachinery/pkg/version/helpers_test.go
+52
-0
helpers.go
...8s.io/kube-aggregator/pkg/apis/apiregistration/helpers.go
+2
-1
helpers_test.go
.../kube-aggregator/pkg/apis/apiregistration/helpers_test.go
+71
-0
types.go
.../k8s.io/kube-aggregator/pkg/apis/apiregistration/types.go
+6
-1
types.go
...s.io/kube-aggregator/pkg/apis/apiregistration/v1/types.go
+6
-1
types.go
...kube-aggregator/pkg/apis/apiregistration/v1beta1/types.go
+6
-1
handler_apis_test.go
...k8s.io/kube-aggregator/pkg/apiserver/handler_apis_test.go
+6
-6
No files found.
staging/src/k8s.io/apimachinery/pkg/version/helpers.go
0 → 100644
View file @
97ada15f
/*
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
}
staging/src/k8s.io/apimachinery/pkg/version/helpers_test.go
0 → 100644
View file @
97ada15f
/*
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
)
}
}
}
}
staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/helpers.go
View file @
97ada15f
...
@@ -22,6 +22,7 @@ import (
...
@@ -22,6 +22,7 @@ import (
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/version"
)
)
func
SortedByGroupAndVersion
(
servers
[]
*
APIService
)
[][]
*
APIService
{
func
SortedByGroupAndVersion
(
servers
[]
*
APIService
)
[][]
*
APIService
{
...
@@ -76,7 +77,7 @@ func (s ByVersionPriority) Less(i, j int) bool {
...
@@ -76,7 +77,7 @@ func (s ByVersionPriority) Less(i, j int) bool {
if
s
[
i
]
.
Spec
.
VersionPriority
!=
s
[
j
]
.
Spec
.
VersionPriority
{
if
s
[
i
]
.
Spec
.
VersionPriority
!=
s
[
j
]
.
Spec
.
VersionPriority
{
return
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
// APIServiceNameToGroupVersion returns the GroupVersion for a given apiServiceName. The name
...
...
staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/helpers_test.go
0 → 100644
View file @
97ada15f
/*
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
)
}
}
}
staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/types.go
View file @
97ada15f
...
@@ -66,8 +66,13 @@ type APIServiceSpec struct {
...
@@ -66,8 +66,13 @@ type APIServiceSpec struct {
// VersionPriority controls the ordering of this API version inside of its group. Must be greater than zero.
// 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 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.
// 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
VersionPriority
int32
}
}
...
...
staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/v1/types.go
View file @
97ada15f
...
@@ -66,8 +66,13 @@ type APIServiceSpec struct {
...
@@ -66,8 +66,13 @@ type APIServiceSpec struct {
// VersionPriority controls the ordering of this API version inside of its group. Must be greater than zero.
// 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 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.
// 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"`
VersionPriority
int32
`json:"versionPriority" protobuf:"varint,8,opt,name=versionPriority"`
// leaving this here so everyone remembers why proto index 6 is skipped
// leaving this here so everyone remembers why proto index 6 is skipped
...
...
staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/v1beta1/types.go
View file @
97ada15f
...
@@ -66,8 +66,13 @@ type APIServiceSpec struct {
...
@@ -66,8 +66,13 @@ type APIServiceSpec struct {
// VersionPriority controls the ordering of this API version inside of its group. Must be greater than zero.
// 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 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.
// 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"`
VersionPriority
int32
`json:"versionPriority" protobuf:"varint,8,opt,name=versionPriority"`
// leaving this here so everyone remembers why proto index 6 is skipped
// leaving this here so everyone remembers why proto index 6 is skipped
...
...
staging/src/k8s.io/kube-aggregator/pkg/apiserver/handler_apis_test.go
View file @
97ada15f
...
@@ -220,17 +220,17 @@ func TestAPIs(t *testing.T) {
...
@@ -220,17 +220,17 @@ func TestAPIs(t *testing.T) {
Name
:
"bar"
,
Name
:
"bar"
,
Versions
:
[]
metav1
.
GroupVersionForDiscovery
{
Versions
:
[]
metav1
.
GroupVersionForDiscovery
{
{
{
GroupVersion
:
"bar/v1"
,
Version
:
"v1"
,
},
{
GroupVersion
:
"bar/v2"
,
GroupVersion
:
"bar/v2"
,
Version
:
"v2"
,
Version
:
"v2"
,
},
},
{
GroupVersion
:
"bar/v1"
,
Version
:
"v1"
,
},
},
},
PreferredVersion
:
metav1
.
GroupVersionForDiscovery
{
PreferredVersion
:
metav1
.
GroupVersionForDiscovery
{
GroupVersion
:
"bar/v
1
"
,
GroupVersion
:
"bar/v
2
"
,
Version
:
"v
1
"
,
Version
:
"v
2
"
,
},
},
},
},
},
},
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment