Skip to content

Commit

Permalink
🚀 change: delete LB if state error
Browse files Browse the repository at this point in the history
  • Loading branch information
anngdinh committed Jul 10, 2024
1 parent 593dbbf commit ecb9487
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
8 changes: 8 additions & 0 deletions pkg/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,14 @@ func (c *Controller) ensureCompareIngress(oldIng, ing *nwv1.Ingress) (*lObjects.
}
lbID, err = c.ensureLoadBalancerInstance(newIngExpander)
if err != nil {
if err == vErrors.ErrLoadBalancerStatusError {
klog.Infof("Load balancer %s is error, delete and create later", lbID)
if errr := vngcloudutil.DeleteLB(c.vLBSC, c.getProjectID(), lbID); errr != nil {
klog.Errorln("error when delete lb", err)
return nil, errr
}
return nil, err
}
klog.Errorln("error when ensure loadbalancer", err)
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/utils/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ var (
ErrNetworkIDNotFound = vErr.New("failed to find network ID")
ErrNoCertificateFound = vErr.New("no certificate found, should use annotation to specify certificate")
ErrListenerProtocolNotMatch = vErr.New("listener protocol does not match")
ErrLoadBalancerStatusError = vErr.New("load balancer status error")
)

var ErrLoadBalancerIDNotFoundAnnotation = vErr.New("failed to find LoadBalancerID from Annotation")
Expand Down
3 changes: 1 addition & 2 deletions pkg/utils/vngcloud/loadbalancer_utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package vngcloud

import (
"fmt"
"strings"

"github.com/vngcloud/cloud-provider-vngcloud/pkg/consts"
Expand Down Expand Up @@ -97,7 +96,7 @@ func WaitForLBActive(client *client.ServiceClient, projectID string, lbID string
if strings.ToUpper(lb.Status) == consts.ERROR_LOADBALANCER_STATUS {
klog.Errorf("Load balancer %s is error", lbID)
resultLb = lb
return true, fmt.Errorf("load balancer %s is error", lbID)
return true, errors.ErrLoadBalancerStatusError
}

klog.Infof("Load balancer %s is not ready yet, waiting...", lbID)
Expand Down
14 changes: 11 additions & 3 deletions pkg/vngcloud/vlb.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (c *vLB) EnsureLoadBalancerDeleted(pCtx context.Context, clusterName string
// ************************************************** PRIVATE METHODS **************************************************

func (c *vLB) ensureLoadBalancer(
pCtx context.Context, clusterName string, pService *lCoreV1.Service, pNodes []*lCoreV1.Node) ( // params
pCtx context.Context, _ string, pService *lCoreV1.Service, pNodes []*lCoreV1.Node) ( // params
rLb *lCoreV1.LoadBalancerStatus, rErr error) { // returns

if option, ok := pService.Annotations[ServiceAnnotationIgnore]; ok {
Expand Down Expand Up @@ -221,6 +221,14 @@ func (c *vLB) ensureLoadBalancer(
}
lbID, err = c.ensureLoadBalancerInstance(newIngExpander)
if err != nil {
if err == vErrors.ErrLoadBalancerStatusError {
klog.Infof("Load balancer %s is error, delete and create later", lbID)
if errr := vngcloudutil.DeleteLB(c.vLBSC, c.getProjectID(), lbID); errr != nil {
klog.Errorln("error when delete lb", err)
return nil, errr
}
return nil, err
}
klog.Errorln("error when ensure loadbalancer", err)
return nil, err
}
Expand Down Expand Up @@ -261,7 +269,7 @@ func (c *vLB) getProjectID() string {
return c.extraInfo.ProjectID
}

func (c *vLB) ensureDeleteLoadBalancer(pCtx context.Context, clusterName string, pService *lCoreV1.Service) error {
func (c *vLB) ensureDeleteLoadBalancer(_ context.Context, _ string, pService *lCoreV1.Service) error {
if option, ok := pService.Annotations[ServiceAnnotationIgnore]; ok {
if isIgnore := utils.ParseBoolAnnotation(option, ServiceAnnotationIgnore, false); isIgnore {
klog.Infof("Ignore ensure for service %s/%s", pService.Namespace, pService.Name)
Expand Down Expand Up @@ -367,7 +375,7 @@ func (c *vLB) ensureDeleteLoadBalancer(pCtx context.Context, clusterName string,
return nil
}

func (c *vLB) ensureGetLoadBalancer(pCtx context.Context, clusterName string, pService *lCoreV1.Service) (*lCoreV1.LoadBalancerStatus, bool, error) {
func (c *vLB) ensureGetLoadBalancer(_ context.Context, _ string, pService *lCoreV1.Service) (*lCoreV1.LoadBalancerStatus, bool, error) {
lbID, _ := c.GetLoadbalancerIDByService(pService)
if lbID == "" {
klog.Infof("Load balancer is not existed")
Expand Down

0 comments on commit ecb9487

Please sign in to comment.