Skip to content

Commit 5115fa4

Browse files
committed
Adding delete all layer versions functionality for layer_version resource
1 parent 2771d6a commit 5115fa4

File tree

7 files changed

+110
-2
lines changed

7 files changed

+110
-2
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2022-12-13T23:07:54Z"
2+
build_date: "2023-01-05T21:17:54Z"
33
build_hash: 12246c7da82841b351ec7a9e1f139f9338f2784b
44
go_version: go1.19
55
version: v0.20.1-14-g12246c7
66
api_directory_checksum: 90c9337a64415662f698cf6cd270706cd560bd16
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.44.93
99
generator_config_info:
10-
file_checksum: e007d88ecf6ad1c45cb6451f5fda9401af9d7305
10+
file_checksum: e3b0b7baf90c92126b784c7147765f29847c303b
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/generator.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ resources:
153153
operation: ReadOne
154154
tags:
155155
ignore: true
156+
hooks:
157+
sdk_delete_pre_build_request:
158+
template_path: hooks/layerversion/sdk_delete_pre_build_request.go.tpl
156159
renames:
157160
operations:
158161
GetLayerVersion:

generator.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ resources:
153153
operation: ReadOne
154154
tags:
155155
ignore: true
156+
hooks:
157+
sdk_delete_pre_build_request:
158+
template_path: hooks/layerversion/sdk_delete_pre_build_request.go.tpl
156159
renames:
157160
operations:
158161
GetLayerVersion:

pkg/resource/layer_version/hooks.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 layer_version
15+
16+
import (
17+
"context"
18+
"fmt"
19+
"sort"
20+
21+
// "github.com/Jeffail/gabs/v2"
22+
svcapitypes "github.com/aws-controllers-k8s/lambda-controller/apis/v1alpha1"
23+
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
24+
svcsdk "github.com/aws/aws-sdk-go/service/lambda"
25+
)
26+
27+
var (
28+
_ = &svcsdk.Lambda{}
29+
_ = &svcapitypes.LayerVersion{}
30+
)
31+
32+
// customPreDelete deletes all the previous versions of a
33+
//LayerVersion except the latest version
34+
35+
// This function is used as a sdk_delete hook, to delete
36+
// all the previous versions of a LayerVersion when delete API call is made
37+
func customPreDelete(
38+
r *resource,
39+
rm *resourceManager,
40+
ctx context.Context,
41+
) error {
42+
var response *svcsdk.ListLayerVersionsOutput
43+
var err error
44+
var version_list []*svcsdk.LayerVersionsListItem
45+
var totalVersion int
46+
var _ *svcsdk.DeleteLayerVersionOutput
47+
48+
// Getting the list of all the versions of a LayerVersion
49+
input := &svcsdk.ListLayerVersionsInput{
50+
LayerName: r.ko.Spec.LayerName,
51+
}
52+
response, err = rm.sdkapi.ListLayerVersionsWithContext(ctx, input)
53+
if err != nil {
54+
return err
55+
}
56+
57+
log := ackrtlog.FromContext(ctx)
58+
log.Info("Deleting previous versions of LayerVersion")
59+
// The above API call returns output containing list of versions as
60+
//LayerVersions and a pagination token as NextMarker
61+
62+
// Extracting the list of versions and assigning it to a new variable
63+
version_list = response.LayerVersions
64+
totalVersion = len(version_list) // Number of total versions
65+
66+
// sorting the list in ascending order
67+
sort.Slice(version_list, func(i, j int) bool {
68+
return *version_list[i].Version < *version_list[j].Version
69+
})
70+
71+
for i := 0; i < (totalVersion - 1); i++ {
72+
input := &svcsdk.DeleteLayerVersionInput{
73+
LayerName: r.ko.Spec.LayerName,
74+
VersionNumber: version_list[i].Version,
75+
}
76+
// Delete API call to delete the versions one by one
77+
// // log := ackrtlog.FromContext(ctx)
78+
logMessage := fmt.Sprintf("Deleting version %v of %v", *input.VersionNumber, *input.LayerName)
79+
log.Info(logMessage)
80+
81+
_, err = rm.sdkapi.DeleteLayerVersionWithContext(ctx, input)
82+
if err != nil {
83+
return err
84+
}
85+
}
86+
87+
return nil
88+
}

pkg/resource/layer_version/sdk.go

Lines changed: 5 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+
err = customPreDelete(r,rm,ctx)
2+
if err != nil{
3+
return nil, err
4+
}
5+

test/e2e/tests/test_layer_version.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ def test_smoke(self, lambda_client):
103103
_, deleted = k8s.delete_custom_resource(ref)
104104
assert deleted is True
105105

106+
layer_name = cr['spec']['layerName']
107+
list = lambda_validator.list_layer_version(layer_name)
108+
assert len(list) == 0
109+
106110
time.sleep(DELETE_WAIT_AFTER_SECONDS)
107111

108112
# Check layer version doesn't exist

0 commit comments

Comments
 (0)