@@ -29,6 +29,7 @@ import (
2929 log "github.com/sirupsen/logrus"
3030 v1 "k8s.io/api/core/v1"
3131 apierrs "k8s.io/apimachinery/pkg/api/errors"
32+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3233 ctrl "sigs.k8s.io/controller-runtime"
3334 "sigs.k8s.io/controller-runtime/pkg/client"
3435 "sigs.k8s.io/controller-runtime/pkg/event"
@@ -44,6 +45,7 @@ import (
4445type HarborSyncConfigReconciler struct {
4546 client.Client
4647 RotationInterval time.Duration
48+ RequeueInterval time.Duration
4749 CredCache reconciler.CredentialStore
4850 Harbor harbor.API
4951}
@@ -66,8 +68,22 @@ func (r *HarborSyncConfigReconciler) Reconcile(ctx context.Context, req ctrl.Req
6668 return ctrl.Result {}, nil
6769 }
6870 log .Error (err , "unable to fetch sync config" )
69- return ctrl.Result {Requeue : true , RequeueAfter : time .Second * 15 }, err
71+ return ctrl.Result {RequeueAfter : time .Second * 15 }, err
7072 }
73+
74+ defer func () {
75+ err := r .Status ().Update (context .Background (), & syncConfig )
76+ if err != nil {
77+ log .Errorf ("unable to update status: %s" , err .Error ())
78+ }
79+ }()
80+
81+ // return early if cr has been updated recently
82+ if ! shouldReconcile (syncConfig ) {
83+ log .Infof ("skipping reconciliation" )
84+ return ctrl.Result {RequeueAfter : r .RequeueInterval }, nil
85+ }
86+
7187 // mappingFunc calls the Kubernetes-specific mapping functions
7288 mappingFunc := func (
7389 mapping crdv1.ProjectMapping ,
@@ -83,12 +99,8 @@ func (r *HarborSyncConfigReconciler) Reconcile(ctx context.Context, req ctrl.Req
8399 err = f (r , mapping , syncConfig , project , * credential , baseURL )
84100 if err != nil {
85101 c := NewSyncCondition (crdv1 .HarborSyncReady , v1 .ConditionFalse , "Mapping failed" , err .Error ())
86- SetSyncCondition (& syncConfig .Status , * c )
87- err = r .Status ().Update (context .Background (), & syncConfig )
88- if err != nil {
89- log .Errorf ("unable to update status mapping func: %s" , err .Error ())
90- }
91102 log .Error (err , "mapping failed" )
103+ SetSyncCondition (& syncConfig .Status , * c )
92104 return
93105 }
94106 }
@@ -97,18 +109,11 @@ func (r *HarborSyncConfigReconciler) Reconcile(ctx context.Context, req ctrl.Req
97109 log .Error (err )
98110 c := NewSyncCondition (crdv1 .HarborSyncReady , v1 .ConditionFalse , "Error Reconciling" , err .Error ())
99111 SetSyncCondition (& syncConfig .Status , * c )
100- err = r .Status ().Update (context .Background (), & syncConfig )
101- if err != nil {
102- log .Errorf ("unable to update status after reconcile error: %s" , err .Error ())
103- }
104- return ctrl.Result {Requeue : true , RequeueAfter : time .Second * 30 }, nil
112+ return ctrl.Result {RequeueAfter : time .Second * 30 }, nil
105113 }
106114 c := NewSyncCondition (crdv1 .HarborSyncReady , v1 .ConditionTrue , "Successfully reconciled" , "Successfully reconciled" )
115+ syncConfig .Status .LastReconciliation = metav1 .Now ()
107116 SetSyncCondition (& syncConfig .Status , * c )
108- err = r .Status ().Update (context .Background (), & syncConfig )
109- if err != nil {
110- log .Errorf ("unable to update status after reconcile: %s" , err .Error ())
111- }
112117 log .Info ("successfully reconciled" )
113118 return ctrl.Result {}, nil
114119}
@@ -153,6 +158,9 @@ func Reconcile(
153158 selector .ProjectName ,
154159 ).Set (float64 (len (matches )))
155160
161+ // reset projectList
162+ cfg .Status .ProjectList = []crdv1.ProjectStatus {}
163+
156164 // reconcile robot accounts
157165 for _ , project := range matches {
158166 credential , changed , err := reconciler .ReconcileRobotAccounts (
@@ -259,3 +267,11 @@ func runWebhook(
259267 }
260268 return fmt .Errorf ("webhook errors: %#v" , errs )
261269}
270+
271+ func shouldReconcile (syncConfig crdv1.HarborSync ) bool {
272+ cmp := syncConfig .Status .LastReconciliation .Time .Add (time .Minute )
273+ if cmp .After (time .Now ()) {
274+ return false
275+ }
276+ return true
277+ }
0 commit comments