Skip to content

Commit

Permalink
Adding Stickiness option for Target Groups
Browse files Browse the repository at this point in the history
  • Loading branch information
drewmudgett committed Aug 15, 2024
1 parent 75496b9 commit 47dcaa6
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ Overview of configuration which can be set via Ingress annotations.
|`zalando.org/aws-load-balancer-http2`| `true` \| `false`|`true`|
|`zalando.org/aws-waf-web-acl-id` | `string` | N/A |
|`kubernetes.io/ingress.class`|`string`|N/A|
|`zalando.org/aws-load-balancer-stickiness`| `true` \| `false`|`false`|

The defaults can also be configured globally via a flag on the controller.

Expand Down
11 changes: 11 additions & 0 deletions aws/cf.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Stack struct {
WAFWebACLID string
CertificateARNs map[string]time.Time
tags map[string]string
Stickiness bool
}

// IsComplete returns true if the stack status is a complete state.
Expand Down Expand Up @@ -148,6 +149,7 @@ const (
parameterLoadBalancerTypeParameter = "Type"
parameterLoadBalancerWAFWebACLIDParameter = "LoadBalancerWAFWebACLIDParameter"
parameterHTTP2Parameter = "HTTP2"
parameterStickinessParameter = "Stickiness"
)

type stackSpec struct {
Expand Down Expand Up @@ -188,6 +190,7 @@ type stackSpec struct {
denyInternalDomainsResponse denyResp
internalDomains []string
tags map[string]string
stickiness bool
}

type healthCheck struct {
Expand Down Expand Up @@ -229,6 +232,7 @@ func createStack(svc cloudformationiface.CloudFormationAPI, spec *stackSpec) (st
cfParam(parameterIpAddressTypeParameter, spec.ipAddressType),
cfParam(parameterLoadBalancerTypeParameter, spec.loadbalancerType),
cfParam(parameterHTTP2Parameter, fmt.Sprintf("%t", spec.http2)),
cfParam(parameterStickinessParameter, spec.stickiness),
},
Tags: tagMapToCloudformationTags(tags),
TemplateBody: aws.String(template),
Expand Down Expand Up @@ -304,6 +308,7 @@ func updateStack(svc cloudformationiface.CloudFormationAPI, spec *stackSpec) (st
cfParam(parameterIpAddressTypeParameter, spec.ipAddressType),
cfParam(parameterLoadBalancerTypeParameter, spec.loadbalancerType),
cfParam(parameterHTTP2Parameter, fmt.Sprintf("%t", spec.http2)),
cfParam(parameterStickinessParameter, spec.stickiness),
},
Tags: tagMapToCloudformationTags(tags),
TemplateBody: aws.String(template),
Expand Down Expand Up @@ -481,6 +486,11 @@ func mapToManagedStack(stack *cloudformation.Stack) *Stack {
http2 = false
}

stickiness := false
if parameters[parameterStickinessParameter] == "true" {
stickiness = true
}

return &Stack{
Name: aws.StringValue(stack.StackName),
DNSName: outputs.dnsName(),
Expand All @@ -498,6 +508,7 @@ func mapToManagedStack(stack *cloudformation.Stack) *Stack {
statusReason: aws.StringValue(stack.StackStatusReason),
CWAlarmConfigHash: tags[cwAlarmConfigHashTag],
WAFWebACLID: parameters[parameterLoadBalancerWAFWebACLIDParameter],
Stickiness: stickiness,
}
}

Expand Down
13 changes: 11 additions & 2 deletions aws/cf_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ func generateTemplate(spec *stackSpec) (string, error) {
Description: "H2 Enabled",
Default: "true",
},
parameterStickinessParameter: {
Type: "String",
Description: "Enable Target Group Stickiness",
Default: "false",
},
}

if spec.wafWebAclId != "" {
Expand Down Expand Up @@ -168,8 +173,12 @@ func generateTemplate(spec *stackSpec) (string, error) {
template.AddResource("HTTPListener", &cloudformation.ElasticLoadBalancingV2Listener{
DefaultActions: &cloudformation.ElasticLoadBalancingV2ListenerActionList{
{
Type: cloudformation.String("forward"),
TargetGroupArn: cloudformation.Ref(httpTargetGroupName).String(),
Type: cloudformation.String("forward"),
TargetGroupArn: cloudformation.Ref(httpTargetGroupName).String(),
TargetGroupStickinessConfig: &cloudformation.ElasticLoadBalancingV2ListenerTargetGroupStickinessConfig{
Enabled: cloudformation.Ref(parameterStickinessParameter).String(),
DurationSeconds: cloudformation.Integer(3600)
},
},
},
LoadBalancerArn: cloudformation.Ref("LB").String(),
Expand Down
7 changes: 7 additions & 0 deletions kubernetes/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type Ingress struct {
LoadBalancerType string
WAFWebACLID string
Hostnames []string
Stickiness bool
}

// String returns a string representation of the Ingress instance containing the type, namespace and the resource name.
Expand Down Expand Up @@ -234,6 +235,11 @@ func (a *Adapter) newIngress(typ IngressType, metadata kubeItemMetadata, host st
if getAnnotationsString(annotations, ingressHTTP2Annotation, "") == "false" {
http2 = false
}

stickiness := false
if getAnnotationsString(annotations, ingressLoadBalancerStickiness, "") == "true" {
stickiness = true
}

return &Ingress{
ResourceType: typ,
Expand All @@ -251,6 +257,7 @@ func (a *Adapter) newIngress(typ IngressType, metadata kubeItemMetadata, host st
LoadBalancerType: loadBalancerType,
WAFWebACLID: wafWebAclId,
HTTP2: http2,
Stickiness: stickiness,
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions kubernetes/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const (
ingressHTTP2Annotation = "zalando.org/aws-load-balancer-http2"
ingressWAFWebACLIDAnnotation = "zalando.org/aws-waf-web-acl-id"
ingressClassAnnotation = "kubernetes.io/ingress.class"
ingressLoadBalancerStickiness = "zalando.org/aws-load-balancer-stickiness"
)

func getAnnotationsString(annotations map[string]string, key string, defaultValue string) string {
Expand Down
1 change: 1 addition & 0 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type loadBalancer struct {
certTTL time.Duration
cwAlarms aws.CloudWatchAlarmList
loadBalancerType string
stickiness bool
}

const (
Expand Down

0 comments on commit 47dcaa6

Please sign in to comment.