Skip to content

Commit

Permalink
🔨 fix: test annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
anngdinh committed Mar 18, 2024
1 parent 73fe5e0 commit 90ea259
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 55 deletions.
8 changes: 6 additions & 2 deletions pkg/ingress/controller/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (c *API) GetPool(lbID, poolID string) (*lObjects.Pool, error) {
opt.LoadBalancerID = lbID
opt.PoolID = poolID

resp, err := pool.Get(c.VLBSC, opt)
resp, err := pool.GetTotal(c.VLBSC, opt)
// logrus.Infoln("*****API__GetPool: ", "resp: ", resp, "err: ", err)
return resp, err
}
Expand Down Expand Up @@ -478,7 +478,11 @@ func (c *API) FindPoolByName(lbID, name string) (*lObjects.Pool, error) {
}
for _, pool := range pools {
if pool.Name == name {
return pool, nil
ipool, err := c.GetPool(lbID, pool.UUID)
if err != nil {
return nil, err
}
return ipool, nil
}
}
return nil, errors.ErrNotFound
Expand Down
50 changes: 25 additions & 25 deletions pkg/ingress/controller/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,31 +108,6 @@ func CreatePoolOptions(ing *nwv1.Ingress) *pool.CreateOpts {
if ing == nil {
return opt
}
if option, ok := ing.Annotations[ServiceAnnotationPoolAlgorithm]; ok {
switch option {
case string(pool.CreateOptsAlgorithmOptRoundRobin),
string(pool.CreateOptsAlgorithmOptLeastConn),
string(pool.CreateOptsAlgorithmOptSourceIP):
opt.Algorithm = pool.CreateOptsAlgorithmOpt(option)
default:
klog.Warningf("Invalid annotation \"%s\" value, must be one of %s, %s, %s", ServiceAnnotationPoolAlgorithm,
pool.CreateOptsAlgorithmOptRoundRobin,
pool.CreateOptsAlgorithmOptLeastConn,
pool.CreateOptsAlgorithmOptSourceIP)
}
}
if option, ok := ing.Annotations[ServiceAnnotationHealthyThreshold]; ok {
opt.HealthMonitor.HealthyThreshold = ParseIntAnnotation(option, ServiceAnnotationHealthyThreshold, opt.HealthMonitor.HealthyThreshold)
}
if option, ok := ing.Annotations[ServiceAnnotationMonitorUnhealthyThreshold]; ok {
opt.HealthMonitor.UnhealthyThreshold = ParseIntAnnotation(option, ServiceAnnotationMonitorUnhealthyThreshold, opt.HealthMonitor.UnhealthyThreshold)
}
if option, ok := ing.Annotations[ServiceAnnotationMonitorTimeout]; ok {
opt.HealthMonitor.Timeout = ParseIntAnnotation(option, ServiceAnnotationMonitorTimeout, opt.HealthMonitor.Timeout)
}
if option, ok := ing.Annotations[ServiceAnnotationMonitorInterval]; ok {
opt.HealthMonitor.Interval = ParseIntAnnotation(option, ServiceAnnotationMonitorInterval, opt.HealthMonitor.Interval)
}
if option, ok := ing.Annotations[ServiceAnnotationMonitorProtocol]; ok {
switch option {
case string(pool.CreateOptsHealthCheckProtocolOptTCP), string(pool.CreateOptsHealthCheckProtocolOptHTTP):
Expand Down Expand Up @@ -190,6 +165,31 @@ func CreatePoolOptions(ing *nwv1.Ingress) *pool.CreateOpts {
pool.CreateOptsHealthCheckProtocolOptHTTP)
}
}
if option, ok := ing.Annotations[ServiceAnnotationPoolAlgorithm]; ok {
switch option {
case string(pool.CreateOptsAlgorithmOptRoundRobin),
string(pool.CreateOptsAlgorithmOptLeastConn),
string(pool.CreateOptsAlgorithmOptSourceIP):
opt.Algorithm = pool.CreateOptsAlgorithmOpt(option)
default:
klog.Warningf("Invalid annotation \"%s\" value, must be one of %s, %s, %s", ServiceAnnotationPoolAlgorithm,
pool.CreateOptsAlgorithmOptRoundRobin,
pool.CreateOptsAlgorithmOptLeastConn,
pool.CreateOptsAlgorithmOptSourceIP)
}
}
if option, ok := ing.Annotations[ServiceAnnotationHealthyThreshold]; ok {
opt.HealthMonitor.HealthyThreshold = ParseIntAnnotation(option, ServiceAnnotationHealthyThreshold, opt.HealthMonitor.HealthyThreshold)
}
if option, ok := ing.Annotations[ServiceAnnotationMonitorUnhealthyThreshold]; ok {
opt.HealthMonitor.UnhealthyThreshold = ParseIntAnnotation(option, ServiceAnnotationMonitorUnhealthyThreshold, opt.HealthMonitor.UnhealthyThreshold)
}
if option, ok := ing.Annotations[ServiceAnnotationMonitorTimeout]; ok {
opt.HealthMonitor.Timeout = ParseIntAnnotation(option, ServiceAnnotationMonitorTimeout, opt.HealthMonitor.Timeout)
}
if option, ok := ing.Annotations[ServiceAnnotationMonitorInterval]; ok {
opt.HealthMonitor.Interval = ParseIntAnnotation(option, ServiceAnnotationMonitorInterval, opt.HealthMonitor.Interval)
}
if option, ok := ing.Annotations[ServiceAnnotationEnableStickySession]; ok {
switch option {
case "true", "false":
Expand Down
34 changes: 6 additions & 28 deletions pkg/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ func (c *Controller) ensureLoadBalancer(ing *nwv1.Ingress) (string, error) {

ensureAnnotation := func() {
if lbName, ok := ing.Annotations[ServiceAnnotationLoadBalancerName]; ok && lb.Name != lbName {
klog.Warning("Annotation lb name not match with lb name, should use when create lb")
logrus.Warnf("Annotation %s not match, only meaning when create new load-balancer", ServiceAnnotationLoadBalancerName)
}
if packageID, ok := ing.Annotations[ServiceAnnotationPackageID]; ok && lb.PackageID != packageID {
klog.Info("Resize load-balancer package to match annotation: ", packageID)
Expand All @@ -982,6 +982,9 @@ func (c *Controller) ensureLoadBalancer(ing *nwv1.Ingress) (string, error) {
}
c.api.WaitForLBActive(lbID)
}
if option, ok := ing.Annotations[ServiceAnnotationLoadBalancerInternal]; ok && lb.Internal != (option == "true") {
logrus.Warnf("Annotation %s not match, only meaning when create new load-balancer", ServiceAnnotationLoadBalancerInternal)
}
}
ensureAnnotation()
return lbID, nil
Expand Down Expand Up @@ -1375,33 +1378,8 @@ func (c *Controller) ensureListener(lbID, lisName string, listenerOpts listener.
}
}

updateOpts := &listener.UpdateOpts{
AllowedCidrs: lis.AllowedCidrs,
DefaultPoolId: lis.DefaultPoolId,
TimeoutClient: lis.TimeoutClient,
TimeoutConnection: lis.TimeoutConnection,
TimeoutMember: lis.TimeoutMember,
Headers: lis.Headers,
DefaultCertificateAuthority: lis.DefaultCertificateAuthority,
ClientCertificate: lis.ClientCertificateAuthentication,
}
isUpdate := false

if lis.DefaultPoolId != listenerOpts.DefaultPoolId && listenerOpts.DefaultPoolId != "" {
updateOpts.DefaultPoolId = listenerOpts.DefaultPoolId
isUpdate = true
klog.Infof("listener need update default pool id: %s", listenerOpts.DefaultPoolId)
}

if listenerOpts.DefaultCertificateAuthority != nil && (lis.DefaultCertificateAuthority == nil || *(lis.DefaultCertificateAuthority) != *(listenerOpts.DefaultCertificateAuthority)) {
updateOpts.DefaultCertificateAuthority = listenerOpts.DefaultCertificateAuthority
isUpdate = true
klog.Infof("listener need update default certificate authority: %s", *listenerOpts.DefaultCertificateAuthority)
}

// update cert SNI here .......................................................

if isUpdate {
updateOpts := compareListenerOptions(lis, &listenerOpts)
if updateOpts != nil {
err := c.api.UpdateListener(lbID, lis.UUID, updateOpts)
if err != nil {
logrus.Error("error when update listener: ", err)
Expand Down
66 changes: 66 additions & 0 deletions pkg/ingress/controller/controller_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

log "github.com/sirupsen/logrus"
lObjects "github.com/vngcloud/vngcloud-go-sdk/vngcloud/objects"
"github.com/vngcloud/vngcloud-go-sdk/vngcloud/services/loadbalancer/v2/listener"
"github.com/vngcloud/vngcloud-go-sdk/vngcloud/services/loadbalancer/v2/pool"
"github.com/vngcloud/vngcloud-ingress-controller/pkg/ingress/utils/errors"
apiv1 "k8s.io/api/core/v1"
Expand All @@ -29,6 +30,7 @@ import (
"k8s.io/client-go/kubernetes"
corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
)

// IsValid returns true if the given Ingress either doesn't specify
Expand Down Expand Up @@ -165,6 +167,70 @@ func comparePoolOptions(ipool *lObjects.Pool, poolOptions *pool.CreateOpts) *poo
ipool.TLSEncryption != *poolOptions.TLSEncryption {
isNeedUpdate = true
}
if ipool.HealthMonitor.HealthyThreshold != poolOptions.HealthMonitor.HealthyThreshold ||
ipool.HealthMonitor.UnhealthyThreshold != poolOptions.HealthMonitor.UnhealthyThreshold ||
ipool.HealthMonitor.Interval != poolOptions.HealthMonitor.Interval ||
ipool.HealthMonitor.Timeout != poolOptions.HealthMonitor.Timeout {
isNeedUpdate = true
}
if ipool.HealthMonitor.HealthCheckProtocol == "HTTP" && poolOptions.HealthMonitor.HealthCheckProtocol == pool.CreateOptsHealthCheckProtocolOptHTTP {
// domain may return nil
if ipool.HealthMonitor.HealthCheckPath == nil || *ipool.HealthMonitor.HealthCheckPath != *poolOptions.HealthMonitor.HealthCheckPath ||
ipool.HealthMonitor.DomainName == nil || *ipool.HealthMonitor.DomainName != *poolOptions.HealthMonitor.DomainName ||
ipool.HealthMonitor.HttpVersion == nil || *ipool.HealthMonitor.HttpVersion != string(*poolOptions.HealthMonitor.HttpVersion) ||
ipool.HealthMonitor.HealthCheckMethod == nil || *ipool.HealthMonitor.HealthCheckMethod != string(*poolOptions.HealthMonitor.HealthCheckMethod) ||
ipool.HealthMonitor.SuccessCode == nil || *ipool.HealthMonitor.SuccessCode != *poolOptions.HealthMonitor.SuccessCode {
isNeedUpdate = true
}
} else if ipool.HealthMonitor.HealthCheckProtocol == "HTTP" && poolOptions.HealthMonitor.HealthCheckProtocol == pool.CreateOptsHealthCheckProtocolOptTCP {
updateOptions.HealthMonitor.HealthCheckProtocol = pool.CreateOptsHealthCheckProtocolOptHTTP
updateOptions.HealthMonitor.HealthCheckPath = ipool.HealthMonitor.HealthCheckPath
updateOptions.HealthMonitor.DomainName = ipool.HealthMonitor.DomainName
*updateOptions.HealthMonitor.HttpVersion = pool.CreateOptsHealthCheckHttpVersionOpt(*ipool.HealthMonitor.HttpVersion)
*updateOptions.HealthMonitor.HealthCheckMethod = pool.CreateOptsHealthCheckMethodOpt(*ipool.HealthMonitor.HealthCheckMethod)
} else if ipool.HealthMonitor.HealthCheckProtocol == "TCP" && poolOptions.HealthMonitor.HealthCheckProtocol == pool.CreateOptsHealthCheckProtocolOptHTTP {
updateOptions.HealthMonitor.HealthCheckProtocol = pool.CreateOptsHealthCheckProtocolOptTCP
updateOptions.HealthMonitor.HealthCheckPath = nil
updateOptions.HealthMonitor.DomainName = nil
updateOptions.HealthMonitor.HttpVersion = nil
updateOptions.HealthMonitor.HealthCheckMethod = nil
}

if !isNeedUpdate {
return nil
}
return updateOptions
}

func compareListenerOptions(ilis *lObjects.Listener, lisOptions *listener.CreateOpts) *listener.UpdateOpts {
isNeedUpdate := false
updateOptions := &listener.UpdateOpts{
AllowedCidrs: lisOptions.AllowedCidrs,
TimeoutClient: lisOptions.TimeoutClient,
TimeoutMember: lisOptions.TimeoutMember,
TimeoutConnection: lisOptions.TimeoutConnection,
DefaultPoolId: lisOptions.DefaultPoolId,
DefaultCertificateAuthority: lisOptions.DefaultCertificateAuthority,
// Headers: lisOptions.Headers,
// ClientCertificate: lisOptions.ClientCertificateAuthentication,
// ......................................... update later
}
if ilis.AllowedCidrs != lisOptions.AllowedCidrs ||
ilis.TimeoutClient != lisOptions.TimeoutClient ||
ilis.TimeoutMember != lisOptions.TimeoutMember ||
ilis.TimeoutConnection != lisOptions.TimeoutConnection {
isNeedUpdate = true
}

if ilis.DefaultPoolId != lisOptions.DefaultPoolId {
klog.Infof("listener need update default pool id: %s", lisOptions.DefaultPoolId)
isNeedUpdate = true
}
if lisOptions.DefaultCertificateAuthority != nil && (ilis.DefaultCertificateAuthority == nil || *(ilis.DefaultCertificateAuthority) != *(lisOptions.DefaultCertificateAuthority)) {
klog.Infof("listener need update default certificate authority: %s", *lisOptions.DefaultCertificateAuthority)
isNeedUpdate = true
}
// update cert SNI here .......................................................
if !isNeedUpdate {
return nil
}
Expand Down

0 comments on commit 90ea259

Please sign in to comment.