@@ -27,6 +27,7 @@ import (
2727 logf "sigs.k8s.io/controller-runtime/pkg/log"
2828
2929 platformv1 "github.com/alphagov/govuk-job-request-operator/api/v1"
30+ "github.com/go-logr/logr"
3031)
3132
3233// JobRequestReviewReconciler reconciles a JobRequestReview object
@@ -58,43 +59,71 @@ func (r *JobRequestReviewReconciler) Reconcile(ctx context.Context, req ctrl.Req
5859 }
5960
6061 // Attempt to retrieve JobRequest object and if not stop reconcile and add something to the CRD to indicate a failure
61- jobRequest := & platformv1.JobRequest {}
62- // Compose namespace with jobRequest.Name instead of req.NamespacedName
63- err = r .Get (ctx , req .NamespacedName , jobRequest )
64- if err != nil {
65- if apierrors .IsNotFound (err ) {
66- // If the custom resource is not found then it usually means that it was deleted or not created
67- // In this way, we will stop the reconciliation
68- log .Error (err , "JobRequest resource not found. Ignoring since object must be deleted" )
69- return ctrl.Result {}, nil
70- }
71- // Error reading the object
72- log .Error (err , "Failed to get JobRequest" )
73- return ctrl.Result {}, nil
62+ resourceResult , jobRequestList := r .getJobRequest (ctx , log , jobRequestReview )
63+ if resourceResult != nil {
64+ return * resourceResult , nil
7465 }
7566
67+ jobRequest := & jobRequestList .Items [0 ]
68+
7669 // If state is nil then reschedule as jobRequest object isn't setup yet
7770 if jobRequest .Status .State == "" {
71+ log .Info ("JobRequest hasn't finished creating so requeueing the reconcile" )
7872 // Requeue the reconcile after certain time
7973 return ctrl.Result {RequeueAfter : time .Minute }, nil
8074 }
8175
82- // If state is failed then stop reconcile and add something to the JobRequestReview to indicate a failure
83- if jobRequest .Status .State == "Failed" {
84- // Create an error and log it
85- log .Error (err , "JobRequest is in a Failed state so can't approve" )
76+ // If JobRequest is in Malformed state then set JobRequestReview as Malformed
77+ if jobRequest .Status .State == "Malformed" {
78+ log .Error (err , "JobRequest is in a Malformed state so can't approve" )
79+ jobRequestReview .Status .State = "JobRequestMalformed"
80+ err := r .Status ().Update (ctx , jobRequestReview )
81+ if err != nil {
82+ log .Error (err , "Failed to update status of JobRequestReview" , "errored_obj" , jobRequestReview )
83+ }
8684 return ctrl.Result {}, nil
8785 }
8886
87+ // Set JobRequest status and review name and update the status
88+ jobRequestReview .Status .State = jobRequestReview .Spec .Decision
8989 jobRequest .Status .State = jobRequestReview .Spec .Decision
90- // Do we need to recover the object again before flushing the state?
91- r .Status ().Update (ctx , jobRequest )
90+ jobRequest .Status .ReviewName = jobRequestReview .GetName ()
91+ err = r .Status ().Update (ctx , jobRequest )
92+ if err != nil {
93+ log .Error (err , "Failed to update status of JobRequest" , "errored_obj" , jobRequest )
94+ }
95+
96+ err = r .Status ().Update (ctx , jobRequestReview )
97+ if err != nil {
98+ log .Error (err , "Failed to update status of JobRequestReview" , "errored_obj" , jobRequest )
99+ }
92100
93101 return ctrl.Result {}, nil
94102}
95103
104+ func (r * JobRequestReviewReconciler ) getJobRequest (ctx context.Context , log logr.Logger , jobRequestReview * platformv1.JobRequestReview ) (* ctrl.Result , * platformv1.JobRequestList ) {
105+ jobRequestlist := & platformv1.JobRequestList {}
106+ opts := []client.ListOption {
107+ client.MatchingFields {"metadata.name" : jobRequestReview .GetName ()},
108+ }
109+
110+ // Retrieve the corresponding JobRequest object
111+ if err := r .List (ctx , jobRequestlist , opts ... ); err != nil || len (jobRequestlist .Items ) == 0 {
112+ jobRequestReview .Status .State = "JobRequestNotFound"
113+ err := r .Status ().Update (ctx , jobRequestReview )
114+ if err != nil {
115+ log .Error (err , "Failed to UPDATE JobRequestReview resource" , "errored_obj" , jobRequestReview )
116+ }
117+
118+ log .Error (err , "Failed to retrieve JobRequest" )
119+ return & ctrl.Result {}, nil
120+ }
121+
122+ return nil , jobRequestlist
123+ }
124+
96125// SetupWithManager sets up the controller with the Manager.
97- func (r * JobRequestReviewReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
126+ func (r * JobRequestReviewReconciler ) SetupControllerWithManager (mgr ctrl.Manager ) error {
98127 return ctrl .NewControllerManagedBy (mgr ).
99128 For (& platformv1.JobRequestReview {}).
100129 Named ("jobrequestreview" ).
0 commit comments