Skip to content

Commit

Permalink
Merge pull request #1298 from ezgidemirel/fix-cf-rhp-ref
Browse files Browse the repository at this point in the history
fix(Cloudfront): LateInit missing fields
  • Loading branch information
ulucinar authored May 11, 2022
2 parents bd6130b + 22f7401 commit aab494e
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 44 deletions.
1 change: 1 addition & 0 deletions apis/cloudfront/generator-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ignore:
- Headers.Quantity
- QueryStringCacheKeys.Quantity
- LambdaFunctionAssociations.Quantity
- FunctionAssociations.Quantity
- TrustedKeyGroups.Quantity
- TrustedSigners.Quantity
- CacheBehaviors.Quantity
Expand Down
5 changes: 0 additions & 5 deletions apis/cloudfront/v1alpha1/zz_generated.deepcopy.go

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

2 changes: 0 additions & 2 deletions apis/cloudfront/v1alpha1/zz_types.go

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

12 changes: 0 additions & 12 deletions package/crds/cloudfront.aws.crossplane.io_distributions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,6 @@ spec:
type: string
type: object
type: array
quantity:
format: int64
type: integer
type: object
lambdaFunctionAssociations:
description: "A complex type that specifies a list
Expand Down Expand Up @@ -469,9 +466,6 @@ spec:
type: string
type: object
type: array
quantity:
format: int64
type: integer
type: object
lambdaFunctionAssociations:
description: "A complex type that specifies a list of
Expand Down Expand Up @@ -1130,9 +1124,6 @@ spec:
type: string
type: object
type: array
quantity:
format: int64
type: integer
type: object
lambdaFunctionAssociations:
description: "A complex type that specifies
Expand Down Expand Up @@ -1384,9 +1375,6 @@ spec:
type: string
type: object
type: array
quantity:
format: int64
type: integer
type: object
lambdaFunctionAssociations:
description: "A complex type that specifies a list
Expand Down
57 changes: 56 additions & 1 deletion pkg/controller/cloudfront/distribution/lateinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ func lateInitDefaultCacheBehavior(in *svcapitypes.DefaultCacheBehavior, from *sv
}
}

if from.FunctionAssociations != nil {
if in.FunctionAssociations == nil {
in.FunctionAssociations = &svcapitypes.FunctionAssociations{}
}
lateInitFunctionAssociations(in.FunctionAssociations, from.FunctionAssociations)
}

if from.LambdaFunctionAssociations != nil {
if in.LambdaFunctionAssociations == nil {
in.LambdaFunctionAssociations = &svcapitypes.LambdaFunctionAssociations{}
Expand All @@ -226,6 +233,7 @@ func lateInitDefaultCacheBehavior(in *svcapitypes.DefaultCacheBehavior, from *sv
in.OriginRequestPolicyID = awsclients.LateInitializeStringPtr(in.OriginRequestPolicyID, from.OriginRequestPolicyId)
in.RealtimeLogConfigARN = awsclients.LateInitializeStringPtr(in.RealtimeLogConfigARN, from.RealtimeLogConfigArn)
in.SmoothStreaming = awsclients.LateInitializeBoolPtr(in.SmoothStreaming, from.SmoothStreaming)
in.ResponseHeadersPolicyID = awsclients.LateInitializeStringPtr(in.ResponseHeadersPolicyID, from.ResponseHeadersPolicyId)
in.TargetOriginID = awsclients.LateInitializeStringPtr(in.TargetOriginID, from.TargetOriginId)

if from.TrustedKeyGroups != nil {
Expand Down Expand Up @@ -370,6 +378,7 @@ func lateInitCacheBehavior(in *svcapitypes.CacheBehavior, from *svcsdk.CacheBeha
in.RealtimeLogConfigARN = awsclients.LateInitializeStringPtr(in.RealtimeLogConfigARN, from.RealtimeLogConfigArn)
in.SmoothStreaming = awsclients.LateInitializeBoolPtr(in.SmoothStreaming, from.SmoothStreaming)
in.TargetOriginID = awsclients.LateInitializeStringPtr(in.TargetOriginID, from.TargetOriginId)
in.ResponseHeadersPolicyID = awsclients.LateInitializeStringPtr(in.ResponseHeadersPolicyID, from.ResponseHeadersPolicyId)

if from.TrustedKeyGroups != nil && *from.TrustedKeyGroups.Enabled && len(from.TrustedKeyGroups.Items) != 0 {
if in.TrustedKeyGroups == nil {
Expand Down Expand Up @@ -696,14 +705,14 @@ func lateInitLambdaFunctionAssociations(in *svcapitypes.LambdaFunctionAssociatio

return
}

// If we have some lambda function associations, we need to late init each one of them
existing := make(map[string]*svcsdk.LambdaFunctionAssociation)
for i := range from.Items {
o := from.Items[i]
if o.LambdaFunctionARN == nil {
continue
}
// TODO(ezgidemirel): Instead of using FunctionARNs, we should use EventTypes as keys
// LambdaFunctionARN must be unique for each LambdaFunctionAssociation
existing[awsclients.StringValue(o.LambdaFunctionARN)] = o
}
Expand All @@ -728,3 +737,49 @@ func lateInitLambdaFunctionAssociation(in *svcapitypes.LambdaFunctionAssociation
in.IncludeBody = awsclients.LateInitializeBoolPtr(in.IncludeBody, from.IncludeBody)
in.LambdaFunctionARN = awsclients.LateInitializeStringPtr(in.LambdaFunctionARN, from.LambdaFunctionARN)
}

func lateInitFunctionAssociations(in *svcapitypes.FunctionAssociations, from *svcsdk.FunctionAssociations) {
if len(from.Items) == 0 {
return
}

// If we have no function associations, late init the entire slice
if in.Items == nil {
in.Items = make([]*svcapitypes.FunctionAssociation, len(from.Items))
for i := range from.Items {
in.Items[i] = &svcapitypes.FunctionAssociation{}
lateInitFunctionAssociation(in.Items[i], from.Items[i])
}

return
}

// If we have some function associations, we need to late init each one of them
existing := make(map[string]*svcsdk.FunctionAssociation)
for _, o := range from.Items {
if o.EventType == nil {
continue
}
// AWS Console allows us to set a single FunctionARN for each predefined EventType
existing[awsclients.StringValue(o.EventType)] = o
}

for _, il := range in.Items {
// If EventType is not nil, we want to use the value coming from the input
if il.EventType != nil {
continue
}

fl := existing[awsclients.StringValue(il.EventType)]
if fl == nil {
continue
}

lateInitFunctionAssociation(il, fl)
}
}

func lateInitFunctionAssociation(in *svcapitypes.FunctionAssociation, from *svcsdk.FunctionAssociation) {
in.EventType = awsclients.LateInitializeStringPtr(in.EventType, from.EventType)
in.FunctionARN = awsclients.LateInitializeStringPtr(in.FunctionARN, from.FunctionARN)
}
20 changes: 20 additions & 0 deletions pkg/controller/cloudfront/distribution/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ func preCreate(_ context.Context, cr *svcapitypes.Distribution, cdi *svcsdk.Crea
}
}

if dcb.FunctionAssociations != nil {
cdi.DistributionConfig.DefaultCacheBehavior.FunctionAssociations.Quantity =
awsclients.Int64(len(dcb.FunctionAssociations.Items), 0)
}

if dcb.LambdaFunctionAssociations != nil {
cdi.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations.Quantity =
awsclients.Int64(len(dcb.LambdaFunctionAssociations.Items), 0)
Expand Down Expand Up @@ -189,6 +194,11 @@ func preCreate(_ context.Context, cr *svcapitypes.Distribution, cdi *svcsdk.Crea
}
}

if cbi.FunctionAssociations != nil {
cdi.DistributionConfig.CacheBehaviors.Items[i].FunctionAssociations.Quantity =
awsclients.Int64(len(cbi.FunctionAssociations.Items), 0)
}

if cbi.LambdaFunctionAssociations != nil {
cdi.DistributionConfig.CacheBehaviors.Items[i].LambdaFunctionAssociations.Quantity =
awsclients.Int64(len(cbi.LambdaFunctionAssociations.Items), 0)
Expand Down Expand Up @@ -374,6 +384,11 @@ func preUpdate(_ context.Context, cr *svcapitypes.Distribution, udi *svcsdk.Upda
awsclients.Int64(len(dcb.ForwardedValues.QueryStringCacheKeys.Items), 0)
}
}
if dcb.FunctionAssociations != nil {
udi.DistributionConfig.DefaultCacheBehavior.FunctionAssociations.Quantity =
awsclients.Int64(len(dcb.FunctionAssociations.Items), 0)
}

if dcb.LambdaFunctionAssociations != nil {
udi.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations.Quantity =
awsclients.Int64(len(dcb.LambdaFunctionAssociations.Items), 0)
Expand Down Expand Up @@ -422,6 +437,11 @@ func preUpdate(_ context.Context, cr *svcapitypes.Distribution, udi *svcsdk.Upda
}
}

if cbi.FunctionAssociations != nil {
udi.DistributionConfig.CacheBehaviors.Items[i].FunctionAssociations.Quantity =
awsclients.Int64(len(cbi.FunctionAssociations.Items), 0)
}

if cbi.LambdaFunctionAssociations != nil {
udi.DistributionConfig.CacheBehaviors.Items[i].LambdaFunctionAssociations.Quantity =
awsclients.Int64(len(cbi.LambdaFunctionAssociations.Items), 0)
Expand Down
6 changes: 0 additions & 6 deletions pkg/controller/cloudfront/distribution/zz_controller.go

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

18 changes: 0 additions & 18 deletions pkg/controller/cloudfront/distribution/zz_conversions.go

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

0 comments on commit aab494e

Please sign in to comment.