github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/api/meta/lazy.go (about)

     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  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema"
    23  )
    24  
    25  // lazyObject defers loading the mapper and typer until necessary.
    26  type lazyObject struct {
    27  	loader func() (RESTMapper, error)
    28  
    29  	lock   sync.Mutex
    30  	loaded bool
    31  	err    error
    32  	mapper RESTMapper
    33  }
    34  
    35  // NewLazyRESTMapperLoader 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}
    40  	return obj
    41  }
    42  
    43  // init lazily loads the mapper and typer, returning an error if initialization has failed.
    44  func (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  
    55  var _ ResettableRESTMapper = &lazyObject{}
    56  
    57  func (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  
    64  func (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  
    71  func (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  
    78  func (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  
    85  func (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  
    92  func (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  
    99  func (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  }
   105  
   106  func (o *lazyObject) Reset() {
   107  	o.lock.Lock()
   108  	defer o.lock.Unlock()
   109  	if o.loaded && o.err == nil {
   110  		MaybeResetRESTMapper(o.mapper)
   111  	}
   112  }