|
| 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 | + |
1 | 14 | package controllers |
2 | 15 |
|
3 | 16 | import ( |
4 | 17 | "context" |
5 | 18 | "fmt" |
6 | 19 | "strconv" |
7 | | - "time" |
8 | 20 |
|
9 | 21 | controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1" |
10 | 22 | "github.com/devfile/devworkspace-operator/pkg/constants" |
@@ -93,14 +105,12 @@ func (r *PrunerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr |
93 | 105 | return ctrl.Result{}, err |
94 | 106 | } |
95 | 107 |
|
96 | | - return ctrl.Result{RequeueAfter: 24 * time.Hour}, nil |
| 108 | + return ctrl.Result{}, nil |
97 | 109 | } |
98 | 110 |
|
99 | 111 | func (r *PrunerReconciler) SetupWithManager(mgr ctrl.Manager) error { |
100 | 112 | return ctrl.NewControllerManagedBy(mgr). |
101 | 113 | For(&controllerv1alpha1.DevWorkspaceOperatorConfig{}). |
102 | | - // Owns(&batchv1.CronJob{}). |
103 | | - // Owns(&corev1.ConfigMap{}). |
104 | 114 | Complete(r) |
105 | 115 | } |
106 | 116 |
|
@@ -319,13 +329,14 @@ func prunerDefaultConfigMap(namespace string) *corev1.ConfigMap { |
319 | 329 | current_time=$(date +%s) |
320 | 330 | echo "Current time: ${current_time}" |
321 | 331 | 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}}') |
323 | 333 | do |
324 | 334 | for workspace in $(oc get devworkspaces -n ${namespace} -o go-template='{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}') |
325 | 335 | do |
326 | 336 | last_start=$(date -d$(oc get devworkspace ${workspace} -n ${namespace} -o go-template='{{range .status.conditions}}{{if eq .type "Started"}}{{.lastTransitionTime}}{{end}}{{end}}') +%s) |
327 | 337 | 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} ]] |
329 | 340 | then |
330 | 341 | echo "Removing workspace: ${workspace} in ${namespace}" |
331 | 342 | oc delete devworkspace ${workspace} -n ${namespace} |
|
0 commit comments