@@ -19,9 +19,12 @@ import (
19
19
"fmt"
20
20
21
21
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
22
+ "github.com/devfile/devworkspace-operator/pkg/config"
22
23
"github.com/devfile/devworkspace-operator/pkg/provision/sync"
23
24
24
25
corev1 "k8s.io/api/core/v1"
26
+ k8sErrors "k8s.io/apimachinery/pkg/api/errors"
27
+ "k8s.io/apimachinery/pkg/types"
25
28
26
29
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
27
30
"github.com/devfile/devworkspace-operator/pkg/constants"
@@ -71,8 +74,35 @@ func (p *CommonStorageProvisioner) ProvisionStorage(podAdditions *v1alpha1.PodAd
71
74
return nil
72
75
}
73
76
74
- func (* CommonStorageProvisioner ) CleanupWorkspaceStorage (workspace * dw.DevWorkspace , clusterAPI sync.ClusterAPI ) error {
75
- return runCommonPVCCleanupJob (workspace , clusterAPI )
77
+ func (p * CommonStorageProvisioner ) CleanupWorkspaceStorage (workspace * dw.DevWorkspace , clusterAPI sync.ClusterAPI ) error {
78
+ totalWorkspaces , err := getSharedPVCWorkspaceCount (workspace .Namespace , clusterAPI )
79
+ if err != nil {
80
+ return err
81
+ }
82
+
83
+ // If the number of common + async workspaces that exist (started or stopped) is zero,
84
+ // delete common PVC instead of running cleanup job
85
+ if totalWorkspaces > 1 {
86
+ return runCommonPVCCleanupJob (workspace , clusterAPI )
87
+ } else {
88
+ sharedPVC := & corev1.PersistentVolumeClaim {}
89
+ namespacedName := types.NamespacedName {Name : config .Workspace .PVCName , Namespace : workspace .Namespace }
90
+ err := clusterAPI .Client .Get (clusterAPI .Ctx , namespacedName , sharedPVC )
91
+
92
+ if err != nil {
93
+ if k8sErrors .IsNotFound (err ) {
94
+ return nil
95
+ }
96
+ return err
97
+ }
98
+
99
+ err = clusterAPI .Client .Delete (clusterAPI .Ctx , sharedPVC )
100
+ if err != nil && ! k8sErrors .IsNotFound (err ) {
101
+ return err
102
+ }
103
+ }
104
+
105
+ return nil
76
106
}
77
107
78
108
// rewriteContainerVolumeMounts rewrites the VolumeMounts in a set of PodAdditions according to the 'common' PVC strategy
0 commit comments