Skip to content

Commit

Permalink
refactor: remove resources after theyve completed reconciling
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Jan 24, 2024
1 parent 7c53977 commit 1e3a9db
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
30 changes: 27 additions & 3 deletions controllers/v1beta1/build_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func (r *LagoonBuildReconciler) Reconcile(ctx context.Context, req ctrl.Request)

// examine DeletionTimestamp to determine if object is under deletion
if lagoonBuild.ObjectMeta.DeletionTimestamp.IsZero() {

// if the build isn't being deleted, but the status is cancelled
// then clean up the undeployable build
if value, ok := lagoonBuild.ObjectMeta.Labels["lagoon.sh/buildStatus"]; ok {
Expand Down Expand Up @@ -162,10 +163,33 @@ func (r *LagoonBuildReconciler) Reconcile(ctx context.Context, req ctrl.Request)
} // end check if running build is current LagoonBuild
} // end loop for running builds
// once running builds are processed, run the qos handler
return r.qosBuildProcessor(ctx, opLog, lagoonBuild, req)
result, err := r.qosBuildProcessor(ctx, opLog, lagoonBuild, req)
if err != nil {
return result, err
}
} else {
// if qos is not enabled, just process it as a standard build
result, err := r.standardBuildProcessor(ctx, opLog, lagoonBuild, req)
if err != nil {
return result, err
}
}
// with the introduction of v1beta2, this will let any existing pending/qeued/running builds continue through
// but once the build is completed or failed and has processed anything else it needs to do, it should delete the resource
if _, ok := lagoonBuild.Labels["lagoon.sh/buildStatus"]; ok {
if !helpers.ContainsString(
lagoonv1beta1.BuildRunningPendingStatus,
lagoonBuild.Labels["lagoon.sh/buildStatus"],
) {
opLog.Info(fmt.Sprintf("%s found in namespace %s is no longer required, removing it. v1beta1 is deprecated in favor of v1beta2",
lagoonBuild.ObjectMeta.Name,
req.NamespacedName.Namespace,
))
if err := r.Delete(ctx, &lagoonBuild); err != nil {
return ctrl.Result{}, err
}
}
}
// if qos is not enabled, just process it as a standard build
return r.standardBuildProcessor(ctx, opLog, lagoonBuild, req)
}
// The object is being deleted
if helpers.ContainsString(lagoonBuild.ObjectMeta.Finalizers, buildFinalizer) {
Expand Down
26 changes: 24 additions & 2 deletions controllers/v1beta1/task_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,33 @@ func (r *LagoonTaskReconciler) Reconcile(ctx context.Context, req ctrl.Request)
// check if the task that has been recieved is a standard or advanced task
if lagoonTask.ObjectMeta.Labels["lagoon.sh/taskStatus"] == lagoonv1beta1.TaskStatusPending.String() &&
lagoonTask.ObjectMeta.Labels["lagoon.sh/taskType"] == lagoonv1beta1.TaskTypeStandard.String() {
return ctrl.Result{}, r.createStandardTask(ctx, &lagoonTask, opLog)
err := r.createStandardTask(ctx, &lagoonTask, opLog)
if err != nil {
return ctrl.Result{}, err
}
}
if lagoonTask.ObjectMeta.Labels["lagoon.sh/taskStatus"] == lagoonv1beta1.TaskStatusPending.String() &&
lagoonTask.ObjectMeta.Labels["lagoon.sh/taskType"] == lagoonv1beta1.TaskTypeAdvanced.String() {
return ctrl.Result{}, r.createAdvancedTask(ctx, &lagoonTask, opLog)
err := r.createAdvancedTask(ctx, &lagoonTask, opLog)
if err != nil {
return ctrl.Result{}, err
}
}
// with the introduction of v1beta2, this will let any existing tasks continue through
// but once the task is done
if _, ok := lagoonTask.Labels["lagoon.sh/taskStatus"]; ok {
if !helpers.ContainsString(
lagoonv1beta1.TaskRunningPendingStatus,
lagoonTask.Labels["lagoon.sh/taskStatus"],
) {
opLog.Info(fmt.Sprintf("%s found in namespace %s is no longer required, removing it. v1alpha1 is deprecated in favor of v1beta1",
lagoonTask.ObjectMeta.Name,
req.NamespacedName.Namespace,
))
if err := r.Delete(ctx, &lagoonTask); err != nil {
return ctrl.Result{}, err
}
}
}
} else {
// The object is being deleted
Expand Down
1 change: 1 addition & 0 deletions controllers/v1beta2/build_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ func (r *LagoonBuildReconciler) getOrCreateBuildResource(ctx context.Context, la
map[string]string{
"lagoon.sh/buildStatus": lagooncrd.BuildStatusPending.String(),
"lagoon.sh/controller": r.ControllerNamespace,
"crd.lagoon.sh/version": crdVersion,
},
)
err := r.Get(ctx, types.NamespacedName{
Expand Down

0 comments on commit 1e3a9db

Please sign in to comment.