Skip to content

Commit 8b356a9

Browse files
Merge pull request #55660 from smarterclayton/get_clean
Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Stop using VersionedObject in resource.Builder, use unstructured Remove the need for kubectl callers to distinguish between unstructured and versioned / type aware builders. The factory should create a single builder than can be set to return unstructured objects. Callers can then use one of the new helpers on `resource.Info` to convert the objects into the desired form - `Internal()` for printers, `Typed()` for external versions, and `Unstructured()` to ensure the object is in the right state. Leverages the new scheme support for unstructured conversion so that higher level callers can perform best effort conversion (get typed versions if you have them, otherwise use default behavior). `get.go` demonstrates this by removing the previous logic that depended on the underlying scheme. Other commands are updated to be consistent. Includes #55650 and #55647. Kubernetes-commit: 40e7101844fd4cce86e3c6df3f7d093d74337418
2 parents d56c180 + 8ee88d9 commit 8b356a9

File tree

3 files changed

+139
-1
lines changed

3 files changed

+139
-1
lines changed

pkg/api/meta/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ go_library(
3434
"firsthit_restmapper.go",
3535
"help.go",
3636
"interfaces.go",
37+
"lazy.go",
3738
"meta.go",
3839
"multirestmapper.go",
3940
"priority.go",

pkg/api/meta/lazy.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package meta
18+
19+
import (
20+
"sync"
21+
22+
"k8s.io/apimachinery/pkg/runtime"
23+
"k8s.io/apimachinery/pkg/runtime/schema"
24+
)
25+
26+
// lazyObject defers loading the mapper and typer until necessary.
27+
type lazyObject struct {
28+
loader func() (RESTMapper, runtime.ObjectTyper, error)
29+
30+
lock sync.Mutex
31+
loaded bool
32+
err error
33+
mapper RESTMapper
34+
typer runtime.ObjectTyper
35+
}
36+
37+
// NewLazyObjectLoader handles unrecoverable errors when creating a RESTMapper / ObjectTyper by
38+
// returning those initialization errors when the interface methods are invoked. This defers the
39+
// initialization and any server calls until a client actually needs to perform the action.
40+
func NewLazyObjectLoader(fn func() (RESTMapper, runtime.ObjectTyper, error)) (RESTMapper, runtime.ObjectTyper) {
41+
obj := &lazyObject{loader: fn}
42+
return obj, obj
43+
}
44+
45+
// init lazily loads the mapper and typer, returning an error if initialization has failed.
46+
func (o *lazyObject) init() error {
47+
o.lock.Lock()
48+
defer o.lock.Unlock()
49+
if o.loaded {
50+
return o.err
51+
}
52+
o.mapper, o.typer, o.err = o.loader()
53+
o.loaded = true
54+
return o.err
55+
}
56+
57+
var _ RESTMapper = &lazyObject{}
58+
var _ runtime.ObjectTyper = &lazyObject{}
59+
60+
func (o *lazyObject) KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
61+
if err := o.init(); err != nil {
62+
return schema.GroupVersionKind{}, err
63+
}
64+
return o.mapper.KindFor(resource)
65+
}
66+
67+
func (o *lazyObject) KindsFor(resource schema.GroupVersionResource) ([]schema.GroupVersionKind, error) {
68+
if err := o.init(); err != nil {
69+
return []schema.GroupVersionKind{}, err
70+
}
71+
return o.mapper.KindsFor(resource)
72+
}
73+
74+
func (o *lazyObject) ResourceFor(input schema.GroupVersionResource) (schema.GroupVersionResource, error) {
75+
if err := o.init(); err != nil {
76+
return schema.GroupVersionResource{}, err
77+
}
78+
return o.mapper.ResourceFor(input)
79+
}
80+
81+
func (o *lazyObject) ResourcesFor(input schema.GroupVersionResource) ([]schema.GroupVersionResource, error) {
82+
if err := o.init(); err != nil {
83+
return []schema.GroupVersionResource{}, err
84+
}
85+
return o.mapper.ResourcesFor(input)
86+
}
87+
88+
func (o *lazyObject) RESTMapping(gk schema.GroupKind, versions ...string) (*RESTMapping, error) {
89+
if err := o.init(); err != nil {
90+
return nil, err
91+
}
92+
return o.mapper.RESTMapping(gk, versions...)
93+
}
94+
95+
func (o *lazyObject) RESTMappings(gk schema.GroupKind, versions ...string) ([]*RESTMapping, error) {
96+
if err := o.init(); err != nil {
97+
return nil, err
98+
}
99+
return o.mapper.RESTMappings(gk, versions...)
100+
}
101+
102+
func (o *lazyObject) ResourceSingularizer(resource string) (singular string, err error) {
103+
if err := o.init(); err != nil {
104+
return "", err
105+
}
106+
return o.mapper.ResourceSingularizer(resource)
107+
}
108+
109+
func (o *lazyObject) ObjectKinds(obj runtime.Object) ([]schema.GroupVersionKind, bool, error) {
110+
if err := o.init(); err != nil {
111+
return nil, false, err
112+
}
113+
return o.typer.ObjectKinds(obj)
114+
}
115+
116+
func (o *lazyObject) Recognizes(gvk schema.GroupVersionKind) bool {
117+
if err := o.init(); err != nil {
118+
return false
119+
}
120+
return o.typer.Recognizes(gvk)
121+
}

pkg/api/meta/unstructured.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,24 @@ import (
2121
"k8s.io/apimachinery/pkg/runtime/schema"
2222
)
2323

24+
// InterfacesForUnstructuredConversion returns VersionInterfaces suitable for
25+
// dealing with unstructured.Unstructured objects and supports conversion
26+
// from typed objects (provided by parent) to untyped objects.
27+
func InterfacesForUnstructuredConversion(parent VersionInterfacesFunc) VersionInterfacesFunc {
28+
return func(version schema.GroupVersion) (*VersionInterfaces, error) {
29+
if i, err := parent(version); err == nil {
30+
return &VersionInterfaces{
31+
ObjectConvertor: i.ObjectConvertor,
32+
MetadataAccessor: NewAccessor(),
33+
}, nil
34+
}
35+
return InterfacesForUnstructured(version)
36+
}
37+
}
38+
2439
// InterfacesForUnstructured returns VersionInterfaces suitable for
25-
// dealing with unstructured.Unstructured objects.
40+
// dealing with unstructured.Unstructured objects. It will return errors for
41+
// other conversions.
2642
func InterfacesForUnstructured(schema.GroupVersion) (*VersionInterfaces, error) {
2743
return &VersionInterfaces{
2844
ObjectConvertor: &unstructured.UnstructuredObjectConverter{},

0 commit comments

Comments
 (0)