@@ -2,6 +2,7 @@ package controllers
22
33import (
44 "context"
5+ "fmt"
56 "time"
67
78 anv1alpha1 "github.com/aws/aws-application-networking-k8s/pkg/apis/applicationnetworking/v1alpha1"
@@ -12,9 +13,12 @@ import (
1213 "github.com/aws/aws-application-networking-k8s/pkg/utils"
1314 "github.com/aws/aws-application-networking-k8s/pkg/utils/gwlog"
1415
16+ "k8s.io/apimachinery/pkg/api/meta"
17+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1518 ctrl "sigs.k8s.io/controller-runtime"
1619 "sigs.k8s.io/controller-runtime/pkg/client"
1720 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
21+ gwv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
1822)
1923
2024type IAMAuthPolicyController struct {
@@ -43,6 +47,12 @@ func (c *IAMAuthPolicyController) Reconcile(ctx context.Context, req ctrl.Reques
4347 }
4448 c .log .Infow ("reconcile" , "req" , req , "targetRef" , k8sPolicy .Spec .TargetRef )
4549
50+ err = c .handleConflicts (ctx , k8sPolicy )
51+ if err != nil {
52+ c .log .Error (err )
53+ return ctrl.Result {RequeueAfter : 30 * time .Second }, nil
54+ }
55+
4656 c .handleFinalizer (ctx , k8sPolicy )
4757
4858 isDelete := ! k8sPolicy .DeletionTimestamp .IsZero ()
@@ -68,11 +78,11 @@ func (c *IAMAuthPolicyController) Reconcile(ctx context.Context, req ctrl.Reques
6878
6979 latticeResourceId , err := reconcileFunc (ctx , k8sPolicy )
7080 if err != nil {
71- if services .IsNotFoundError (err ) {
72- c .log .Infof ("reconcile error, retry in 30sec: %s" , err )
81+ // ignore Sn/Svc not found when deleting iam policy
82+ if ! (isDelete && services .IsNotFoundError (err )) {
83+ c .log .Infof ("reconcile error, retry in 30 sec: %s" , err )
7384 return ctrl.Result {RequeueAfter : time .Second * 30 }, nil
7485 }
75- return ctrl.Result {}, err
7686 }
7787
7888 k8sPolicy .Annotations ["application-networking.k8s.aws/resourceId" ] = latticeResourceId
@@ -196,3 +206,56 @@ func (c *IAMAuthPolicyController) putPolicy(ctx context.Context, resId, policy s
196206 _ , err := c .policyMgr .Put (ctx , modelPolicy )
197207 return err
198208}
209+
210+ func (c * IAMAuthPolicyController ) handleConflicts (ctx context.Context , k8sPolicy * anv1alpha1.IAMAuthPolicy ) error {
211+ if ! k8sPolicy .DeletionTimestamp .IsZero () {
212+ return nil
213+ }
214+ conflictingPolicies , err := c .findConflictingPolicies (ctx , k8sPolicy )
215+ if err != nil {
216+ return err
217+ }
218+ if len (conflictingPolicies ) > 0 {
219+ c .updatePolicyCondition (ctx , k8sPolicy , gwv1alpha2 .PolicyReasonConflicted )
220+ return fmt .Errorf ("conflict with other policies for same TargetRef, policy: %s, conflicted with: %v" ,
221+ k8sPolicy .Name , conflictingPolicies )
222+ }
223+ return nil
224+ }
225+
226+ func (c * IAMAuthPolicyController ) findConflictingPolicies (ctx context.Context , k8sPolicy * anv1alpha1.IAMAuthPolicy ) ([]string , error ) {
227+ var out []string
228+ policies := & anv1alpha1.IAMAuthPolicyList {}
229+ err := c .client .List (ctx , policies , & client.ListOptions {
230+ Namespace : k8sPolicy .Namespace ,
231+ })
232+ if err != nil {
233+ return out , err
234+ }
235+ for _ , p := range policies .Items {
236+ if k8sPolicy .Name == p .Name {
237+ continue
238+ }
239+ if * k8sPolicy .Spec .TargetRef == * p .Spec .TargetRef {
240+ out = append (out , p .Name )
241+ }
242+ }
243+ return out , nil
244+ }
245+
246+ func (c IAMAuthPolicyController ) updatePolicyCondition (ctx context.Context , k8sPolicy * anv1alpha1.IAMAuthPolicy , reason gwv1alpha2.PolicyConditionReason ) error {
247+ status := metav1 .ConditionTrue
248+ if reason != gwv1alpha2 .PolicyReasonAccepted {
249+ status = metav1 .ConditionFalse
250+ }
251+ cnd := metav1.Condition {
252+ Type : string (gwv1alpha2 .PolicyConditionAccepted ),
253+ Status : status ,
254+ ObservedGeneration : k8sPolicy .Generation ,
255+ LastTransitionTime : metav1 .Now (),
256+ Reason : string (reason ),
257+ }
258+ meta .SetStatusCondition (& k8sPolicy .Status .Conditions , cnd )
259+ err := c .client .Status ().Update (ctx , k8sPolicy )
260+ return err
261+ }
0 commit comments