Skip to content

Commit

Permalink
🚀 change: use id annotation to manage, revert fix delete LB when error
Browse files Browse the repository at this point in the history
  • Loading branch information
anngdinh committed Jul 23, 2024
1 parent eca7a4d commit 09bfecb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 68 deletions.
53 changes: 15 additions & 38 deletions pkg/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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}}
Expand Down Expand Up @@ -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
}
Expand Down
42 changes: 12 additions & 30 deletions pkg/vngcloud/vlb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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}}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 09bfecb

Please sign in to comment.