@@ -45,19 +45,20 @@ var (
4545 requeueTimeoutWhenDeploymentNotFound = 10 * time .Minute
4646)
4747
48- // ApplicationHealthDeploymentReconciler reconciles an Application object
48+ // ApplicationHealthDeploymentReconciler reconciles an Application object.
49+ // It creates a deployment status when the health status is changed.
4950type ApplicationHealthDeploymentReconciler struct {
5051 client.Client
5152 Scheme * runtime.Scheme
5253 Recorder record.EventRecorder
5354 Notification notification.Client
5455}
5556
56- //+kubebuilder:rbac:groups=argoproj.io,resources=applications,verbs=get;watch;list;patch
57+ //+kubebuilder:rbac:groups=argoproj.io,resources=applications,verbs=get;watch;list
5758//+kubebuilder:rbac:groups=core,resources=configmaps,verbs=get;watch;list
59+ //+kubebuilder:rbac:groups=core,resources=events,verbs=create;patch
5860//+kubebuilder:rbac:groups=argocdcommenter.int128.github.io,resources=applicationhealths,verbs=get;list;watch;create;update;patch
5961//+kubebuilder:rbac:groups=argocdcommenter.int128.github.io,resources=applicationhealths/status,verbs=get;update;patch
60- //+kubebuilder:rbac:groups=core,resources=events,verbs=create;patch
6162
6263func (r * ApplicationHealthDeploymentReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
6364 logger := log .FromContext (ctx )
@@ -78,20 +79,19 @@ func (r *ApplicationHealthDeploymentReconciler) Reconcile(ctx context.Context, r
7879 if notification .IsNotFoundError (err ) {
7980 // Retry until the application is synced with a valid GitHub Deployment.
8081 // https://github.com/int128/argocd-commenter/issues/762
81- lastOperationAt := argocd .GetLastOperationAt (app )
82- if time .Now ().Before (lastOperationAt .Add (requeueTimeoutWhenDeploymentNotFound )) {
83- logger .Info ("retry due to deployment not found error" , "after" , requeueIntervalWhenDeploymentNotFound , "error" , err )
82+ lastOperationAt := argocd .GetLastOperationAt (app ).Time
83+ if time .Since (lastOperationAt ) < requeueTimeoutWhenDeploymentNotFound {
8484 r .Recorder .Eventf (& app , corev1 .EventTypeNormal , "DeploymentNotFound" ,
8585 "deployment %s not found, retry after %s" , deploymentURL , requeueIntervalWhenDeploymentNotFound )
8686 return ctrl.Result {RequeueAfter : requeueIntervalWhenDeploymentNotFound }, nil
8787 }
88- logger .Info ("retry timeout because last operation is too old" , "lastOperationAt" , lastOperationAt )
8988 r .Recorder .Eventf (& app , corev1 .EventTypeWarning , "DeploymentNotFoundRetryTimeout" ,
90- "deployment %s not found, retry timeout " , deploymentURL )
89+ "deployment %s not found but retry timed out " , deploymentURL )
9190 return ctrl.Result {}, nil
9291 }
9392 if deploymentIsAlreadyHealthy {
94- logger .Info ("skip notification because the deployment is already healthy" , "deployment" , deploymentURL )
93+ r .Recorder .Eventf (& app , corev1 .EventTypeNormal , "DeploymentAlreadyHealthy" ,
94+ "skip on status %s because deployment %s is already healthy" , app .Status .Health .Status , deploymentURL )
9595 return ctrl.Result {}, nil
9696 }
9797
@@ -101,11 +101,11 @@ func (r *ApplicationHealthDeploymentReconciler) Reconcile(ctx context.Context, r
101101 }
102102
103103 if err := r .Notification .CreateDeploymentStatusOnHealthChanged (ctx , app , argocdURL ); err != nil {
104- logger .Error (err , "unable to create a deployment status" )
105- r .Recorder .Eventf (& app , corev1 .EventTypeWarning , "CreateDeploymentError" ,
106- "unable to create a deployment status by %s: %s" , app .Status .Health .Status , err )
104+ r .Recorder .Eventf (& app , corev1 .EventTypeWarning , "CreateDeploymentStatusError" ,
105+ "unable to create a deployment status on health status %s: %s" , app .Status .Health .Status , err )
107106 } else {
108- r .Recorder .Eventf (& app , corev1 .EventTypeNormal , "CreatedDeployment" , "created a deployment status by %s" , app .Status .Health .Status )
107+ r .Recorder .Eventf (& app , corev1 .EventTypeNormal , "CreatedDeploymentStatus" ,
108+ "created a deployment status on health status %s" , app .Status .Health .Status )
109109 }
110110 return ctrl.Result {}, nil
111111}
0 commit comments