|
| 1 | +// |
| 2 | +// Copyright (c) 2019-2024 Red Hat, Inc. |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | + |
| 16 | +package controllers |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/go-logr/logr" |
| 24 | + "k8s.io/apimachinery/pkg/runtime" |
| 25 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 26 | + ctrl "sigs.k8s.io/controller-runtime" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/event" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 31 | + |
| 32 | + dwv2 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" |
| 33 | + controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1" |
| 34 | + "github.com/devfile/devworkspace-operator/pkg/conditions" |
| 35 | + "github.com/devfile/devworkspace-operator/pkg/config" |
| 36 | + |
| 37 | + "github.com/operator-framework/operator-lib/prune" |
| 38 | + "github.com/robfig/cron/v3" |
| 39 | + |
| 40 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 41 | +) |
| 42 | + |
| 43 | +// DevWorkspacePrunerReconciler reconciles DevWorkspace objects for pruning purposes. |
| 44 | +type DevWorkspacePrunerReconciler struct { |
| 45 | + client.Client |
| 46 | + Log logr.Logger |
| 47 | + Scheme *runtime.Scheme |
| 48 | + |
| 49 | + cron *cron.Cron |
| 50 | +} |
| 51 | + |
| 52 | +// SetupWithManager sets up the controller with the Manager. |
| 53 | +func (r *DevWorkspacePrunerReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 54 | + log := r.Log.WithName("setupWithManager") |
| 55 | + log.Info("Setting up DevWorkspacePrunerReconciler") |
| 56 | + |
| 57 | + // Predicate to filter DevWorkspaceOperatorConfig events. Only reconcile on changes to the |
| 58 | + // cleanup cronjob parameters. |
| 59 | + configPredicate := predicate.Funcs{ |
| 60 | + UpdateFunc: func(e event.UpdateEvent) bool { |
| 61 | + log.Info("DevWorkspaceOperatorConfig update event received") |
| 62 | + oldConfig, ok := e.ObjectOld.(*controllerv1alpha1.DevWorkspaceOperatorConfig) |
| 63 | + if !ok { |
| 64 | + return false |
| 65 | + } |
| 66 | + newConfig, ok := e.ObjectNew.(*controllerv1alpha1.DevWorkspaceOperatorConfig) |
| 67 | + if !ok { |
| 68 | + return false |
| 69 | + } |
| 70 | + |
| 71 | + oldCleanup := oldConfig.Config.Workspace.CleanupCronJob |
| 72 | + newCleanup := newConfig.Config.Workspace.CleanupCronJob |
| 73 | + |
| 74 | + // Reconcile if any of the cleanup configuration fields have changed. |
| 75 | + return oldCleanup.Enable != newCleanup.Enable || |
| 76 | + oldCleanup.RetainTime != newCleanup.RetainTime || |
| 77 | + oldCleanup.DryRun != newCleanup.DryRun || |
| 78 | + oldCleanup.Schedule != newCleanup.Schedule |
| 79 | + }, |
| 80 | + CreateFunc: func(e event.CreateEvent) bool { return true }, |
| 81 | + DeleteFunc: func(e event.DeleteEvent) bool { return false }, |
| 82 | + GenericFunc: func(e event.GenericEvent) bool { return false }, |
| 83 | + } |
| 84 | + |
| 85 | + maxConcurrentReconciles, err := config.GetMaxConcurrentReconciles() |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + // Initialize cron scheduler |
| 91 | + r.cron = cron.New() |
| 92 | + |
| 93 | + return ctrl.NewControllerManagedBy(mgr). |
| 94 | + WithOptions(controller.Options{MaxConcurrentReconciles: maxConcurrentReconciles}). |
| 95 | + For(&controllerv1alpha1.DevWorkspaceOperatorConfig{}). |
| 96 | + WithEventFilter(configPredicate). |
| 97 | + Complete(r) |
| 98 | +} |
| 99 | + |
| 100 | +// +kubebuilder:rbac:groups=workspace.devfile.io,resources=devworkspaces,verbs=get;list;delete |
| 101 | +// +kubebuilder:rbac:groups=controller.devfile.io,resources=devworkspaceoperatorconfigs,verbs=get;list;watch |
| 102 | + |
| 103 | +// Reconcile is the main reconciliation loop for the pruner controller. |
| 104 | +func (r *DevWorkspacePrunerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 105 | + log := r.Log |
| 106 | + log.Info("Reconciling DevWorkspacePruner", "DWOC", req.NamespacedName) |
| 107 | + |
| 108 | + dwOperatorConfig := &controllerv1alpha1.DevWorkspaceOperatorConfig{} |
| 109 | + err := r.Get(ctx, req.NamespacedName, dwOperatorConfig) |
| 110 | + if err != nil { |
| 111 | + log.Error(err, "Failed to get DevWorkspaceOperatorConfig") |
| 112 | + return ctrl.Result{}, client.IgnoreNotFound(err) |
| 113 | + } |
| 114 | + |
| 115 | + cleanupConfig := dwOperatorConfig.Config.Workspace.CleanupCronJob |
| 116 | + log = log.WithValues("CleanupCronJob", cleanupConfig) |
| 117 | + |
| 118 | + if cleanupConfig == nil { |
| 119 | + log.Info("DevWorkspaceOperatorConfig does not have cleanup configuration, stopping cron schedler and skipping reconciliation") |
| 120 | + r.stopCron(log) |
| 121 | + return ctrl.Result{}, nil |
| 122 | + } |
| 123 | + if !*cleanupConfig.Enable { |
| 124 | + log.Info("DevWorkspace pruning is disabled, stopping cron scheduler and skipping reconciliation") |
| 125 | + r.stopCron(log) |
| 126 | + return ctrl.Result{}, nil |
| 127 | + } |
| 128 | + if cleanupConfig.Schedule == "" { |
| 129 | + log.Info("DevWorkspace pruning schedule is not defined, stopping cron scheduler and skipping reconciliation") |
| 130 | + r.stopCron(log) |
| 131 | + return ctrl.Result{}, nil |
| 132 | + } |
| 133 | + |
| 134 | + r.startCron(ctx, cleanupConfig, log) |
| 135 | + |
| 136 | + return ctrl.Result{}, nil |
| 137 | +} |
| 138 | + |
| 139 | +func (r *DevWorkspacePrunerReconciler) startCron(ctx context.Context, cleanupConfig *controllerv1alpha1.CleanupCronJobConfig, logger logr.Logger) { |
| 140 | + log := logger.WithName("cron") |
| 141 | + log.Info("Starting cron scheduler") |
| 142 | + |
| 143 | + // remove existing cronjob tasks |
| 144 | + // we cannot update the existing tasks, so we need to remove them and add new ones |
| 145 | + entries := r.cron.Entries() |
| 146 | + for _, entry := range entries { |
| 147 | + r.cron.Remove(entry.ID) |
| 148 | + } |
| 149 | + |
| 150 | + // add cronjob task |
| 151 | + _, err := r.cron.AddFunc(cleanupConfig.Schedule, func() { |
| 152 | + taskLog := logger.WithName("cronTask") |
| 153 | + |
| 154 | + // define pruning parameters |
| 155 | + retainTime := time.Duration(*cleanupConfig.RetainTime) * time.Second |
| 156 | + dryRun := *cleanupConfig.DryRun |
| 157 | + |
| 158 | + taskLog.Info("Starting DevWorkspace pruning job") |
| 159 | + if err := r.pruneDevWorkspaces(ctx, retainTime, dryRun, logger); err != nil { |
| 160 | + taskLog.Error(err, "Failed to prune DevWorkspaces") |
| 161 | + } |
| 162 | + taskLog.Info("DevWorkspace pruning job finished") |
| 163 | + }) |
| 164 | + if err != nil { |
| 165 | + log.Error(err, "Failed to add cronjob function") |
| 166 | + return |
| 167 | + } |
| 168 | + |
| 169 | + r.cron.Start() |
| 170 | +} |
| 171 | + |
| 172 | +func (r *DevWorkspacePrunerReconciler) stopCron(logger logr.Logger) { |
| 173 | + log := logger.WithName("cron") |
| 174 | + log.Info("Stopping cron scheduler") |
| 175 | + |
| 176 | + r.cron.Stop() |
| 177 | +} |
| 178 | + |
| 179 | +func (r *DevWorkspacePrunerReconciler) pruneDevWorkspaces(ctx context.Context, retainTime time.Duration, dryRun bool, logger logr.Logger) error { |
| 180 | + log := logger.WithName("pruner") |
| 181 | + |
| 182 | + // create a prune strategy based on the configuration |
| 183 | + var pruneStrategy prune.StrategyFunc |
| 184 | + if dryRun { |
| 185 | + pruneStrategy = r.dryRunPruneStrategy(retainTime, log) |
| 186 | + } else { |
| 187 | + pruneStrategy = r.pruneStrategy(retainTime, log) |
| 188 | + } |
| 189 | + |
| 190 | + gvk := schema.GroupVersionKind{ |
| 191 | + Group: dwv2.SchemeGroupVersion.Group, |
| 192 | + Version: dwv2.SchemeGroupVersion.Version, |
| 193 | + Kind: "DevWorkspace", |
| 194 | + } |
| 195 | + |
| 196 | + // create pruner that uses our custom strategy |
| 197 | + pruner, err := prune.NewPruner(r.Client, gvk, pruneStrategy) |
| 198 | + if err != nil { |
| 199 | + return fmt.Errorf("failed to create pruner: %w", err) |
| 200 | + } |
| 201 | + |
| 202 | + deletedObjects, err := pruner.Prune(ctx) |
| 203 | + if err != nil { |
| 204 | + return fmt.Errorf("failed to prune objects: %w", err) |
| 205 | + } |
| 206 | + log.Info(fmt.Sprintf("Pruned %d DevWorkspaces", len(deletedObjects))) |
| 207 | + |
| 208 | + for _, obj := range deletedObjects { |
| 209 | + devWorkspace, ok := obj.(*dwv2.DevWorkspace) |
| 210 | + if !ok { |
| 211 | + log.Error(err, fmt.Sprintf("failed to convert %v to DevWorkspace", obj)) |
| 212 | + continue |
| 213 | + } |
| 214 | + log.Info(fmt.Sprintf("Pruned DevWorkspace '%s' in namespace '%s'", devWorkspace.Name, devWorkspace.Namespace)) |
| 215 | + } |
| 216 | + |
| 217 | + return nil |
| 218 | +} |
| 219 | + |
| 220 | +// pruneStrategy returns a StrategyFunc that will return a list of |
| 221 | +// DevWorkspaces to prune based on the lastTransitionTime of the 'Started' condition. |
| 222 | +func (r *DevWorkspacePrunerReconciler) pruneStrategy(retainTime time.Duration, logger logr.Logger) prune.StrategyFunc { |
| 223 | + log := logger.WithName("pruneStrategy") |
| 224 | + |
| 225 | + return func(ctx context.Context, objs []client.Object) ([]client.Object, error) { |
| 226 | + filteredObjs := filterByInactivityTime(objs, retainTime, log) |
| 227 | + log.Info(fmt.Sprintf("Found %d DevWorkspaces to prune", len(filteredObjs))) |
| 228 | + return filteredObjs, nil |
| 229 | + } |
| 230 | +} |
| 231 | + |
| 232 | +// dryRunPruneStrategy returns a StrategyFunc that will always return an empty list of DevWorkspaces to prune. |
| 233 | +// This is used for dry-run mode. |
| 234 | +func (r *DevWorkspacePrunerReconciler) dryRunPruneStrategy(retainTime time.Duration, logger logr.Logger) prune.StrategyFunc { |
| 235 | + log := logger.WithName("dryRunPruneStrategy") |
| 236 | + |
| 237 | + return func(ctx context.Context, objs []client.Object) ([]client.Object, error) { |
| 238 | + filteredObjs := filterByInactivityTime(objs, retainTime, log) |
| 239 | + log.Info(fmt.Sprintf("Found %d DevWorkspaces to prune", len(filteredObjs))) |
| 240 | + |
| 241 | + // Return an empty list of DevWorkspaces because this is a dry-run |
| 242 | + return []client.Object{}, nil |
| 243 | + } |
| 244 | +} |
| 245 | + |
| 246 | +// filterByInactivityTime filters DevWorkspaces based on the lastTransitionTime of the 'Started' condition. |
| 247 | +func filterByInactivityTime(objs []client.Object, retainTime time.Duration, log logr.Logger) []client.Object { |
| 248 | + var filteredObjs []client.Object |
| 249 | + for _, obj := range objs { |
| 250 | + devWorkspace, ok := obj.(*dwv2.DevWorkspace) |
| 251 | + if !ok { |
| 252 | + log.Error(nil, fmt.Sprintf("failed to convert %v to DevWorkspace", obj)) |
| 253 | + continue |
| 254 | + } |
| 255 | + |
| 256 | + if canPrune(*devWorkspace, retainTime, log) { |
| 257 | + filteredObjs = append(filteredObjs, devWorkspace) |
| 258 | + log.Info(fmt.Sprintf("Adding DevWorkspace '%s/%s' to prune list", devWorkspace.Namespace, devWorkspace.Name)) |
| 259 | + } else { |
| 260 | + log.Info(fmt.Sprintf("Skipping DevWorkspace '%s/%s': not eligible for pruning", devWorkspace.Namespace, devWorkspace.Name)) |
| 261 | + } |
| 262 | + } |
| 263 | + return filteredObjs |
| 264 | +} |
| 265 | + |
| 266 | +// canPrune returns true if the DevWorkspace is eligible for pruning. |
| 267 | +func canPrune(dw dwv2.DevWorkspace, retainTime time.Duration, log logr.Logger) bool { |
| 268 | + // Skip started and running DevWorkspaces |
| 269 | + if dw.Spec.Started { |
| 270 | + log.Info(fmt.Sprintf("Skipping DevWorkspace '%s/%s': already started", dw.Namespace, dw.Name)) |
| 271 | + return false |
| 272 | + } |
| 273 | + |
| 274 | + var startTime *metav1.Time |
| 275 | + for _, condition := range dw.Status.Conditions { |
| 276 | + if condition.Type == conditions.Started { |
| 277 | + startTime = &condition.LastTransitionTime |
| 278 | + break |
| 279 | + } |
| 280 | + } |
| 281 | + if startTime == nil { |
| 282 | + log.Info(fmt.Sprintf("Skipping DevWorkspace '%s/%s': missing 'Started' condition", dw.Namespace, dw.Name)) |
| 283 | + return false |
| 284 | + } |
| 285 | + if time.Since(startTime.Time) <= retainTime { |
| 286 | + log.Info(fmt.Sprintf("Skipping DevWorkspace '%s/%s': not eligible for pruning", dw.Namespace, dw.Name)) |
| 287 | + return false |
| 288 | + } |
| 289 | + return true |
| 290 | +} |
0 commit comments