Skip to content

Propagate workspace .spec.started to DevWorkspaceRouting #604

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 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions apis/controller/v1alpha1/devworkspacerouting_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
type DevWorkspaceRoutingSpec struct {
// Id for the DevWorkspace being routed
DevWorkspaceId string `json:"devworkspaceId"`
// Started is true if the owning DevWorkspace has .spec.started=true
Started bool `json:"started"`
// Class of the routing: this drives which DevWorkspaceRouting controller will manage this routing
RoutingClass DevWorkspaceRoutingClass `json:"routingClass,omitempty"`
// Machines to endpoints map
Expand Down
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.Spec.Started {
return reconcile.Result{}, nil
}

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

Expand Down
20 changes: 20 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,25 @@ 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.Spec.Started {
routing.Spec.Started = false
err := r.Update(context.TODO(), routing)
if err != nil && !k8sErrors.IsConflict(err) {
return false, err
}
}

replicas := workspaceDeployment.Spec.Replicas
if replicas == nil || *replicas > 0 {
logger.Info("Stopping workspace")
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions deploy/deployment/kubernetes/combined.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions deploy/deployment/openshift/combined.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
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 @@ -169,7 +170,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 All @@ -178,6 +179,7 @@ func getSpecRouting(
},
Spec: v1alpha1.DevWorkspaceRoutingSpec{
DevWorkspaceId: workspace.Status.DevWorkspaceId,
Started: workspace.Spec.Started,
RoutingClass: v1alpha1.DevWorkspaceRoutingClass(routingClass),
Endpoints: endpoints,
PodSelector: map[string]string{
Expand Down