Skip to content

Commit 45ad14d

Browse files
committed
fixup! feat: add DevWorkspace pruner
Signed-off-by: Oleksii Kurinnyi <[email protected]>
1 parent ddab552 commit 45ad14d

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

controllers/workspace/pruner_controller.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1+
// Copyright (c) 2019-2024 Red Hat, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
114
package controllers
215

316
import (
417
"context"
518
"fmt"
619
"strconv"
7-
"time"
820

921
controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
1022
"github.com/devfile/devworkspace-operator/pkg/constants"
@@ -93,14 +105,12 @@ func (r *PrunerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
93105
return ctrl.Result{}, err
94106
}
95107

96-
return ctrl.Result{RequeueAfter: 24 * time.Hour}, nil
108+
return ctrl.Result{}, nil
97109
}
98110

99111
func (r *PrunerReconciler) SetupWithManager(mgr ctrl.Manager) error {
100112
return ctrl.NewControllerManagedBy(mgr).
101113
For(&controllerv1alpha1.DevWorkspaceOperatorConfig{}).
102-
// Owns(&batchv1.CronJob{}).
103-
// Owns(&corev1.ConfigMap{}).
104114
Complete(r)
105115
}
106116

@@ -319,13 +329,14 @@ func prunerDefaultConfigMap(namespace string) *corev1.ConfigMap {
319329
current_time=$(date +%s)
320330
echo "Current time: ${current_time}"
321331
echo "RETAIN_TIME: ${RETAIN_TIME}"
322-
for namespace in $(oc get namespaces -o go-template='{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}' | grep ".-devspaces")
332+
for namespace in $(oc get namespaces -l app.kubernetes.io/component=workspaces-namespace -o go-template='{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
323333
do
324334
for workspace in $(oc get devworkspaces -n ${namespace} -o go-template='{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
325335
do
326336
last_start=$(date -d$(oc get devworkspace ${workspace} -n ${namespace} -o go-template='{{range .status.conditions}}{{if eq .type "Started"}}{{.lastTransitionTime}}{{end}}{{end}}') +%s)
327337
workspace_age=$(( ${current_time} - ${last_start} ))
328-
if [[ ${workspace_age} -gt ${RETAIN_TIME} ]]
338+
started=$(oc get devworkspace ${workspace} -n ${namespace} -o go-template='{{.spec.started}}')
339+
if [[ "$started" != "true" ]] && [[ ${workspace_age} -gt ${RETAIN_TIME} ]]
329340
then
330341
echo "Removing workspace: ${workspace} in ${namespace}"
331342
oc delete devworkspace ${workspace} -n ${namespace}

0 commit comments

Comments
 (0)