Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / sigs.k8s.io / controller-runtime / pkg / cache / internal / deleg_map.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 internal
18
19 import (
20         "time"
21
22         "k8s.io/apimachinery/pkg/api/meta"
23         "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
24         "k8s.io/apimachinery/pkg/runtime"
25         "k8s.io/apimachinery/pkg/runtime/schema"
26         "k8s.io/client-go/rest"
27         "k8s.io/client-go/tools/cache"
28 )
29
30 // InformersMap create and caches Informers for (runtime.Object, schema.GroupVersionKind) pairs.
31 // It uses a standard parameter codec constructed based on the given generated Scheme.
32 type InformersMap struct {
33         // we abstract over the details of structured vs unstructured with the specificInformerMaps
34
35         structured   *specificInformersMap
36         unstructured *specificInformersMap
37
38         // Scheme maps runtime.Objects to GroupVersionKinds
39         Scheme *runtime.Scheme
40 }
41
42 // NewInformersMap creates a new InformersMap that can create informers for
43 // both structured and unstructured objects.
44 func NewInformersMap(config *rest.Config,
45         scheme *runtime.Scheme,
46         mapper meta.RESTMapper,
47         resync time.Duration,
48         namespace string) *InformersMap {
49
50         return &InformersMap{
51                 structured:   newStructuredInformersMap(config, scheme, mapper, resync, namespace),
52                 unstructured: newUnstructuredInformersMap(config, scheme, mapper, resync, namespace),
53
54                 Scheme: scheme,
55         }
56 }
57
58 // Start calls Run on each of the informers and sets started to true.  Blocks on the stop channel.
59 func (m *InformersMap) Start(stop <-chan struct{}) error {
60         go m.structured.Start(stop)
61         go m.unstructured.Start(stop)
62         <-stop
63         return nil
64 }
65
66 // WaitForCacheSync waits until all the caches have been synced.
67 func (m *InformersMap) WaitForCacheSync(stop <-chan struct{}) bool {
68         syncedFuncs := append([]cache.InformerSynced(nil), m.structured.HasSyncedFuncs()...)
69         syncedFuncs = append(syncedFuncs, m.unstructured.HasSyncedFuncs()...)
70
71         return cache.WaitForCacheSync(stop, syncedFuncs...)
72 }
73
74 // Get will create a new Informer and add it to the map of InformersMap if none exists.  Returns
75 // the Informer from the map.
76 func (m *InformersMap) Get(gvk schema.GroupVersionKind, obj runtime.Object) (*MapEntry, error) {
77         _, isUnstructured := obj.(*unstructured.Unstructured)
78         _, isUnstructuredList := obj.(*unstructured.UnstructuredList)
79         isUnstructured = isUnstructured || isUnstructuredList
80
81         if isUnstructured {
82                 return m.unstructured.Get(gvk, obj)
83         }
84
85         return m.structured.Get(gvk, obj)
86 }
87
88 // newStructuredInformersMap creates a new InformersMap for structured objects.
89 func newStructuredInformersMap(config *rest.Config, scheme *runtime.Scheme, mapper meta.RESTMapper, resync time.Duration, namespace string) *specificInformersMap {
90         return newSpecificInformersMap(config, scheme, mapper, resync, namespace, createStructuredListWatch)
91 }
92
93 // newUnstructuredInformersMap creates a new InformersMap for unstructured objects.
94 func newUnstructuredInformersMap(config *rest.Config, scheme *runtime.Scheme, mapper meta.RESTMapper, resync time.Duration, namespace string) *specificInformersMap {
95         return newSpecificInformersMap(config, scheme, mapper, resync, namespace, createUnstructuredListWatch)
96 }