From 09bfecbddf39f53c174ad49854f104f742a5aaf0 Mon Sep 17 00:00:00 2001 From: andrew Date: Tue, 23 Jul 2024 15:06:29 +0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20change:=20use=20id=20annotation?= =?UTF-8?q?=20to=20manage,=20revert=20fix=20delete=20LB=20when=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/ingress/controller/controller.go | 53 ++++++++-------------------- pkg/vngcloud/vlb.go | 42 +++++++--------------- 2 files changed, 27 insertions(+), 68 deletions(-) diff --git a/pkg/ingress/controller/controller.go b/pkg/ingress/controller/controller.go index dedcd43..db004b1 100644 --- a/pkg/ingress/controller/controller.go +++ b/pkg/ingress/controller/controller.go @@ -486,12 +486,6 @@ func (c *Controller) ensureIngress(oldIng, ing *nwv1.Ingress) error { } } - ingressKey := fmt.Sprintf("%s/%s", ing.Namespace, ing.Name) - ing, err := c.updateIngressAnnotation(ingressKey) - if err != nil { - return err - } - lb, err := c.ensureCompareIngress(oldIng, ing) if err != nil { c.isReApplyNextTime = true @@ -505,29 +499,6 @@ func (c *Controller) ensureIngress(oldIng, ing *nwv1.Ingress) error { return err } -// when create new ingress, you should update the load balancer name annotation immediately, -// avoid the case user update this annotation before load balancer is created -// then webhook will not allow to update this annotation (just allow when this annotation is nil) -func (c *Controller) updateIngressAnnotation(ingressKey string) (*nwv1.Ingress, error) { - latestIngress, err := utils.GetIngress(c.ingressLister, ingressKey) - if err != nil { - logrus.Errorf("Failed to get the latest version of ingress %s", ingressKey) - return nil, vErrors.ErrIngressNotFound - } - if latestIngress.ObjectMeta.Annotations == nil { - latestIngress.ObjectMeta.Annotations = map[string]string{} - } - if _, ok := latestIngress.ObjectMeta.Annotations[ServiceAnnotationLoadBalancerName]; !ok { - latestIngress.ObjectMeta.Annotations[ServiceAnnotationLoadBalancerName] = utils.GenerateLBName(c.getClusterID(), latestIngress.Namespace, latestIngress.Name, consts.RESOURCE_TYPE_INGRESS) - newObj, err := c.kubeClient.NetworkingV1().Ingresses(latestIngress.Namespace).Update(context.TODO(), latestIngress, apimetav1.UpdateOptions{}) - if err != nil { - return nil, err - } - return newObj, nil - } - return latestIngress, nil -} - func (c *Controller) updateIngressStatus(ing *nwv1.Ingress, lb *lObjects.LoadBalancer) (*nwv1.Ingress, error) { // get the latest version of ingress before update ingressKey := fmt.Sprintf("%s/%s", ing.Namespace, ing.Name) @@ -537,6 +508,12 @@ func (c *Controller) updateIngressStatus(ing *nwv1.Ingress, lb *lObjects.LoadBal return nil, vErrors.ErrIngressNotFound } + if latestIngress.ObjectMeta.Annotations == nil { + latestIngress.ObjectMeta.Annotations = map[string]string{} + } + latestIngress.ObjectMeta.Annotations[ServiceAnnotationLoadBalancerID] = lb.UUID + delete(latestIngress.ObjectMeta.Annotations, ServiceAnnotationLoadBalancerName) + newIng := latestIngress.DeepCopy() newState := new(nwv1.IngressLoadBalancerStatus) newState.Ingress = []nwv1.IngressLoadBalancerIngress{{IP: lb.Address}} @@ -1113,16 +1090,16 @@ func (c *Controller) ensureLoadBalancerInstance(inspect *Expander) (string, erro klog.Errorln("error when ensure tags", err) } inspect.serviceConf.LoadBalancerID = lb.UUID - lb, err = vngcloudutil.WaitForLBActive(c.vLBSC, c.getProjectID(), inspect.serviceConf.LoadBalancerID) + _, err = vngcloudutil.WaitForLBActive(c.vLBSC, c.getProjectID(), inspect.serviceConf.LoadBalancerID) if err != nil { - if err == vErrors.ErrLoadBalancerStatusError { - klog.Infof("Load balancer %s is error, delete and create later", lb.UUID) - if errr := vngcloudutil.DeleteLB(c.vLBSC, c.getProjectID(), lb.UUID); errr != nil { - klog.Errorln("error when delete lb", err) - return "", errr - } - return "", err - } + // if err == vErrors.ErrLoadBalancerStatusError { + // klog.Infof("Load balancer %s is error, delete and create later", lb.UUID) + // if errr := vngcloudutil.DeleteLB(c.vLBSC, c.getProjectID(), lb.UUID); errr != nil { + // klog.Errorln("error when delete lb", err) + // return "", errr + // } + // return "", err + // } klog.Errorf("error when get lb: %v", err) return inspect.serviceConf.LoadBalancerID, err } diff --git a/pkg/vngcloud/vlb.go b/pkg/vngcloud/vlb.go index e3ae609..0566937 100644 --- a/pkg/vngcloud/vlb.go +++ b/pkg/vngcloud/vlb.go @@ -21,7 +21,6 @@ import ( "github.com/vngcloud/vngcloud-go-sdk/vngcloud/services/loadbalancer/v2/loadbalancer" "github.com/vngcloud/vngcloud-go-sdk/vngcloud/services/loadbalancer/v2/pool" lCoreV1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/informers" @@ -206,10 +205,6 @@ func (c *vLB) ensureLoadBalancer( }() serviceKey := fmt.Sprintf("%s/%s", pService.Namespace, pService.Name) - pService, err := c.updateServiceAnnotation(pService) - if err != nil { - return nil, err - } oldIngExpander, _ := c.inspectService(nil, pNodes) if oldService, ok := c.serviceCache[serviceKey]; ok { oldIngExpander, _ = c.inspectService(oldService, pNodes) @@ -249,26 +244,13 @@ func (c *vLB) ensureLoadBalancer( return lbStatus, nil } -// when create new ingress, you should update the load balancer name annotation immediately, -// avoid the case user update this annotation before load balancer is created -// then webhook will not allow to update this annotation (just allow when this annotation is nil) -// func (c *vLB) updateServiceAnnotation(serviceKey string) (*nwv1.Ingress, error) -func (c *vLB) updateServiceAnnotation(pService *lCoreV1.Service) (*lCoreV1.Service, error) { +func (c *vLB) createLoadBalancerStatus(pService *lCoreV1.Service, lb *lObjects.LoadBalancer) *lCoreV1.LoadBalancerStatus { if pService.ObjectMeta.Annotations == nil { pService.ObjectMeta.Annotations = map[string]string{} } - if _, ok := pService.ObjectMeta.Annotations[ServiceAnnotationLoadBalancerName]; !ok { - pService.ObjectMeta.Annotations[ServiceAnnotationLoadBalancerName] = utils.GenerateLBName(c.getClusterID(), pService.Namespace, pService.Name, consts.RESOURCE_TYPE_SERVICE) - newObj, err := c.kubeClient.CoreV1().Services(pService.Namespace).Update(context.Background(), pService, metav1.UpdateOptions{}) - if err != nil { - return nil, err - } - return newObj, nil - } - return pService, nil -} + pService.ObjectMeta.Annotations[ServiceAnnotationLoadBalancerID] = lb.UUID + delete(pService.ObjectMeta.Annotations, ServiceAnnotationLoadBalancerName) -func (c *vLB) createLoadBalancerStatus(pService *lCoreV1.Service, lb *lObjects.LoadBalancer) *lCoreV1.LoadBalancerStatus { status := &lCoreV1.LoadBalancerStatus{} // Default to IP status.Ingress = []lCoreV1.LoadBalancerIngress{{IP: lb.Address}} @@ -622,16 +604,16 @@ func (c *vLB) ensureLoadBalancerInstance(inspect *Expander) (string, error) { klog.Errorln("error when ensure tags", err) } inspect.serviceConf.LoadBalancerID = lb.UUID - lb, err = vngcloudutil.WaitForLBActive(c.vLBSC, c.getProjectID(), inspect.serviceConf.LoadBalancerID) + _, err = vngcloudutil.WaitForLBActive(c.vLBSC, c.getProjectID(), inspect.serviceConf.LoadBalancerID) if err != nil { - if err == vErrors.ErrLoadBalancerStatusError { - klog.Infof("Load balancer %s is error, delete and create later", lb.UUID) - if errr := vngcloudutil.DeleteLB(c.vLBSC, c.getProjectID(), lb.UUID); errr != nil { - klog.Errorln("error when delete lb", err) - return "", errr - } - return "", err - } + // if err == vErrors.ErrLoadBalancerStatusError { + // klog.Infof("Load balancer %s is error, delete and create later", lb.UUID) + // if errr := vngcloudutil.DeleteLB(c.vLBSC, c.getProjectID(), lb.UUID); errr != nil { + // klog.Errorln("error when delete lb", err) + // return "", errr + // } + // return "", err + // } klog.Errorf("error when get lb: %v", err) return inspect.serviceConf.LoadBalancerID, err }