...

Source file src/sigs.k8s.io/controller-runtime/pkg/controller/controllertest/util.go

Documentation: sigs.k8s.io/controller-runtime/pkg/controller/controllertest

     1  /*
     2  Copyright 2017 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 controllertest
    18  
    19  import (
    20  	"time"
    21  
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"k8s.io/client-go/tools/cache"
    24  )
    25  
    26  var _ cache.SharedIndexInformer = &FakeInformer{}
    27  
    28  // FakeInformer provides fake Informer functionality for testing.
    29  type FakeInformer struct {
    30  	// Synced is returned by the HasSynced functions to implement the Informer interface
    31  	Synced bool
    32  
    33  	// RunCount is incremented each time RunInformersAndControllers is called
    34  	RunCount int
    35  
    36  	handlers []eventHandlerWrapper
    37  }
    38  
    39  type modernResourceEventHandler interface {
    40  	OnAdd(obj interface{}, isInInitialList bool)
    41  	OnUpdate(oldObj, newObj interface{})
    42  	OnDelete(obj interface{})
    43  }
    44  
    45  type legacyResourceEventHandler interface {
    46  	OnAdd(obj interface{})
    47  	OnUpdate(oldObj, newObj interface{})
    48  	OnDelete(obj interface{})
    49  }
    50  
    51  // eventHandlerWrapper wraps a ResourceEventHandler in a manner that is compatible with client-go 1.27+ and older.
    52  // The interface was changed in these versions.
    53  type eventHandlerWrapper struct {
    54  	handler any
    55  }
    56  
    57  func (e eventHandlerWrapper) OnAdd(obj interface{}) {
    58  	if m, ok := e.handler.(modernResourceEventHandler); ok {
    59  		m.OnAdd(obj, false)
    60  		return
    61  	}
    62  	e.handler.(legacyResourceEventHandler).OnAdd(obj)
    63  }
    64  
    65  func (e eventHandlerWrapper) OnUpdate(oldObj, newObj interface{}) {
    66  	if m, ok := e.handler.(modernResourceEventHandler); ok {
    67  		m.OnUpdate(oldObj, newObj)
    68  		return
    69  	}
    70  	e.handler.(legacyResourceEventHandler).OnUpdate(oldObj, newObj)
    71  }
    72  
    73  func (e eventHandlerWrapper) OnDelete(obj interface{}) {
    74  	if m, ok := e.handler.(modernResourceEventHandler); ok {
    75  		m.OnDelete(obj)
    76  		return
    77  	}
    78  	e.handler.(legacyResourceEventHandler).OnDelete(obj)
    79  }
    80  
    81  // AddIndexers does nothing.  TODO(community): Implement this.
    82  func (f *FakeInformer) AddIndexers(indexers cache.Indexers) error {
    83  	return nil
    84  }
    85  
    86  // GetIndexer does nothing.  TODO(community): Implement this.
    87  func (f *FakeInformer) GetIndexer() cache.Indexer {
    88  	return nil
    89  }
    90  
    91  // Informer returns the fake Informer.
    92  func (f *FakeInformer) Informer() cache.SharedIndexInformer {
    93  	return f
    94  }
    95  
    96  // HasSynced implements the Informer interface.  Returns f.Synced.
    97  func (f *FakeInformer) HasSynced() bool {
    98  	return f.Synced
    99  }
   100  
   101  // AddEventHandler implements the Informer interface.  Adds an EventHandler to the fake Informers. TODO(community): Implement Registration.
   102  func (f *FakeInformer) AddEventHandler(handler cache.ResourceEventHandler) (cache.ResourceEventHandlerRegistration, error) {
   103  	f.handlers = append(f.handlers, eventHandlerWrapper{handler})
   104  	return nil, nil
   105  }
   106  
   107  // Run implements the Informer interface.  Increments f.RunCount.
   108  func (f *FakeInformer) Run(<-chan struct{}) {
   109  	f.RunCount++
   110  }
   111  
   112  // Add fakes an Add event for obj.
   113  func (f *FakeInformer) Add(obj metav1.Object) {
   114  	for _, h := range f.handlers {
   115  		h.OnAdd(obj)
   116  	}
   117  }
   118  
   119  // Update fakes an Update event for obj.
   120  func (f *FakeInformer) Update(oldObj, newObj metav1.Object) {
   121  	for _, h := range f.handlers {
   122  		h.OnUpdate(oldObj, newObj)
   123  	}
   124  }
   125  
   126  // Delete fakes an Delete event for obj.
   127  func (f *FakeInformer) Delete(obj metav1.Object) {
   128  	for _, h := range f.handlers {
   129  		h.OnDelete(obj)
   130  	}
   131  }
   132  
   133  // AddEventHandlerWithResyncPeriod does nothing.  TODO(community): Implement this.
   134  func (f *FakeInformer) AddEventHandlerWithResyncPeriod(handler cache.ResourceEventHandler, resyncPeriod time.Duration) (cache.ResourceEventHandlerRegistration, error) {
   135  	return nil, nil
   136  }
   137  
   138  // RemoveEventHandler does nothing.  TODO(community): Implement this.
   139  func (f *FakeInformer) RemoveEventHandler(handle cache.ResourceEventHandlerRegistration) error {
   140  	return nil
   141  }
   142  
   143  // GetStore does nothing.  TODO(community): Implement this.
   144  func (f *FakeInformer) GetStore() cache.Store {
   145  	return nil
   146  }
   147  
   148  // GetController does nothing.  TODO(community): Implement this.
   149  func (f *FakeInformer) GetController() cache.Controller {
   150  	return nil
   151  }
   152  
   153  // LastSyncResourceVersion does nothing.  TODO(community): Implement this.
   154  func (f *FakeInformer) LastSyncResourceVersion() string {
   155  	return ""
   156  }
   157  
   158  // SetWatchErrorHandler does nothing.  TODO(community): Implement this.
   159  func (f *FakeInformer) SetWatchErrorHandler(cache.WatchErrorHandler) error {
   160  	return nil
   161  }
   162  
   163  // SetTransform does nothing.  TODO(community): Implement this.
   164  func (f *FakeInformer) SetTransform(t cache.TransformFunc) error {
   165  	return nil
   166  }
   167  
   168  // IsStopped does nothing.  TODO(community): Implement this.
   169  func (f *FakeInformer) IsStopped() bool {
   170  	return false
   171  }
   172  

View as plain text