@@ -15,6 +15,7 @@ package controllers
15
15
16
16
import (
17
17
"context"
18
+ "fmt"
18
19
19
20
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
20
21
wkspConfig "github.com/devfile/devworkspace-operator/pkg/config"
@@ -47,7 +48,14 @@ func dwRelatedPodsHandler(obj client.Object) []reconcile.Request {
47
48
}
48
49
49
50
func (r * DevWorkspaceReconciler ) dwPVCHandler (obj client.Object ) []reconcile.Request {
51
+ if obj .GetDeletionTimestamp () == nil {
52
+ // Do not reconcile unless PVC is being deleted.
53
+ return []reconcile.Request {}
54
+ }
55
+
50
56
// Check if PVC is owned by a DevWorkspace (per-workspace storage case)
57
+ // TODO: Ensure all new and existing PVC's get the `controller.devfile.io/devworkspace_pvc_type` label.
58
+ // See: https://github.com/devfile/devworkspace-operator/issues/1250
51
59
for _ , ownerref := range obj .GetOwnerReferences () {
52
60
if ownerref .Kind != "DevWorkspace" {
53
61
continue
@@ -62,26 +70,49 @@ func (r *DevWorkspaceReconciler) dwPVCHandler(obj client.Object) []reconcile.Req
62
70
}
63
71
}
64
72
65
- // TODO: Label PVCs used for workspace storage so that they can be cleaned up if non-default name is used.
66
- // Otherwise, check if common PVC is deleted to make sure all DevWorkspaces see it happen
67
- if obj .GetName () != wkspConfig .GetGlobalConfig ().Workspace .PVCName || obj .GetDeletionTimestamp () == nil {
68
- // We're looking for a deleted common PVC
69
- return []reconcile.Request {}
73
+ pvcLabel := obj .GetLabels ()[constants .DevWorkspacePVCTypeLabel ]
74
+
75
+ // TODO: This check is for legacy reasons as existing PVCs might not have the `controller.devfile.io/devworkspace_pvc_type` label.
76
+ // Remove once https://github.com/devfile/devworkspace-operator/issues/1250 is resolved
77
+ if pvcLabel == "" {
78
+ if obj .GetName () != wkspConfig .GetGlobalConfig ().Workspace .PVCName {
79
+ // No need to reconcile if PVC doesn't have a PVC type label
80
+ // and it doesn't have a name of PVC from global config.
81
+ return []reconcile.Request {}
82
+ }
70
83
}
84
+
71
85
dwList := & dw.DevWorkspaceList {}
72
86
if err := r .Client .List (context .Background (), dwList , & client.ListOptions {Namespace : obj .GetNamespace ()}); err != nil {
73
87
return []reconcile.Request {}
74
88
}
75
89
var reconciles []reconcile.Request
90
+
76
91
for _ , workspace := range dwList .Items {
77
92
storageType := workspace .Spec .Template .Attributes .GetString (constants .DevWorkspaceStorageTypeAttribute , nil )
78
93
if storageType == constants .CommonStorageClassType || storageType == constants .PerUserStorageClassType || storageType == "" {
79
- reconciles = append (reconciles , reconcile.Request {
80
- NamespacedName : types.NamespacedName {
81
- Name : workspace .GetName (),
82
- Namespace : workspace .GetNamespace (),
83
- },
84
- })
94
+
95
+ // Determine workspaces to reconcile that use the current common PVC.
96
+ // Workspaces can either use the common PVC where the PVC name
97
+ // is coming from the global config, or from an external config the workspace might use
98
+ workspacePVCName := wkspConfig .GetGlobalConfig ().Workspace .PVCName
99
+
100
+ if workspace .Spec .Template .Attributes .Exists (constants .ExternalDevWorkspaceConfiguration ) {
101
+ externalConfig , err := wkspConfig .ResolveConfigForWorkspace (& workspace , r .Client )
102
+ if err != nil {
103
+ 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 ()))
104
+ } else {
105
+ workspacePVCName = externalConfig .Workspace .PVCName
106
+ }
107
+ }
108
+ if obj .GetName () == workspacePVCName {
109
+ reconciles = append (reconciles , reconcile.Request {
110
+ NamespacedName : types.NamespacedName {
111
+ Name : workspace .GetName (),
112
+ Namespace : workspace .GetNamespace (),
113
+ },
114
+ })
115
+ }
85
116
}
86
117
}
87
118
return reconciles
0 commit comments