diff --git a/apis/v1alpha1/ack-generate-metadata.yaml b/apis/v1alpha1/ack-generate-metadata.yaml index 53692aa..0473aa5 100755 --- a/apis/v1alpha1/ack-generate-metadata.yaml +++ b/apis/v1alpha1/ack-generate-metadata.yaml @@ -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 diff --git a/apis/v1alpha1/generator.yaml b/apis/v1alpha1/generator.yaml index 0a32afd..d99bd00 100644 --- a/apis/v1alpha1/generator.yaml +++ b/apis/v1alpha1/generator.yaml @@ -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 @@ -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: diff --git a/generator.yaml b/generator.yaml index 0a32afd..d99bd00 100644 --- a/generator.yaml +++ b/generator.yaml @@ -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 @@ -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: diff --git a/pkg/resource/listener/delta.go b/pkg/resource/listener/delta.go index c306edb..121a8c4 100644 --- a/pkg/resource/listener/delta.go +++ b/pkg/resource/listener/delta.go @@ -41,6 +41,7 @@ func newResourceDelta( delta.Add("", a, b) return delta } + customPreCompare(delta, a, b) if len(a.ko.Spec.AlpnPolicy) != len(b.ko.Spec.AlpnPolicy) { delta.Add("Spec.AlpnPolicy", a.ko.Spec.AlpnPolicy, b.ko.Spec.AlpnPolicy) @@ -56,13 +57,6 @@ func newResourceDelta( delta.Add("Spec.Certificates", a.ko.Spec.Certificates, b.ko.Spec.Certificates) } } - if len(a.ko.Spec.DefaultActions) != len(b.ko.Spec.DefaultActions) { - delta.Add("Spec.DefaultActions", a.ko.Spec.DefaultActions, b.ko.Spec.DefaultActions) - } else if len(a.ko.Spec.DefaultActions) > 0 { - if !equality.Semantic.Equalities.DeepEqual(a.ko.Spec.DefaultActions, b.ko.Spec.DefaultActions) { - delta.Add("Spec.DefaultActions", a.ko.Spec.DefaultActions, b.ko.Spec.DefaultActions) - } - } if ackcompare.HasNilDifference(a.ko.Spec.LoadBalancerARN, b.ko.Spec.LoadBalancerARN) { delta.Add("Spec.LoadBalancerARN", a.ko.Spec.LoadBalancerARN, b.ko.Spec.LoadBalancerARN) } else if a.ko.Spec.LoadBalancerARN != nil && b.ko.Spec.LoadBalancerARN != nil { diff --git a/pkg/resource/listener/hooks.go b/pkg/resource/listener/hooks.go index 2be33ef..9696876 100644 --- a/pkg/resource/listener/hooks.go +++ b/pkg/resource/listener/hooks.go @@ -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. @@ -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 +} + +// 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 +} diff --git a/pkg/resource/listener/hooks_test.go b/pkg/resource/listener/hooks_test.go new file mode 100644 index 0000000..843b25b --- /dev/null +++ b/pkg/resource/listener/hooks_test.go @@ -0,0 +1,384 @@ +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. + +package listener + +import ( + "testing" + + ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/stretchr/testify/assert" + + svcapitypes "github.com/aws-controllers-k8s/elbv2-controller/apis/v1alpha1" +) + +func tgRef(name string) *ackv1alpha1.AWSResourceReferenceWrapper { + return &ackv1alpha1.AWSResourceReferenceWrapper{ + From: &ackv1alpha1.AWSResourceReference{Name: aws.String(name)}, + } +} + +// forwardAsReturned is the shape DescribeListeners returns for a forward +// action that was created with a single target group. +func forwardAsReturned(order int64, arn string) *svcapitypes.Action { + return &svcapitypes.Action{ + Type: aws.String("forward"), + Order: aws.Int64(order), + TargetGroupARN: aws.String(arn), + ForwardConfig: &svcapitypes.ForwardActionConfig{ + TargetGroups: []*svcapitypes.TargetGroupTuple{ + {TargetGroupARN: aws.String(arn), Weight: aws.Int64(1)}, + }, + TargetGroupStickinessConfig: &svcapitypes.TargetGroupStickinessConfig{ + Enabled: aws.Bool(false), + }, + }, + } +} + +func weightedForward(order *int64, weightA, weightB int64) *svcapitypes.Action { + return &svcapitypes.Action{ + Type: aws.String("forward"), + Order: order, + ForwardConfig: &svcapitypes.ForwardActionConfig{ + TargetGroups: []*svcapitypes.TargetGroupTuple{ + {TargetGroupARN: aws.String("arn:tg/a"), Weight: aws.Int64(weightA)}, + {TargetGroupARN: aws.String("arn:tg/b"), Weight: aws.Int64(weightB)}, + }, + TargetGroupStickinessConfig: &svcapitypes.TargetGroupStickinessConfig{ + Enabled: aws.Bool(true), + DurationSeconds: aws.Int64(3600), + }, + }, + } +} + +func redirect(path *string) *svcapitypes.RedirectActionConfig { + return &svcapitypes.RedirectActionConfig{ + Protocol: aws.String("HTTPS"), + Port: aws.String("#{port}"), + Host: aws.String("#{host}"), + Path: path, + Query: aws.String("#{query}"), + StatusCode: aws.String("HTTP_301"), + } +} + +func oidc(secret *string, defaults bool) *svcapitypes.AuthenticateOIDCActionConfig { + cfg := &svcapitypes.AuthenticateOIDCActionConfig{ + Issuer: aws.String("https://idp.example.com"), + AuthorizationEndpoint: aws.String("https://idp.example.com/auth"), + TokenEndpoint: aws.String("https://idp.example.com/token"), + UserInfoEndpoint: aws.String("https://idp.example.com/userinfo"), + ClientID: aws.String("client-id"), + ClientSecret: secret, + } + if defaults { + cfg.OnUnauthenticatedRequest = aws.String("authenticate") + cfg.Scope = aws.String("openid") + cfg.SessionCookieName = aws.String("AWSELBAuthSessionCookie") + cfg.SessionTimeout = aws.Int64(604800) + } + return cfg +} + +func oidcWithScope(scope string) *svcapitypes.AuthenticateOIDCActionConfig { + cfg := oidc(nil, true) + cfg.Scope = aws.String(scope) + return cfg +} + +func TestNewResourceDelta_DefaultActions(t *testing.T) { + tests := []struct { + name string + desired []*svcapitypes.Action + observed []*svcapitypes.Action + expectDelta bool + }{ + { + name: "targetGroupARN only, service adds order and forward config", + desired: []*svcapitypes.Action{ + {Type: aws.String("forward"), TargetGroupARN: aws.String("arn:tg/a")}, + }, + observed: []*svcapitypes.Action{forwardAsReturned(1, "arn:tg/a")}, + expectDelta: false, + }, + { + name: "targetGroupRef only, resolved ARN matches", + desired: []*svcapitypes.Action{ + { + Type: aws.String("forward"), + TargetGroupRef: tgRef("my-tg"), + TargetGroupARN: aws.String("arn:tg/a"), + }, + }, + observed: []*svcapitypes.Action{forwardAsReturned(1, "arn:tg/a")}, + expectDelta: false, + }, + { + name: "forwardConfig with ref and no weight, service adds targetGroupARN weight and stickiness", + desired: []*svcapitypes.Action{ + { + Type: aws.String("forward"), + ForwardConfig: &svcapitypes.ForwardActionConfig{ + TargetGroups: []*svcapitypes.TargetGroupTuple{ + {TargetGroupRef: tgRef("my-tg"), TargetGroupARN: aws.String("arn:tg/a")}, + }, + }, + }, + }, + observed: []*svcapitypes.Action{forwardAsReturned(1, "arn:tg/a")}, + expectDelta: false, + }, + { + name: "weighted forward config unchanged", + desired: []*svcapitypes.Action{weightedForward(nil, 90, 10)}, + observed: []*svcapitypes.Action{weightedForward(aws.Int64(1), 90, 10)}, + expectDelta: false, + }, + { + name: "weights changed on the load balancer", + desired: []*svcapitypes.Action{weightedForward(nil, 90, 10)}, + observed: []*svcapitypes.Action{weightedForward(aws.Int64(1), 50, 50)}, + expectDelta: true, + }, + { + name: "target group changed", + desired: []*svcapitypes.Action{ + {Type: aws.String("forward"), TargetGroupARN: aws.String("arn:tg/b")}, + }, + observed: []*svcapitypes.Action{forwardAsReturned(1, "arn:tg/a")}, + expectDelta: true, + }, + { + name: "explicit order differs", + desired: []*svcapitypes.Action{ + {Type: aws.String("forward"), Order: aws.Int64(2), TargetGroupARN: aws.String("arn:tg/a")}, + }, + observed: []*svcapitypes.Action{forwardAsReturned(1, "arn:tg/a")}, + expectDelta: true, + }, + { + name: "redirect with service keyword defaults", + desired: []*svcapitypes.Action{ + { + Type: aws.String("redirect"), + RedirectConfig: &svcapitypes.RedirectActionConfig{ + Protocol: aws.String("HTTPS"), + StatusCode: aws.String("HTTP_301"), + }, + }, + }, + observed: []*svcapitypes.Action{ + {Type: aws.String("redirect"), Order: aws.Int64(1), RedirectConfig: redirect(aws.String("/#{path}"))}, + }, + expectDelta: false, + }, + { + name: "redirect path changed", + desired: []*svcapitypes.Action{ + {Type: aws.String("redirect"), RedirectConfig: redirect(aws.String("/new/#{path}"))}, + }, + observed: []*svcapitypes.Action{ + {Type: aws.String("redirect"), Order: aws.Int64(1), RedirectConfig: redirect(aws.String("/#{path}"))}, + }, + expectDelta: true, + }, + { + name: "authenticate-oidc then forward, secret not returned and defaults filled", + desired: []*svcapitypes.Action{ + {Type: aws.String("authenticate-oidc"), AuthenticateOIDCConfig: oidc(aws.String("s3cret"), false)}, + {Type: aws.String("forward"), TargetGroupARN: aws.String("arn:tg/a")}, + }, + observed: []*svcapitypes.Action{ + {Type: aws.String("authenticate-oidc"), Order: aws.Int64(1), AuthenticateOIDCConfig: oidc(nil, true)}, + forwardAsReturned(2, "arn:tg/a"), + }, + expectDelta: false, + }, + { + name: "action type changed", + desired: []*svcapitypes.Action{ + { + Type: aws.String("fixed-response"), + FixedResponseConfig: &svcapitypes.FixedResponseActionConfig{StatusCode: aws.String("503")}, + }, + }, + observed: []*svcapitypes.Action{forwardAsReturned(1, "arn:tg/a")}, + expectDelta: true, + }, + { + name: "stickiness removed from the spec must reach the load balancer", + desired: []*svcapitypes.Action{ + { + Type: aws.String("forward"), + ForwardConfig: &svcapitypes.ForwardActionConfig{ + TargetGroups: []*svcapitypes.TargetGroupTuple{ + {TargetGroupARN: aws.String("arn:tg/a"), Weight: aws.Int64(90)}, + {TargetGroupARN: aws.String("arn:tg/b"), Weight: aws.Int64(10)}, + }, + }, + }, + }, + observed: []*svcapitypes.Action{weightedForward(aws.Int64(1), 90, 10)}, + expectDelta: true, + }, + { + name: "weight removed from the spec must reach the load balancer", + desired: []*svcapitypes.Action{ + { + Type: aws.String("forward"), + ForwardConfig: &svcapitypes.ForwardActionConfig{ + TargetGroups: []*svcapitypes.TargetGroupTuple{ + {TargetGroupARN: aws.String("arn:tg/a")}, + {TargetGroupARN: aws.String("arn:tg/b"), Weight: aws.Int64(10)}, + }, + TargetGroupStickinessConfig: &svcapitypes.TargetGroupStickinessConfig{ + Enabled: aws.Bool(true), DurationSeconds: aws.Int64(3600), + }, + }, + }, + }, + observed: []*svcapitypes.Action{weightedForward(aws.Int64(1), 90, 10)}, + expectDelta: true, + }, + { + name: "target groups declared in another order than the service returns", + desired: []*svcapitypes.Action{ + { + Type: aws.String("forward"), + ForwardConfig: &svcapitypes.ForwardActionConfig{ + TargetGroups: []*svcapitypes.TargetGroupTuple{ + {TargetGroupARN: aws.String("arn:tg/b"), Weight: aws.Int64(10)}, + {TargetGroupARN: aws.String("arn:tg/a"), Weight: aws.Int64(90)}, + }, + TargetGroupStickinessConfig: &svcapitypes.TargetGroupStickinessConfig{ + Enabled: aws.Bool(true), DurationSeconds: aws.Int64(3600), + }, + }, + }, + }, + observed: []*svcapitypes.Action{weightedForward(aws.Int64(1), 90, 10)}, + expectDelta: false, + }, + { + name: "redirect host removed from the spec must reach the load balancer", + desired: []*svcapitypes.Action{ + {Type: aws.String("redirect"), RedirectConfig: &svcapitypes.RedirectActionConfig{ + Protocol: aws.String("HTTPS"), StatusCode: aws.String("HTTP_301"), + }}, + }, + observed: []*svcapitypes.Action{ + {Type: aws.String("redirect"), Order: aws.Int64(1), RedirectConfig: &svcapitypes.RedirectActionConfig{ + Protocol: aws.String("HTTPS"), Port: aws.String("#{port}"), Host: aws.String("www.example.com"), + Path: aws.String("/#{path}"), Query: aws.String("#{query}"), StatusCode: aws.String("HTTP_301"), + }}, + }, + expectDelta: true, + }, + { + name: "fixed response without contentType, service fills text/plain", + desired: []*svcapitypes.Action{ + {Type: aws.String("fixed-response"), FixedResponseConfig: &svcapitypes.FixedResponseActionConfig{StatusCode: aws.String("503")}}, + }, + observed: []*svcapitypes.Action{ + {Type: aws.String("fixed-response"), Order: aws.Int64(1), FixedResponseConfig: &svcapitypes.FixedResponseActionConfig{ + StatusCode: aws.String("503"), ContentType: aws.String("text/plain"), + }}, + }, + expectDelta: false, + }, + { + name: "oidc scope removed from the spec must reach the load balancer", + desired: []*svcapitypes.Action{ + {Type: aws.String("authenticate-oidc"), AuthenticateOIDCConfig: oidc(aws.String("s3cret"), false)}, + {Type: aws.String("forward"), TargetGroupARN: aws.String("arn:tg/a")}, + }, + observed: []*svcapitypes.Action{ + {Type: aws.String("authenticate-oidc"), Order: aws.Int64(1), AuthenticateOIDCConfig: oidcWithScope("email openid")}, + forwardAsReturned(2, "arn:tg/a"), + }, + expectDelta: true, + }, + { + name: "oidc secret changed alone produces no delta, the service never returns it", + desired: []*svcapitypes.Action{ + {Type: aws.String("authenticate-oidc"), AuthenticateOIDCConfig: oidc(aws.String("rotated"), false)}, + {Type: aws.String("forward"), TargetGroupARN: aws.String("arn:tg/a")}, + }, + observed: []*svcapitypes.Action{ + {Type: aws.String("authenticate-oidc"), Order: aws.Int64(1), AuthenticateOIDCConfig: oidc(nil, true)}, + forwardAsReturned(2, "arn:tg/a"), + }, + expectDelta: false, + }, + { + name: "single action without order from the service", + desired: []*svcapitypes.Action{ + {Type: aws.String("forward"), TargetGroupARN: aws.String("arn:tg/a")}, + }, + observed: []*svcapitypes.Action{ + {Type: aws.String("forward"), TargetGroupARN: aws.String("arn:tg/a"), ForwardConfig: &svcapitypes.ForwardActionConfig{ + TargetGroups: []*svcapitypes.TargetGroupTuple{{TargetGroupARN: aws.String("arn:tg/a"), Weight: aws.Int64(1)}}, + TargetGroupStickinessConfig: &svcapitypes.TargetGroupStickinessConfig{Enabled: aws.Bool(false)}, + }}, + }, + expectDelta: false, + }, + { + name: "multiple actions returned in another order than declared", + desired: []*svcapitypes.Action{ + {Type: aws.String("authenticate-oidc"), Order: aws.Int64(1), AuthenticateOIDCConfig: oidc(aws.String("s3cret"), false)}, + {Type: aws.String("forward"), Order: aws.Int64(2), TargetGroupARN: aws.String("arn:tg/a")}, + }, + observed: []*svcapitypes.Action{ + forwardAsReturned(2, "arn:tg/a"), + {Type: aws.String("authenticate-oidc"), Order: aws.Int64(1), AuthenticateOIDCConfig: oidc(nil, true)}, + }, + expectDelta: false, + }, + { + name: "action count differs", + desired: []*svcapitypes.Action{ + {Type: aws.String("forward"), TargetGroupARN: aws.String("arn:tg/a")}, + }, + observed: []*svcapitypes.Action{ + {Type: aws.String("authenticate-oidc"), Order: aws.Int64(1), AuthenticateOIDCConfig: oidc(nil, true)}, + forwardAsReturned(2, "arn:tg/a"), + }, + expectDelta: true, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + desired := &resource{ + ko: &svcapitypes.Listener{ + Spec: svcapitypes.ListenerSpec{DefaultActions: tc.desired}, + }, + } + observed := &resource{ + ko: &svcapitypes.Listener{ + Spec: svcapitypes.ListenerSpec{DefaultActions: tc.observed}, + }, + } + + delta := newResourceDelta(desired, observed) + assert.Equal(t, tc.expectDelta, delta.DifferentAt("Spec.DefaultActions"), + "Spec.DefaultActions delta mismatch") + assert.Equal(t, tc.expectDelta, delta.DifferentAt("Spec"), + "unexpected delta outside Spec.DefaultActions: %v", delta.Differences) + }) + } +} diff --git a/pkg/resource/listener/sdk.go b/pkg/resource/listener/sdk.go index eee185f..34bd24c 100644 --- a/pkg/resource/listener/sdk.go +++ b/pkg/resource/listener/sdk.go @@ -399,159 +399,6 @@ func (rm *resourceManager) sdkCreate( } else { ko.Spec.Certificates = nil } - if elem.DefaultActions != nil { - f2 := []*svcapitypes.Action{} - for _, f2iter := range elem.DefaultActions { - f2elem := &svcapitypes.Action{} - if f2iter.AuthenticateCognitoConfig != nil { - f2elemf0 := &svcapitypes.AuthenticateCognitoActionConfig{} - if f2iter.AuthenticateCognitoConfig.AuthenticationRequestExtraParams != nil { - f2elemf0.AuthenticationRequestExtraParams = aws.StringMap(f2iter.AuthenticateCognitoConfig.AuthenticationRequestExtraParams) - } - if f2iter.AuthenticateCognitoConfig.OnUnauthenticatedRequest != "" { - f2elemf0.OnUnauthenticatedRequest = aws.String(string(f2iter.AuthenticateCognitoConfig.OnUnauthenticatedRequest)) - } - if f2iter.AuthenticateCognitoConfig.Scope != nil { - f2elemf0.Scope = f2iter.AuthenticateCognitoConfig.Scope - } - if f2iter.AuthenticateCognitoConfig.SessionCookieName != nil { - f2elemf0.SessionCookieName = f2iter.AuthenticateCognitoConfig.SessionCookieName - } - if f2iter.AuthenticateCognitoConfig.SessionTimeout != nil { - f2elemf0.SessionTimeout = f2iter.AuthenticateCognitoConfig.SessionTimeout - } - if f2iter.AuthenticateCognitoConfig.UserPoolArn != nil { - f2elemf0.UserPoolARN = f2iter.AuthenticateCognitoConfig.UserPoolArn - } - if f2iter.AuthenticateCognitoConfig.UserPoolClientId != nil { - f2elemf0.UserPoolClientID = f2iter.AuthenticateCognitoConfig.UserPoolClientId - } - if f2iter.AuthenticateCognitoConfig.UserPoolDomain != nil { - f2elemf0.UserPoolDomain = f2iter.AuthenticateCognitoConfig.UserPoolDomain - } - f2elem.AuthenticateCognitoConfig = f2elemf0 - } - if f2iter.AuthenticateOidcConfig != nil { - f2elemf1 := &svcapitypes.AuthenticateOIDCActionConfig{} - if f2iter.AuthenticateOidcConfig.AuthenticationRequestExtraParams != nil { - f2elemf1.AuthenticationRequestExtraParams = aws.StringMap(f2iter.AuthenticateOidcConfig.AuthenticationRequestExtraParams) - } - if f2iter.AuthenticateOidcConfig.AuthorizationEndpoint != nil { - f2elemf1.AuthorizationEndpoint = f2iter.AuthenticateOidcConfig.AuthorizationEndpoint - } - if f2iter.AuthenticateOidcConfig.ClientId != nil { - f2elemf1.ClientID = f2iter.AuthenticateOidcConfig.ClientId - } - if f2iter.AuthenticateOidcConfig.ClientSecret != nil { - f2elemf1.ClientSecret = f2iter.AuthenticateOidcConfig.ClientSecret - } - if f2iter.AuthenticateOidcConfig.Issuer != nil { - f2elemf1.Issuer = f2iter.AuthenticateOidcConfig.Issuer - } - if f2iter.AuthenticateOidcConfig.OnUnauthenticatedRequest != "" { - f2elemf1.OnUnauthenticatedRequest = aws.String(string(f2iter.AuthenticateOidcConfig.OnUnauthenticatedRequest)) - } - if f2iter.AuthenticateOidcConfig.Scope != nil { - f2elemf1.Scope = f2iter.AuthenticateOidcConfig.Scope - } - if f2iter.AuthenticateOidcConfig.SessionCookieName != nil { - f2elemf1.SessionCookieName = f2iter.AuthenticateOidcConfig.SessionCookieName - } - if f2iter.AuthenticateOidcConfig.SessionTimeout != nil { - f2elemf1.SessionTimeout = f2iter.AuthenticateOidcConfig.SessionTimeout - } - if f2iter.AuthenticateOidcConfig.TokenEndpoint != nil { - f2elemf1.TokenEndpoint = f2iter.AuthenticateOidcConfig.TokenEndpoint - } - if f2iter.AuthenticateOidcConfig.UseExistingClientSecret != nil { - f2elemf1.UseExistingClientSecret = f2iter.AuthenticateOidcConfig.UseExistingClientSecret - } - if f2iter.AuthenticateOidcConfig.UserInfoEndpoint != nil { - f2elemf1.UserInfoEndpoint = f2iter.AuthenticateOidcConfig.UserInfoEndpoint - } - f2elem.AuthenticateOIDCConfig = f2elemf1 - } - if f2iter.FixedResponseConfig != nil { - f2elemf2 := &svcapitypes.FixedResponseActionConfig{} - if f2iter.FixedResponseConfig.ContentType != nil { - f2elemf2.ContentType = f2iter.FixedResponseConfig.ContentType - } - if f2iter.FixedResponseConfig.MessageBody != nil { - f2elemf2.MessageBody = f2iter.FixedResponseConfig.MessageBody - } - if f2iter.FixedResponseConfig.StatusCode != nil { - f2elemf2.StatusCode = f2iter.FixedResponseConfig.StatusCode - } - f2elem.FixedResponseConfig = f2elemf2 - } - if f2iter.ForwardConfig != nil { - f2elemf3 := &svcapitypes.ForwardActionConfig{} - if f2iter.ForwardConfig.TargetGroupStickinessConfig != nil { - f2elemf3f0 := &svcapitypes.TargetGroupStickinessConfig{} - if f2iter.ForwardConfig.TargetGroupStickinessConfig.DurationSeconds != nil { - durationSecondsCopy := int64(*f2iter.ForwardConfig.TargetGroupStickinessConfig.DurationSeconds) - f2elemf3f0.DurationSeconds = &durationSecondsCopy - } - if f2iter.ForwardConfig.TargetGroupStickinessConfig.Enabled != nil { - f2elemf3f0.Enabled = f2iter.ForwardConfig.TargetGroupStickinessConfig.Enabled - } - f2elemf3.TargetGroupStickinessConfig = f2elemf3f0 - } - if f2iter.ForwardConfig.TargetGroups != nil { - f2elemf3f1 := []*svcapitypes.TargetGroupTuple{} - for _, f2elemf3f1iter := range f2iter.ForwardConfig.TargetGroups { - f2elemf3f1elem := &svcapitypes.TargetGroupTuple{} - if f2elemf3f1iter.TargetGroupArn != nil { - f2elemf3f1elem.TargetGroupARN = f2elemf3f1iter.TargetGroupArn - } - if f2elemf3f1iter.Weight != nil { - weightCopy := int64(*f2elemf3f1iter.Weight) - f2elemf3f1elem.Weight = &weightCopy - } - f2elemf3f1 = append(f2elemf3f1, f2elemf3f1elem) - } - f2elemf3.TargetGroups = f2elemf3f1 - } - f2elem.ForwardConfig = f2elemf3 - } - if f2iter.Order != nil { - orderCopy := int64(*f2iter.Order) - f2elem.Order = &orderCopy - } - if f2iter.RedirectConfig != nil { - f2elemf5 := &svcapitypes.RedirectActionConfig{} - if f2iter.RedirectConfig.Host != nil { - f2elemf5.Host = f2iter.RedirectConfig.Host - } - if f2iter.RedirectConfig.Path != nil { - f2elemf5.Path = f2iter.RedirectConfig.Path - } - if f2iter.RedirectConfig.Port != nil { - f2elemf5.Port = f2iter.RedirectConfig.Port - } - if f2iter.RedirectConfig.Protocol != nil { - f2elemf5.Protocol = f2iter.RedirectConfig.Protocol - } - if f2iter.RedirectConfig.Query != nil { - f2elemf5.Query = f2iter.RedirectConfig.Query - } - if f2iter.RedirectConfig.StatusCode != "" { - f2elemf5.StatusCode = aws.String(string(f2iter.RedirectConfig.StatusCode)) - } - f2elem.RedirectConfig = f2elemf5 - } - if f2iter.TargetGroupArn != nil { - f2elem.TargetGroupARN = f2iter.TargetGroupArn - } - if f2iter.Type != "" { - f2elem.Type = aws.String(string(f2iter.Type)) - } - f2 = append(f2, f2elem) - } - ko.Spec.DefaultActions = f2 - } else { - ko.Spec.DefaultActions = nil - } if elem.ListenerArn != nil { if ko.Status.ACKResourceMetadata == nil { ko.Status.ACKResourceMetadata = &ackv1alpha1.ResourceMetadata{} @@ -907,159 +754,6 @@ func (rm *resourceManager) sdkUpdate( } else { ko.Spec.Certificates = nil } - if elem.DefaultActions != nil { - f2 := []*svcapitypes.Action{} - for _, f2iter := range elem.DefaultActions { - f2elem := &svcapitypes.Action{} - if f2iter.AuthenticateCognitoConfig != nil { - f2elemf0 := &svcapitypes.AuthenticateCognitoActionConfig{} - if f2iter.AuthenticateCognitoConfig.AuthenticationRequestExtraParams != nil { - f2elemf0.AuthenticationRequestExtraParams = aws.StringMap(f2iter.AuthenticateCognitoConfig.AuthenticationRequestExtraParams) - } - if f2iter.AuthenticateCognitoConfig.OnUnauthenticatedRequest != "" { - f2elemf0.OnUnauthenticatedRequest = aws.String(string(f2iter.AuthenticateCognitoConfig.OnUnauthenticatedRequest)) - } - if f2iter.AuthenticateCognitoConfig.Scope != nil { - f2elemf0.Scope = f2iter.AuthenticateCognitoConfig.Scope - } - if f2iter.AuthenticateCognitoConfig.SessionCookieName != nil { - f2elemf0.SessionCookieName = f2iter.AuthenticateCognitoConfig.SessionCookieName - } - if f2iter.AuthenticateCognitoConfig.SessionTimeout != nil { - f2elemf0.SessionTimeout = f2iter.AuthenticateCognitoConfig.SessionTimeout - } - if f2iter.AuthenticateCognitoConfig.UserPoolArn != nil { - f2elemf0.UserPoolARN = f2iter.AuthenticateCognitoConfig.UserPoolArn - } - if f2iter.AuthenticateCognitoConfig.UserPoolClientId != nil { - f2elemf0.UserPoolClientID = f2iter.AuthenticateCognitoConfig.UserPoolClientId - } - if f2iter.AuthenticateCognitoConfig.UserPoolDomain != nil { - f2elemf0.UserPoolDomain = f2iter.AuthenticateCognitoConfig.UserPoolDomain - } - f2elem.AuthenticateCognitoConfig = f2elemf0 - } - if f2iter.AuthenticateOidcConfig != nil { - f2elemf1 := &svcapitypes.AuthenticateOIDCActionConfig{} - if f2iter.AuthenticateOidcConfig.AuthenticationRequestExtraParams != nil { - f2elemf1.AuthenticationRequestExtraParams = aws.StringMap(f2iter.AuthenticateOidcConfig.AuthenticationRequestExtraParams) - } - if f2iter.AuthenticateOidcConfig.AuthorizationEndpoint != nil { - f2elemf1.AuthorizationEndpoint = f2iter.AuthenticateOidcConfig.AuthorizationEndpoint - } - if f2iter.AuthenticateOidcConfig.ClientId != nil { - f2elemf1.ClientID = f2iter.AuthenticateOidcConfig.ClientId - } - if f2iter.AuthenticateOidcConfig.ClientSecret != nil { - f2elemf1.ClientSecret = f2iter.AuthenticateOidcConfig.ClientSecret - } - if f2iter.AuthenticateOidcConfig.Issuer != nil { - f2elemf1.Issuer = f2iter.AuthenticateOidcConfig.Issuer - } - if f2iter.AuthenticateOidcConfig.OnUnauthenticatedRequest != "" { - f2elemf1.OnUnauthenticatedRequest = aws.String(string(f2iter.AuthenticateOidcConfig.OnUnauthenticatedRequest)) - } - if f2iter.AuthenticateOidcConfig.Scope != nil { - f2elemf1.Scope = f2iter.AuthenticateOidcConfig.Scope - } - if f2iter.AuthenticateOidcConfig.SessionCookieName != nil { - f2elemf1.SessionCookieName = f2iter.AuthenticateOidcConfig.SessionCookieName - } - if f2iter.AuthenticateOidcConfig.SessionTimeout != nil { - f2elemf1.SessionTimeout = f2iter.AuthenticateOidcConfig.SessionTimeout - } - if f2iter.AuthenticateOidcConfig.TokenEndpoint != nil { - f2elemf1.TokenEndpoint = f2iter.AuthenticateOidcConfig.TokenEndpoint - } - if f2iter.AuthenticateOidcConfig.UseExistingClientSecret != nil { - f2elemf1.UseExistingClientSecret = f2iter.AuthenticateOidcConfig.UseExistingClientSecret - } - if f2iter.AuthenticateOidcConfig.UserInfoEndpoint != nil { - f2elemf1.UserInfoEndpoint = f2iter.AuthenticateOidcConfig.UserInfoEndpoint - } - f2elem.AuthenticateOIDCConfig = f2elemf1 - } - if f2iter.FixedResponseConfig != nil { - f2elemf2 := &svcapitypes.FixedResponseActionConfig{} - if f2iter.FixedResponseConfig.ContentType != nil { - f2elemf2.ContentType = f2iter.FixedResponseConfig.ContentType - } - if f2iter.FixedResponseConfig.MessageBody != nil { - f2elemf2.MessageBody = f2iter.FixedResponseConfig.MessageBody - } - if f2iter.FixedResponseConfig.StatusCode != nil { - f2elemf2.StatusCode = f2iter.FixedResponseConfig.StatusCode - } - f2elem.FixedResponseConfig = f2elemf2 - } - if f2iter.ForwardConfig != nil { - f2elemf3 := &svcapitypes.ForwardActionConfig{} - if f2iter.ForwardConfig.TargetGroupStickinessConfig != nil { - f2elemf3f0 := &svcapitypes.TargetGroupStickinessConfig{} - if f2iter.ForwardConfig.TargetGroupStickinessConfig.DurationSeconds != nil { - durationSecondsCopy := int64(*f2iter.ForwardConfig.TargetGroupStickinessConfig.DurationSeconds) - f2elemf3f0.DurationSeconds = &durationSecondsCopy - } - if f2iter.ForwardConfig.TargetGroupStickinessConfig.Enabled != nil { - f2elemf3f0.Enabled = f2iter.ForwardConfig.TargetGroupStickinessConfig.Enabled - } - f2elemf3.TargetGroupStickinessConfig = f2elemf3f0 - } - if f2iter.ForwardConfig.TargetGroups != nil { - f2elemf3f1 := []*svcapitypes.TargetGroupTuple{} - for _, f2elemf3f1iter := range f2iter.ForwardConfig.TargetGroups { - f2elemf3f1elem := &svcapitypes.TargetGroupTuple{} - if f2elemf3f1iter.TargetGroupArn != nil { - f2elemf3f1elem.TargetGroupARN = f2elemf3f1iter.TargetGroupArn - } - if f2elemf3f1iter.Weight != nil { - weightCopy := int64(*f2elemf3f1iter.Weight) - f2elemf3f1elem.Weight = &weightCopy - } - f2elemf3f1 = append(f2elemf3f1, f2elemf3f1elem) - } - f2elemf3.TargetGroups = f2elemf3f1 - } - f2elem.ForwardConfig = f2elemf3 - } - if f2iter.Order != nil { - orderCopy := int64(*f2iter.Order) - f2elem.Order = &orderCopy - } - if f2iter.RedirectConfig != nil { - f2elemf5 := &svcapitypes.RedirectActionConfig{} - if f2iter.RedirectConfig.Host != nil { - f2elemf5.Host = f2iter.RedirectConfig.Host - } - if f2iter.RedirectConfig.Path != nil { - f2elemf5.Path = f2iter.RedirectConfig.Path - } - if f2iter.RedirectConfig.Port != nil { - f2elemf5.Port = f2iter.RedirectConfig.Port - } - if f2iter.RedirectConfig.Protocol != nil { - f2elemf5.Protocol = f2iter.RedirectConfig.Protocol - } - if f2iter.RedirectConfig.Query != nil { - f2elemf5.Query = f2iter.RedirectConfig.Query - } - if f2iter.RedirectConfig.StatusCode != "" { - f2elemf5.StatusCode = aws.String(string(f2iter.RedirectConfig.StatusCode)) - } - f2elem.RedirectConfig = f2elemf5 - } - if f2iter.TargetGroupArn != nil { - f2elem.TargetGroupARN = f2iter.TargetGroupArn - } - if f2iter.Type != "" { - f2elem.Type = aws.String(string(f2iter.Type)) - } - f2 = append(f2, f2elem) - } - ko.Spec.DefaultActions = f2 - } else { - ko.Spec.DefaultActions = nil - } if elem.ListenerArn != nil { if ko.Status.ACKResourceMetadata == nil { ko.Status.ACKResourceMetadata = &ackv1alpha1.ResourceMetadata{}