Skip to content

Commit 6d61fbc

Browse files
authored
Support security groups with cyclic references (#213)
Issue #, if available: aws-controllers-k8s/community#2119 Description of changes: Cyclic references support is done via. the following workflow: 1. skip runtime reference state validations by setting `SecurityGroup.Rules.UserIDGroupPairs.GroupID.skip_resource_state_validations: true` (see aws-controllers-k8s/code-generator#544). This allows runtime to proceed with the `sdkCreate` call. 2. inside `sdkCreate` and `sdkUpdate` add custom logic that checks whether referenced security groups are being created on AWS end (i.e. `groupID != nil`). If the checks succeed, move forward with syncing SG rules. Otherwise, requeue and wait for all referenced SGs to be created. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 740dfa1 commit 6d61fbc

File tree

11 files changed

+191
-22
lines changed

11 files changed

+191
-22
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: "2024-08-29T17:15:01Z"
2+
build_date: "2024-08-29T20:21:49Z"
33
build_hash: f8f98563404066ac3340db0a049d2e530e5c51cc
44
go_version: go1.22.5
55
version: v0.38.1
66
api_directory_checksum: 1b53401670898ce50e6d6cc8bfba6b63ea7d5683
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.44.93
99
generator_config_info:
10-
file_checksum: ff3f54d44dba872977fef4f23c5f766a1bebbbc2
10+
file_checksum: b6cf44fddbe38dd354160538b750818e10bda45c
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
@@ -528,6 +528,7 @@ resources:
528528
references:
529529
resource: SecurityGroup
530530
path: Status.ID
531+
skip_resource_state_validations: true
531532
is_required: false
532533
renames:
533534
operations:
@@ -553,6 +554,8 @@ resources:
553554
template_path: hooks/security_group/sdk_create_post_set_output.go.tpl
554555
sdk_read_many_post_set_output:
555556
template_path: hooks/security_group/sdk_read_many_post_set_output.go.tpl
557+
sdk_delete_pre_build_request:
558+
template_path: hooks/security_group/sdk_delete_pre_build_request.go.tpl
556559
update_operation:
557560
custom_method_name: customUpdateSecurityGroup
558561
NetworkAcl:

generator.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ resources:
528528
references:
529529
resource: SecurityGroup
530530
path: Status.ID
531+
skip_resource_state_validations: true
531532
is_required: false
532533
renames:
533534
operations:
@@ -553,6 +554,8 @@ resources:
553554
template_path: hooks/security_group/sdk_create_post_set_output.go.tpl
554555
sdk_read_many_post_set_output:
555556
template_path: hooks/security_group/sdk_read_many_post_set_output.go.tpl
557+
sdk_delete_pre_build_request:
558+
template_path: hooks/security_group/sdk_delete_pre_build_request.go.tpl
556559
update_operation:
557560
custom_method_name: customUpdateSecurityGroup
558561
NetworkAcl:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module github.com/aws-controllers-k8s/ec2-controller
22

33
go 1.22.0
44

5-
toolchain go1.22.5
5+
toolchain go1.22.6
66

77
require (
88
github.com/aws-controllers-k8s/runtime v0.38.0

pkg/resource/security_group/hooks.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ import (
1717
"context"
1818

1919
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
20+
ackcondition "github.com/aws-controllers-k8s/runtime/pkg/condition"
2021
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
2122
awserr "github.com/aws/aws-sdk-go/aws/awserr"
2223
svcsdk "github.com/aws/aws-sdk-go/service/ec2"
24+
corev1 "k8s.io/api/core/v1"
2325

2426
svcapitypes "github.com/aws-controllers-k8s/ec2-controller/apis/v1alpha1"
2527
"github.com/aws-controllers-k8s/ec2-controller/pkg/tags"
@@ -102,6 +104,29 @@ func (rm *resourceManager) requiredFieldsMissingForSGRule(
102104
return r.ko.Status.ID == nil
103105
}
104106

107+
// referencesResolved checks that any referenced security group actually exists in AWS, before proceeding with syncSGRules.
108+
// This is required because Rules.UserIDGroupPairs.GroupID.skip_resource_state_validations is set to true,
109+
// meaning that any state validations performed at runtime, during ResolveReferences step, are being skipped.
110+
func (rm *resourceManager) referencesResolved(
111+
r *resource,
112+
) bool {
113+
for _, rule := range r.ko.Spec.IngressRules {
114+
for _, groupPair := range rule.UserIDGroupPairs {
115+
if groupPair.GroupRef != nil && groupPair.GroupID == nil {
116+
return false
117+
}
118+
}
119+
}
120+
for _, rule := range r.ko.Spec.EgressRules {
121+
for _, groupPair := range rule.UserIDGroupPairs {
122+
if groupPair.GroupRef != nil && groupPair.GroupID == nil {
123+
return false
124+
}
125+
}
126+
}
127+
return true
128+
}
129+
105130
// syncSGRules analyzes desired and latest (if any)
106131
// resources and executes API calls to Create/Delete
107132
// rules in order to achieve desired state.
@@ -357,6 +382,11 @@ func (rm *resourceManager) customUpdateSecurityGroup(
357382
updated = rm.concreteResource(desired.DeepCopy())
358383

359384
if delta.DifferentAt("Spec.IngressRules") || delta.DifferentAt("Spec.EgressRules") {
385+
if !rm.referencesResolved(desired) {
386+
ackcondition.SetSynced(latest, corev1.ConditionFalse, nil, nil)
387+
return latest, nil
388+
}
389+
360390
if err := rm.syncSGRules(ctx, desired, latest); err != nil {
361391
return nil, err
362392
}

pkg/resource/security_group/references.go

Lines changed: 1 addition & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/resource/security_group/sdk.go

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

templates/hooks/security_group/sdk_create_post_set_output.go.tpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
return &resource{ko}, err
99
}
1010

11+
if !rm.referencesResolved(&resource{ko}) {
12+
ackcondition.SetSynced(&resource{ko}, corev1.ConditionFalse, nil, nil)
13+
return &resource{ko}, nil
14+
}
15+
1116
if err = rm.syncSGRules(ctx, &resource{ko}, nil); err != nil {
1217
return &resource{ko}, err
1318
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
sgCpy := r.ko.DeepCopy()
2+
sgCpy.Spec.IngressRules = nil
3+
sgCpy.Spec.EgressRules = nil
4+
if err := rm.syncSGRules(ctx, &resource{ko: sgCpy}, r); err != nil {
5+
return nil, err
6+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: ec2.services.k8s.aws/v1alpha1
2+
kind: SecurityGroup
3+
metadata:
4+
name: $SECURITY_GROUP_NAME
5+
spec:
6+
name: $SECURITY_GROUP_NAME
7+
description: test sg
8+
vpcID: $VPC_ID
9+
ingressRules:
10+
- fromPort: 443
11+
toPort: 443
12+
ipProtocol: tcp
13+
userIDGroupPairs:
14+
- description: test UID group pair
15+
groupRef:
16+
from:
17+
name: $SECURITY_GROUP_REF_NAME

0 commit comments

Comments
 (0)