Skip to content

Commit

Permalink
🌱 add annotation support enable proxy protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
anngdinh committed Jun 10, 2024
1 parent 40d9416 commit 08cdd1d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 25 deletions.
23 changes: 4 additions & 19 deletions pkg/utils/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"strings"
"unicode"

apiv1 "k8s.io/api/core/v1"

"github.com/vngcloud/cloud-provider-vngcloud/pkg/consts"
"k8s.io/klog/v2"
)
Expand All @@ -25,15 +23,15 @@ func GenerateLBName(clusterID, namespace, resourceName, resourceType string) str
TrimString(namespace, 10),
TrimString(resourceName, 10),
hash)
return validateName(name)
return ValidateName(name)
}

func GeneratePolicyName(clusterID, namespace, resourceName, resourceType string, mode bool, ruleIndex, pathIndex int) string {
prefix := GenerateHashName(clusterID, namespace, resourceName, resourceType)
name := fmt.Sprintf("%s_%s_%t_r%d_p%d",
consts.DEFAULT_LB_PREFIX_NAME,
prefix, mode, ruleIndex, pathIndex)
return validateName(name)
return ValidateName(name)
}

func GeneratePoolName(clusterID, namespace, resourceName, resourceType, serviceName string, port int) string {
Expand All @@ -43,10 +41,10 @@ func GeneratePoolName(clusterID, namespace, resourceName, resourceType, serviceN
prefix,
TrimString(strings.ReplaceAll(serviceName, "/", "-"), 35),
port)
return validateName(name)
return ValidateName(name)
}

func validateName(newName string) string {
func ValidateName(newName string) string {
for _, char := range newName {
if !unicode.IsLetter(char) && !unicode.IsDigit(char) && char != '-' && char != '.' {
newName = strings.ReplaceAll(newName, string(char), "-")
Expand All @@ -57,16 +55,3 @@ func validateName(newName string) string {
}
return TrimString(newName, consts.DEFAULT_PORTAL_NAME_LENGTH)
}

func GenListenerAndPoolName(clusterName string, pService *apiv1.Service, resourceType string, pPort apiv1.ServicePort) string {
hash := GenerateHashName(clusterName, pService.Namespace, pService.Name, resourceType)
name := fmt.Sprintf("%s_%s_%s_%s_%s_%s_%d",
consts.DEFAULT_LB_PREFIX_NAME,
TrimString(clusterName, 10),
TrimString(pService.Namespace, 10),
TrimString(pService.Name, 10),
hash,
pPort.Protocol,
pPort.Port)
return validateName(name)
}
2 changes: 1 addition & 1 deletion pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func ParsePoolAlgorithm(pOpt string) lPoolV2.CreateOptsAlgorithmOpt {
return lPoolV2.CreateOptsAlgorithmOptRoundRobin
}

func ParsePoolProtocol(pPoolProtocol apiv1.Protocol) lPoolV2.CreateOptsProtocolOpt {
func ParsePoolProtocol(pPoolProtocol string) lPoolV2.CreateOptsProtocolOpt {
opt := lStr.TrimSpace(lStr.ToUpper(string(pPoolProtocol)))
switch opt {
case string(lPoolV2.CreateOptsProtocolOptProxy):
Expand Down
55 changes: 52 additions & 3 deletions pkg/vngcloud/annotation.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package vngcloud

import (
"fmt"
"github.com/vngcloud/cloud-provider-vngcloud/pkg/consts"
"github.com/vngcloud/cloud-provider-vngcloud/pkg/utils"
"github.com/vngcloud/vngcloud-go-sdk/vngcloud/services/loadbalancer/v2/listener"
"github.com/vngcloud/vngcloud-go-sdk/vngcloud/services/loadbalancer/v2/loadbalancer"
"github.com/vngcloud/vngcloud-go-sdk/vngcloud/services/loadbalancer/v2/pool"
apiv1 "k8s.io/api/core/v1"
lCoreV1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
)

Expand Down Expand Up @@ -44,6 +44,7 @@ const (

// // Pool annotations
ServiceAnnotationPoolAlgorithm = DEFAULT_K8S_SERVICE_ANNOTATION_PREFIX + "/pool-algorithm" // both annotation and cloud-config
ServiceAnnotationProxyProtocol = DEFAULT_K8S_SERVICE_ANNOTATION_PREFIX + "/enable-proxy-protocol"
ServiceAnnotationHealthcheckPort = DEFAULT_K8S_SERVICE_ANNOTATION_PREFIX + "/healthcheck-port"
// ServiceAnnotationEnableStickySession = DEFAULT_K8S_SERVICE_ANNOTATION_PREFIX + "/enable-sticky-session"
// ServiceAnnotationEnableTLSEncryption = DEFAULT_K8S_SERVICE_ANNOTATION_PREFIX + "/enable-tls-encryption"
Expand Down Expand Up @@ -93,9 +94,10 @@ type ServiceConfig struct {
TargetNodeLabels map[string]string
IsAutoCreateSecurityGroup bool
SecurityGroups []string
EnableProxyProtocol []string
}

func NewServiceConfig(pService *lCoreV1.Service) *ServiceConfig {
func NewServiceConfig(pService *apiv1.Service) *ServiceConfig {
opt := &ServiceConfig{
LoadBalancerID: "",
LoadBalancerName: "",
Expand All @@ -122,6 +124,7 @@ func NewServiceConfig(pService *lCoreV1.Service) *ServiceConfig {
TargetNodeLabels: map[string]string{},
IsAutoCreateSecurityGroup: false,
SecurityGroups: []string{},
EnableProxyProtocol: []string{},
}
if pService == nil {
return opt
Expand Down Expand Up @@ -248,6 +251,9 @@ func NewServiceConfig(pService *lCoreV1.Service) *ServiceConfig {
if port, ok := pService.Annotations[ServiceAnnotationHealthcheckPort]; ok {
opt.HealthcheckPort = utils.ParseIntAnnotation(port, ServiceAnnotationHealthcheckPort, opt.HealthcheckPort)
}
if proxy, ok := pService.Annotations[ServiceAnnotationProxyProtocol]; ok {
opt.EnableProxyProtocol = utils.ParseStringListAnnotation(proxy, ServiceAnnotationProxyProtocol)
}
return opt
}

Expand Down Expand Up @@ -304,12 +310,55 @@ func (s *ServiceConfig) CreatePoolOptions(pPort apiv1.ServicePort) *pool.CreateO
}
opt := &pool.CreateOpts{
PoolName: "",
PoolProtocol: utils.ParsePoolProtocol(pPort.Protocol),
PoolProtocol: utils.ParsePoolProtocol(s.MappingProtocol(pPort)),
Stickiness: nil,
TLSEncryption: nil,
HealthMonitor: healthMonitor,
Algorithm: s.PoolAlgorithm,
Members: []*pool.Member{},
}
for _, name := range s.EnableProxyProtocol {
if name == pPort.Name && pPort.Protocol == apiv1.ProtocolTCP {
opt.PoolProtocol = pool.CreateOptsProtocolOptProxy
break
}
}
return opt
}

func (s *ServiceConfig) MappingProtocol(pPort apiv1.ServicePort) string {
for _, name := range s.EnableProxyProtocol {
if name == pPort.Name && pPort.Protocol == apiv1.ProtocolTCP {
return string(pool.CreateOptsProtocolOptProxy)
}
}
return string(pPort.Protocol)
}

func (s *ServiceConfig) GenListenerName(clusterName string, pService *apiv1.Service, resourceType string, pPort apiv1.ServicePort) string {
hash := utils.GenerateHashName(clusterName, pService.Namespace, pService.Name, resourceType)
name := fmt.Sprintf("%s_%s_%s_%s_%s_%s_%d",
consts.DEFAULT_LB_PREFIX_NAME,
utils.TrimString(clusterName, 10),
utils.TrimString(pService.Namespace, 10),
utils.TrimString(pService.Name, 10),
hash,
pPort.Protocol,
pPort.Port)
return utils.ValidateName(name)
}

func (s *ServiceConfig) GenPoolName(clusterName string, pService *apiv1.Service, resourceType string, pPort apiv1.ServicePort) string {
realProtocol := s.MappingProtocol(pPort)

hash := utils.GenerateHashName(clusterName, pService.Namespace, pService.Name, resourceType)
name := fmt.Sprintf("%s_%s_%s_%s_%s_%s_%d",
consts.DEFAULT_LB_PREFIX_NAME,
utils.TrimString(clusterName, 10),
utils.TrimString(pService.Namespace, 10),
utils.TrimString(pService.Name, 10),
hash,
realProtocol,
pPort.Port)
return utils.ValidateName(name)
}
5 changes: 3 additions & 2 deletions pkg/vngcloud/vlb.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,8 @@ func (c *vLB) inspectService(pService *lCoreV1.Service) (*Expander, error) {

// Ensure pools and listener for this loadbalancer
for _, port := range pService.Spec.Ports {
poolName := utils.GenListenerAndPoolName(c.getClusterID(), pService, consts.RESOURCE_TYPE_SERVICE, port)
poolName := serviceConf.GenPoolName(c.getClusterID(), pService, consts.RESOURCE_TYPE_SERVICE, port)
listenerName := serviceConf.GenListenerName(c.getClusterID(), pService, consts.RESOURCE_TYPE_SERVICE, port)

monitorPort := int(port.NodePort)
if serviceConf.HealthcheckPort != 0 {
Expand Down Expand Up @@ -577,7 +578,7 @@ func (c *vLB) inspectService(pService *lCoreV1.Service) (*Expander, error) {
}

listenerOptions := serviceConf.CreateListenerOptions(port)
listenerOptions.ListenerName = poolName
listenerOptions.ListenerName = listenerName

ingressInspect.PoolExpander = append(ingressInspect.PoolExpander, &utils.PoolExpander{
UUID: "",
Expand Down

0 comments on commit 08cdd1d

Please sign in to comment.