Skip to content

Commit cb725af

Browse files
authored
Add LifecyclePolicy field to Repository (#22)
aws-controllers-k8s/community#1043 Description of changes: - Add `LifecyclePolicy` field to `Repository` resource - e2e tests for updates or creation of repositories with lifecycle policies By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent b2deedd commit cb725af

File tree

14 files changed

+227
-12
lines changed

14 files changed

+227
-12
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2021-10-27T21:20:07Z"
3-
build_hash: d2b063806d25cfcae4f2d4eb44f8e3f713b23e8e
4-
go_version: go1.15
2+
build_date: "2021-11-16T14:08:50Z"
3+
build_hash: ff5700c3e093082af2cbb53a461acb94ab1674d9
4+
go_version: go1.16.4
55
version: v0.15.2
6-
api_directory_checksum: 3c829828db77ef587929da78f41d52b2459fb054
6+
api_directory_checksum: aa86a638fe9bbdb47b2f0cad0e82abafcff8571e
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.38.35
99
generator_config_info:
10-
file_checksum: b271840866f91cb16dbad00532c85716620cbbee
10+
file_checksum: 746fd9f662f685e64ba766c60ac6c5bb5eea7524
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/generator.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ resources:
77
from:
88
operation: CreateRepository
99
path: RepositoryName
10+
LifecyclePolicy:
11+
from:
12+
operation: PutLifecyclePolicy
13+
path: LifecyclePolicyText
1014
renames:
1115
operations:
1216
CreateRepository:
@@ -25,5 +29,8 @@ resources:
2529
list_operation:
2630
match_fields:
2731
- Name
32+
hooks:
33+
sdk_read_many_post_set_output:
34+
template_path: hooks/repository/sdk_read_many_post_set_output.go.tpl
2835
update_operation:
29-
custom_method_name: customUpdateRepository
36+
custom_method_name: customUpdateRepository

apis/v1alpha1/repository.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apis/v1alpha1/zz_generated.deepcopy.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.

config/crd/bases/ecr.services.k8s.aws_repositories.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ spec:
6262
all image tags within the repository will be immutable which will
6363
prevent them from being overwritten.
6464
type: string
65+
lifecyclePolicy:
66+
description: The JSON repository policy text to apply to the repository.
67+
type: string
6568
name:
6669
description: The name to use for the repository. The repository name
6770
may be specified on its own (such as nginx-web-app) or it can be

generator.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ resources:
77
from:
88
operation: CreateRepository
99
path: RepositoryName
10+
LifecyclePolicy:
11+
from:
12+
operation: PutLifecyclePolicy
13+
path: LifecyclePolicyText
1014
renames:
1115
operations:
1216
CreateRepository:
@@ -25,5 +29,8 @@ resources:
2529
list_operation:
2630
match_fields:
2731
- Name
32+
hooks:
33+
sdk_read_many_post_set_output:
34+
template_path: hooks/repository/sdk_read_many_post_set_output.go.tpl
2835
update_operation:
29-
custom_method_name: customUpdateRepository
36+
custom_method_name: customUpdateRepository

helm/crds/ecr.services.k8s.aws_repositories.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ spec:
6262
all image tags within the repository will be immutable which will
6363
prevent them from being overwritten.
6464
type: string
65+
lifecyclePolicy:
66+
description: The JSON repository policy text to apply to the repository.
67+
type: string
6568
name:
6669
description: The name to use for the repository. The repository name
6770
may be specified on its own (such as nginx-web-app) or it can be

pkg/resource/repository/custom_update_api.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ func (rm *resourceManager) customUpdateRepository(
6666
return nil, err
6767
}
6868
}
69+
if delta.DifferentAt("Spec.LifecyclePolicy") {
70+
updated, err = rm.updateLifecyclePolicy(ctx, updated)
71+
if err != nil {
72+
return nil, err
73+
}
74+
}
6975
return updated, nil
7076
}
7177

@@ -131,3 +137,61 @@ func (rm *resourceManager) updateImageTagMutability(
131137
}
132138
return desired, nil
133139
}
140+
141+
// updateLifecyclePolicy calls the PutLifecyclePolicy ECR API call for a
142+
// specific repository
143+
func (rm *resourceManager) updateLifecyclePolicy(
144+
ctx context.Context,
145+
desired *resource,
146+
) (*resource, error) {
147+
var err error
148+
rlog := ackrtlog.FromContext(ctx)
149+
exit := rlog.Trace("rm.updateRepositoryLifecyclePolicy")
150+
defer exit(err)
151+
152+
dspec := desired.ko.Spec
153+
dstatus := desired.ko.Status
154+
155+
if dspec.LifecyclePolicy == nil || *dspec.LifecyclePolicy == "" {
156+
return rm.deleteLifecyclePolicy(ctx, desired)
157+
}
158+
159+
input := &svcsdk.PutLifecyclePolicyInput{
160+
RepositoryName: aws.String(*dspec.Name),
161+
RegistryId: aws.String(*dstatus.RegistryID),
162+
LifecyclePolicyText: aws.String(*dspec.LifecyclePolicy),
163+
}
164+
165+
_, err = rm.sdkapi.PutLifecyclePolicyWithContext(ctx, input)
166+
rm.metrics.RecordAPICall("UPDATE", "PutLifecyclePolicy", err)
167+
if err != nil {
168+
return nil, err
169+
}
170+
return desired, nil
171+
}
172+
173+
// deleteLifecyclePolicy calls the DeleteLifecyclePolicy ECR API call for a
174+
// specific repository
175+
func (rm *resourceManager) deleteLifecyclePolicy(
176+
ctx context.Context,
177+
desired *resource,
178+
) (*resource, error) {
179+
var err error
180+
rlog := ackrtlog.FromContext(ctx)
181+
exit := rlog.Trace("rm.deleteLifecyclePolicy")
182+
defer exit(err)
183+
184+
dspec := desired.ko.Spec
185+
dstatus := desired.ko.Status
186+
input := &svcsdk.DeleteLifecyclePolicyInput{
187+
RepositoryName: aws.String(*dspec.Name),
188+
RegistryId: aws.String(*dstatus.RegistryID),
189+
}
190+
191+
_, err = rm.sdkapi.DeleteLifecyclePolicyWithContext(ctx, input)
192+
rm.metrics.RecordAPICall("DELETE", "DeleteLifecyclePolicy", err)
193+
if err != nil {
194+
return nil, err
195+
}
196+
return desired, nil
197+
}

pkg/resource/repository/delta.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/resource/repository/hook.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package repository
2+
3+
import (
4+
"context"
5+
6+
svcapitypes "github.com/aws-controllers-k8s/ecr-controller/apis/v1alpha1"
7+
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
8+
svcsdk "github.com/aws/aws-sdk-go/service/ecr"
9+
)
10+
11+
// setResourceAdditionalFields will describe the fields that are not return by
12+
// DescribeRepository calls
13+
func (rm *resourceManager) setResourceAdditionalFields(
14+
ctx context.Context,
15+
r *resource,
16+
ko *svcapitypes.Repository,
17+
) (err error) {
18+
rlog := ackrtlog.FromContext(ctx)
19+
exit := rlog.Trace("rm.setResourceAdditionalFields")
20+
defer exit(err)
21+
22+
getLifecyclePolicyResponse, err := rm.sdkapi.GetLifecyclePolicyWithContext(
23+
ctx,
24+
&svcsdk.GetLifecyclePolicyInput{
25+
RepositoryName: r.ko.Spec.Name,
26+
RegistryId: r.ko.Status.RegistryID,
27+
},
28+
)
29+
rm.metrics.RecordAPICall("GET", "GetLifecyclePolicy", err)
30+
ko.Spec.LifecyclePolicy = getLifecyclePolicyResponse.LifecyclePolicyText
31+
return nil
32+
}

0 commit comments

Comments
 (0)