Skip to content

Commit e16d479

Browse files
authored
fix: add a Describe call with filters before readOne if ID not populated (#280)
fixes aws-controllers-k8s/community#2561 Description of changes: Controller assumes a security group does not exist if the `Status.ID` is empty. During adoption, users might want to adopt the resource by its name, as the ID is not trivial to guess (the security group ID is generated by AWS). These changes are adding one API call only when the `Status.ID` is empty. Here we would make a single `DescribeSecurityGroups` call filtering by `name` and `VPC ID`, and populate the resource ID, to be used in subsequent API calls. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent af3c5dc commit e16d479

File tree

7 files changed

+71
-4
lines changed

7 files changed

+71
-4
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2025-08-28T17:59:00Z"
3-
build_hash: 1d9076d0211773ff8ab8682b28b912c7ece10676
2+
build_date: "2025-09-11T20:48:05Z"
3+
build_hash: 9e29f017d9e942548af133d2f31aecae248a8816
44
go_version: go1.25.0
5-
version: v0.51.0-2-g1d9076d
5+
version: v0.51.0-3-g9e29f01
66
api_directory_checksum: b32f97274be98ca3f4cf5cbf559258210c872946
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.32.6
99
generator_config_info:
10-
file_checksum: d5e0ce1661bd55bd3c0a8c4316de6885c8039ea0
10+
file_checksum: 381d3f31a88cd00e07717b8957ffb5141218130a
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/generator.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ resources:
750750
template_path: hooks/security_group/sdk_read_many_post_set_output.go.tpl
751751
sdk_delete_pre_build_request:
752752
template_path: hooks/security_group/sdk_delete_pre_build_request.go.tpl
753+
sdk_read_many_pre_build_request:
754+
template_path: hooks/security_group/sdk_read_many_pre_build_request.go.tpl
753755
update_operation:
754756
custom_method_name: customUpdateSecurityGroup
755757
NetworkAcl:

generator.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ resources:
750750
template_path: hooks/security_group/sdk_read_many_post_set_output.go.tpl
751751
sdk_delete_pre_build_request:
752752
template_path: hooks/security_group/sdk_delete_pre_build_request.go.tpl
753+
sdk_read_many_pre_build_request:
754+
template_path: hooks/security_group/sdk_read_many_pre_build_request.go.tpl
753755
update_operation:
754756
custom_method_name: customUpdateSecurityGroup
755757
NetworkAcl:

helm/templates/deployment.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ metadata:
1010
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
1111
k8s-app: {{ include "ack-ec2-controller.app.name" . }}
1212
helm.sh/chart: {{ include "ack-ec2-controller.chart.name-version" . }}
13+
{{- range $key, $value := .Values.deployment.labels }}
14+
{{ $key }}: {{ $value | quote }}
15+
{{- end }}
1316
spec:
1417
replicas: {{ .Values.deployment.replicas }}
1518
selector:

pkg/resource/security_group/hooks.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,3 +515,45 @@ func toStrPtr(str string) *string {
515515
func toInt64Ptr(integer int64) *int64 {
516516
return &integer
517517
}
518+
519+
func (rm *resourceManager) getSecurityGroupID(
520+
ctx context.Context,
521+
r *resource,
522+
) (id *string, err error) {
523+
rlog := ackrtlog.FromContext(ctx)
524+
exit := rlog.Trace("rm.getSecurityGroupID")
525+
defer func() {
526+
exit(err)
527+
}()
528+
529+
// Both name and VPC ID are required for safe lookup
530+
if r.ko.Spec.Name == nil || r.ko.Spec.VPCID == nil {
531+
return nil, nil
532+
}
533+
534+
// Build filters for name and VPC ID
535+
filters := []svcsdktypes.Filter{
536+
{
537+
Name: aws.String("group-name"),
538+
Values: []string{*r.ko.Spec.Name},
539+
},
540+
{
541+
Name: aws.String("vpc-id"),
542+
Values: []string{*r.ko.Spec.VPCID},
543+
},
544+
}
545+
546+
resp, err := rm.sdkapi.DescribeSecurityGroups(ctx, &svcsdk.DescribeSecurityGroupsInput{
547+
Filters: filters,
548+
})
549+
if err != nil {
550+
return nil, err
551+
}
552+
553+
if resp == nil || len(resp.SecurityGroups) == 0 {
554+
return nil, nil
555+
}
556+
557+
// Security group names are unique within a VPC, so there should be exactly one match
558+
return resp.SecurityGroups[0].GroupId, nil
559+
}

pkg/resource/security_group/sdk.go

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
if rm.requiredFieldsMissingFromReadManyInput(r) {
2+
id, err := rm.getSecurityGroupID(ctx, r)
3+
if err != nil {
4+
return nil, err
5+
}
6+
if id != nil {
7+
r.ko.Status.ID = id
8+
}
9+
}

0 commit comments

Comments
 (0)