Skip to content

Commit ee84f66

Browse files
committed
Add auto-publish functionality for Function resources
This patch introduces the ability for CF Functions to be automatically published after a successful create or update operation. It adds an annotation `cloudfront.services.k8s.aws/auto-publish` to the `Function` resource, which when set to `true`, will trigger the automatic publishing of the Function after a create or update. Signed-off-by: Amine Hilaly <[email protected]>
1 parent c417342 commit ee84f66

File tree

11 files changed

+157
-13
lines changed

11 files changed

+157
-13
lines changed
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2024-03-06T21:22:30Z"
3-
build_hash: a5ba3c851434263128a1464a2c41e528779eeefa
4-
go_version: go1.22.0
5-
version: v0.32.1
6-
api_directory_checksum: 86a18c0bfcc849ad234f64249fd8951ce418dd14
2+
build_date: "2024-03-23T14:30:00Z"
3+
build_hash: d86061f5f579fe5d3a07528917d95e34e79c4dc0
4+
go_version: go1.22.1
5+
version: v0.32.1-1-gd86061f
6+
api_directory_checksum: 27fd205fbbd08711210430b8e19ed65ab91b5d32
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.44.214
99
generator_config_info:
10-
file_checksum: 250151346bb9c7a0bcd8cdcdb77f482f2eee0d03
10+
file_checksum: 9b125128dc96ab9b6c92fb4d368a0562a3f549fb
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/annotation.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package v1alpha1
15+
16+
import "fmt"
17+
18+
var (
19+
// AutoPublishAnnotation is the annotation key for the auto-publish annotation
20+
// for CloudFront functions. This annotation is used to indicate whether a
21+
// function should be automatically published after a successful update or create
22+
// operation.
23+
AutoPublishAnnotation = fmt.Sprintf("%s/auto-publish", GroupVersion.Group)
24+
)

apis/v1alpha1/generator.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ resources:
213213
hooks:
214214
sdk_read_one_post_set_output:
215215
template_path: hooks/function/sdk_read_one_post_set_output.go.tpl
216+
sdk_create_post_set_output:
217+
template_path: hooks/function/sdk_create_post_set_output.go.tpl
218+
sdk_update_post_set_output:
219+
template_path: hooks/function/sdk_update_post_set_output.go.tpl
216220
OriginRequestPolicy:
217221
tags:
218222
ignore: true

generator.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ resources:
213213
hooks:
214214
sdk_read_one_post_set_output:
215215
template_path: hooks/function/sdk_read_one_post_set_output.go.tpl
216+
sdk_create_post_set_output:
217+
template_path: hooks/function/sdk_create_post_set_output.go.tpl
218+
sdk_update_post_set_output:
219+
template_path: hooks/function/sdk_update_post_set_output.go.tpl
216220
OriginRequestPolicy:
217221
tags:
218222
ignore: true

pkg/resource/function/hooks.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package function
1515

1616
import (
1717
"context"
18+
"strconv"
1819

1920
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
2021
svcsdk "github.com/aws/aws-sdk-go/service/cloudfront"
@@ -57,3 +58,40 @@ func (rm *resourceManager) setFunctionCode(ctx context.Context, r *v1alpha1.Func
5758
r.Spec.FunctionCode = []byte(output.FunctionCode)
5859
return nil
5960
}
61+
62+
// publishFunction publishes the a CloudFront function.
63+
func (rm *resourceManager) publishFunction(ctx context.Context, r *v1alpha1.Function) (err error) {
64+
rlog := ackrtlog.FromContext(ctx)
65+
exit := rlog.Trace("rm.publishFunction")
66+
defer func() { exit(err) }()
67+
68+
_, err = rm.sdkapi.PublishFunctionWithContext(
69+
ctx,
70+
&svcsdk.PublishFunctionInput{
71+
Name: r.Spec.Name,
72+
IfMatch: r.Status.ETag,
73+
},
74+
)
75+
rm.metrics.RecordAPICall("POST", "PublishFunction", err)
76+
if err != nil {
77+
return err
78+
}
79+
return nil
80+
}
81+
82+
// functionAutoPublishEnabled returns true if the function should be
83+
// automatically published after a successful update or create operation.
84+
func functionAutoPublishEnabled(f *v1alpha1.Function) bool {
85+
annotations := f.ObjectMeta.GetAnnotations()
86+
if annotations == nil {
87+
return false
88+
}
89+
90+
autoPublish, ok := annotations[v1alpha1.AutoPublishAnnotation]
91+
if ok {
92+
return autoPublish == strconv.FormatBool(true)
93+
}
94+
95+
// By default we do not auto-publish functions.
96+
return false
97+
}

pkg/resource/function/sdk.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if functionAutoPublishEnabled(ko) {
2+
if err := rm.publishFunction(ctx, ko); err != nil {
3+
return nil, err
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if functionAutoPublishEnabled(ko) {
2+
if err := rm.publishFunction(ctx, ko); err != nil {
3+
return nil, err
4+
}
5+
}

test/e2e/conftest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
import boto3
1515
import pytest
16-
1716
from acktest import k8s
1817

18+
1919
def pytest_addoption(parser):
2020
parser.addoption("--runslow", action="store_true", default=False, help="run slow tests")
2121

@@ -26,6 +26,9 @@ def pytest_configure(config):
2626
config.addinivalue_line(
2727
"markers", "slow: mark test as slow to run"
2828
)
29+
config.addinivalue_line(
30+
"markers", "function_overrides: mark test with function overrides"
31+
)
2932

3033
def pytest_collection_modifyitems(config, items):
3134
if config.getoption("--runslow"):

test/e2e/resources/function.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ apiVersion: cloudfront.services.k8s.aws/v1alpha1
22
kind: Function
33
metadata:
44
name: $FUNCTION_NAME
5+
annotations:
6+
cloudfront.services.k8s.aws/auto-publish: "$AUTO_PUBLISH"
57
spec:
68
name: $FUNCTION_NAME
79
functionCode: ZnVuY3Rpb24gaGFuZGxlcihldmVudCkgewogIHJldHVybiB7CiAgICBzdGF0dXNDb2RlOiAzMDIsCiAgICBzdGF0dXNEZXNjcmlwdGlvbjogJ0ZvdW5kJywKICAgIGhlYWRlcnM6IHsKICAgICAgbG9jYXRpb246IHsgdmFsdWU6ICdodHRwczovLy8nIH0KICAgIH0KICB9Cn0K
810
functionConfig:
911
comment: function to redirect to $DOMAIN_NAME
10-
runtime: $FUNCTION_RUNTIME
12+
runtime: $FUNCTION_RUNTIME

0 commit comments

Comments
 (0)