Skip to content

Commit 3f71658

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 3f71658

File tree

10 files changed

+146
-12
lines changed

10 files changed

+146
-12
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:14:11Z"
3+
build_hash: d86061f5f579fe5d3a07528917d95e34e79c4dc0
4+
go_version: go1.22.1
5+
version: v0.32.1-1-gd86061f-dirty
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: 37 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,39 @@ 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+
autoPublish, ok := annotations[v1alpha1.AutoPublishAnnotation]
90+
if ok {
91+
return autoPublish == strconv.FormatBool(true)
92+
}
93+
94+
// By default we do not auto-publish functions.
95+
return false
96+
}

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/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

test/e2e/tests/test_function.py

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616
import time
1717

1818
import pytest
19-
19+
from acktest.aws import identity
2020
from acktest.k8s import condition
2121
from acktest.k8s import resource as k8s
22-
from acktest.aws import identity
2322
from acktest.resources import random_suffix_name
24-
from e2e import service_marker, CRD_GROUP, CRD_VERSION, load_resource
23+
from e2e import CRD_GROUP, CRD_VERSION, function, load_resource, service_marker
2524
from e2e.bootstrap_resources import get_bootstrap_resources
2625
from e2e.replacement_values import REPLACEMENT_VALUES
27-
from e2e import function
2826

2927
FUNCTIONS_RESOURCE_PLURAL = "functions"
3028
DELETE_WAIT_AFTER_SECONDS = 10
@@ -33,7 +31,7 @@
3331

3432

3533
@pytest.fixture(scope="module")
36-
def simple_function():
34+
def simple_function(request):
3735
function_name = random_suffix_name("my-function", 24)
3836

3937
# resources = get_bootstrap_resources()
@@ -46,6 +44,16 @@ def simple_function():
4644
replacements['FUNCTION_RUNTIME'] = "cloudfront-js-2.0"
4745
replacements['DOMAIN_NAME'] = "example.com"
4846

47+
auto_publish = "false"
48+
marker = request.node.get_closest_marker("function_overrides")
49+
if marker is not None:
50+
data = marker.args[0]
51+
if 'auto-publish' in data:
52+
if data['auto-publish'] == "true":
53+
auto_publish = "true"
54+
55+
replacements['AUTO_PUBLISH'] = auto_publish
56+
4957
resource_data = load_resource(
5058
"function",
5159
additional_replacements=replacements,
@@ -107,4 +115,39 @@ def test_crud(self, simple_function):
107115

108116
latest = function.get(function_name)
109117
assert latest is not None
118+
assert latest['Status'] == "UNPUBLISHED"
110119
assert latest['FunctionConfig']['Comment'] == "New comment"
120+
121+
@pytest.mark.function_overrides({
122+
'auto-publish': True,
123+
})
124+
def test_auto_publish(self, simple_function):
125+
ref, res, function_name = simple_function
126+
127+
time.sleep(CHECK_STATUS_WAIT_SECONDS)
128+
129+
# Check that function exists
130+
cr = k8s.get_resource(ref)
131+
assert cr is not None
132+
133+
condition.assert_synced(ref)
134+
135+
latest = function.get(function_name)
136+
assert latest is not None
137+
assert latest['Status'] == "PUBLISHED"
138+
139+
updates = {
140+
"spec": {
141+
"functionConfig": {
142+
"comment": "New comment"
143+
}
144+
},
145+
}
146+
k8s.patch_custom_resource(ref, updates)
147+
time.sleep(MODIFY_WAIT_AFTER_SECONDS)
148+
149+
latest = function.get(function_name)
150+
assert latest is not None
151+
assert latest['FunctionConfig']['Comment'] == "New comment"
152+
assert latest['Status'] == "PUBLISHED"
153+

0 commit comments

Comments
 (0)