lazy.go 3.0 KB

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