k3s/vendor/k8s.io/client-go/metadata/metadatainformer/informer.go

158 lines
5.2 KiB
Go
Raw Normal View History

2019-08-30 18:33:25 +00:00
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2019-09-27 21:51:53 +00:00
package metadatainformer
2019-08-30 18:33:25 +00:00
import (
2020-03-26 21:07:15 +00:00
"context"
2019-08-30 18:33:25 +00:00
"sync"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/informers"
2019-09-27 21:51:53 +00:00
"k8s.io/client-go/metadata"
"k8s.io/client-go/metadata/metadatalister"
2019-08-30 18:33:25 +00:00
"k8s.io/client-go/tools/cache"
)
2019-09-27 21:51:53 +00:00
// NewSharedInformerFactory constructs a new instance of metadataSharedInformerFactory for all namespaces.
func NewSharedInformerFactory(client metadata.Interface, defaultResync time.Duration) SharedInformerFactory {
return NewFilteredSharedInformerFactory(client, defaultResync, metav1.NamespaceAll, nil)
2019-08-30 18:33:25 +00:00
}
2019-09-27 21:51:53 +00:00
// NewFilteredSharedInformerFactory constructs a new instance of metadataSharedInformerFactory.
2019-08-30 18:33:25 +00:00
// Listers obtained via this factory will be subject to the same filters as specified here.
2019-09-27 21:51:53 +00:00
func NewFilteredSharedInformerFactory(client metadata.Interface, defaultResync time.Duration, namespace string, tweakListOptions TweakListOptionsFunc) SharedInformerFactory {
return &metadataSharedInformerFactory{
2019-08-30 18:33:25 +00:00
client: client,
defaultResync: defaultResync,
namespace: namespace,
informers: map[schema.GroupVersionResource]informers.GenericInformer{},
startedInformers: make(map[schema.GroupVersionResource]bool),
tweakListOptions: tweakListOptions,
}
}
2019-09-27 21:51:53 +00:00
type metadataSharedInformerFactory struct {
client metadata.Interface
2019-08-30 18:33:25 +00:00
defaultResync time.Duration
namespace string
lock sync.Mutex
informers map[schema.GroupVersionResource]informers.GenericInformer
// startedInformers is used for tracking which informers have been started.
// This allows Start() to be called multiple times safely.
startedInformers map[schema.GroupVersionResource]bool
tweakListOptions TweakListOptionsFunc
}
2019-09-27 21:51:53 +00:00
var _ SharedInformerFactory = &metadataSharedInformerFactory{}
2019-08-30 18:33:25 +00:00
2019-09-27 21:51:53 +00:00
func (f *metadataSharedInformerFactory) ForResource(gvr schema.GroupVersionResource) informers.GenericInformer {
2019-08-30 18:33:25 +00:00
f.lock.Lock()
defer f.lock.Unlock()
key := gvr
informer, exists := f.informers[key]
if exists {
return informer
}
2019-09-27 21:51:53 +00:00
informer = NewFilteredMetadataInformer(f.client, gvr, f.namespace, f.defaultResync, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, f.tweakListOptions)
2019-08-30 18:33:25 +00:00
f.informers[key] = informer
return informer
}
// Start initializes all requested informers.
2019-09-27 21:51:53 +00:00
func (f *metadataSharedInformerFactory) Start(stopCh <-chan struct{}) {
2019-08-30 18:33:25 +00:00
f.lock.Lock()
defer f.lock.Unlock()
for informerType, informer := range f.informers {
if !f.startedInformers[informerType] {
go informer.Informer().Run(stopCh)
f.startedInformers[informerType] = true
}
}
}
// WaitForCacheSync waits for all started informers' cache were synced.
2019-09-27 21:51:53 +00:00
func (f *metadataSharedInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[schema.GroupVersionResource]bool {
2019-08-30 18:33:25 +00:00
informers := func() map[schema.GroupVersionResource]cache.SharedIndexInformer {
f.lock.Lock()
defer f.lock.Unlock()
informers := map[schema.GroupVersionResource]cache.SharedIndexInformer{}
for informerType, informer := range f.informers {
if f.startedInformers[informerType] {
informers[informerType] = informer.Informer()
}
}
return informers
}()
res := map[schema.GroupVersionResource]bool{}
for informType, informer := range informers {
res[informType] = cache.WaitForCacheSync(stopCh, informer.HasSynced)
}
return res
}
2019-09-27 21:51:53 +00:00
// NewFilteredMetadataInformer constructs a new informer for a metadata type.
func NewFilteredMetadataInformer(client metadata.Interface, gvr schema.GroupVersionResource, namespace string, resyncPeriod time.Duration, indexers cache.Indexers, tweakListOptions TweakListOptionsFunc) informers.GenericInformer {
return &metadataInformer{
2019-08-30 18:33:25 +00:00
gvr: gvr,
informer: cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
if tweakListOptions != nil {
tweakListOptions(&options)
}
2020-03-26 21:07:15 +00:00
return client.Resource(gvr).Namespace(namespace).List(context.TODO(), options)
2019-08-30 18:33:25 +00:00
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
if tweakListOptions != nil {
tweakListOptions(&options)
}
2020-03-26 21:07:15 +00:00
return client.Resource(gvr).Namespace(namespace).Watch(context.TODO(), options)
2019-08-30 18:33:25 +00:00
},
},
2019-09-27 21:51:53 +00:00
&metav1.PartialObjectMetadata{},
2019-08-30 18:33:25 +00:00
resyncPeriod,
indexers,
),
}
}
2019-09-27 21:51:53 +00:00
type metadataInformer struct {
2019-08-30 18:33:25 +00:00
informer cache.SharedIndexInformer
gvr schema.GroupVersionResource
}
2019-09-27 21:51:53 +00:00
var _ informers.GenericInformer = &metadataInformer{}
2019-08-30 18:33:25 +00:00
2019-09-27 21:51:53 +00:00
func (d *metadataInformer) Informer() cache.SharedIndexInformer {
2019-08-30 18:33:25 +00:00
return d.informer
}
2019-09-27 21:51:53 +00:00
func (d *metadataInformer) Lister() cache.GenericLister {
return metadatalister.NewRuntimeObjectShim(metadatalister.New(d.informer.GetIndexer(), d.gvr))
2019-08-30 18:33:25 +00:00
}