2 Copyright 2017 The Kubernetes Authors.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
22 "k8s.io/apimachinery/pkg/runtime/schema"
25 // lazyObject defers loading the mapper and typer until necessary.
26 type lazyObject struct {
27 loader func() (RESTMapper, error)
35 // NewLazyObjectLoader handles unrecoverable errors when creating a RESTMapper / ObjectTyper by
36 // returning those initialization errors when the interface methods are invoked. This defers the
37 // initialization and any server calls until a client actually needs to perform the action.
38 func NewLazyRESTMapperLoader(fn func() (RESTMapper, error)) RESTMapper {
39 obj := &lazyObject{loader: fn}
43 // init lazily loads the mapper and typer, returning an error if initialization has failed.
44 func (o *lazyObject) init() error {
50 o.mapper, o.err = o.loader()
55 var _ RESTMapper = &lazyObject{}
57 func (o *lazyObject) KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
58 if err := o.init(); err != nil {
59 return schema.GroupVersionKind{}, err
61 return o.mapper.KindFor(resource)
64 func (o *lazyObject) KindsFor(resource schema.GroupVersionResource) ([]schema.GroupVersionKind, error) {
65 if err := o.init(); err != nil {
66 return []schema.GroupVersionKind{}, err
68 return o.mapper.KindsFor(resource)
71 func (o *lazyObject) ResourceFor(input schema.GroupVersionResource) (schema.GroupVersionResource, error) {
72 if err := o.init(); err != nil {
73 return schema.GroupVersionResource{}, err
75 return o.mapper.ResourceFor(input)
78 func (o *lazyObject) ResourcesFor(input schema.GroupVersionResource) ([]schema.GroupVersionResource, error) {
79 if err := o.init(); err != nil {
80 return []schema.GroupVersionResource{}, err
82 return o.mapper.ResourcesFor(input)
85 func (o *lazyObject) RESTMapping(gk schema.GroupKind, versions ...string) (*RESTMapping, error) {
86 if err := o.init(); err != nil {
89 return o.mapper.RESTMapping(gk, versions...)
92 func (o *lazyObject) RESTMappings(gk schema.GroupKind, versions ...string) ([]*RESTMapping, error) {
93 if err := o.init(); err != nil {
96 return o.mapper.RESTMappings(gk, versions...)
99 func (o *lazyObject) ResourceSingularizer(resource string) (singular string, err error) {
100 if err := o.init(); err != nil {
103 return o.mapper.ResourceSingularizer(resource)