Remove BPA from Makefile
[icn.git] / cmd / bpa-operator / vendor / sigs.k8s.io / controller-runtime / pkg / cache / cache.go
1 /*
2 Copyright 2018 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 cache
18
19 import (
20         "fmt"
21         "time"
22
23         "k8s.io/apimachinery/pkg/api/meta"
24         "k8s.io/apimachinery/pkg/runtime"
25         "k8s.io/apimachinery/pkg/runtime/schema"
26         "k8s.io/client-go/kubernetes/scheme"
27         "k8s.io/client-go/rest"
28         toolscache "k8s.io/client-go/tools/cache"
29         "sigs.k8s.io/controller-runtime/pkg/cache/internal"
30         "sigs.k8s.io/controller-runtime/pkg/client"
31         "sigs.k8s.io/controller-runtime/pkg/client/apiutil"
32         logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
33 )
34
35 var log = logf.KBLog.WithName("object-cache")
36
37 // Cache implements CacheReader by reading objects from a cache populated by InformersMap
38 type Cache interface {
39         // Cache implements the client CacheReader
40         client.Reader
41
42         // Cache implements InformersMap
43         Informers
44 }
45
46 // Informers knows how to create or fetch informers for different group-version-kinds.
47 // It's safe to call GetInformer from multiple threads.
48 type Informers interface {
49         // GetInformer fetches or constructs an informer for the given object that corresponds to a single
50         // API kind and resource.
51         GetInformer(obj runtime.Object) (toolscache.SharedIndexInformer, error)
52
53         // GetInformerForKind is similar to GetInformer, except that it takes a group-version-kind, instead
54         // of the underlying object.
55         GetInformerForKind(gvk schema.GroupVersionKind) (toolscache.SharedIndexInformer, error)
56
57         // Start runs all the informers known to this cache until the given channel is closed.
58         // It blocks.
59         Start(stopCh <-chan struct{}) error
60
61         // WaitForCacheSync waits for all the caches to sync.  Returns false if it could not sync a cache.
62         WaitForCacheSync(stop <-chan struct{}) bool
63
64         // IndexField adds an index with the given field name on the given object type
65         // by using the given function to extract the value for that field.  If you want
66         // compatibility with the Kubernetes API server, only return one key, and only use
67         // fields that the API server supports.  Otherwise, you can return multiple keys,
68         // and "equality" in the field selector means that at least one key matches the value.
69         IndexField(obj runtime.Object, field string, extractValue client.IndexerFunc) error
70 }
71
72 // Options are the optional arguments for creating a new InformersMap object
73 type Options struct {
74         // Scheme is the scheme to use for mapping objects to GroupVersionKinds
75         Scheme *runtime.Scheme
76
77         // Mapper is the RESTMapper to use for mapping GroupVersionKinds to Resources
78         Mapper meta.RESTMapper
79
80         // Resync is the resync period. Defaults to defaultResyncTime.
81         Resync *time.Duration
82
83         // Namespace restricts the cache's ListWatch to the desired namespace
84         // Default watches all namespaces
85         Namespace string
86 }
87
88 var defaultResyncTime = 10 * time.Hour
89
90 // New initializes and returns a new Cache
91 func New(config *rest.Config, opts Options) (Cache, error) {
92         opts, err := defaultOpts(config, opts)
93         if err != nil {
94                 return nil, err
95         }
96         im := internal.NewInformersMap(config, opts.Scheme, opts.Mapper, *opts.Resync, opts.Namespace)
97         return &informerCache{InformersMap: im}, nil
98 }
99
100 func defaultOpts(config *rest.Config, opts Options) (Options, error) {
101         // Use the default Kubernetes Scheme if unset
102         if opts.Scheme == nil {
103                 opts.Scheme = scheme.Scheme
104         }
105
106         // Construct a new Mapper if unset
107         if opts.Mapper == nil {
108                 var err error
109                 opts.Mapper, err = apiutil.NewDiscoveryRESTMapper(config)
110                 if err != nil {
111                         log.WithName("setup").Error(err, "Failed to get API Group-Resources")
112                         return opts, fmt.Errorf("could not create RESTMapper from config")
113                 }
114         }
115
116         // Default the resync period to 10 hours if unset
117         if opts.Resync == nil {
118                 opts.Resync = &defaultResyncTime
119         }
120         return opts, nil
121 }