Skip to content

Commit ecf5d51

Browse files
committed
chore: slightly refactor code to resemble iam-controller logic
1 parent 4f143d2 commit ecf5d51

File tree

1 file changed

+38
-47
lines changed
  • pkg/resource/vpc_endpoint_service_configuration

1 file changed

+38
-47
lines changed

pkg/resource/vpc_endpoint_service_configuration/hooks.go

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
ackerr "github.com/aws-controllers-k8s/runtime/pkg/errors"
2525
ackrequeue "github.com/aws-controllers-k8s/runtime/pkg/requeue"
2626
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
27+
ackutil "github.com/aws-controllers-k8s/runtime/pkg/util"
2728

2829
svcsdk "github.com/aws/aws-sdk-go/service/ec2"
2930
)
@@ -58,73 +59,63 @@ func (rm *resourceManager) syncAllowedPrincipals(
5859
exit(err)
5960
}(err)
6061

61-
var listOfPrincipalsToAdd []*string
62-
var listOfPrincipalsToRemove []*string
62+
toAdd := []*string{}
63+
toDelete := []*string{}
6364

64-
// If the latest list of principals is empty, we want to add all principals
65-
if len(latest.ko.Spec.AllowedPrincipals) == 0 && len(desired.ko.Spec.AllowedPrincipals) > 0 {
66-
listOfPrincipalsToAdd = desired.ko.Spec.AllowedPrincipals
65+
currentlyAllowedPrincipals := latest.ko.Spec.AllowedPrincipals
66+
desiredAllowedPrincipals := desired.ko.Spec.AllowedPrincipals
6767

68-
// If the desired list of principals is empty, we want to remove all principals
69-
} else if len(desired.ko.Spec.AllowedPrincipals) == 0 && len(latest.ko.Spec.AllowedPrincipals) > 0 {
70-
listOfPrincipalsToRemove = latest.ko.Spec.AllowedPrincipals
71-
// Otherwise, we'll compare the two lists and add/remove principals as needed
72-
} else {
73-
// Add any 'desired' principal that is not on the allowed list
74-
for _, desiredPrincipal := range desired.ko.Spec.AllowedPrincipals {
75-
principalToAddAlreadyFound := false
76-
for _, latestPrincipal := range latest.ko.Spec.AllowedPrincipals {
77-
if *desiredPrincipal == *latestPrincipal {
78-
// Principal already in Allow List, skip
79-
principalToAddAlreadyFound = true
80-
break
81-
}
82-
}
83-
if !principalToAddAlreadyFound {
84-
// Desired Principal is not in the Allowed List, add it to the list of those to add
85-
listOfPrincipalsToAdd = append(listOfPrincipalsToAdd, desiredPrincipal)
86-
}
68+
// Check if any desired allowed principals need to be added
69+
for _, p := range desiredAllowedPrincipals {
70+
if !ackutil.InStringPs(*p, currentlyAllowedPrincipals) {
71+
toAdd = append(toAdd, p)
8772
}
73+
}
8874

89-
// Remove any 'latest' principal that is not on the allowed list anymore
90-
for _, latestPrincipal := range latest.ko.Spec.AllowedPrincipals {
91-
principalToRemoveAlreadyFound := false
92-
for _, desiredPrincipal := range desired.ko.Spec.AllowedPrincipals {
93-
if *desiredPrincipal == *latestPrincipal {
94-
// Principal still in Allow List, skip
95-
principalToRemoveAlreadyFound = true
96-
break
97-
}
98-
}
99-
if !principalToRemoveAlreadyFound {
100-
// Latest Principal is not in the Allowed List, add it to the list of those to remove
101-
listOfPrincipalsToRemove = append(listOfPrincipalsToRemove, latestPrincipal)
102-
}
75+
// Check if any currently allowed principals need to be deleted
76+
for _, p := range currentlyAllowedPrincipals {
77+
if !ackutil.InStringPs(*p, desiredAllowedPrincipals) {
78+
toDelete = append(toDelete, p)
10379
}
80+
}
10481

82+
// Modify the allowed principals
83+
rlog.Debug("Syncing Allowed Principals", "toAdd", toAdd, "toDelete", toDelete)
84+
if err = rm.modifyAllowedPrincipals(ctx, latest, toAdd, toDelete); err != nil {
85+
return desired, err
10586
}
10687

107-
// Make the AWS API call to update the allowed principals
108-
if len(listOfPrincipalsToAdd) > 0 || len(listOfPrincipalsToRemove) > 0 {
88+
return desired, nil
89+
}
90+
91+
// Makes the AWS API call 'ModifyVpcEndpointServicePermissions' to add and/or remove the allowed principals
92+
func (rm *resourceManager) modifyAllowedPrincipals(
93+
ctx context.Context,
94+
latest *resource,
95+
toAdd []*string,
96+
toDelete []*string,
97+
) (err error) {
98+
if len(toAdd) > 0 || len(toDelete) > 0 {
10999
modifyPermissionsInput := &svcsdk.ModifyVpcEndpointServicePermissionsInput{
110100
ServiceId: latest.ko.Status.ServiceID,
111101
}
112102

113-
if len(listOfPrincipalsToAdd) > 0 {
114-
modifyPermissionsInput.AddAllowedPrincipals = listOfPrincipalsToAdd
103+
if len(toAdd) > 0 {
104+
modifyPermissionsInput.AddAllowedPrincipals = toAdd
115105
}
116106

117-
if len(listOfPrincipalsToRemove) > 0 {
118-
modifyPermissionsInput.RemoveAllowedPrincipals = listOfPrincipalsToRemove
107+
if len(toDelete) > 0 {
108+
modifyPermissionsInput.RemoveAllowedPrincipals = toDelete
119109
}
120110

121-
_, err := rm.sdkapi.ModifyVpcEndpointServicePermissions(modifyPermissionsInput)
111+
_, err := rm.sdkapi.ModifyVpcEndpointServicePermissionsWithContext(ctx, modifyPermissionsInput)
122112
rm.metrics.RecordAPICall("UPDATE", "ModifyVpcEndpointServicePermissions", err)
123113
if err != nil {
124-
return desired, err
114+
return err
125115
}
126116
}
127-
return desired, nil
117+
118+
return nil
128119
}
129120

130121
// Sets additional fields (not covered by CREATE Op) on the resource's object

0 commit comments

Comments
 (0)