@@ -22,9 +22,9 @@ import (
2222 "io"
2323 "os"
2424 "strings"
25- "sync"
2625 "time"
2726
27+ ecsmodel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ecs/v2/model"
2828 gocache "github.com/patrickmn/go-cache"
2929 "google.golang.org/grpc/codes"
3030 "google.golang.org/grpc/status"
@@ -46,11 +46,6 @@ import (
4646 "k8s.io/cloud-provider/options"
4747 servicehelper "k8s.io/cloud-provider/service/helpers"
4848 "k8s.io/klog/v2"
49- "k8s.io/utils/pointer"
50-
51- ecsmodel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ecs/v2/model"
52- vpcmodel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/vpc/v2/model"
53-
5449 "sigs.k8s.io/cloud-provider-huaweicloud/pkg/cloudprovider/huaweicloud/wrapper"
5550 "sigs.k8s.io/cloud-provider-huaweicloud/pkg/common"
5651 "sigs.k8s.io/cloud-provider-huaweicloud/pkg/config"
@@ -108,17 +103,15 @@ const (
108103 ProtocolHTTPS = "HTTPS"
109104 ProtocolTerminatedHTTPS = "TERMINATED_HTTPS"
110105
111- healthCheckCidr = "100.125.0.0/16"
112-
113106 endpointAdded = "endpointAdded"
114107 endpointUpdate = "endpointUpdate"
108+
109+ kubeSystemNamespace = "kube-system"
115110)
116111
117112type ELBProtocol string
118113type ELBAlgorithm string
119114
120- var healthCheckCidrOptLock = & sync.Mutex {}
121-
122115type Basic struct {
123116 cloudControllerManagerOpts * options.CloudControllerManagerOptions
124117 cloudConfig * config.CloudConfig
@@ -217,103 +210,6 @@ func (b Basic) getNodeSubnetID(node *v1.Node) (string, error) {
217210 return "" , fmt .Errorf ("failed to get node subnet ID" )
218211}
219212
220- func (b Basic ) allowHealthCheckRule (node * v1.Node ) error {
221- if b .loadbalancerOpts .DisableCreateSecurityGroup {
222- klog .Infof ("automatic creation of security groups has been disabled" )
223- return nil
224- }
225- // Avoid adding security group rules in parallel.
226- healthCheckCidrOptLock .Lock ()
227- defer func () {
228- healthCheckCidrOptLock .Unlock ()
229- }()
230-
231- instance , err := b .ecsClient .GetByNodeName (node .Name )
232- if err != nil {
233- return err
234- }
235-
236- secGroups , err := b .ecsClient .ListSecurityGroups (instance .Id )
237- if err != nil {
238- return err
239- }
240- if len (secGroups ) == 0 {
241- klog .Warningf ("not found any security groups on %s" , node .Name )
242- return nil
243- }
244-
245- for _ , sg := range secGroups {
246- rules , err := b .vpcClient .ListSecurityGroupRules (sg .Id )
247- if err != nil {
248- return fmt .Errorf ("failed to list security group[%s] rules: %s" , sg .Id , err )
249- }
250-
251- for _ , r := range rules {
252- if r .Direction == "ingress" && r .RemoteIpPrefix == healthCheckCidr &&
253- r .Ethertype == "IPv4" && r .PortRangeMin == 0 && r .PortRangeMax == 0 {
254- klog .Infof ("the health check IP is already in the security group, no need to add rules" )
255- return nil
256- }
257- }
258- }
259-
260- desc := fmt .Sprintf ("DO NOT EDIT. %s are internal IP addresses used by ELB to check the health of backend" +
261- " servers. Created by K8s CCM." , healthCheckCidr )
262-
263- securityGroupID := secGroups [0 ].Id
264- _ , err = b .vpcClient .CreateSecurityGroupRule (& vpcmodel.CreateSecurityGroupRuleOption {
265- SecurityGroupId : securityGroupID ,
266- Description : & desc ,
267- Direction : "ingress" ,
268- Ethertype : pointer .String ("IPv4" ),
269- RemoteIpPrefix : pointer .String (healthCheckCidr ),
270- })
271-
272- if err != nil {
273- return fmt .Errorf ("failed to create security group[%s] rules: %s" , securityGroupID , err )
274- }
275-
276- return err
277- }
278-
279- func (b Basic ) removeHealthCheckRules (node * v1.Node ) error {
280- instance , err := b .ecsClient .GetByNodeName (node .Name )
281- if err != nil {
282- return err
283- }
284-
285- secGroups , err := b .ecsClient .ListSecurityGroups (instance .Id )
286- if err != nil {
287- return err
288- }
289- if len (secGroups ) == 0 {
290- klog .Warningf ("not found any security groups on %s" , node .Name )
291- return nil
292- }
293-
294- desc := fmt .Sprintf ("DO NOT EDIT. %s are internal IP addresses used by ELB to check the health of backend" +
295- " servers. Created by K8s CCM." , healthCheckCidr )
296-
297- for _ , sg := range secGroups {
298- rules , err := b .vpcClient .ListSecurityGroupRules (sg .Id )
299- if err != nil {
300- return fmt .Errorf ("failed to list security group[%s] rules: %s" , sg .Id , err )
301- }
302-
303- for _ , r := range rules {
304- if r .Direction == "ingress" && r .RemoteIpPrefix == healthCheckCidr &&
305- r .Ethertype == "IPv4" && r .PortRangeMin == 0 && r .PortRangeMax == 0 && r .Description == desc {
306- err := b .vpcClient .DeleteSecurityGroupRule (r .Id )
307- if err != nil {
308- klog .Errorf ("failed to delete security group[%s] rule[%s]" , sg .Id , r .Id )
309- }
310- }
311- }
312- }
313-
314- return nil
315- }
316-
317213func (b Basic ) updateService (service * v1.Service , lbStatus * v1.LoadBalancerStatus ) {
318214 if service .Spec .LoadBalancerClass == nil || * service .Spec .LoadBalancerClass != LoadBalancerClass {
319215 return
@@ -702,7 +598,6 @@ type LoadBalancerServiceListener struct {
702598func (e * LoadBalancerServiceListener ) stopListenerSlice () {
703599 klog .Warningf ("Stop listening to Endpoints" )
704600 e .stopChannel <- struct {}{}
705- close (e .stopChannel )
706601}
707602
708603func (e * LoadBalancerServiceListener ) startEndpointListener (handle func (* v1.Service , bool )) {
@@ -861,73 +756,6 @@ func (e *LoadBalancerServiceListener) dispatcher(namespace, name, eType string,
861756 handle (svc , false )
862757}
863758
864- func (e * LoadBalancerServiceListener ) autoRemoveHealthCheckRule (handle func (node * v1.Node ) error ) {
865- informer := cache .NewSharedIndexInformer (
866- & cache.ListWatch {
867- ListFunc : func (options metav1.ListOptions ) (runtime.Object , error ) {
868- return e .kubeClient .Services (metav1 .NamespaceAll ).List (context .Background (), options )
869- },
870- WatchFunc : func (options metav1.ListOptions ) (watch.Interface , error ) {
871- return e .kubeClient .Services (metav1 .NamespaceAll ).Watch (context .Background (), options )
872- },
873- },
874- & v1.Service {},
875- 0 ,
876- cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc },
877- )
878-
879- _ , err := informer .AddEventHandlerWithResyncPeriod (cache.ResourceEventHandlerFuncs {
880- AddFunc : func (obj interface {}) {
881- service := obj .(* v1.Service )
882- if service .Spec .Type != v1 .ServiceTypeLoadBalancer {
883- return
884- }
885-
886- key := fmt .Sprintf ("%s/%s" , service .Namespace , service .Name )
887- e .serviceCache [key ] = service
888- klog .V (6 ).Infof ("new LoadBalancer service %s/%s added, cache size: %v" ,
889- service .Namespace , service .Name , len (e .serviceCache ))
890- },
891- UpdateFunc : func (oldObj , newObj interface {}) {},
892- DeleteFunc : func (obj interface {}) {
893- service := obj .(* v1.Service )
894- if service .Spec .Type != v1 .ServiceTypeLoadBalancer {
895- return
896- }
897- key := fmt .Sprintf ("%s/%s" , service .Namespace , service .Name )
898- delete (e .serviceCache , key )
899- klog .V (6 ).Infof ("found LoadBalancer service %s/%s deleted, cache size: %v" ,
900- service .Namespace , service .Name , len (e .serviceCache ))
901-
902- if len (e .serviceCache ) > 0 {
903- klog .V (6 ).Infof ("found %v LoadBalancer service(s), " +
904- "skip clearing the security group rules for ELB health check" , len (e .serviceCache ))
905- return
906- }
907-
908- nodes , err := e .kubeClient .Nodes ().List (context .TODO (), metav1.ListOptions {
909- Limit : 1 ,
910- })
911- if err != nil {
912- klog .Errorf ("failed to query a list of nodes in autoRemoveHealthCheckRule: %s" , err )
913- }
914-
915- if len (nodes .Items ) <= 0 {
916- klog .Warningf ("not found any nodes, skip clearing the security group rules for ELB health check" )
917- return
918- }
919- klog .Infof ("all LoadBalancer services has been deleted, start to clean health check rules" )
920- n := nodes .Items [0 ]
921- handle (& n ) //nolint:errcheck
922- },
923- }, 5 * time .Second )
924- if err != nil {
925- klog .Errorf ("failed to watch service: %s" , err )
926- }
927-
928- informer .Run (e .stopChannel )
929- }
930-
931759func (h * CloudProvider ) listenerDeploy () error {
932760 listener := LoadBalancerServiceListener {
933761 Basic : h .Basic ,
@@ -946,12 +774,6 @@ func (h *CloudProvider) listenerDeploy() error {
946774 }
947775
948776 go leaderElection (id , h .restConfig , h .eventRecorder , func (ctx context.Context ) {
949- if ! h .loadbalancerOpts .DisableCreateSecurityGroup {
950- go listener .autoRemoveHealthCheckRule (h .removeHealthCheckRules )
951- } else {
952- klog .Infof ("automatic creation of security groups has been disabled" )
953- }
954-
955777 listener .startEndpointListener (func (service * v1.Service , isDelete bool ) {
956778 klog .Infof ("Got service %s/%s using loadbalancer class %s" ,
957779 service .Namespace , service .Name , utils .ToString (service .Spec .LoadBalancerClass ))
@@ -1008,9 +830,10 @@ func (h *CloudProvider) listenerDeploy() error {
1008830 service .Namespace , service .Name , err )
1009831 })
1010832 }, func () {
1011- listener .stopListenerSlice ()
1012833 listener .goroutinePool .Stop ()
834+ listener .stopListenerSlice ()
1013835 })
836+
1014837 return nil
1015838}
1016839
@@ -1021,7 +844,7 @@ func leaderElection(id string, restConfig *rest.Config, recorder record.EventRec
1021844 retryPeriod := 30 * time .Second
1022845
1023846 configmapLock , err := resourcelock .NewFromKubeconfig (resourcelock .ConfigMapsLeasesResourceLock ,
1024- "kube-system" ,
847+ kubeSystemNamespace ,
1025848 leaseName ,
1026849 resourcelock.ResourceLockConfig {
1027850 Identity : fmt .Sprintf ("%s_%s" , id , string (uuid .NewUUID ())),
@@ -1047,6 +870,13 @@ func leaderElection(id string, restConfig *rest.Config, recorder record.EventRec
1047870 klog .Infof ("[Listener EndpointSlices] leader election lost: %s" , id )
1048871 onStop ()
1049872 },
873+ OnNewLeader : func (identity string ) {
874+ klog .Infof ("[Listener EndpointSlices] leader chenged to %s" , identity )
875+ if strings .Contains (identity , id ) {
876+ return
877+ }
878+ onStop ()
879+ },
1050880 },
1051881 Name : leaseName ,
1052882 })
0 commit comments