Commit b5f869c6 authored by michaelguarino's avatar michaelguarino
Browse files

Add job expiration controller

parent 3547567d
Pipeline #539 passed with stage
in 2 minutes and 10 seconds
Showing with 124 additions and 1 deletion
+124 -1
/*
Copyright 2021.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controllers
import (
"context"
"fmt"
"time"
"github.com/go-logr/logr"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// JobReconciler manages additional features on jobs
type JobReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
}
const (
expiresAfter = 5 * time.Hour * 24
)
//+kubebuilder:rbac:groups="batch",resources=jobs,verbs=get;list;update
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the SecretSync object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.7.2/pkg/reconcile
func (r *JobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := r.Log.WithValues("Job", req.NamespacedName)
ns := req.NamespacedName.Namespace
var namespace corev1.Namespace
if err := r.Get(ctx, types.NamespacedName{Name: ns}, &namespace); err != nil {
log.Error(err, "Failed to fetch namespace")
return ctrl.Result{}, client.IgnoreNotFound(err)
}
if val, ok := namespace.Labels[managedLabel]; !ok || val != "plural" {
log.Info(fmt.Sprintf("Namespace %s not managed by plural", ns))
return ctrl.Result{}, nil
}
// your logic here
var job batchv1.Job
if err := r.Get(ctx, req.NamespacedName, &job); err != nil {
log.Error(err, "Failed to fetch Job resource")
return ctrl.Result{}, client.IgnoreNotFound(err)
}
completion := job.Status.CompletionTime
meta := job.ObjectMeta
if completion == nil {
log.Info("Job not yet completed")
return ctrl.Result{}, nil
}
for _, owner := range meta.OwnerReferences {
if owner.Kind == "CronJob" {
log.Info(fmt.Sprintf("Job managed by CronJob %s, ignore", owner.Name))
return ctrl.Result{}, nil
}
}
dt := completion.Time
expiry := dt.Add(expiresAfter)
if time.Now().Before(expiry) {
log.Info("Requeueing the job until after expiry")
return ctrl.Result{RequeueAfter: expiry.Sub(time.Now())}, nil
}
if err := r.Delete(ctx, &job); err != nil {
log.Error(err, "failed to delete job")
return ctrl.Result{}, client.IgnoreNotFound(err)
}
log.Info("Successfully expired job")
return ctrl.Result{}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *JobReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&batchv1.Job{}).
Complete(r)
}
......@@ -92,7 +92,16 @@ func main() {
Log: ctrl.Log.WithName("controllers").WithName("ServiceAccount"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "SecretSync")
setupLog.Error(err, "unable to create controller", "controller", "ServiceAccount")
os.Exit(1)
}
if err = (&controllers.JobReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Job"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Job")
os.Exit(1)
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment