@@ -2,7 +2,9 @@ package handler
22
33import (
44 "context"
5+ "errors"
56 "fmt"
7+ "strings"
68
79 "github.com/go-logr/logr"
810 batchv1 "k8s.io/api/batch/v1"
@@ -21,6 +23,10 @@ import (
2123
2224type AppdeploymentHandlerContextKey struct {}
2325
26+ var (
27+ ErrJobFailed = errors .New ("job failed" )
28+ )
29+
2430//go:generate mockgen -destination=./mocks/mock_appdeployment.go -package=mocks github.com/Azure/operation-cache-controller/internal/handler AppDeploymentHandlerInterface
2531type AppDeploymentHandlerInterface interface {
2632 EnsureApplicationValid (ctx context.Context ) (reconciler.OperationResult , error )
@@ -166,7 +172,15 @@ func (a *AppDeploymentHandler) initializeJobAndAwaitCompletion(ctx context.Conte
166172 a .recorder .Event (a .appDeployment , "Error" , "FailedDeleteJob" , err .Error ())
167173 return fmt .Errorf ("failed to delete job %s: %w" , job .Name , err )
168174 }
169- // create a new job
175+ // complete the job if it is a teardown job
176+ if strings .HasPrefix (jobTemplate .Name , ctrlutils .JobTypeTeardown ) {
177+ a .logger .Error (ErrJobFailed , "teardown job failed" , log .FieldKeyAppDeploymentJobName , jobTemplate .Name )
178+ a .recorder .Event (a .appDeployment , "Warning" , "TeardownJobFailed" , fmt .Sprintf ("Teardown job %s failed, requeuing for retry" , jobTemplate .Name ))
179+ // return nil to make the teardown job complete
180+ return nil
181+ }
182+
183+ // create a new job if it is not a teardown job
170184 if err := ctrl .SetControllerReference (a .appDeployment , jobTemplate , a .client .Scheme ()); err != nil {
171185 return fmt .Errorf ("failed to set controller reference for job %s: %w" , job .Name , err )
172186 }
@@ -203,7 +217,7 @@ func (a *AppDeploymentHandler) EnsureDeployingFinished(ctx context.Context) (rec
203217 a .appDeployment .Status .Phase = v1alpha1 .AppDeploymentPhaseReady
204218 return reconciler .RequeueOnErrorOrContinue (a .client .Status ().Update (ctx , a .appDeployment ))
205219 case errJobNotCompleted :
206- a .logger .V (1 ).WithValues (log .AppDeploymentJobName , provisionJob .Name ).Info ("provision job is not completed yet" )
220+ a .logger .V (1 ).WithValues (log .FieldKeyAppDeploymentJobName , provisionJob .Name ).Info ("provision job is not completed yet" )
207221 return reconciler .Requeue ()
208222 default :
209223 a .logger .Error (err , "provision job failed %s" , provisionJob .Name )
@@ -224,10 +238,10 @@ func (a *AppDeploymentHandler) EnsureTeardownFinished(ctx context.Context) (reco
224238 a .appDeployment .Status .Phase = v1alpha1 .AppDeploymentPhaseDeleted
225239 return reconciler .RequeueOnErrorOrContinue (a .client .Status ().Update (ctx , a .appDeployment ))
226240 case errJobNotCompleted :
227- a .logger .V (1 ).WithValues (log .AppDeploymentJobName , teardownJob .Name ).Info ("teardown job is not completed yet" )
241+ a .logger .V (1 ).WithValues (log .FieldKeyAppDeploymentJobName , teardownJob .Name ).Info ("teardown job is not completed yet" )
228242 return reconciler .Requeue ()
229243 default :
230- a .logger .WithValues (log .AppDeploymentJobName , teardownJob .Name ).Error (err , "teardown job failed %s" )
244+ a .logger .WithValues (log .FieldKeyAppDeploymentJobName , teardownJob .Name ).Error (err , "teardown job failed %s" )
231245 return reconciler .RequeueWithError (err )
232246 }
233247}
0 commit comments