2 Copyright 2018 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.
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"
35 var log = logf.KBLog.WithName("object-cache")
37 // Cache implements CacheReader by reading objects from a cache populated by InformersMap
38 type Cache interface {
39 // Cache implements the client CacheReader
42 // Cache implements InformersMap
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)
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)
57 // Start runs all the informers known to this cache until the given channel is closed.
59 Start(stopCh <-chan struct{}) error
61 // WaitForCacheSync waits for all the caches to sync. Returns false if it could not sync a cache.
62 WaitForCacheSync(stop <-chan struct{}) bool
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
72 // Options are the optional arguments for creating a new InformersMap object
74 // Scheme is the scheme to use for mapping objects to GroupVersionKinds
75 Scheme *runtime.Scheme
77 // Mapper is the RESTMapper to use for mapping GroupVersionKinds to Resources
78 Mapper meta.RESTMapper
80 // Resync is the resync period. Defaults to defaultResyncTime.
83 // Namespace restricts the cache's ListWatch to the desired namespace
84 // Default watches all namespaces
88 var defaultResyncTime = 10 * time.Hour
90 // New initializes and returns a new Cache
91 func New(config *rest.Config, opts Options) (Cache, error) {
92 opts, err := defaultOpts(config, opts)
96 im := internal.NewInformersMap(config, opts.Scheme, opts.Mapper, *opts.Resync, opts.Namespace)
97 return &informerCache{InformersMap: im}, nil
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
106 // Construct a new Mapper if unset
107 if opts.Mapper == nil {
109 opts.Mapper, err = apiutil.NewDiscoveryRESTMapper(config)
111 log.WithName("setup").Error(err, "Failed to get API Group-Resources")
112 return opts, fmt.Errorf("could not create RESTMapper from config")
116 // Default the resync period to 10 hours if unset
117 if opts.Resync == nil {
118 opts.Resync = &defaultResyncTime