Skip to content

Commit abe7bda

Browse files
Fix default egress rule deletion logic in case of an egress rule definition (#123)
**Issue:** aws-controllers-k8s/community#1604 **Description of changes:** If the user defines an egress rule which is the same as the “default“ egress rule (defined implicitly by AWS), then the ec2-controller raises the below error. Although the SG is created in AWS, the controller is unable to sync the resource. ```txt 2023-01-26T16:19:39.988Z DEBUG ackrt <<<<< rm.syncSGRules {"account": "647927084307", "role": "", "region": "eu-west-1", "kind": "SecurityGroup", "namespace": "ack-system", "name": "ack-sg-test", "is_adopted": false, "generation": 1} 2023-01-26T16:19:39.988Z DEBUG ackrt <<<< rm.sdkCreate {"account": "647927084307", "role": "", "region": "eu-west-1", "kind": "SecurityGroup", "namespace": "ack-system", "name": "ack-sg-test", "is_adopted": false, "generation": 1, "error": "InvalidPermission.Duplicate: the specified rule \"peer: 0.0.0.0/0, ALL, ALLOW\" already exists\n\tstatus code: 400, request id: 160a5101-48d5-4de3-afc9-7ff6d7b99f16"} 2023-01-26T16:19:39.988Z DEBUG ackrt <<< rm.Create {"account": "647927084307", "role": "", "region": "eu-west-1", "kind": "SecurityGroup", "namespace": "ack-system", "name": "ack-sg-test", "is_adopted": false, "generation": 1, "error": "InvalidPermission.Duplicate: the specified rule \"peer: 0.0.0.0/0, ALL, ALLOW\" already exists\n\tstatus code: 400, request id: 160a5101-48d5-4de3-afc9-7ff6d7b99f16"} 2023-01-26T16:19:39.988Z DEBUG ackrt << r.createResource {"account": "647927084307", "role": "", "region": "eu-west-1", "kind": "SecurityGroup", "namespace": "ack-system", "name": "ack-sg-test", "is_adopted": false, "generation": 1, "error": "InvalidPermission.Duplicate: the specified rule \"peer: 0.0.0.0/0, ALL, ALLOW\" already exists\n\tstatus code: 400, request id: 160a5101-48d5-4de3-afc9-7ff6d7b99f16"} ``` The change in this PR just changes the order of the flow executing the deletion of the “default“ egress rule in case an egress rule is defined by the user. Because the `syncSGRules` during creation does not really sync the SG rules as it does not have a `latest` state during performing the sync, the “default“ egress rule hangs there and basically causes the issue. **Note:** I am not sure whether this case requires a specific e2e test case; the current `test_rules_create_update_delete` case covers the situation where only an ingress rule is defined and then it only patches the egress rule definition which does not 100% cover this situation, because this particular case only pops up during the creation step where the default egress rule check occurs. In order to cover it, the egress rule should be defined from the beginning on I suppose. Just wanted to share my limited perspective 😄 By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent c027a79 commit abe7bda

File tree

10 files changed

+42
-56
lines changed

10 files changed

+42
-56
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-10-13T20:54:09Z"
2+
build_date: "2023-02-01T15:41:09Z"
33
build_hash: 5ee0ac052c54f008dff50f6f5ebb73f2cf3a0bd7
44
go_version: go1.18.1
55
version: v0.20.1-4-g5ee0ac0
66
api_directory_checksum: b3a2878ca8a156389214b900257c4d572ad4e3a5
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.44.93
99
generator_config_info:
10-
file_checksum: cc2c6590c6e77a6125d5eec82ff5f693109d4f99
10+
file_checksum: d9d0156fc1156be66ef8542caa31686764629ad7
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/generator.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ resources:
419419
custom_field:
420420
list_of: IpPermission
421421
EgressRules:
422+
late_initialize: {}
422423
custom_field:
423424
list_of: IpPermission
424425
Rules:

config/crd/common/bases/services.k8s.aws_adoptedresources.yaml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
21
---
32
apiVersion: apiextensions.k8s.io/v1
43
kind: CustomResourceDefinition
54
metadata:
65
annotations:
7-
controller-gen.kubebuilder.io/version: v0.7.0
6+
controller-gen.kubebuilder.io/version: v0.9.2
87
creationTimestamp: null
98
name: adoptedresources.services.k8s.aws
109
spec:
@@ -170,6 +169,7 @@ spec:
170169
- name
171170
- uid
172171
type: object
172+
x-kubernetes-map-type: atomic
173173
type: array
174174
type: object
175175
required:
@@ -224,9 +224,3 @@ spec:
224224
storage: true
225225
subresources:
226226
status: {}
227-
status:
228-
acceptedNames:
229-
kind: ""
230-
plural: ""
231-
conditions: []
232-
storedVersions: []

config/crd/common/bases/services.k8s.aws_fieldexports.yaml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
21
---
32
apiVersion: apiextensions.k8s.io/v1
43
kind: CustomResourceDefinition
54
metadata:
65
annotations:
7-
controller-gen.kubebuilder.io/version: v0.7.0
6+
controller-gen.kubebuilder.io/version: v0.9.2
87
creationTimestamp: null
98
name: fieldexports.services.k8s.aws
109
spec:
@@ -133,9 +132,3 @@ spec:
133132
storage: true
134133
subresources:
135134
status: {}
136-
status:
137-
acceptedNames:
138-
kind: ""
139-
plural: ""
140-
conditions: []
141-
storedVersions: []

generator.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ resources:
419419
custom_field:
420420
list_of: IpPermission
421421
EgressRules:
422+
late_initialize: {}
422423
custom_field:
423424
list_of: IpPermission
424425
Rules:

pkg/resource/security_group/hooks.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
2222
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
23+
awserr "github.com/aws/aws-sdk-go/aws/awserr"
2324
svcsdk "github.com/aws/aws-sdk-go/service/ec2"
2425
)
2526

@@ -353,6 +354,12 @@ func (rm *resourceManager) deleteDefaultSecurityGroupRule(
353354
_, err = rm.sdkapi.RevokeSecurityGroupEgressWithContext(ctx, req)
354355
rm.metrics.RecordAPICall("DELETE", "RevokeSecurityGroupEgress", err)
355356
if err != nil {
357+
if aerr, ok := err.(awserr.Error); ok {
358+
switch aerr.Code() {
359+
case "InvalidPermission.NotFound":
360+
return
361+
}
362+
}
356363
return err
357364
}
358365

@@ -463,18 +470,6 @@ func compareTags(
463470
}
464471
}
465472

466-
// defaultEgressRule returns the egress rule that
467-
// is created and associated with a security group by default
468-
func (rm *resourceManager) defaultEgressRule() *svcapitypes.IPPermission {
469-
defaultRule := &svcapitypes.IPPermission{
470-
IPRanges: []*svcapitypes.IPRange{{CIDRIP: toStrPtr("0.0.0.0/0")}},
471-
FromPort: toInt64Ptr(-1),
472-
IPProtocol: toStrPtr("-1"),
473-
ToPort: toInt64Ptr(-1),
474-
}
475-
return defaultRule
476-
}
477-
478473
// containsRule returns true if security group rule
479474
// is found in the rule collection (all fields must match);
480475
// otherwise, return false.

pkg/resource/security_group/manager.go

Lines changed: 11 additions & 2 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: 8 additions & 11 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: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
if rm.requiredFieldsMissingForSGRule(&resource{ko}) {
33
return nil, ackerr.NotFound
44
}
5+
6+
// if user defines any egress rule, then remove the default egress rule
7+
if len(desired.ko.Spec.EgressRules) > 0 {
8+
if err = rm.deleteDefaultSecurityGroupRule(ctx, &resource{ko}); err != nil {
9+
return nil, err
10+
}
11+
}
12+
513
if err = rm.syncSGRules(ctx, &resource{ko}, nil); err != nil {
614
return nil, err
715
}
@@ -14,14 +22,3 @@
1422
} else {
1523
ko.Status.Rules = rules
1624
}
17-
18-
// if user defines any egress rule, then remove the default
19-
// egress rule; otherwise, add default rule Spec to align with
20-
// resource's server-side state (i.e. Status.Rules)
21-
if len(desired.ko.Spec.EgressRules) > 0 {
22-
if err = rm.deleteDefaultSecurityGroupRule(ctx, &resource{ko}); err != nil {
23-
return nil, err
24-
}
25-
} else {
26-
ko.Spec.EgressRules = append(ko.Spec.EgressRules, rm.defaultEgressRule())
27-
}

test/e2e/tests/test_security_group.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
@pytest.fixture
3737
def simple_security_group(request):
38-
resource_name = random_suffix_name("security-group-tes", 24)
38+
resource_name = random_suffix_name("security-group-test", 24)
3939
resource_file = "security_group"
4040
test_vpc = get_bootstrap_resources().SharedTestVPC
4141

@@ -159,7 +159,6 @@ def test_create_delete(self, ec2_client, simple_security_group):
159159
# Check Security Group no longer exists in AWS
160160
ec2_validator.assert_security_group(resource_id, exists=False)
161161

162-
@pytest.mark.xfail
163162
def test_create_with_vpc_egress_dups_default_delete(self, ec2_client, security_group_with_vpc):
164163
(ref, cr) = security_group_with_vpc
165164
resource_id = cr["status"]["id"]

0 commit comments

Comments
 (0)