Skip to content

Commit 18985be

Browse files
committed
chore: put logic in syncAllowedPrincipals func
1 parent 407e5ae commit 18985be

File tree

4 files changed

+91
-132
lines changed

4 files changed

+91
-132
lines changed

apis/v1alpha1/ack-generate-metadata.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
ack_generate_info:
2-
build_date: "2023-12-29T16:40:07Z"
2+
build_date: "2023-12-29T17:15:23Z"
33
build_hash: 994d9abdb629dd34b9c5afe4db42b05ff0eca9f1
44
go_version: go1.21.5
55
version: 994d9ab
6-
api_directory_checksum: 6575949feb243b8f23c4ae3006e6a0a12b8bfa4f
6+
api_directory_checksum: 8b27f9e65dbad1f5f825c84d1dbe8fd333baf2a5
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.44.93
99
generator_config_info:

pkg/resource/vpc_endpoint_service_configuration/hooks.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,87 @@ func addIDToDeleteRequest(r *resource,
4545
return nil
4646
}
4747

48+
// syncAllowedPrincipals adds & removes allowed principals with the 'ModifyVpcEndpointServicePermissions' API call
49+
func (rm *resourceManager) syncAllowedPrincipals(
50+
ctx context.Context,
51+
desired *resource,
52+
latest *resource,
53+
) (updated *resource, err error) {
54+
rlog := ackrtlog.FromContext(ctx)
55+
exit := rlog.Trace("updateAllowedPrincipals")
56+
defer func(err error) {
57+
exit(err)
58+
}(err)
59+
60+
var listOfPrincipalsToAdd []*string
61+
var listOfPrincipalsToRemove []*string
62+
63+
// If the latest list of principals is empty, we want to add all principals
64+
if len(latest.ko.Spec.AllowedPrincipals) == 0 && len(desired.ko.Spec.AllowedPrincipals) > 0 {
65+
listOfPrincipalsToAdd = desired.ko.Spec.AllowedPrincipals
66+
67+
// If the desired list of principals is empty, we want to remove all principals
68+
} else if len(desired.ko.Spec.AllowedPrincipals) == 0 && len(latest.ko.Spec.AllowedPrincipals) > 0 {
69+
listOfPrincipalsToRemove = latest.ko.Spec.AllowedPrincipals
70+
// Otherwise, we'll compare the two lists and add/remove principals as needed
71+
} else {
72+
// Add any 'desired' principal that is not on the allowed list
73+
for _, desiredPrincipal := range desired.ko.Spec.AllowedPrincipals {
74+
principalToAddAlreadyFound := false
75+
for _, latestPrincipal := range latest.ko.Spec.AllowedPrincipals {
76+
if *desiredPrincipal == *latestPrincipal {
77+
// Principal already in Allow List, skip
78+
principalToAddAlreadyFound = true
79+
break
80+
}
81+
}
82+
if !principalToAddAlreadyFound {
83+
// Desired Principal is not in the Allowed List, add it to the list of those to add
84+
listOfPrincipalsToAdd = append(listOfPrincipalsToAdd, desiredPrincipal)
85+
}
86+
}
87+
88+
// Remove any 'latest' principal that is not on the allowed list anymore
89+
for _, latestPrincipal := range latest.ko.Spec.AllowedPrincipals {
90+
principalToRemoveAlreadyFound := false
91+
for _, desiredPrincipal := range desired.ko.Spec.AllowedPrincipals {
92+
if *desiredPrincipal == *latestPrincipal {
93+
// Principal still in Allow List, skip
94+
principalToRemoveAlreadyFound = true
95+
break
96+
}
97+
}
98+
if !principalToRemoveAlreadyFound {
99+
// Latest Principal is not in the Allowed List, add it to the list of those to remove
100+
listOfPrincipalsToRemove = append(listOfPrincipalsToRemove, latestPrincipal)
101+
}
102+
}
103+
104+
}
105+
106+
// Make the AWS API call to update the allowed principals
107+
if len(listOfPrincipalsToAdd) > 0 || len(listOfPrincipalsToRemove) > 0 {
108+
modifyPermissionsInput := &svcsdk.ModifyVpcEndpointServicePermissionsInput{
109+
ServiceId: latest.ko.Status.ServiceID,
110+
}
111+
112+
if len(listOfPrincipalsToAdd) > 0 {
113+
modifyPermissionsInput.AddAllowedPrincipals = listOfPrincipalsToAdd
114+
}
115+
116+
if len(listOfPrincipalsToRemove) > 0 {
117+
modifyPermissionsInput.RemoveAllowedPrincipals = listOfPrincipalsToRemove
118+
}
119+
120+
_, err := rm.sdkapi.ModifyVpcEndpointServicePermissions(modifyPermissionsInput)
121+
rm.metrics.RecordAPICall("UPDATE", "ModifyVpcEndpointServicePermissions", err)
122+
if err != nil {
123+
return desired, err
124+
}
125+
}
126+
return desired, nil
127+
}
128+
48129
// syncTags used to keep tags in sync by calling Create and Delete API's
49130
func (rm *resourceManager) syncTags(
50131
ctx context.Context,

pkg/resource/vpc_endpoint_service_configuration/sdk.go

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

templates/hooks/vpc_endpoint_service_configuration/sdk_update_pre_build_request.go.tpl

Lines changed: 4 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -13,71 +13,10 @@
1313
}
1414

1515
if delta.DifferentAt("Spec.AllowedPrincipals") {
16-
var listOfPrincipalsToAdd []*string
17-
var listOfPrincipalsToRemove []*string
18-
19-
// If the latest list of principals is empty, we want to add all principals
20-
if len(latest.ko.Spec.AllowedPrincipals) == 0 && len(desired.ko.Spec.AllowedPrincipals) > 0 {
21-
listOfPrincipalsToAdd = desired.ko.Spec.AllowedPrincipals
22-
23-
// If the desired list of principals is empty, we want to remove all principals
24-
} else if len(desired.ko.Spec.AllowedPrincipals) == 0 && len(latest.ko.Spec.AllowedPrincipals) > 0 {
25-
listOfPrincipalsToRemove = latest.ko.Spec.AllowedPrincipals
26-
// Otherwise, we'll compare the two lists and add/remove principals as needed
27-
} else {
28-
// Add any 'desired' principal that is not on the allowed list
29-
for _, desiredPrincipal := range desired.ko.Spec.AllowedPrincipals {
30-
principalToAddAlreadyFound := false
31-
for _, latestPrincipal := range latest.ko.Spec.AllowedPrincipals {
32-
if *desiredPrincipal == *latestPrincipal {
33-
// Principal already in Allow List, skip
34-
principalToAddAlreadyFound = true
35-
break
36-
}
37-
}
38-
if !principalToAddAlreadyFound {
39-
// Desired Principal is not in the Allowed List, add it to the list of those to add
40-
listOfPrincipalsToAdd = append(listOfPrincipalsToAdd, desiredPrincipal)
41-
}
42-
}
43-
44-
// Remove any 'latest' principal that is not on the allowed list anymore
45-
for _, latestPrincipal := range latest.ko.Spec.AllowedPrincipals {
46-
principalToRemoveAlreadyFound := false
47-
for _, desiredPrincipal := range desired.ko.Spec.AllowedPrincipals {
48-
if *desiredPrincipal == *latestPrincipal {
49-
// Principal still in Allow List, skip
50-
principalToRemoveAlreadyFound = true
51-
break
52-
}
53-
}
54-
if !principalToRemoveAlreadyFound {
55-
// Latest Principal is not in the Allowed List, add it to the list of those to remove
56-
listOfPrincipalsToRemove = append(listOfPrincipalsToRemove, latestPrincipal)
57-
}
58-
}
59-
60-
}
61-
62-
// Make the AWS API call to update the allowed principals
63-
if len(listOfPrincipalsToAdd) > 0 || len(listOfPrincipalsToRemove) > 0 {
64-
modifyPermissionsInput := &svcsdk.ModifyVpcEndpointServicePermissionsInput{
65-
ServiceId: latest.ko.Status.ServiceID,
66-
}
67-
68-
if len(listOfPrincipalsToAdd) > 0 {
69-
modifyPermissionsInput.AddAllowedPrincipals = listOfPrincipalsToAdd
70-
}
71-
72-
if len(listOfPrincipalsToRemove) > 0 {
73-
modifyPermissionsInput.RemoveAllowedPrincipals = listOfPrincipalsToRemove
74-
}
75-
76-
_, err := rm.sdkapi.ModifyVpcEndpointServicePermissions(modifyPermissionsInput)
77-
rm.metrics.RecordAPICall("UPDATE", "ModifyVpcEndpointServicePermissions", err)
78-
if err != nil {
79-
return desired, err
80-
}
16+
if desired, err := rm.syncAllowedPrincipals(ctx, desired, latest); err != nil {
17+
// This causes a requeue and the rest of the fields will be synced on the next reconciliation loop
18+
ackcondition.SetSynced(desired, corev1.ConditionFalse, nil, nil)
19+
return desired, err
8120
}
8221
}
8322

0 commit comments

Comments
 (0)