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
69f751a2
Commit
69f751a2
authored
Oct 04, 2016
by
Maciej Szulik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Match GroupVersionKind against specific version
parent
1dc82775
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
71 additions
and
4 deletions
+71
-4
group_version.go
pkg/api/unversioned/group_version.go
+22
-2
group_version_test.go
pkg/api/unversioned/group_version_test.go
+44
-0
scheme_test.go
pkg/runtime/scheme_test.go
+5
-2
No files found.
pkg/api/unversioned/group_version.go
View file @
69f751a2
...
@@ -268,17 +268,37 @@ type GroupVersions []GroupVersion
...
@@ -268,17 +268,37 @@ type GroupVersions []GroupVersion
// KindForGroupVersionKinds identifies the preferred GroupVersionKind out of a list. It returns ok false
// KindForGroupVersionKinds identifies the preferred GroupVersionKind out of a list. It returns ok false
// if none of the options match the group.
// if none of the options match the group.
func
(
gvs
GroupVersions
)
KindForGroupVersionKinds
(
kinds
[]
GroupVersionKind
)
(
target
GroupVersionKind
,
ok
bool
)
{
func
(
gvs
GroupVersions
)
KindForGroupVersionKinds
(
kinds
[]
GroupVersionKind
)
(
GroupVersionKind
,
bool
)
{
var
targets
[]
GroupVersionKind
for
_
,
gv
:=
range
gvs
{
for
_
,
gv
:=
range
gvs
{
target
,
ok
:=
gv
.
KindForGroupVersionKinds
(
kinds
)
target
,
ok
:=
gv
.
KindForGroupVersionKinds
(
kinds
)
if
!
ok
{
if
!
ok
{
continue
continue
}
}
return
target
,
true
targets
=
append
(
targets
,
target
)
}
if
len
(
targets
)
==
1
{
return
targets
[
0
],
true
}
if
len
(
targets
)
>
1
{
return
bestMatch
(
kinds
,
targets
),
true
}
}
return
GroupVersionKind
{},
false
return
GroupVersionKind
{},
false
}
}
// bestMatch tries to pick best matching GroupVersionKind and falls back to the first
// found if no exact match exists.
func
bestMatch
(
kinds
[]
GroupVersionKind
,
targets
[]
GroupVersionKind
)
GroupVersionKind
{
for
_
,
gvk
:=
range
targets
{
for
_
,
k
:=
range
kinds
{
if
k
==
gvk
{
return
k
}
}
}
return
targets
[
0
]
}
// ToAPIVersionAndKind is a convenience method for satisfying runtime.Object on types that
// ToAPIVersionAndKind is a convenience method for satisfying runtime.Object on types that
// do not use TypeMeta.
// do not use TypeMeta.
func
(
gvk
*
GroupVersionKind
)
ToAPIVersionAndKind
()
(
string
,
string
)
{
func
(
gvk
*
GroupVersionKind
)
ToAPIVersionAndKind
()
(
string
,
string
)
{
...
...
pkg/api/unversioned/group_version_test.go
View file @
69f751a2
...
@@ -147,3 +147,47 @@ func TestGroupVersionMarshalJSON(t *testing.T) {
...
@@ -147,3 +147,47 @@ func TestGroupVersionMarshalJSON(t *testing.T) {
}
}
}
}
}
}
func
TestKindForGroupVersionKinds
(
t
*
testing
.
T
)
{
gvks
:=
GroupVersions
{
GroupVersion
{
Group
:
"batch"
,
Version
:
"v1"
},
GroupVersion
{
Group
:
"batch"
,
Version
:
"v2alpha1"
},
GroupVersion
{
Group
:
"policy"
,
Version
:
"v1alpha1"
},
}
cases
:=
[]
struct
{
input
[]
GroupVersionKind
target
GroupVersionKind
ok
bool
}{
{
input
:
[]
GroupVersionKind
{{
Group
:
"batch"
,
Version
:
"v2alpha1"
,
Kind
:
"ScheduledJob"
}},
target
:
GroupVersionKind
{
Group
:
"batch"
,
Version
:
"v2alpha1"
,
Kind
:
"ScheduledJob"
},
ok
:
true
,
},
{
input
:
[]
GroupVersionKind
{{
Group
:
"batch"
,
Version
:
"v3alpha1"
,
Kind
:
"CronJob"
}},
target
:
GroupVersionKind
{
Group
:
"batch"
,
Version
:
"v1"
,
Kind
:
"CronJob"
},
ok
:
true
,
},
{
input
:
[]
GroupVersionKind
{{
Group
:
"policy"
,
Version
:
"v1alpha1"
,
Kind
:
"PodDisruptionBudget"
}},
target
:
GroupVersionKind
{
Group
:
"policy"
,
Version
:
"v1alpha1"
,
Kind
:
"PodDisruptionBudget"
},
ok
:
true
,
},
{
input
:
[]
GroupVersionKind
{{
Group
:
"apps"
,
Version
:
"v1alpha1"
,
Kind
:
"PetSet"
}},
target
:
GroupVersionKind
{},
ok
:
false
,
},
}
for
i
,
c
:=
range
cases
{
target
,
ok
:=
gvks
.
KindForGroupVersionKinds
(
c
.
input
)
if
c
.
target
!=
target
{
t
.
Errorf
(
"%d: unexpected target: %v, expected %v"
,
i
,
target
,
c
.
target
)
}
if
c
.
ok
!=
ok
{
t
.
Errorf
(
"%d: unexpected ok: %v, expected %v"
,
i
,
ok
,
c
.
ok
)
}
}
}
pkg/runtime/scheme_test.go
View file @
69f751a2
...
@@ -602,12 +602,15 @@ func TestConvertToVersion(t *testing.T) {
...
@@ -602,12 +602,15 @@ func TestConvertToVersion(t *testing.T) {
gv
:
unversioned
.
GroupVersion
{
Version
:
"__internal"
},
gv
:
unversioned
.
GroupVersion
{
Version
:
"__internal"
},
out
:
&
TestType1
{
A
:
"test"
},
out
:
&
TestType1
{
A
:
"test"
},
},
},
// prefers the
first group version in the list
// prefers the
best match
{
{
scheme
:
GetTestScheme
(),
scheme
:
GetTestScheme
(),
in
:
&
ExternalTestType1
{
A
:
"test"
},
in
:
&
ExternalTestType1
{
A
:
"test"
},
gv
:
unversioned
.
GroupVersions
{{
Version
:
"__internal"
},
{
Version
:
"v1"
}},
gv
:
unversioned
.
GroupVersions
{{
Version
:
"__internal"
},
{
Version
:
"v1"
}},
out
:
&
TestType1
{
A
:
"test"
},
out
:
&
ExternalTestType1
{
MyWeirdCustomEmbeddedVersionKindField
:
MyWeirdCustomEmbeddedVersionKindField
{
APIVersion
:
"v1"
,
ObjectKind
:
"TestType1"
},
A
:
"test"
,
},
},
},
// unversioned type returned as-is
// unversioned type returned as-is
{
{
...
...
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