...

Source file src/sigs.k8s.io/cli-utils/pkg/kstatus/watcher/dynamic_informer_factory.go

Documentation: sigs.k8s.io/cli-utils/pkg/kstatus/watcher

     1  // Copyright 2022 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package watcher
     5  
     6  import (
     7  	"context"
     8  	"time"
     9  
    10  	"k8s.io/apimachinery/pkg/api/meta"
    11  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    12  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    13  	"k8s.io/apimachinery/pkg/runtime"
    14  	"k8s.io/apimachinery/pkg/watch"
    15  	"k8s.io/client-go/dynamic"
    16  	"k8s.io/client-go/tools/cache"
    17  )
    18  
    19  type DynamicInformerFactory struct {
    20  	Client       dynamic.Interface
    21  	ResyncPeriod time.Duration
    22  	Indexers     cache.Indexers
    23  }
    24  
    25  func NewDynamicInformerFactory(client dynamic.Interface, resyncPeriod time.Duration) *DynamicInformerFactory {
    26  	return &DynamicInformerFactory{
    27  		Client:       client,
    28  		ResyncPeriod: resyncPeriod,
    29  		Indexers: cache.Indexers{
    30  			cache.NamespaceIndex: cache.MetaNamespaceIndexFunc,
    31  		},
    32  	}
    33  }
    34  
    35  func (f *DynamicInformerFactory) NewInformer(ctx context.Context, mapping *meta.RESTMapping, namespace string) cache.SharedIndexInformer {
    36  	// Unstructured example output need `"apiVersion"` and `"kind"` set.
    37  	example := &unstructured.Unstructured{}
    38  	example.SetGroupVersionKind(mapping.GroupVersionKind)
    39  
    40  	return cache.NewSharedIndexInformer(
    41  		&cache.ListWatch{
    42  			ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
    43  				return f.Client.Resource(mapping.Resource).
    44  					Namespace(namespace).
    45  					List(ctx, options)
    46  			},
    47  			WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
    48  				return f.Client.Resource(mapping.Resource).
    49  					Namespace(namespace).
    50  					Watch(ctx, options)
    51  			},
    52  		},
    53  		example,
    54  		f.ResyncPeriod,
    55  		f.Indexers,
    56  	)
    57  }
    58  

View as plain text