Skip to content

Propagate DevWorkspace .spec.started status to an annotation on routings #617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func (r *DevWorkspaceRoutingReconciler) Reconcile(ctx context.Context, req ctrl.
// Error reading the object - requeue the request.
return reconcile.Result{}, err
}
if instance.Annotations != nil && instance.Annotations[constants.DevWorkspaceStartedStatusAnnotation] == "false" {
return reconcile.Result{}, nil
}

reqLogger = reqLogger.WithValues(constants.DevWorkspaceIDLoggerKey, instance.Spec.DevWorkspaceId)
reqLogger.Info("Reconciling DevWorkspaceRouting")

Expand Down
23 changes: 23 additions & 0 deletions controllers/workspace/devworkspace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

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

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

// Update DevWorkspaceRouting to have .spec.started=false
routing := &v1alpha1.DevWorkspaceRouting{}
routingRef := types.NamespacedName{
Name: common.DevWorkspaceRoutingName(workspace.Status.DevWorkspaceId),
Namespace: workspace.Namespace,
}
err = r.Get(context.TODO(), routingRef, routing)
if err != nil {
if !k8sErrors.IsNotFound(err) {
return false, err
}
} else if routing.Annotations != nil && routing.Annotations[constants.DevWorkspaceStartedStatusAnnotation] != "false" {
routing.Annotations[constants.DevWorkspaceStartedStatusAnnotation] = "false"
err := r.Update(context.TODO(), routing)
if err != nil {
if k8sErrors.IsConflict(err) {
return false, nil
}
return false, err
}
}

replicas := workspaceDeployment.Spec.Replicas
if replicas == nil || *replicas > 0 {
logger.Info("Stopping workspace")
Expand Down
4 changes: 4 additions & 0 deletions pkg/common/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (

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

func DevWorkspaceRoutingName(workspaceId string) string {
return fmt.Sprintf("routing-%s", workspaceId)
}

func EndpointName(endpointName string) string {
name := strings.ToLower(endpointName)
name = NonAlphaNumRegexp.ReplaceAllString(name, "-")
Expand Down
5 changes: 5 additions & 0 deletions pkg/constants/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ const (
// Operator also propagates it to the devworkspace-related objects to perform authorization.
DevWorkspaceRestrictedAccessAnnotation = "controller.devfile.io/restricted-access"

// DevWorkspaceStartedStatusAnnotation is applied to subresources of DevWorkspaces to indicate the owning object's
// .spec.started value. This annotation is applied to DevWorkspaceRoutings to trigger reconciles when a DevWorkspace
// is started or stopped.
DevWorkspaceStartedStatusAnnotation = "controller.devfile.io/devworkspace-started"

// DevWorkspaceStopReasonAnnotation marks the reason why the devworkspace was stopped; when a devworkspace is restarted
// this annotation will be cleared
DevWorkspaceStopReasonAnnotation = "controller.devfile.io/stopped-by"
Expand Down
4 changes: 3 additions & 1 deletion pkg/provision/workspace/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
maputils "github.com/devfile/devworkspace-operator/internal/map"
"github.com/devfile/devworkspace-operator/pkg/common"
"github.com/devfile/devworkspace-operator/pkg/config"
"github.com/devfile/devworkspace-operator/pkg/constants"

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

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

routing := &v1alpha1.DevWorkspaceRouting{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("routing-%s", workspace.Status.DevWorkspaceId),
Name: common.DevWorkspaceRoutingName(workspace.Status.DevWorkspaceId),
Namespace: workspace.Namespace,
Labels: map[string]string{
constants.DevWorkspaceIDLabel: workspace.Status.DevWorkspaceId,
Expand Down