From 44f6fb7ad9c3da720b7ed340464ff60e3a1c50aa Mon Sep 17 00:00:00 2001 From: Rael Garcia Date: Mon, 13 Jul 2020 19:09:14 +0200 Subject: [PATCH] Parse annotation booleans using strconv --- pkg/controller/service/service_controller.go | 49 ++++++++++++++------ 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/pkg/controller/service/service_controller.go b/pkg/controller/service/service_controller.go index d2abaf9..0f80a9f 100644 --- a/pkg/controller/service/service_controller.go +++ b/pkg/controller/service/service_controller.go @@ -23,17 +23,20 @@ import ( ) const ( - helperAnnotationPrefix = "aws-nlb-helper.3scale.net" - helperAnnotationTargetGroupsProxyProcotolKey = "aws-nlb-helper.3scale.net/enable-targetgroups-proxy-protocol" - helperAnnotationTargetGroupsSticknessKey = "aws-nlb-helper.3scale.net/enable-targetgroups-stickness" - helperAnnotationTargetGroupsDeregistrationDelayKey = "aws-nlb-helper.3scale.net/targetgroups-deregisration-delay" - helperAnnotationTargetGroupsDeregistrationDelayDefault = 300 - helperAnnotationLoadBalancerTerminationProtectionKey = "aws-nlb-helper.3scale.net/loadbalanacer-termination-protection" - awsLoadBalancerTypeAnnotationKey = "service.beta.kubernetes.io/aws-load-balancer-type" - awsLoadBalancerTypeNLBAnnotationValue = "nlb" - awsLoadBalancerTypeELBAnnotationValue = "elb" - loadBalancerNotReadyRetryInterval = 30 - reconcileInterval = 60 + helperAnnotationPrefix = "aws-nlb-helper.3scale.net" + helperAnnotationLoadBalancerTerminationProtectionKey = "aws-nlb-helper.3scale.net/loadbalanacer-termination-protection" + helperAnnotationLoadBalancerTerminationProtectionDefault = false + helperAnnotationTargetGroupsProxyProcotolKey = "aws-nlb-helper.3scale.net/enable-targetgroups-proxy-protocol" + helperAnnotationTargetGroupsProxyProcotolDefault = false + helperAnnotationTargetGroupsSticknessKey = "aws-nlb-helper.3scale.net/enable-targetgroups-stickness" + helperAnnotationTargetGroupsSticknessDefault = false + helperAnnotationTargetGroupsDeregistrationDelayKey = "aws-nlb-helper.3scale.net/targetgroups-deregisration-delay" + helperAnnotationTargetGroupsDeregistrationDelayDefault = 300 + awsLoadBalancerTypeAnnotationKey = "service.beta.kubernetes.io/aws-load-balancer-type" + awsLoadBalancerTypeNLBAnnotationValue = "nlb" + awsLoadBalancerTypeELBAnnotationValue = "elb" + loadBalancerNotReadyRetryInterval = 30 + reconcileInterval = 60 ) var log = logf.Log.WithName("controller_service") @@ -171,17 +174,35 @@ func (r *ReconcileService) Reconcile(request reconcile.Request) (reconcile.Resul if awsLoadBalancerType == "nlb" { + awsLoadBalancerSettingsTerminationProtection, err := strconv.ParseBool(svc.GetAnnotations()[helperAnnotationLoadBalancerTerminationProtectionKey]) + if err != nil { + reqLogger.Info("Unable to parse Termination Protection value, defaulting.", "awsLoadBalancerSettingsTerminationProtection", helperAnnotationLoadBalancerTerminationProtectionDefault) + awsLoadBalancerSettingsTerminationProtection = helperAnnotationLoadBalancerTerminationProtectionDefault + } + awsLoadBalancerSettingsDeregistrationDelay, err := strconv.Atoi(svc.GetAnnotations()[helperAnnotationTargetGroupsDeregistrationDelayKey]) if err != nil { reqLogger.Info("Unable to parse Deregistration Delay value, defaulting.", "awsLoadBalancerSettingsDeregistrationDelay", helperAnnotationTargetGroupsDeregistrationDelayDefault) awsLoadBalancerSettingsDeregistrationDelay = helperAnnotationTargetGroupsDeregistrationDelayDefault } + awsLoadBalancerSettingsTargetGroupProxyProtocol, err := strconv.ParseBool(svc.GetAnnotations()[helperAnnotationTargetGroupsProxyProcotolKey]) + if err != nil { + reqLogger.Info("Unable to parse Target Group Proxy Protocol value, defaulting.", "awsLoadBalancerSettingsTargetGroupProxyProtocol", helperAnnotationTargetGroupsProxyProcotolDefault) + awsLoadBalancerSettingsTargetGroupProxyProtocol = helperAnnotationTargetGroupsProxyProcotolDefault + } + + awsLoadBalancerSettingsTargetGroupStickness, err := strconv.ParseBool(svc.GetAnnotations()[helperAnnotationTargetGroupsSticknessKey]) + if err != nil { + reqLogger.Info("Unable to parse Target Group Sticknesss value, defaulting.", "awsLoadBalancerSettingsTargetGroupStickness", helperAnnotationTargetGroupsSticknessDefault) + awsLoadBalancerSettingsTargetGroupStickness = helperAnnotationTargetGroupsSticknessDefault + } + awsLoadBalancerSettings := aws_helper.NetworkLoadBalancerAttributes{ - LoadBalancerTerminationProtection: (svc.GetAnnotations()[helperAnnotationLoadBalancerTerminationProtectionKey] == "true"), + LoadBalancerTerminationProtection: awsLoadBalancerSettingsTerminationProtection, TargetGroupDeregistrationDelay: awsLoadBalancerSettingsDeregistrationDelay, - TargetGroupStickness: (svc.GetAnnotations()[helperAnnotationTargetGroupsSticknessKey] == "true"), - TargetGroupProxyProtocol: (svc.GetAnnotations()[helperAnnotationTargetGroupsProxyProcotolKey] == "true"), + TargetGroupStickness: awsLoadBalancerSettingsTargetGroupStickness, + TargetGroupProxyProtocol: awsLoadBalancerSettingsTargetGroupProxyProtocol, } updated, err := aws_helper.UpdateNetworkLoadBalancer(awsLoadBalancerIngressHostname, serviceNameTagValue, awsLoadBalancerSettings) if err != nil {