Skip to content

Commit

Permalink
Merge pull request #170 from linode/feat/refactor-annotations
Browse files Browse the repository at this point in the history
Replace helper
  • Loading branch information
tchinmai7 authored Jan 19, 2024
2 parents c9c1f4a + ec24cd3 commit 2878e34
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions cloud/linode/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func newLoadbalancers(client Client, zone string) cloudprovider.LoadBalancer {
}

func (l *loadbalancers) getNodeBalancerForService(ctx context.Context, service *v1.Service) (*linodego.NodeBalancer, error) {
rawID, _ := getServiceAnnotation(service, annLinodeNodeBalancerID)
rawID := service.GetAnnotations()[annLinodeNodeBalancerID]
id, idErr := strconv.Atoi(rawID)
hasIDAnn := idErr == nil && id != 0

Expand Down Expand Up @@ -120,7 +120,7 @@ func (l *loadbalancers) getNodeBalancerByStatus(ctx context.Context, service *v1
func (l *loadbalancers) cleanupOldNodeBalancer(ctx context.Context, service *v1.Service) error {
// unless there's an annotation, we can never get a past and current NB to differ,
// because they're looked up the same way
if _, ok := getServiceAnnotation(service, annLinodeNodeBalancerID); !ok {
if _, ok := service.GetAnnotations()[annLinodeNodeBalancerID]; !ok {
return nil
}

Expand Down Expand Up @@ -474,7 +474,7 @@ func (l *loadbalancers) getNodeBalancerByID(ctx context.Context, service *v1.Ser

func (l *loadbalancers) getLoadBalancerTags(_ context.Context, clusterName string, service *v1.Service) []string {
tags := []string{clusterName}
tagStr, ok := getServiceAnnotation(service, annLinodeLoadBalancerTags)
tagStr, ok := service.GetAnnotations()[annLinodeLoadBalancerTags]
if ok {
return append(tags, strings.Split(tagStr, ",")...)
}
Expand All @@ -494,7 +494,7 @@ func (l *loadbalancers) createNodeBalancer(ctx context.Context, clusterName stri
Tags: tags,
}

fwid, ok := getServiceAnnotation(service, annLinodeCloudFirewallID)
fwid, ok := service.GetAnnotations()[annLinodeCloudFirewallID]
if ok {
firewallID, err := strconv.Atoi(fwid)
if err != nil {
Expand Down Expand Up @@ -525,46 +525,46 @@ func (l *loadbalancers) buildNodeBalancerConfig(ctx context.Context, service *v1
}

if health == linodego.CheckHTTP || health == linodego.CheckHTTPBody {
path := service.Annotations[annLinodeCheckPath]
path := service.GetAnnotations()[annLinodeCheckPath]
if path == "" {
path = "/"
}
config.CheckPath = path
}

if health == linodego.CheckHTTPBody {
body := service.Annotations[annLinodeCheckBody]
body := service.GetAnnotations()[annLinodeCheckBody]
if body == "" {
return config, fmt.Errorf("for health check type http_body need body regex annotation %v", annLinodeCheckBody)
}
config.CheckBody = body
}
checkInterval := 5
if ci, ok := service.Annotations[annLinodeHealthCheckInterval]; ok {
if ci, ok := service.GetAnnotations()[annLinodeHealthCheckInterval]; ok {
if checkInterval, err = strconv.Atoi(ci); err != nil {
return config, err
}
}
config.CheckInterval = checkInterval

checkTimeout := 3
if ct, ok := service.Annotations[annLinodeHealthCheckTimeout]; ok {
if ct, ok := service.GetAnnotations()[annLinodeHealthCheckTimeout]; ok {
if checkTimeout, err = strconv.Atoi(ct); err != nil {
return config, err
}
}
config.CheckTimeout = checkTimeout

checkAttempts := 2
if ca, ok := service.Annotations[annLinodeHealthCheckAttempts]; ok {
if ca, ok := service.GetAnnotations()[annLinodeHealthCheckAttempts]; ok {
if checkAttempts, err = strconv.Atoi(ca); err != nil {
return config, err
}
}
config.CheckAttempts = checkAttempts

checkPassive := true
if cp, ok := service.Annotations[annLinodeHealthCheckPassive]; ok {
if cp, ok := service.GetAnnotations()[annLinodeHealthCheckPassive]; ok {
if checkPassive, err = strconv.ParseBool(cp); err != nil {
return config, err
}
Expand Down Expand Up @@ -685,7 +685,7 @@ func getPortConfig(service *v1.Service, port int) (portConfig, error) {
protocol := portConfigAnnotation.Protocol
if protocol == "" {
protocol = "tcp"
if p, ok := service.Annotations[annLinodeDefaultProtocol]; ok {
if p, ok := service.GetAnnotations()[annLinodeDefaultProtocol]; ok {
protocol = p
}
}
Expand All @@ -695,7 +695,7 @@ func getPortConfig(service *v1.Service, port int) (portConfig, error) {
if proxyProtocol == "" {
proxyProtocol = string(linodego.ProxyProtocolNone)
for _, ann := range []string{annLinodeDefaultProxyProtocol, annLinodeProxyProtocolDeprecated} {
if pp, ok := service.Annotations[ann]; ok {
if pp, ok := service.GetAnnotations()[ann]; ok {
proxyProtocol = pp
break
}
Expand All @@ -722,7 +722,7 @@ func getPortConfig(service *v1.Service, port int) (portConfig, error) {
}

func getHealthCheckType(service *v1.Service) (linodego.ConfigCheck, error) {
hType, ok := service.Annotations[annLinodeHealthCheckType]
hType, ok := service.GetAnnotations()[annLinodeHealthCheckType]
if !ok {
return linodego.CheckConnection, nil
}
Expand All @@ -735,7 +735,7 @@ func getHealthCheckType(service *v1.Service) (linodego.ConfigCheck, error) {
func getPortConfigAnnotation(service *v1.Service, port int) (portConfigAnnotation, error) {
annotation := portConfigAnnotation{}
annotationKey := annLinodePortConfigPrefix + strconv.Itoa(port)
annotationJSON, ok := service.Annotations[annotationKey]
annotationJSON, ok := service.GetAnnotations()[annotationKey]

if !ok {
return annotation, nil
Expand Down Expand Up @@ -789,7 +789,7 @@ func getTLSCertInfo(ctx context.Context, kubeClient kubernetes.Interface, namesp
func getConnectionThrottle(service *v1.Service) int {
connThrottle := 20

if connThrottleString := service.Annotations[annLinodeThrottle]; connThrottleString != "" {
if connThrottleString := service.GetAnnotations()[annLinodeThrottle]; connThrottleString != "" {
parsed, err := strconv.Atoi(connThrottleString)
if err == nil {
if parsed < 0 {
Expand Down Expand Up @@ -836,16 +836,8 @@ func getServiceNn(service *v1.Service) string {
return fmt.Sprintf("%s/%s", service.Namespace, service.Name)
}

func getServiceAnnotation(service *v1.Service, name string) (string, bool) {
if service.Annotations == nil {
return "", false
}
val, ok := service.Annotations[name]
return val, ok
}

func getServiceBoolAnnotation(service *v1.Service, name string) bool {
value, ok := getServiceAnnotation(service, name)
value, ok := service.GetAnnotations()[name]
if !ok {
return false
}
Expand Down

0 comments on commit 2878e34

Please sign in to comment.