Skip to content

Commit

Permalink
🔨 fix: wait lb active before reconcide or delete, add logs api v5
Browse files Browse the repository at this point in the history
  • Loading branch information
anngdinh committed Jun 20, 2024
1 parent 487b3cc commit 7a70936
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 29 deletions.
9 changes: 6 additions & 3 deletions pkg/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,9 @@ func (c *Controller) DeleteLoadbalancer(ing *nwv1.Ingress) error {
}
}

// LB should active before delete
vngcloudutil.WaitForLBActive(c.vLBSC, c.getProjectID(), lbID)

canDeleteAllLB := func(lbID string) bool {
getPool, err := vngcloudutil.ListPoolOfLB(c.vLBSC, c.getProjectID(), lbID)
if err != nil {
Expand Down Expand Up @@ -1086,7 +1089,7 @@ func (c *Controller) ensureLoadBalancerInstance(inspect *Expander) (string, erro
vngcloudutil.WaitForLBActive(c.vLBSC, c.getProjectID(), inspect.serviceConf.LoadBalancerID)
}

lb, err := vngcloudutil.GetLB(c.vLBSC, c.getProjectID(), inspect.serviceConf.LoadBalancerID)
lb, err := vngcloudutil.WaitForLBActive(c.vLBSC, c.getProjectID(), inspect.serviceConf.LoadBalancerID)
if err != nil {
klog.Errorf("error when get lb: %v", err)
return inspect.serviceConf.LoadBalancerID, err
Expand All @@ -1098,12 +1101,12 @@ func (c *Controller) ensureLoadBalancerInstance(inspect *Expander) (string, erro
}
if lb.PackageID != inspect.LbOptions.PackageID {
klog.Info("Resize load-balancer package to: ", inspect.LbOptions.PackageID)
err := vngcloudutil.ResizeLB(c.vLBSC, c.getProjectID(), inspect.serviceConf.LoadBalancerName, inspect.LbOptions.PackageID)
err := vngcloudutil.ResizeLB(c.vLBSC, c.getProjectID(), inspect.serviceConf.LoadBalancerID, inspect.LbOptions.PackageID)
if err != nil {
klog.Errorf("error when resize lb: %v", err)
return
}
vngcloudutil.WaitForLBActive(c.vLBSC, c.getProjectID(), inspect.serviceConf.LoadBalancerName)
vngcloudutil.WaitForLBActive(c.vLBSC, c.getProjectID(), inspect.serviceConf.LoadBalancerID)
}
if lb.Internal != (inspect.LbOptions.Scheme == loadbalancer.CreateOptsSchemeOptInternal) {
klog.Warning("Load balancer scheme not match, must delete and recreate")
Expand Down
8 changes: 4 additions & 4 deletions pkg/utils/vngcloud/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
)

func ListLBBySubnetID(client *client.ServiceClient, projectID string, subnetID string) ([]*objects.LoadBalancer, error) {
// klog.V(5).Infoln("[API] ListLBBySubnetID: ", "subnetID: ", subnetID)
klog.V(5).Infoln("[API] ListLBBySubnetID: ", "subnetID: ", subnetID)
opt := &loadbalancer.ListBySubnetIDOpts{}
opt.ProjectID = projectID
opt.SubnetID = subnetID

resp, err := loadbalancer.ListBySubnetID(client, opt)
// klog.V(5).Infoln("[API] ListLBBySubnetID: ", "resp: ", resp, "err: ", err)
klog.V(5).Infoln("[API] ListLBBySubnetID: ", "resp: ", resp, "err: ", err)
return resp, err
}

Expand All @@ -31,13 +31,13 @@ func ListLB(client *client.ServiceClient, projectID string) ([]*objects.LoadBala
}

func GetLB(client *client.ServiceClient, projectID string, lbID string) (*objects.LoadBalancer, error) {
// klog.V(5).Infoln("[API] GetLB: ", "lbID: ", lbID)
klog.V(5).Infoln("[API] GetLB: ", "lbID: ", lbID)
opt := &loadbalancer.GetOpts{}
opt.ProjectID = projectID
opt.LoadBalancerID = lbID

resp, err := loadbalancer.Get(client, opt)
// klog.V(5).Infoln("[API] GetLB: ", "resp: ", resp, "err: ", err)
klog.V(5).Infoln("[API] GetLB: ", "resp: ", resp, "err: ", err)
if err != nil {
return resp, err.Error
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/vngcloud/loadbalancer_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ func ImportCertificate(client *client.ServiceClient, projectID string, opt *cert
}

func ListCertificate(client *client.ServiceClient, projectID string) ([]*lObjects.Certificate, error) {
// klog.V(5).Infoln("[API] ListCertificate: ")
klog.V(5).Infoln("[API] ListCertificate: ")
opt := &certificates.ListOpts{}
opt.ProjectID = projectID
resp, err := certificates.List(client, opt)
// klog.V(5).Infoln("[API] ListCertificate: ", "resp: ", resp, "err: ", err)
klog.V(5).Infoln("[API] ListCertificate: ", "resp: ", resp, "err: ", err)
return resp, err
}

Expand Down
13 changes: 7 additions & 6 deletions pkg/utils/vngcloud/loadbalancer_policy.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package vngcloud

import (
"time"

"github.com/vngcloud/vngcloud-go-sdk/client"
lObjects "github.com/vngcloud/vngcloud-go-sdk/vngcloud/objects"
"github.com/vngcloud/vngcloud-go-sdk/vngcloud/services/loadbalancer/v2/policy"
"k8s.io/klog/v2"
"time"
)

func CreatePolicy(client *client.ServiceClient, projectID string, lbID, listenerID string, opt *policy.CreateOptsBuilder) (*lObjects.Policy, error) {
Expand All @@ -26,30 +27,30 @@ func CreatePolicy(client *client.ServiceClient, projectID string, lbID, listener
break
}
}
// klog.V(5).Infoln("[API] CreatePolicy: ", "resp: ", resp, "err: ", err)
klog.V(5).Infoln("[API] CreatePolicy: ", "resp: ", resp, "err: ", err)
return resp, err
}

func ListPolicyOfListener(client *client.ServiceClient, projectID string, lbID, listenerID string) ([]*lObjects.Policy, error) {
// klog.V(5).Infoln("[API] ListPolicyOfListener: ", "lbID: ", lbID, "listenerID: ", listenerID)
klog.V(5).Infoln("[API] ListPolicyOfListener: ", "lbID: ", lbID, "listenerID: ", listenerID)
opt := &policy.ListOptsBuilder{}
opt.ProjectID = projectID
opt.LoadBalancerID = lbID
opt.ListenerID = listenerID
resp, err := policy.List(client, opt)
// klog.V(5).Infoln("[API] ListPolicyOfListener: ", "resp: ", resp, "err: ", err)
klog.V(5).Infoln("[API] ListPolicyOfListener: ", "resp: ", resp, "err: ", err)
return resp, err
}

func GetPolicy(client *client.ServiceClient, projectID string, lbID, listenerID, policyID string) (*lObjects.Policy, error) {
// klog.V(5).Infoln("[API] GetPolicy: ", "lbID: ", lbID, "listenerID: ", listenerID, "policyID: ", policyID)
klog.V(5).Infoln("[API] GetPolicy: ", "lbID: ", lbID, "listenerID: ", listenerID, "policyID: ", policyID)
opt := &policy.GetOptsBuilder{}
opt.ProjectID = projectID
opt.LoadBalancerID = lbID
opt.ListenerID = listenerID
opt.PolicyID = policyID
resp, err := policy.Get(client, opt)
// klog.V(5).Infoln("[API] GetPolicy: ", "resp: ", resp, "err: ", err)
klog.V(5).Infoln("[API] GetPolicy: ", "resp: ", resp, "err: ", err)
return resp, err
}

Expand Down
22 changes: 11 additions & 11 deletions pkg/utils/vngcloud/loadbalancer_pool.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package vngcloud

import (
"time"

"github.com/vngcloud/vngcloud-go-sdk/client"
lObjects "github.com/vngcloud/vngcloud-go-sdk/vngcloud/objects"
"github.com/vngcloud/vngcloud-go-sdk/vngcloud/services/loadbalancer/v2/pool"
"k8s.io/klog/v2"
"time"
)

func CreatePool(client *client.ServiceClient, projectID string, lbID string, opt *pool.CreateOpts) (*lObjects.Pool, error) {
Expand All @@ -25,18 +26,18 @@ func CreatePool(client *client.ServiceClient, projectID string, lbID string, opt
break
}
}
// klog.V(5).Infoln("[API] CreatePool: ", "resp: ", resp, "err: ", err)
klog.V(5).Infoln("[API] CreatePool: ", "resp: ", resp, "err: ", err)
return resp, err
}

func ListPoolOfLB(client *client.ServiceClient, projectID string, lbID string) ([]*lObjects.Pool, error) {
// klog.V(5).Infoln("[API] ListPool: ", "lbID: ", lbID)
klog.V(5).Infoln("[API] ListPool: ", "lbID: ", lbID)
opt := &pool.ListPoolsBasedLoadBalancerOpts{}
opt.ProjectID = projectID
opt.LoadBalancerID = lbID

resp, err := pool.ListPoolsBasedLoadBalancer(client, opt)
// klog.V(5).Infoln("[API] ListPool: ", "resp: ", resp, "err: ", err)
klog.V(5).Infoln("[API] ListPool: ", "resp: ", resp, "err: ", err)
return resp, err
}

Expand Down Expand Up @@ -64,31 +65,31 @@ func UpdatePoolMember(client *client.ServiceClient, projectID string, lbID, pool
}
}

// klog.V(5).Infoln("[API] UpdatePoolMember: ", "err: ", err)
klog.V(5).Infoln("[API] UpdatePoolMember: ", "err: ", err)
return err
}

func GetPool(client *client.ServiceClient, projectID string, lbID, poolID string) (*lObjects.Pool, error) {
// klog.V(5).Infoln("[API] GetPool: ", "poolID: ", poolID)
klog.V(5).Infoln("[API] GetPool: ", "poolID: ", poolID)
opt := &pool.GetOpts{}
opt.ProjectID = projectID
opt.LoadBalancerID = lbID
opt.PoolID = poolID

resp, err := pool.GetTotal(client, opt)
// klog.V(5).Infoln("[API] GetPool: ", "resp: ", resp, "err: ", err)
klog.V(5).Infoln("[API] GetPool: ", "resp: ", resp, "err: ", err)
return resp, err
}

func GetMemberPool(client *client.ServiceClient, projectID string, lbID, poolID string) ([]*lObjects.Member, error) {
// klog.V(5).Infoln("[API] GetMemberPool: ", "poolID: ", poolID)
klog.V(5).Infoln("[API] GetMemberPool: ", "poolID: ", poolID)
opt := &pool.GetMemberOpts{}
opt.ProjectID = projectID
opt.LoadBalancerID = lbID
opt.PoolID = poolID

resp, err := pool.GetMember(client, opt)
// klog.V(5).Infoln("[API] GetMemberPool: ", "resp: ", resp, "err: ", err)
klog.V(5).Infoln("[API] GetMemberPool: ", "resp: ", resp, "err: ", err)
return resp, err
}

Expand All @@ -115,8 +116,7 @@ func DeletePool(client *client.ServiceClient, projectID string, lbID, poolID str
}

func UpdatePool(client *client.ServiceClient, projectID string, lbID, poolID string, opt *pool.UpdateOpts) error {
klog.V(5).Infoln("[API] UpdatePool: ", "lbID: ", lbID, "poolID: ", poolID)
klog.V(5).Infoln("[API] UpdatePool: ", "opt: ", opt)
klog.V(5).Infoln("[API] UpdatePool: ", "lbID: ", lbID, "poolID: ", poolID, "opt: ", opt)
opt.ProjectID = projectID
opt.LoadBalancerID = lbID
opt.PoolID = poolID
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/vngcloud/loadbalancer_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func FindListenerByPort(client *client.ServiceClient, projectID string, lbID str
return nil, errors.ErrNotFound
}

func WaitForLBActive(client *client.ServiceClient, projectID string, lbID string) {
func WaitForLBActive(client *client.ServiceClient, projectID string, lbID string) (*lObjects.LoadBalancer, error) {
klog.Infof("Waiting for load balancer %s to be ready", lbID)
var resultLb *lObjects.LoadBalancer

Expand Down Expand Up @@ -109,7 +109,7 @@ func WaitForLBActive(client *client.ServiceClient, projectID string, lbID string
klog.Errorf("timeout waiting for the loadbalancer %s with lb status %s", lbID, resultLb.Status)
}

// return resultLb, err
return resultLb, err
}

func ComparePoolOptions(ipool *lObjects.Pool, poolOptions *pool.CreateOpts) *pool.UpdateOpts {
Expand Down
5 changes: 4 additions & 1 deletion pkg/vngcloud/vlb.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ func (c *vLB) ensureDeleteLoadBalancer(pCtx context.Context, clusterName string,
}
}

// LB should active before delete
vngcloudutil.WaitForLBActive(c.vLBSC, c.getProjectID(), lbID)

canDeleteAllLB := func(lbID string) bool {
getPool, err := vngcloudutil.ListPoolOfLB(c.vLBSC, c.getProjectID(), lbID)
if err != nil {
Expand Down Expand Up @@ -610,7 +613,7 @@ func (c *vLB) ensureLoadBalancerInstance(inspect *Expander) (string, error) {
vngcloudutil.WaitForLBActive(c.vLBSC, c.getProjectID(), inspect.serviceConf.LoadBalancerID)
}

lb, err := vngcloudutil.GetLB(c.vLBSC, c.getProjectID(), inspect.serviceConf.LoadBalancerID)
lb, err := vngcloudutil.WaitForLBActive(c.vLBSC, c.getProjectID(), inspect.serviceConf.LoadBalancerID)
if err != nil {
klog.Errorf("error when get lb: %v", err)
return inspect.serviceConf.LoadBalancerID, err
Expand Down

0 comments on commit 7a70936

Please sign in to comment.