github.com/cilium/cilium@v1.16.2/operator/pkg/ciliumendpointslice/controller.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package ciliumendpointslice 5 6 import ( 7 "context" 8 "time" 9 10 "github.com/cilium/hive/cell" 11 "github.com/cilium/workerpool" 12 "github.com/sirupsen/logrus" 13 "k8s.io/client-go/util/workqueue" 14 15 v2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2" 16 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2alpha1" 17 k8sClient "github.com/cilium/cilium/pkg/k8s/client" 18 "github.com/cilium/cilium/pkg/k8s/resource" 19 "github.com/cilium/cilium/pkg/lock" 20 ) 21 22 // params contains all the dependencies for the CiliumEndpointSlice controller. 23 // They will be provided through dependency injection. 24 type params struct { 25 cell.In 26 27 Logger logrus.FieldLogger 28 Lifecycle cell.Lifecycle 29 30 NewClient k8sClient.ClientBuilderFunc 31 CiliumEndpoint resource.Resource[*v2.CiliumEndpoint] 32 CiliumEndpointSlice resource.Resource[*v2alpha1.CiliumEndpointSlice] 33 CiliumNodes resource.Resource[*v2.CiliumNode] 34 35 Cfg Config 36 SharedCfg SharedConfig 37 38 Metrics *Metrics 39 } 40 41 type Controller struct { 42 logger logrus.FieldLogger 43 context context.Context 44 contextCancel context.CancelFunc 45 46 // Cilium kubernetes clients to access V2 and V2alpha1 resources 47 clientset k8sClient.Clientset 48 ciliumEndpoint resource.Resource[*v2.CiliumEndpoint] 49 ciliumEndpointSlice resource.Resource[*v2alpha1.CiliumEndpointSlice] 50 ciliumNodes resource.Resource[*v2.CiliumNode] 51 52 // reconciler is an util used to reconcile CiliumEndpointSlice changes. 53 reconciler *reconciler 54 55 // Manager is used to create and maintain a local datastore. Manager watches for 56 // cilium endpoint changes and enqueues/dequeues the cilium endpoint changes in CES. 57 // It maintains the desired state of the CESs in dataStore 58 manager operations 59 slicingMode string 60 maxCEPsInCES int 61 62 // workqueue is used to sync CESs with the api-server. this will rate-limit the 63 // CES requests going to api-server, ensures a single CES will not be proccessed 64 // multiple times concurrently, and if CES is added multiple times before it 65 // can be processed, this will only be processed only once. 66 queue workqueue.RateLimitingInterface 67 rateLimit rateLimitConfig 68 69 enqueuedAt map[CESName]time.Time 70 enqueuedAtLock lock.Mutex 71 72 wp *workerpool.WorkerPool 73 74 metrics *Metrics 75 } 76 77 // registerController creates and initializes the CES controller 78 func registerController(p params) error { 79 clientset, err := p.NewClient("ciliumendpointslice-controller") 80 if err != nil { 81 return err 82 } 83 if !clientset.IsEnabled() || !p.SharedCfg.EnableCiliumEndpointSlice { 84 return nil 85 } 86 87 rateLimitConfig, err := getRateLimitConfig(p) 88 if err != nil { 89 return err 90 } 91 92 checkDeprecatedOpts(p.Cfg, p.Logger) 93 94 cesController := &Controller{ 95 logger: p.Logger, 96 clientset: clientset, 97 ciliumEndpoint: p.CiliumEndpoint, 98 ciliumEndpointSlice: p.CiliumEndpointSlice, 99 ciliumNodes: p.CiliumNodes, 100 slicingMode: p.Cfg.CESSlicingMode, 101 maxCEPsInCES: p.Cfg.CESMaxCEPsInCES, 102 rateLimit: rateLimitConfig, 103 enqueuedAt: make(map[CESName]time.Time), 104 metrics: p.Metrics, 105 } 106 107 p.Lifecycle.Append(cesController) 108 return nil 109 } 110 111 // checkDeprecatedOpts will log an error if the user has supplied any of the 112 // no-op, deprecated rate limit options. 113 // TODO: Remove this function when the deprecated options are removed. 114 func checkDeprecatedOpts(cfg Config, logger logrus.FieldLogger) { 115 switch { 116 case cfg.CESWriteQPSLimit > 0: 117 case cfg.CESWriteQPSBurst > 0: 118 case cfg.CESEnableDynamicRateLimit: 119 case len(cfg.CESDynamicRateLimitNodes) > 0: 120 case len(cfg.CESDynamicRateLimitQPSLimit) > 0: 121 case len(cfg.CESDynamicRateLimitQPSBurst) > 0: 122 default: 123 return 124 } 125 logger.Errorf("You are using deprecated rate limit option(s) that have no effect. To configure custom rate limits please use --%s", CESRateLimits) 126 }