From 5dd59018670c2933e549d9bdb8e7d62370be9aac Mon Sep 17 00:00:00 2001 From: David Kwon Date: Fri, 13 May 2022 15:44:24 -0400 Subject: [PATCH] feat: add WorkspaceStopped method to RoutingSolver Signed-off-by: David Kwon --- .../v1alpha1/devworkspacerouting_types.go | 5 +++++ .../devworkspacerouting_controller.go | 17 +++++++++-------- .../devworkspacerouting/solvers/basic_solver.go | 4 ++++ .../solvers/cluster_solver.go | 4 ++++ .../devworkspacerouting/solvers/solver.go | 3 +++ 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/apis/controller/v1alpha1/devworkspacerouting_types.go b/apis/controller/v1alpha1/devworkspacerouting_types.go index d27b4cf89..a96376d54 100644 --- a/apis/controller/v1alpha1/devworkspacerouting_types.go +++ b/apis/controller/v1alpha1/devworkspacerouting_types.go @@ -18,6 +18,7 @@ package v1alpha1 import ( apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "github.com/devfile/devworkspace-operator/pkg/constants" ) // DevWorkspaceRoutingSpec defines the desired state of DevWorkspaceRouting @@ -205,3 +206,7 @@ type DevWorkspaceRoutingList struct { func init() { SchemeBuilder.Register(&DevWorkspaceRouting{}, &DevWorkspaceRoutingList{}) } + +func (d *DevWorkspaceRouting) IsWorkspaceStopped() bool { + return d.Annotations != nil && d.Annotations[constants.DevWorkspaceStartedStatusAnnotation] == "false" +} diff --git a/controllers/controller/devworkspacerouting/devworkspacerouting_controller.go b/controllers/controller/devworkspacerouting/devworkspacerouting_controller.go index 533ad7e87..ae4a7669b 100644 --- a/controllers/controller/devworkspacerouting/devworkspacerouting_controller.go +++ b/controllers/controller/devworkspacerouting/devworkspacerouting_controller.go @@ -107,8 +107,15 @@ func (r *DevWorkspaceRoutingReconciler) Reconcile(ctx context.Context, req ctrl. return reconcile.Result{}, r.finalize(solver, instance) } - if instance.Annotations != nil && instance.Annotations[constants.DevWorkspaceStartedStatusAnnotation] == "false" { - return reconcile.Result{}, nil + workspaceMeta := solvers.DevWorkspaceMetadata{ + DevWorkspaceId: instance.Spec.DevWorkspaceId, + Namespace: instance.Namespace, + PodSelector: instance.Spec.PodSelector, + } + + if instance.IsWorkspaceStopped() { + err := solver.WorkspaceStopped(instance, workspaceMeta) + return reconcile.Result{}, err } if instance.Status.Phase == controllerv1alpha1.RoutingFailed { @@ -120,12 +127,6 @@ func (r *DevWorkspaceRoutingReconciler) Reconcile(ctx context.Context, req ctrl. return reconcile.Result{}, err } - workspaceMeta := solvers.DevWorkspaceMetadata{ - DevWorkspaceId: instance.Spec.DevWorkspaceId, - Namespace: instance.Namespace, - PodSelector: instance.Spec.PodSelector, - } - restrictedAccess, setRestrictedAccess := instance.Annotations[constants.DevWorkspaceRestrictedAccessAnnotation] routingObjects, err := solver.GetSpecObjects(instance, workspaceMeta) if err != nil { diff --git a/controllers/controller/devworkspacerouting/solvers/basic_solver.go b/controllers/controller/devworkspacerouting/solvers/basic_solver.go index 23beeccc3..c90c8c575 100644 --- a/controllers/controller/devworkspacerouting/solvers/basic_solver.go +++ b/controllers/controller/devworkspacerouting/solvers/basic_solver.go @@ -80,3 +80,7 @@ func (s *BasicSolver) GetExposedEndpoints( routingObj RoutingObjects) (exposedEndpoints map[string]controllerv1alpha1.ExposedEndpointList, ready bool, err error) { return getExposedEndpoints(endpoints, routingObj) } + +func (s *BasicSolver) WorkspaceStopped(routing *controllerv1alpha1.DevWorkspaceRouting, workspaceMeta DevWorkspaceMetadata) error { + return nil +} diff --git a/controllers/controller/devworkspacerouting/solvers/cluster_solver.go b/controllers/controller/devworkspacerouting/solvers/cluster_solver.go index 193fa2dc7..a4e75e095 100644 --- a/controllers/controller/devworkspacerouting/solvers/cluster_solver.go +++ b/controllers/controller/devworkspacerouting/solvers/cluster_solver.go @@ -126,3 +126,7 @@ func getHostnameFromService(service corev1.Service, port int32) string { } return fmt.Sprintf("%s://%s.%s.svc:%d", scheme, service.Name, service.Namespace, port) } + +func (s *ClusterSolver) WorkspaceStopped(routing *controllerv1alpha1.DevWorkspaceRouting, workspaceMeta DevWorkspaceMetadata) error { + return nil +} diff --git a/controllers/controller/devworkspacerouting/solvers/solver.go b/controllers/controller/devworkspacerouting/solvers/solver.go index 06823e2cc..7a5e3933e 100644 --- a/controllers/controller/devworkspacerouting/solvers/solver.go +++ b/controllers/controller/devworkspacerouting/solvers/solver.go @@ -59,6 +59,9 @@ type RoutingSolver interface { // Return value "ready" specifies if all endpoints are resolved on the cluster; if false it is necessary to retry, as // URLs will be undefined. GetExposedEndpoints(endpoints map[string]controllerv1alpha1.EndpointList, routingObj RoutingObjects) (exposedEndpoints map[string]controllerv1alpha1.ExposedEndpointList, ready bool, err error) + + // WorkspaceStopped is called when the DevWorkspace for the current routing has .spec.started set to false + WorkspaceStopped(routing *controllerv1alpha1.DevWorkspaceRouting, workspaceMeta DevWorkspaceMetadata) error } type RoutingSolverGetter interface {