|
| 1 | +package huaweicloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + v1 "k8s.io/api/core/v1" |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + "k8s.io/apimachinery/pkg/runtime" |
| 10 | + corev1 "k8s.io/client-go/kubernetes/typed/core/v1" |
| 11 | + "k8s.io/client-go/tools/cache" |
| 12 | + "sigs.k8s.io/cloud-provider-huaweicloud/pkg/cloudprovider/huaweicloud/wrapper" |
| 13 | + |
| 14 | + "k8s.io/apimachinery/pkg/watch" |
| 15 | + "k8s.io/klog/v2" |
| 16 | +) |
| 17 | + |
| 18 | +type SecurityGroupListener struct { |
| 19 | + kubeClient *corev1.CoreV1Client |
| 20 | + ecsClient *wrapper.EcsClient |
| 21 | + |
| 22 | + securityGroupID string |
| 23 | + |
| 24 | + stopChannel chan struct{} |
| 25 | +} |
| 26 | + |
| 27 | +func (s *SecurityGroupListener) startSecurityGroupListener() { |
| 28 | + if s.securityGroupID == "" { |
| 29 | + klog.Infof(`"security-group-id" is empty, nodes are added or removed will not be associated or disassociated |
| 30 | + any security groups`) |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + klog.Info("starting SecurityGroupListener") |
| 35 | + for { |
| 36 | + securityGroupInformer, err := s.CreateSecurityGroupInformer() |
| 37 | + if err != nil { |
| 38 | + klog.Errorf("failed to watch kubernetes cluster node list, starting SecurityGroupListener failed: %s", err) |
| 39 | + continue |
| 40 | + } |
| 41 | + |
| 42 | + go securityGroupInformer.Run(s.stopChannel) |
| 43 | + break |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func (s *SecurityGroupListener) CreateSecurityGroupInformer() (cache.SharedIndexInformer, error) { |
| 48 | + nodeList, err := s.kubeClient.Nodes().List(context.TODO(), metav1.ListOptions{}) |
| 49 | + if err != nil { |
| 50 | + klog.Errorf("failed to query a list of node, try again later, error: %s", err) |
| 51 | + time.Sleep(5 * time.Second) |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + nodeInformer := cache.NewSharedIndexInformer( |
| 55 | + &cache.ListWatch{ |
| 56 | + ListFunc: func(options metav1.ListOptions) (runtime.Object, error) { |
| 57 | + if options.ResourceVersion == "" || options.ResourceVersion == "0" { |
| 58 | + options.ResourceVersion = nodeList.ResourceVersion |
| 59 | + } |
| 60 | + return s.kubeClient.Nodes().List(context.TODO(), options) |
| 61 | + }, |
| 62 | + WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { |
| 63 | + if options.ResourceVersion == "" || options.ResourceVersion == "0" { |
| 64 | + options.ResourceVersion = nodeList.ResourceVersion |
| 65 | + } |
| 66 | + return s.kubeClient.Nodes().Watch(context.TODO(), options) |
| 67 | + }, |
| 68 | + }, |
| 69 | + &v1.Node{}, |
| 70 | + 0, |
| 71 | + cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, |
| 72 | + ) |
| 73 | + |
| 74 | + _, err = nodeInformer.AddEventHandlerWithResyncPeriod(cache.ResourceEventHandlerFuncs{ |
| 75 | + AddFunc: func(obj interface{}) { |
| 76 | + kubeNode, ok := obj.(*v1.Node) |
| 77 | + if !ok { |
| 78 | + klog.Errorf("detected that a node has been added, but the type conversion failed: %#v", obj) |
| 79 | + return |
| 80 | + } |
| 81 | + klog.Infof("detected that a new node has been added to the cluster(%v): %s", ok, kubeNode.Name) |
| 82 | + |
| 83 | + ecsNode, err := s.ecsClient.GetByNodeName(kubeNode.Name) |
| 84 | + if err != nil { |
| 85 | + klog.Error("Add node: can not get kubernetes node name: %v", err) |
| 86 | + return |
| 87 | + } |
| 88 | + err = s.ecsClient.AssociateSecurityGroup(ecsNode.Id, s.securityGroupID) |
| 89 | + if err != nil { |
| 90 | + klog.Errorf("failed to associate security group %s to ECS %s: %s", s.securityGroupID, ecsNode.Id, err) |
| 91 | + } |
| 92 | + }, |
| 93 | + UpdateFunc: func(oldObj, newObj interface{}) {}, |
| 94 | + DeleteFunc: func(obj interface{}) { |
| 95 | + kubeNode, ok := obj.(*v1.Node) |
| 96 | + if !ok { |
| 97 | + klog.Errorf("detected that a node has been removed, but the type conversion failed: %#v", obj) |
| 98 | + return |
| 99 | + } |
| 100 | + klog.Infof("detected that a node has been removed to the cluster: %s", kubeNode.Name) |
| 101 | + |
| 102 | + ecsNode, err := s.ecsClient.GetByNodeName(kubeNode.Name) |
| 103 | + if err != nil { |
| 104 | + klog.Error("Delete node: can not get kubernetes node name: %v", err) |
| 105 | + return |
| 106 | + } |
| 107 | + err = s.ecsClient.DisassociateSecurityGroup(ecsNode.Id, s.securityGroupID) |
| 108 | + if err != nil { |
| 109 | + klog.Errorf("failed to disassociate security group %s from ECS %s: %s", s.securityGroupID, ecsNode.Id, err) |
| 110 | + } |
| 111 | + }, |
| 112 | + }, 5*time.Second) |
| 113 | + if err != nil { |
| 114 | + klog.Errorf("failed to start nodeEventHandler, try again later, error: %s", err) |
| 115 | + time.Sleep(5 * time.Second) |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + return nodeInformer, nil |
| 119 | +} |
| 120 | + |
| 121 | +func (s *SecurityGroupListener) stopSecurityGroupListener() { |
| 122 | + klog.Warningf("Stop listening to Security Group") |
| 123 | + s.stopChannel <- struct{}{} |
| 124 | +} |
0 commit comments