|
| 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 | + ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log" |
| 22 | + svcsdk "github.com/aws/aws-sdk-go/service/lambda" |
| 23 | +) |
| 24 | + |
| 25 | +// customPreDelete deletes all the previous versions of a |
| 26 | +// LayerVersion except the latest version |
| 27 | +// This function is used as a sdk_delete hook, to delete all the previous versions of a LayerVersion when delete API call is made |
| 28 | +func customPreDelete( |
| 29 | + r *resource, |
| 30 | + rm *resourceManager, |
| 31 | + ctx context.Context, |
| 32 | +) error { |
| 33 | + // Getting the list of all the versions of a LayerVersion |
| 34 | + input := &svcsdk.ListLayerVersionsInput{ |
| 35 | + LayerName: r.ko.Spec.LayerName, |
| 36 | + } |
| 37 | + response, err := rm.sdkapi.ListLayerVersionsWithContext(ctx, input) |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + |
| 42 | + log := ackrtlog.FromContext(ctx) |
| 43 | + log.Debug("Deleting previous versions of LayerVersion") |
| 44 | + |
| 45 | + // The above API call returns output containing list of versions as LayerVersions and a pagination token as NextMarker |
| 46 | + |
| 47 | + // Extracting the list of versions and assigning it to a new variable |
| 48 | + versionList := response.LayerVersions |
| 49 | + |
| 50 | + // sorting the list in ascending order |
| 51 | + sort.Slice(versionList, func(i, j int) bool { |
| 52 | + return *versionList[i].Version < *versionList[j].Version |
| 53 | + }) |
| 54 | + |
| 55 | + for i := 0; i < len(versionList)-1; i++ { |
| 56 | + input := &svcsdk.DeleteLayerVersionInput{ |
| 57 | + LayerName: r.ko.Spec.LayerName, |
| 58 | + VersionNumber: versionList[i].Version, |
| 59 | + } |
| 60 | + // Delete API call to delete the versions one by one |
| 61 | + logMessage := fmt.Sprintf("Deleting version %v of %v", *input.VersionNumber, *input.LayerName) |
| 62 | + log.Debug(logMessage) |
| 63 | + |
| 64 | + _, err = rm.sdkapi.DeleteLayerVersionWithContext(ctx, input) |
| 65 | + rm.metrics.RecordAPICall("DELETE", "DeleteLayerVersion", err) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return nil |
| 72 | +} |
0 commit comments