Skip to content

Commit 9775498

Browse files
committed
fix ipsec_controller golint warnings
Signed-off-by: mdimado <mdimad005@gmail.com>
1 parent af3e8e5 commit 9775498

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

pkg/controller/encryption/ipsec/ipsec_controller.go

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import (
5050
"kmesh.net/kmesh/pkg/utils"
5151
)
5252

53+
// MaxRetries defines the maximum number of retry attempts for failed operations
5354
const (
5455
MaxRetries = 5
5556
)
@@ -61,7 +62,8 @@ type lpmKey struct {
6162
ip [4]uint32
6263
}
6364

64-
type IPSecController struct {
65+
// Controller manages IPSec configuration and synchronization across Kubernetes nodes
66+
type Controller struct {
6567
informer cache.SharedIndexInformer
6668
lister kmeshnodeinfov1alpha1.KmeshNodeInfoLister
6769
queue workqueue.TypedRateLimitingInterface[any]
@@ -72,7 +74,8 @@ type IPSecController struct {
7274
tcDecryptProg *ebpf.Program
7375
}
7476

75-
func NewIPsecController(k8sClientSet kubernetes.Interface, kniMap *ebpf.Map, decryptProg *ebpf.Program) (*IPSecController, error) {
77+
// NewController creates a new IPSec controller instance with the provided Kubernetes client, KNI map, and decryption program
78+
func NewController(k8sClientSet kubernetes.Interface, kniMap *ebpf.Map, decryptProg *ebpf.Program) (*Controller, error) {
7679
clientSet, err := kube.GetKmeshNodeInfoClient()
7780
if err != nil {
7881
return nil, fmt.Errorf("failed to get kmesh node info client: %v", err)
@@ -81,7 +84,7 @@ func NewIPsecController(k8sClientSet kubernetes.Interface, kniMap *ebpf.Map, dec
8184
nodeinfoLister := factroy.Kmesh().V1alpha1().KmeshNodeInfos().Lister()
8285
nodeinfoInformer := factroy.Kmesh().V1alpha1().KmeshNodeInfos().Informer()
8386

84-
ipsecController := &IPSecController{
87+
ipsecController := &Controller{
8588
informer: nodeinfoInformer,
8689
lister: nodeinfoLister,
8790
queue: workqueue.NewTypedRateLimitingQueue(workqueue.DefaultTypedControllerRateLimiter[any]()),
@@ -138,7 +141,8 @@ func NewIPsecController(k8sClientSet kubernetes.Interface, kniMap *ebpf.Map, dec
138141
return ipsecController, nil
139142
}
140143

141-
func (c *IPSecController) Run(stop <-chan struct{}) {
144+
// Run starts the IPSec controller, initializing informers, attaching TC programs, and processing node information updates until the stop channel is closed
145+
func (c *Controller) Run(stop <-chan struct{}) {
142146
defer c.queue.ShutDown()
143147
go c.informer.Run(stop)
144148
if !cache.WaitForCacheSync(stop, c.informer.HasSynced) {
@@ -177,7 +181,8 @@ func (c *IPSecController) Run(stop <-chan struct{}) {
177181
<-stop
178182
}
179183

180-
func (c *IPSecController) Stop() {
184+
// Stop gracefully shuts down the IPSec controller, cleaning up resources, detaching TC programs, and removing node information if not restarting
185+
func (c *Controller) Stop() {
181186
c.ipsecHandler.StopWatch()
182187
if restart.GetStartType() == restart.Normal {
183188
_ = c.knclient.Delete(context.TODO(), c.kmeshNodeInfo.Name, metav1.DeleteOptions{})
@@ -186,7 +191,7 @@ func (c *IPSecController) Stop() {
186191
}
187192
}
188193

189-
func (c *IPSecController) handleTc(mode int) error {
194+
func (c *Controller) handleTc(mode int) error {
190195
ifaces, err := net.Interfaces()
191196
if err != nil {
192197
return fmt.Errorf("failed to get interfaces: %v", err)
@@ -219,7 +224,7 @@ func (c *IPSecController) handleTc(mode int) error {
219224
return nil
220225
}
221226

222-
func (c *IPSecController) attachTcDecrypt() error {
227+
func (c *Controller) attachTcDecrypt() error {
223228
nodeNsPath := kmesh_netns.GetNodeNSpath()
224229
attachFunc := func(netns.NetNS) error {
225230
return c.handleTc(constants.TC_ATTACH)
@@ -231,7 +236,7 @@ func (c *IPSecController) attachTcDecrypt() error {
231236
return nil
232237
}
233238

234-
func (c *IPSecController) detachTcDecrypt() error {
239+
func (c *Controller) detachTcDecrypt() error {
235240
nodeNsPath := kmesh_netns.GetNodeNSpath()
236241
detachFunc := func(netns.NetNS) error {
237242
return c.handleTc(constants.TC_DETACH)
@@ -243,7 +248,7 @@ func (c *IPSecController) detachTcDecrypt() error {
243248
return nil
244249
}
245250

246-
func (c *IPSecController) handleKNIAdd(obj interface{}) {
251+
func (c *Controller) handleKNIAdd(obj interface{}) {
247252
kni, ok := obj.(*v1alpha1.KmeshNodeInfo)
248253
if !ok {
249254
log.Errorf("expected *v1alpha1_core.KmeshNodeInfo but got %T in handle add func", obj)
@@ -256,7 +261,7 @@ func (c *IPSecController) handleKNIAdd(obj interface{}) {
256261
c.queue.AddRateLimited(kni.Name)
257262
}
258263

259-
func (c *IPSecController) handleKNIUpdate(oldObj, newObj interface{}) {
264+
func (c *Controller) handleKNIUpdate(oldObj, newObj interface{}) {
260265
newKni, okNew := newObj.(*v1alpha1.KmeshNodeInfo)
261266
if !okNew {
262267
log.Errorf("expected *v1alpha1_core.KmeshNodeInfo but got %T in handle update new obj func", newObj)
@@ -280,7 +285,7 @@ func (c *IPSecController) handleKNIUpdate(oldObj, newObj interface{}) {
280285
c.queue.AddRateLimited(newKni.Name)
281286
}
282287

283-
func (c *IPSecController) handleKNIDelete(obj interface{}) {
288+
func (c *Controller) handleKNIDelete(obj interface{}) {
284289
node, ok := obj.(*v1alpha1.KmeshNodeInfo)
285290
if !ok {
286291
log.Errorf("expected *v1alpha1_core.KmeshNodeInfo but got %T in handle delete func", obj)
@@ -308,7 +313,7 @@ func (c *IPSecController) handleKNIDelete(obj interface{}) {
308313
}
309314
}
310315

311-
func (c *IPSecController) handleOneNodeInfo(node *v1alpha1.KmeshNodeInfo) error {
316+
func (c *Controller) handleOneNodeInfo(node *v1alpha1.KmeshNodeInfo) error {
312317
// can't change ipsec information when process
313318
c.ipsecHandler.mutex.Lock()
314319
defer c.ipsecHandler.mutex.Unlock()
@@ -331,7 +336,7 @@ func (c *IPSecController) handleOneNodeInfo(node *v1alpha1.KmeshNodeInfo) error
331336
return nil
332337
}
333338

334-
func (c *IPSecController) generalKNIMapKey(remoteCIDR string) (*lpmKey, error) {
339+
func (c *Controller) generalKNIMapKey(remoteCIDR string) (*lpmKey, error) {
335340
prefix, err := netip.ParsePrefix(remoteCIDR)
336341
if err != nil {
337342
err = fmt.Errorf("update kni map podCIDR failed, podCIDR is %v, %v", remoteCIDR, err)
@@ -353,7 +358,7 @@ func (c *IPSecController) generalKNIMapKey(remoteCIDR string) (*lpmKey, error) {
353358
return kniKey, nil
354359
}
355360

356-
func (c *IPSecController) updateKNIMapCIDR(remoteCIDR string, mapfd *ebpf.Map) error {
361+
func (c *Controller) updateKNIMapCIDR(remoteCIDR string, mapfd *ebpf.Map) error {
357362
kniKey, err := c.generalKNIMapKey(remoteCIDR)
358363
if err != nil {
359364
return err
@@ -364,15 +369,15 @@ func (c *IPSecController) updateKNIMapCIDR(remoteCIDR string, mapfd *ebpf.Map) e
364369
return mapfd.Update(kniKey, &kniValue, ebpf.UpdateAny)
365370
}
366371

367-
func (c *IPSecController) deleteKNIMapCIDR(remoteCIDR string, mapfd *ebpf.Map) {
372+
func (c *Controller) deleteKNIMapCIDR(remoteCIDR string, mapfd *ebpf.Map) {
368373
kniKey, err := c.generalKNIMapKey(remoteCIDR)
369374
if err != nil {
370375
return
371376
}
372377
_ = mapfd.Delete(kniKey)
373378
}
374379

375-
func (c *IPSecController) syncAllNodeInfo() error {
380+
func (c *Controller) syncAllNodeInfo() error {
376381
nodeList, err := c.lister.KmeshNodeInfos(kube.KmeshNamespace).List(labels.Everything())
377382
if err != nil {
378383
return fmt.Errorf("failed to get kmesh node info list: %v", err)
@@ -388,7 +393,7 @@ func (c *IPSecController) syncAllNodeInfo() error {
388393
return nil
389394
}
390395

391-
func (c *IPSecController) updateLocalKmeshNodeInfo() error {
396+
func (c *Controller) updateLocalKmeshNodeInfo() error {
392397
node, _ := c.lister.KmeshNodeInfos(kube.KmeshNamespace).Get(c.kmeshNodeInfo.Name)
393398
if node == nil {
394399
_, err := c.knclient.Create(context.TODO(), &c.kmeshNodeInfo, metav1.CreateOptions{})
@@ -410,7 +415,8 @@ func (c *IPSecController) updateLocalKmeshNodeInfo() error {
410415
return nil
411416
}
412417

413-
func (c *IPSecController) CleanAllIPsec() {
418+
// CleanAllIPsec removes all IPSec configurations and rules from the current node
419+
func (c *Controller) CleanAllIPsec() {
414420
nodeNsPath := kmesh_netns.GetNodeNSpath()
415421
cleanFunc := func(netns.NetNS) error {
416422
c.ipsecHandler.Flush()
@@ -420,7 +426,7 @@ func (c *IPSecController) CleanAllIPsec() {
420426
_ = netns.WithNetNSPath(nodeNsPath, cleanFunc)
421427
}
422428

423-
func (c *IPSecController) processNextItem() bool {
429+
func (c *Controller) processNextItem() bool {
424430
key, quit := c.queue.Get()
425431
if quit {
426432
return false
@@ -456,7 +462,7 @@ func (c *IPSecController) processNextItem() bool {
456462
}
457463

458464
// this function need ipsechanler mutex lock before use
459-
func (c *IPSecController) handleIpsecUpdate() {
465+
func (c *Controller) handleIpsecUpdate() {
460466
c.kmeshNodeInfo.Spec.SPI = c.ipsecHandler.Spi
461467
nodeNsPath := kmesh_netns.GetNodeNSpath()
462468

0 commit comments

Comments
 (0)