From 08cdd1ddb71167086eda98153fdba4bb846d0a95 Mon Sep 17 00:00:00 2001 From: andrew Date: Mon, 10 Jun 2024 13:46:27 +0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=B1=20add=20annotation=20support=20ena?= =?UTF-8?q?ble=20proxy=20protocol?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/utils/generate.go | 23 +++------------- pkg/utils/utils.go | 2 +- pkg/vngcloud/annotation.go | 55 +++++++++++++++++++++++++++++++++++--- pkg/vngcloud/vlb.go | 5 ++-- 4 files changed, 60 insertions(+), 25 deletions(-) diff --git a/pkg/utils/generate.go b/pkg/utils/generate.go index a6e7d5b..19ef189 100644 --- a/pkg/utils/generate.go +++ b/pkg/utils/generate.go @@ -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" ) @@ -25,7 +23,7 @@ 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 { @@ -33,7 +31,7 @@ func GeneratePolicyName(clusterID, namespace, resourceName, resourceType string, 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 { @@ -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), "-") @@ -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) -} diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 14b1035..5d157e6 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -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): diff --git a/pkg/vngcloud/annotation.go b/pkg/vngcloud/annotation.go index 2b8016a..8cd9595 100644 --- a/pkg/vngcloud/annotation.go +++ b/pkg/vngcloud/annotation.go @@ -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" ) @@ -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" @@ -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: "", @@ -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 @@ -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 } @@ -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) +} diff --git a/pkg/vngcloud/vlb.go b/pkg/vngcloud/vlb.go index db8fb0f..e560b32 100644 --- a/pkg/vngcloud/vlb.go +++ b/pkg/vngcloud/vlb.go @@ -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 { @@ -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: "",