@@ -14,8 +14,8 @@ import (
1414 "k8s.io/client-go/kubernetes"
1515 "k8s.io/client-go/rest"
1616 "k8s.io/client-go/tools/clientcmd"
17- "k8s.io/klog "
18- "k8s.io/kubernetes/pkg/cloudprovider "
17+ cloudprovider "k8s.io/cloud-provider "
18+ "k8s.io/klog/v2 "
1919
2020 "github.com/linode/linode-cloud-controller-manager/sentry"
2121 "github.com/linode/linodego"
@@ -111,7 +111,7 @@ func (l *loadbalancers) getLatestServiceLoadBalancerStatus(ctx context.Context,
111111 return v1.LoadBalancerStatus {}, err
112112 }
113113
114- service , err = l .kubeClient .CoreV1 ().Services (service .Namespace ).Get (service .Name , metav1.GetOptions {})
114+ service , err = l .kubeClient .CoreV1 ().Services (service .Namespace ).Get (ctx , service .Name , metav1.GetOptions {})
115115 if err != nil {
116116 return v1.LoadBalancerStatus {}, err
117117 }
@@ -164,6 +164,14 @@ func (l *loadbalancers) cleanupOldNodeBalancer(ctx context.Context, service *v1.
164164 return nil
165165}
166166
167+ // GetLoadBalancerName returns the name of the load balancer.
168+ //
169+ // GetLoadBalancer will not modify service.
170+ func (l * loadbalancers ) GetLoadBalancerName (ctx context.Context , clusterName string , service * v1.Service ) string {
171+ unixNano := strconv .FormatInt (time .Now ().UnixNano (), 16 )
172+ return fmt .Sprintf ("ccm-%s" , unixNano [len (unixNano )- 12 :])
173+ }
174+
167175// GetLoadBalancer returns the *v1.LoadBalancerStatus of service.
168176//
169177// GetLoadBalancer will not modify service.
@@ -203,7 +211,7 @@ func (l *loadbalancers) EnsureLoadBalancer(ctx context.Context, clusterName stri
203211 nb , err = l .getNodeBalancerForService (ctx , service )
204212 switch err .(type ) {
205213 case lbNotFoundError :
206- if nb , err = l .buildLoadBalancerRequest (ctx , service , nodes ); err != nil {
214+ if nb , err = l .buildLoadBalancerRequest (ctx , clusterName , service , nodes ); err != nil {
207215 sentry .CaptureError (ctx , err )
208216 return nil , err
209217 }
@@ -269,7 +277,7 @@ func (l *loadbalancers) updateNodeBalancer(ctx context.Context, service *v1.Serv
269277 }
270278
271279 // Construct a new config for this port
272- newNBCfg , err := l .buildNodeBalancerConfig (service , int (port .Port ))
280+ newNBCfg , err := l .buildNodeBalancerConfig (ctx , service , int (port .Port ))
273281 if err != nil {
274282 sentry .CaptureError (ctx , err )
275283 return err
@@ -453,11 +461,10 @@ func (l *loadbalancers) getNodeBalancerByID(ctx context.Context, service *v1.Ser
453461 return nb , nil
454462}
455463
456- func (l * loadbalancers ) createNodeBalancer (ctx context.Context , service * v1.Service , configs []* linodego.NodeBalancerConfigCreateOptions ) (lb * linodego.NodeBalancer , err error ) {
464+ func (l * loadbalancers ) createNodeBalancer (ctx context.Context , clusterName string , service * v1.Service , configs []* linodego.NodeBalancerConfigCreateOptions ) (lb * linodego.NodeBalancer , err error ) {
457465 connThrottle := getConnectionThrottle (service )
458466
459- unixNano := strconv .FormatInt (time .Now ().UnixNano (), 16 )
460- label := fmt .Sprintf ("ccm-%s" , unixNano [len (unixNano )- 12 :])
467+ label := l .GetLoadBalancerName (ctx , clusterName , service )
461468 createOpts := linodego.NodeBalancerCreateOptions {
462469 Label : & label ,
463470 Region : l .zone ,
@@ -468,7 +475,7 @@ func (l *loadbalancers) createNodeBalancer(ctx context.Context, service *v1.Serv
468475}
469476
470477//nolint:funlen
471- func (l * loadbalancers ) buildNodeBalancerConfig (service * v1.Service , port int ) (linodego.NodeBalancerConfig , error ) {
478+ func (l * loadbalancers ) buildNodeBalancerConfig (ctx context. Context , service * v1.Service , port int ) (linodego.NodeBalancerConfig , error ) {
472479 portConfig , err := getPortConfig (service , port )
473480 if err != nil {
474481 return linodego.NodeBalancerConfig {}, err
@@ -534,21 +541,21 @@ func (l *loadbalancers) buildNodeBalancerConfig(service *v1.Service, port int) (
534541 config .CheckPassive = checkPassive
535542
536543 if portConfig .Protocol == linodego .ProtocolHTTPS {
537- if err = l .addTLSCert (service , & config , portConfig ); err != nil {
544+ if err = l .addTLSCert (ctx , service , & config , portConfig ); err != nil {
538545 return config , err
539546 }
540547 }
541548
542549 return config , nil
543550}
544551
545- func (l * loadbalancers ) addTLSCert (service * v1.Service , nbConfig * linodego.NodeBalancerConfig , config portConfig ) error {
552+ func (l * loadbalancers ) addTLSCert (ctx context. Context , service * v1.Service , nbConfig * linodego.NodeBalancerConfig , config portConfig ) error {
546553 err := l .retrieveKubeClient ()
547554 if err != nil {
548555 return err
549556 }
550557
551- nbConfig .SSLCert , nbConfig .SSLKey , err = getTLSCertInfo (l .kubeClient , service .Namespace , config )
558+ nbConfig .SSLCert , nbConfig .SSLKey , err = getTLSCertInfo (ctx , l .kubeClient , service .Namespace , config )
552559 if err != nil {
553560 return err
554561 }
@@ -557,7 +564,7 @@ func (l *loadbalancers) addTLSCert(service *v1.Service, nbConfig *linodego.NodeB
557564
558565// buildLoadBalancerRequest returns a linodego.NodeBalancer
559566// requests for service across nodes.
560- func (l * loadbalancers ) buildLoadBalancerRequest (ctx context.Context , service * v1.Service , nodes []* v1.Node ) (* linodego.NodeBalancer , error ) {
567+ func (l * loadbalancers ) buildLoadBalancerRequest (ctx context.Context , clusterName string , service * v1.Service , nodes []* v1.Node ) (* linodego.NodeBalancer , error ) {
561568 ports := service .Spec .Ports
562569 configs := make ([]* linodego.NodeBalancerConfigCreateOptions , 0 , len (ports ))
563570
@@ -566,7 +573,7 @@ func (l *loadbalancers) buildLoadBalancerRequest(ctx context.Context, service *v
566573 return nil , fmt .Errorf ("error creating NodeBalancer Config: ports with the UDP protocol are not supported" )
567574 }
568575
569- config , err := l .buildNodeBalancerConfig (service , int (port .Port ))
576+ config , err := l .buildNodeBalancerConfig (ctx , service , int (port .Port ))
570577 if err != nil {
571578 return nil , err
572579 }
@@ -578,7 +585,7 @@ func (l *loadbalancers) buildLoadBalancerRequest(ctx context.Context, service *v
578585
579586 configs = append (configs , & createOpt )
580587 }
581- return l .createNodeBalancer (ctx , service , configs )
588+ return l .createNodeBalancer (ctx , clusterName , service , configs )
582589}
583590
584591func (l * loadbalancers ) buildNodeBalancerNodeCreateOptions (node * v1.Node , nodePort int32 ) linodego.NodeBalancerNodeCreateOptions {
@@ -705,12 +712,12 @@ func getNodeInternalIP(node *v1.Node) string {
705712 return ""
706713}
707714
708- func getTLSCertInfo (kubeClient kubernetes.Interface , namespace string , config portConfig ) (string , string , error ) {
715+ func getTLSCertInfo (ctx context. Context , kubeClient kubernetes.Interface , namespace string , config portConfig ) (string , string , error ) {
709716 if config .TLSSecretName == "" {
710717 return "" , "" , fmt .Errorf ("TLS secret name for port %v is not specified" , config .Port )
711718 }
712719
713- secret , err := kubeClient .CoreV1 ().Secrets (namespace ).Get (config .TLSSecretName , metav1.GetOptions {})
720+ secret , err := kubeClient .CoreV1 ().Secrets (namespace ).Get (ctx , config .TLSSecretName , metav1.GetOptions {})
714721 if err != nil {
715722 return "" , "" , err
716723 }
0 commit comments