Skip to content

Commit

Permalink
fixup! fixup! fixup! Update controllers/workspace/eventhandlers.go
Browse files Browse the repository at this point in the history
  • Loading branch information
mkuznyetsov committed Apr 10, 2024
1 parent 8fee274 commit 37212ba
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
33 changes: 18 additions & 15 deletions controllers/workspace/eventhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package controllers

import (
"context"
"fmt"

dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
wkspConfig "github.com/devfile/devworkspace-operator/pkg/config"
Expand Down Expand Up @@ -48,12 +49,13 @@ func dwRelatedPodsHandler(obj client.Object) []reconcile.Request {

func (r *DevWorkspaceReconciler) dwPVCHandler(obj client.Object) []reconcile.Request {
if obj.GetDeletionTimestamp() == nil {
// Do not reconcile unless PVC is being deleted.
return []reconcile.Request{}
}
// we can wrap the code below for handling per-workspace PVCs
// in the if-block to check for presence of the respective label,
// once we ensure that it is applied to all per-workspace PVCs
// see comments to https://github.com/devfile/devworkspace-operator/pull/1233/files

// Check if PVC is owned by a DevWorkspace (per-workspace storage case)
// TODO: Ensure all new and existing PVC's get the `controller.devfile.io/devworkspace_pvc_type` label.
// See: https://github.com/devfile/devworkspace-operator/issues/1250
for _, ownerref := range obj.GetOwnerReferences() {
if ownerref.Kind != "DevWorkspace" {
continue
Expand All @@ -69,12 +71,13 @@ func (r *DevWorkspaceReconciler) dwPVCHandler(obj client.Object) []reconcile.Req
}

pvcLabel := obj.GetLabels()[constants.DevWorkspacePVCTypeLabel]
// No need to reconcile if PVC is being deleted, or it doesn't have a PVC type label.
// However, since it is possible for PVCs to not have such label,
// we will handle this PVC if it has a name that correspons with PVC name in global config
// see comments to https://github.com/devfile/devworkspace-operator/pull/1233/files

// TODO: This check is for legacy reasons as existing PVCs might not have the `controller.devfile.io/devworkspace_pvc_type` label.
// Remove once https://github.com/devfile/devworkspace-operator/issues/1250 is resolved
if pvcLabel != "" {
if obj.GetName() != wkspConfig.GetGlobalConfig().Workspace.PVCName {
// No need to reconcile if PVC doesn't have a PVC type label
// and it doesn't have a name of PVC from global config.
return []reconcile.Request{}
}
}
Expand All @@ -86,21 +89,21 @@ func (r *DevWorkspaceReconciler) dwPVCHandler(obj client.Object) []reconcile.Req
var reconciles []reconcile.Request

for _, workspace := range dwList.Items {
//Determine workspaces to reconcile that use the current common PVC.
// Determine workspaces to reconcile that use the current common PVC.
// Workspaces can either use the common PVC where the PVC name
// is coming from the global config, or from an external config the workspace might use
workspacePVCName := wkspConfig.GetGlobalConfig().Workspace.PVCName

if workspace.Spec.Template.Attributes.Exists(constants.ExternalDevWorkspaceConfiguration) {
externalConfig, err := wkspConfig.ResolveConfigForWorkspace(&workspace, r.Client)
if err != nil {
r.Log.Info("Couldn't fetch external config for workspace %s, using PVC Name from global config instead", err.Error())
r.Log.Info(fmt.Sprintf("Couldn't resolve PVC name for workspace '%s' in namespace '%s', using PVC name '%s' from global config instead: %s.", workspace.Name, workspace.Namespace, workspacePVCName, err.Error()))
} else {
storageType := workspace.Spec.Template.Attributes.GetString(constants.DevWorkspaceStorageTypeAttribute, nil)
if storageType == constants.CommonStorageClassType || storageType == constants.PerUserStorageClassType {
workspacePVCName = externalConfig.Workspace.PVCName
}
}
storageType := workspace.Spec.Template.Attributes.GetString(constants.DevWorkspaceStorageTypeAttribute, nil)
if storageType == constants.CommonStorageClassType || storageType == constants.PerUserStorageClassType {
workspacePVCName = externalConfig.Workspace.PVCName
}

}
if obj.GetName() == workspacePVCName {
reconciles = append(reconciles, reconcile.Request{
Expand Down
2 changes: 1 addition & 1 deletion pkg/constants/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
DevWorkspaceIDLabel = "controller.devfile.io/devworkspace_id"

// DevWorkspacePVCTypeLabel is the label key to identify PVCs used by DevWorkspaces and indicate their storage strategy.
DevWorkspacePVCTypeLabel = "controller.devfile.io/devworkspace_pvc_type_label"
DevWorkspacePVCTypeLabel = "controller.devfile.io/devworkspace_pvc_type"

// WorkspaceIdOverrideAnnotation is an annotation that can be applied to DevWorkspaces
// to override the default DevWorkspace ID assigned by the Operator. Is only respected
Expand Down

0 comments on commit 37212ba

Please sign in to comment.