github.phpd.cn/cilium/cilium@v1.6.12/pkg/k8s/informer/informer.go (about)

     1  // Copyright 2019 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package informer
    16  
    17  import (
    18  	"time"
    19  
    20  	"k8s.io/apimachinery/pkg/runtime"
    21  	"k8s.io/client-go/tools/cache"
    22  )
    23  
    24  type ConvertFunc func(obj interface{}) interface{}
    25  
    26  // NewInformer is a copy of k8s.io/client-go/tools/cache/NewInformer with a new
    27  // argument which converts an object into another object that can be stored in
    28  // the local cache.
    29  func NewInformer(
    30  	lw cache.ListerWatcher,
    31  	objType runtime.Object,
    32  	resyncPeriod time.Duration,
    33  	h cache.ResourceEventHandler,
    34  	convertFunc ConvertFunc,
    35  ) (cache.Store, cache.Controller) {
    36  	// This will hold the client state, as we know it.
    37  	clientState := cache.NewStore(cache.DeletionHandlingMetaNamespaceKeyFunc)
    38  
    39  	return clientState, NewInformerWithStore(lw, objType, resyncPeriod, h, convertFunc, clientState)
    40  }
    41  
    42  // NewInformerWithStore uses the same arguments as NewInformer for which a
    43  // caller can also set a cache.Store.
    44  func NewInformerWithStore(
    45  	lw cache.ListerWatcher,
    46  	objType runtime.Object,
    47  	resyncPeriod time.Duration,
    48  	h cache.ResourceEventHandler,
    49  	convertFunc ConvertFunc,
    50  	clientState cache.Store,
    51  ) cache.Controller {
    52  
    53  	// This will hold incoming changes. Note how we pass clientState in as a
    54  	// KeyLister, that way resync operations will result in the correct set
    55  	// of update/delete deltas.
    56  	fifo := cache.NewDeltaFIFO(cache.MetaNamespaceKeyFunc, clientState)
    57  
    58  	cfg := &cache.Config{
    59  		Queue:            fifo,
    60  		ListerWatcher:    lw,
    61  		ObjectType:       objType,
    62  		FullResyncPeriod: resyncPeriod,
    63  		RetryOnError:     false,
    64  
    65  		Process: func(obj interface{}) error {
    66  			// from oldest to newest
    67  			for _, d := range obj.(cache.Deltas) {
    68  
    69  				obj := convertFunc(d.Object)
    70  
    71  				switch d.Type {
    72  				case cache.Sync, cache.Added, cache.Updated:
    73  					if old, exists, err := clientState.Get(obj); err == nil && exists {
    74  						if err := clientState.Update(obj); err != nil {
    75  							return err
    76  						}
    77  						h.OnUpdate(old, obj)
    78  					} else {
    79  						if err := clientState.Add(obj); err != nil {
    80  							return err
    81  						}
    82  						h.OnAdd(obj)
    83  					}
    84  				case cache.Deleted:
    85  					if err := clientState.Delete(obj); err != nil {
    86  						return err
    87  					}
    88  					h.OnDelete(obj)
    89  				}
    90  			}
    91  			return nil
    92  		},
    93  	}
    94  	return cache.New(cfg)
    95  }