Skip to content

Commit 07d2e6f

Browse files
committed
Propagate DevWorkspace .spec.started status to an annotation on routings
Add annotation 'controller.devfile.io/devworkspace-started' to be applied to DevWorkspaceRoutings. If a workspace is stopped, the annotation is set to "false", otherwise it is "true". If the DevWorkspaceRouting reconciler encounters a DevWorkspaceRouting with the annotation set to false, it ends the reconcile early. This ensures that every workspace start/stop event triggers a reconcile, and allows the routing to clean up resources if necessary. Signed-off-by: Angel Misevski <[email protected]>
1 parent c6bf489 commit 07d2e6f

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

controllers/controller/devworkspacerouting/devworkspacerouting_controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ func (r *DevWorkspaceRoutingReconciler) Reconcile(ctx context.Context, req ctrl.
8181
// Error reading the object - requeue the request.
8282
return reconcile.Result{}, err
8383
}
84+
if instance.Annotations != nil && instance.Annotations[constants.DevWorkspaceStartedStatusAnnotation] == "false" {
85+
return reconcile.Result{}, nil
86+
}
87+
8488
reqLogger = reqLogger.WithValues(constants.DevWorkspaceIDLoggerKey, instance.Spec.DevWorkspaceId)
8589
reqLogger.Info("Reconciling DevWorkspaceRouting")
8690

controllers/workspace/devworkspace_controller.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
devfilevalidation "github.com/devfile/api/v2/pkg/validation"
2323

24+
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
2425
controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
2526
"github.com/devfile/devworkspace-operator/controllers/workspace/metrics"
2627
"github.com/devfile/devworkspace-operator/pkg/common"
@@ -416,6 +417,28 @@ func (r *DevWorkspaceReconciler) doStop(workspace *dw.DevWorkspace, logger logr.
416417
return false, err
417418
}
418419

420+
// Update DevWorkspaceRouting to have .spec.started=false
421+
routing := &v1alpha1.DevWorkspaceRouting{}
422+
routingRef := types.NamespacedName{
423+
Name: common.DevWorkspaceRoutingName(workspace.Status.DevWorkspaceId),
424+
Namespace: workspace.Namespace,
425+
}
426+
err = r.Get(context.TODO(), routingRef, routing)
427+
if err != nil {
428+
if !k8sErrors.IsNotFound(err) {
429+
return false, err
430+
}
431+
} else if routing.Annotations != nil && routing.Annotations[constants.DevWorkspaceStartedStatusAnnotation] != "false" {
432+
routing.Annotations[constants.DevWorkspaceStartedStatusAnnotation] = "false"
433+
err := r.Update(context.TODO(), routing)
434+
if err != nil {
435+
if k8sErrors.IsConflict(err) {
436+
return false, nil
437+
}
438+
return false, err
439+
}
440+
}
441+
419442
replicas := workspaceDeployment.Spec.Replicas
420443
if replicas == nil || *replicas > 0 {
421444
logger.Info("Stopping workspace")

pkg/common/naming.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import (
2020

2121
var NonAlphaNumRegexp = regexp.MustCompile(`[^a-z0-9]+`)
2222

23+
func DevWorkspaceRoutingName(workspaceId string) string {
24+
return fmt.Sprintf("routing-%s", workspaceId)
25+
}
26+
2327
func EndpointName(endpointName string) string {
2428
name := strings.ToLower(endpointName)
2529
name = NonAlphaNumRegexp.ReplaceAllString(name, "-")

pkg/constants/metadata.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ const (
5656
// Operator also propagates it to the devworkspace-related objects to perform authorization.
5757
DevWorkspaceRestrictedAccessAnnotation = "controller.devfile.io/restricted-access"
5858

59+
// DevWorkspaceStartedStatusAnnotation is applied to subresources of DevWorkspaces to indicate the owning object's
60+
// .spec.started value. This annotation is applied to DevWorkspaceRoutings to trigger reconciles when a DevWorkspace
61+
// is started or stopped.
62+
DevWorkspaceStartedStatusAnnotation = "controller.devfile.io/devworkspace-started"
63+
5964
// DevWorkspaceStopReasonAnnotation marks the reason why the devworkspace was stopped; when a devworkspace is restarted
6065
// this annotation will be cleared
6166
DevWorkspaceStopReasonAnnotation = "controller.devfile.io/stopped-by"

pkg/provision/workspace/routing.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
2323
maputils "github.com/devfile/devworkspace-operator/internal/map"
24+
"github.com/devfile/devworkspace-operator/pkg/common"
2425
"github.com/devfile/devworkspace-operator/pkg/config"
2526
"github.com/devfile/devworkspace-operator/pkg/constants"
2627

@@ -153,6 +154,7 @@ func getSpecRouting(
153154
if val, ok := workspace.Annotations[constants.DevWorkspaceRestrictedAccessAnnotation]; ok {
154155
annotations = maputils.Append(annotations, constants.DevWorkspaceRestrictedAccessAnnotation, val)
155156
}
157+
annotations[constants.DevWorkspaceStartedStatusAnnotation] = "true"
156158

157159
// copy the annotations for the specific routingClass from the workspace object to the routing
158160
expectedAnnotationPrefix := workspace.Spec.RoutingClass + constants.RoutingAnnotationInfix
@@ -169,7 +171,7 @@ func getSpecRouting(
169171

170172
routing := &v1alpha1.DevWorkspaceRouting{
171173
ObjectMeta: metav1.ObjectMeta{
172-
Name: fmt.Sprintf("routing-%s", workspace.Status.DevWorkspaceId),
174+
Name: common.DevWorkspaceRoutingName(workspace.Status.DevWorkspaceId),
173175
Namespace: workspace.Namespace,
174176
Labels: map[string]string{
175177
constants.DevWorkspaceIDLabel: workspace.Status.DevWorkspaceId,

0 commit comments

Comments
 (0)