Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apis/v1alpha1/ack-generate-metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ api_directory_checksum: b90448fd5950eedbe16b579f98dc6b6b01781430
api_version: v1alpha1
aws_sdk_go_version: v1.32.6
generator_config_info:
file_checksum: ce1168f649f03d9652bc8e82f8322d6dd2d909f0
file_checksum: 7d389c6806d2b7836e782bcdbad062dad7aca952
original_file_name: generator.yaml
last_modification:
reason: API generation
11 changes: 11 additions & 0 deletions apis/v1alpha1/generator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ resources:
Listener:
is_arn_primary_key: true
fields:
DefaultActions:
compare:
is_ignored: true
# keep the declared form in the spec; ReadOne still fills latest
set:
- method: Create
ignore: from
- method: Update
ignore: from
DefaultActions.TargetGroupARN:
references:
resource: TargetGroup
Expand Down Expand Up @@ -165,6 +174,8 @@ resources:
tags:
ignore: true
hooks:
delta_pre_compare:
code: customPreCompare(delta, a, b)
sdk_read_many_post_build_request:
template_path: hooks/listener/sdk_read_many_post_build_request.go.tpl
TargetGroup:
Expand Down
11 changes: 11 additions & 0 deletions generator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ resources:
Listener:
is_arn_primary_key: true
fields:
DefaultActions:
compare:
is_ignored: true
# keep the declared form in the spec; ReadOne still fills latest
set:
- method: Create
ignore: from
- method: Update
ignore: from
DefaultActions.TargetGroupARN:
references:
resource: TargetGroup
Expand Down Expand Up @@ -165,6 +174,8 @@ resources:
tags:
ignore: true
hooks:
delta_pre_compare:
code: customPreCompare(delta, a, b)
sdk_read_many_post_build_request:
template_path: hooks/listener/sdk_read_many_post_build_request.go.tpl
TargetGroup:
Expand Down
8 changes: 1 addition & 7 deletions pkg/resource/listener/delta.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

199 changes: 199 additions & 0 deletions pkg/resource/listener/hooks.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package listener

import (
"sort"

svcapitypes "github.com/aws-controllers-k8s/elbv2-controller/apis/v1alpha1"
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
"github.com/aws/aws-sdk-go-v2/aws"
"k8s.io/apimachinery/pkg/api/equality"
)

// customCheckRequiredFieldsMissingMethod returns true if there are any fields
// for the ReadOne Input shape that are required but not present in the
// resource's Spec or Status.
Expand All @@ -8,3 +17,193 @@ func (rm *resourceManager) customCheckRequiredFieldsMissingMethod(
) bool {
return r.Identifiers().ARN() == nil
}

func customPreCompare(
delta *ackcompare.Delta,
a *resource,
b *resource,
) {
customCompareDefaultActions(delta, a, b)
}

// customCompareDefaultActions compares DefaultActions in the form
// DescribeListeners reports them: both sides get the members the service
// fills in when the spec leaves them unset, target groups are sorted by ARN,
// and the members the service never returns (the k8s-only references and the
// OIDC client secret) are left out. A member removed from the spec then
// differs from the value still on the load balancer and reaches
// ModifyListener.
func customCompareDefaultActions(
delta *ackcompare.Delta,
a *resource,
b *resource,
) {
if a == nil || b == nil {
return
}
desired := a.ko.Spec.DefaultActions
observed := b.ko.Spec.DefaultActions
if !equality.Semantic.DeepEqual(normalizeActions(desired), normalizeActions(observed)) {
delta.Add("Spec.DefaultActions", desired, observed)
}
}

// normalizeActions returns copies of actions in the form DescribeListeners
// returns them.
func normalizeActions(actions []*svcapitypes.Action) []*svcapitypes.Action {
out := make([]*svcapitypes.Action, len(actions))
for i, action := range actions {
if action == nil {
continue
}
c := action.DeepCopy()
c.TargetGroupRef = nil
if c.Order == nil {
c.Order = aws.Int64(int64(i + 1))
}
normalizeForward(c)
normalizeRedirect(c.RedirectConfig)
normalizeFixedResponse(c.FixedResponseConfig)
normalizeOIDC(c.AuthenticateOIDCConfig)
normalizeCognito(c.AuthenticateCognitoConfig)
out[i] = c
}
// DescribeListeners returns multiple actions in an order of its own,
// neither declared nor by Order, so compare them sorted by Order.
sort.SliceStable(out, func(i, j int) bool {
var oi, oj int64
if out[i] != nil {
oi = aws.ToInt64(out[i].Order)
}
if out[j] != nil {
oj = aws.ToInt64(out[j].Order)
}
return oi < oj
})
return out

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorting by Order makes the comparison independent of the order DescribeListeners returns the actions in.

Suggested change
return out
// DescribeListeners returns multiple actions in an order of its own,
// neither declared nor by Order, so compare them sorted by Order.
sort.SliceStable(out, func(i, j int) bool {
var oi, oj int64
if out[i] != nil {
oi = aws.ToInt64(out[i].Order)
}
if out[j] != nil {
oj = aws.ToInt64(out[j].Order)
}
return oi < oj
})
return out

}

// normalizeForward fills the forward members the service returns: a forward
// with only targetGroupARN comes back with a one-entry forwardConfig, a
// one-entry forwardConfig comes back with the action-level targetGroupARN,
// an unset weight is 1 and unset stickiness is disabled. Target groups are
// returned in ARN order, not declared order.
func normalizeForward(c *svcapitypes.Action) {
if c.ForwardConfig == nil && c.TargetGroupARN != nil {
c.ForwardConfig = &svcapitypes.ForwardActionConfig{
TargetGroups: []*svcapitypes.TargetGroupTuple{{TargetGroupARN: c.TargetGroupARN}},
}
}
fc := c.ForwardConfig
if fc == nil {
return
}
if fc.TargetGroupStickinessConfig == nil {
fc.TargetGroupStickinessConfig = &svcapitypes.TargetGroupStickinessConfig{}
}
if fc.TargetGroupStickinessConfig.Enabled == nil {
fc.TargetGroupStickinessConfig.Enabled = aws.Bool(false)
}
if !*fc.TargetGroupStickinessConfig.Enabled {
fc.TargetGroupStickinessConfig.DurationSeconds = nil
}
for _, tg := range fc.TargetGroups {
if tg == nil {
continue
}
tg.TargetGroupRef = nil
if tg.Weight == nil {
tg.Weight = aws.Int64(1)
}
}
sort.SliceStable(fc.TargetGroups, func(i, j int) bool {
return targetGroupARN(fc.TargetGroups[i]) < targetGroupARN(fc.TargetGroups[j])
})
if c.TargetGroupARN == nil && len(fc.TargetGroups) == 1 && fc.TargetGroups[0] != nil {
c.TargetGroupARN = fc.TargetGroups[0].TargetGroupARN
}
}

func targetGroupARN(tg *svcapitypes.TargetGroupTuple) string {
if tg == nil {
return ""
}
return aws.ToString(tg.TargetGroupARN)
}

// normalizeRedirect fills the keyword the service returns for each unset
// redirect component.
func normalizeRedirect(r *svcapitypes.RedirectActionConfig) {
if r == nil {
return
}
if r.Protocol == nil {
r.Protocol = aws.String("#{protocol}")
}
if r.Port == nil {
r.Port = aws.String("#{port}")
}
if r.Host == nil {
r.Host = aws.String("#{host}")
}
if r.Path == nil {
r.Path = aws.String("/#{path}")
}
if r.Query == nil {
r.Query = aws.String("#{query}")
}
}

func normalizeFixedResponse(f *svcapitypes.FixedResponseActionConfig) {
if f == nil {
return
}
if f.ContentType == nil {
f.ContentType = aws.String("text/plain")
}
}

// normalizeOIDC fills the session defaults and drops the client secret, which
// the service never returns: a spec that changes only the secret produces no
// delta and the new secret only reaches AWS with another change.
func normalizeOIDC(o *svcapitypes.AuthenticateOIDCActionConfig) {
if o == nil {
return
}
o.ClientSecret = nil
o.UseExistingClientSecret = nil
if len(o.AuthenticationRequestExtraParams) == 0 {
o.AuthenticationRequestExtraParams = nil
}
o.OnUnauthenticatedRequest, o.Scope, o.SessionCookieName, o.SessionTimeout = authDefaults(
o.OnUnauthenticatedRequest, o.Scope, o.SessionCookieName, o.SessionTimeout)
}

func normalizeCognito(c *svcapitypes.AuthenticateCognitoActionConfig) {
if c == nil {
return
}
if len(c.AuthenticationRequestExtraParams) == 0 {
c.AuthenticationRequestExtraParams = nil
}
c.OnUnauthenticatedRequest, c.Scope, c.SessionCookieName, c.SessionTimeout = authDefaults(
c.OnUnauthenticatedRequest, c.Scope, c.SessionCookieName, c.SessionTimeout)
}

// authDefaults returns the authenticate-oidc and authenticate-cognito session
// members with the service defaults filled in.
func authDefaults(onUnauthenticated, scope, cookie *string, timeout *int64) (*string, *string, *string, *int64) {
if onUnauthenticated == nil {
onUnauthenticated = aws.String("authenticate")
}
if scope == nil {
scope = aws.String("openid")
}
if cookie == nil {
cookie = aws.String("AWSELBAuthSessionCookie")
}
if timeout == nil {
timeout = aws.Int64(604800)
}
return onUnauthenticated, scope, cookie, timeout
}
Loading