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
925003b4
Commit
925003b4
authored
May 12, 2016
by
Kris
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add mapper and typer based on discovery info
parent
db515f47
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
197 additions
and
0 deletions
+197
-0
dynamic_util.go
pkg/client/typed/dynamic/dynamic_util.go
+119
-0
dynamic_util_test.go
pkg/client/typed/dynamic/dynamic_util_test.go
+78
-0
No files found.
pkg/client/typed/dynamic/dynamic_util.go
0 → 100644
View file @
925003b4
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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
dynamic
import
(
"fmt"
"k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/runtime"
)
// VersionInterfaces provides an object converter and metadata
// accessor appropriate for use with unstructured objects.
func
VersionInterfaces
(
unversioned
.
GroupVersion
)
(
*
meta
.
VersionInterfaces
,
error
)
{
return
&
meta
.
VersionInterfaces
{
ObjectConvertor
:
&
runtime
.
UnstructuredObjectConverter
{},
MetadataAccessor
:
meta
.
NewAccessor
(),
},
nil
}
// NewDiscoveryRESTMapper returns a RESTMapper based on discovery information.
func
NewDiscoveryRESTMapper
(
resources
[]
*
unversioned
.
APIResourceList
,
versionFunc
meta
.
VersionInterfacesFunc
)
(
*
meta
.
DefaultRESTMapper
,
error
)
{
rm
:=
meta
.
NewDefaultRESTMapper
(
nil
,
versionFunc
)
for
_
,
resourceList
:=
range
resources
{
gv
,
err
:=
unversioned
.
ParseGroupVersion
(
resourceList
.
GroupVersion
)
if
err
!=
nil
{
return
nil
,
err
}
for
_
,
resource
:=
range
resourceList
.
APIResources
{
gvk
:=
gv
.
WithKind
(
resource
.
Kind
)
scope
:=
meta
.
RESTScopeRoot
if
resource
.
Namespaced
{
scope
=
meta
.
RESTScopeNamespace
}
rm
.
Add
(
gvk
,
scope
)
}
}
return
rm
,
nil
}
// ObjectTyper provides an ObjectTyper implmentation for
// runtime.Unstructured object based on discovery information.
type
ObjectTyper
struct
{
registered
map
[
unversioned
.
GroupVersionKind
]
bool
}
// NewObjectTyper constructs an ObjectTyper from discovery information.
func
NewObjectTyper
(
resources
[]
*
unversioned
.
APIResourceList
)
(
runtime
.
ObjectTyper
,
error
)
{
ot
:=
&
ObjectTyper
{
registered
:
make
(
map
[
unversioned
.
GroupVersionKind
]
bool
)}
for
_
,
resourceList
:=
range
resources
{
gv
,
err
:=
unversioned
.
ParseGroupVersion
(
resourceList
.
GroupVersion
)
if
err
!=
nil
{
return
nil
,
err
}
for
_
,
resource
:=
range
resourceList
.
APIResources
{
ot
.
registered
[
gv
.
WithKind
(
resource
.
Kind
)]
=
true
}
}
return
ot
,
nil
}
// ObjectKind returns the group,version,kind of the provided object,
// or an error if the object in not *runtime.Unstructured or has no
// group,version,kind information.
func
(
ot
*
ObjectTyper
)
ObjectKind
(
obj
runtime
.
Object
)
(
unversioned
.
GroupVersionKind
,
error
)
{
if
_
,
ok
:=
obj
.
(
*
runtime
.
Unstructured
);
!
ok
{
return
unversioned
.
GroupVersionKind
{},
fmt
.
Errorf
(
"type %T is invalid for dynamic object typer"
,
obj
)
}
return
obj
.
GetObjectKind
()
.
GroupVersionKind
(),
nil
}
// ObjectKinds returns a slice of one element with the
// group,version,kind of the provided object, or an error if the
// object is not *runtime.Unstructured or has no group,version,kind
// information.
func
(
ot
*
ObjectTyper
)
ObjectKinds
(
obj
runtime
.
Object
)
([]
unversioned
.
GroupVersionKind
,
error
)
{
gvk
,
err
:=
ot
.
ObjectKind
(
obj
)
if
err
!=
nil
{
return
nil
,
err
}
return
[]
unversioned
.
GroupVersionKind
{
gvk
},
nil
}
// Recognizes returns true if the provided group,version,kind was in
// the discovery information.
func
(
ot
*
ObjectTyper
)
Recognizes
(
gvk
unversioned
.
GroupVersionKind
)
bool
{
return
ot
.
registered
[
gvk
]
}
// IsUnversioned returns false always because *runtime.Unstructured
// objects should always have group,version,kind information set. ok
// will be true if the object's group,version,kind is registered.
func
(
ot
*
ObjectTyper
)
IsUnversioned
(
obj
runtime
.
Object
)
(
unversioned
bool
,
ok
bool
)
{
gvk
,
err
:=
ot
.
ObjectKind
(
obj
)
if
err
!=
nil
{
return
false
,
false
}
return
false
,
ot
.
registered
[
gvk
]
}
pkg/client/typed/dynamic/dynamic_util_test.go
0 → 100644
View file @
925003b4
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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
dynamic
import
(
"testing"
"k8s.io/kubernetes/pkg/api/unversioned"
)
func
TestDiscoveryRESTMapper
(
t
*
testing
.
T
)
{
resources
:=
[]
*
unversioned
.
APIResourceList
{
{
GroupVersion
:
"test/beta1"
,
APIResources
:
[]
unversioned
.
APIResource
{
{
Name
:
"test_kinds"
,
Namespaced
:
true
,
Kind
:
"test_kind"
,
},
},
},
}
gvk
:=
unversioned
.
GroupVersionKind
{
Group
:
"test"
,
Version
:
"beta1"
,
Kind
:
"test_kind"
,
}
mapper
,
err
:=
NewDiscoveryRESTMapper
(
resources
,
VersionInterfaces
)
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error creating mapper: %s"
,
err
)
}
for
_
,
res
:=
range
[]
unversioned
.
GroupVersionResource
{
{
Group
:
"test"
,
Version
:
"beta1"
,
Resource
:
"test_kinds"
,
},
{
Version
:
"beta1"
,
Resource
:
"test_kinds"
,
},
{
Group
:
"test"
,
Resource
:
"test_kinds"
,
},
{
Resource
:
"test_kinds"
,
},
}
{
got
,
err
:=
mapper
.
KindFor
(
res
)
if
err
!=
nil
{
t
.
Errorf
(
"KindFor(%#v) unexpected error: %s"
,
res
,
err
)
continue
}
if
got
!=
gvk
{
t
.
Errorf
(
"KindFor(%#v) = %#v; want %#v"
,
res
,
got
,
gvk
)
}
}
}
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