blob: 431a0a6353247d280d1bd8aa60b59f47ab0308ab [file] [log] [blame]
Scott Bakere7144bc2019-10-01 14:16:47 -07001/*
2Copyright 2017 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package meta
18
19import (
20 "sync"
21
22 "k8s.io/apimachinery/pkg/runtime/schema"
23)
24
25// lazyObject defers loading the mapper and typer until necessary.
26type lazyObject struct {
27 loader func() (RESTMapper, error)
28
29 lock sync.Mutex
30 loaded bool
31 err error
32 mapper RESTMapper
33}
34
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.
38func NewLazyRESTMapperLoader(fn func() (RESTMapper, error)) RESTMapper {
39 obj := &lazyObject{loader: fn}
40 return obj
41}
42
43// init lazily loads the mapper and typer, returning an error if initialization has failed.
44func (o *lazyObject) init() error {
45 o.lock.Lock()
46 defer o.lock.Unlock()
47 if o.loaded {
48 return o.err
49 }
50 o.mapper, o.err = o.loader()
51 o.loaded = true
52 return o.err
53}
54
55var _ RESTMapper = &lazyObject{}
56
57func (o *lazyObject) KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
58 if err := o.init(); err != nil {
59 return schema.GroupVersionKind{}, err
60 }
61 return o.mapper.KindFor(resource)
62}
63
64func (o *lazyObject) KindsFor(resource schema.GroupVersionResource) ([]schema.GroupVersionKind, error) {
65 if err := o.init(); err != nil {
66 return []schema.GroupVersionKind{}, err
67 }
68 return o.mapper.KindsFor(resource)
69}
70
71func (o *lazyObject) ResourceFor(input schema.GroupVersionResource) (schema.GroupVersionResource, error) {
72 if err := o.init(); err != nil {
73 return schema.GroupVersionResource{}, err
74 }
75 return o.mapper.ResourceFor(input)
76}
77
78func (o *lazyObject) ResourcesFor(input schema.GroupVersionResource) ([]schema.GroupVersionResource, error) {
79 if err := o.init(); err != nil {
80 return []schema.GroupVersionResource{}, err
81 }
82 return o.mapper.ResourcesFor(input)
83}
84
85func (o *lazyObject) RESTMapping(gk schema.GroupKind, versions ...string) (*RESTMapping, error) {
86 if err := o.init(); err != nil {
87 return nil, err
88 }
89 return o.mapper.RESTMapping(gk, versions...)
90}
91
92func (o *lazyObject) RESTMappings(gk schema.GroupKind, versions ...string) ([]*RESTMapping, error) {
93 if err := o.init(); err != nil {
94 return nil, err
95 }
96 return o.mapper.RESTMappings(gk, versions...)
97}
98
99func (o *lazyObject) ResourceSingularizer(resource string) (singular string, err error) {
100 if err := o.init(); err != nil {
101 return "", err
102 }
103 return o.mapper.ResourceSingularizer(resource)
104}