Skip to content

Commit ecfb532

Browse files
AObuchowibuziuk
authored andcommitted
feat: cleanup shared PVC when no longer in use
Fix #826 Signed-off-by: Andrew Obuchowicz <[email protected]>
1 parent 8f2da2b commit ecfb532

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

pkg/provision/storage/commonStorage.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ import (
1919
"fmt"
2020

2121
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
22+
"github.com/devfile/devworkspace-operator/pkg/config"
2223
"github.com/devfile/devworkspace-operator/pkg/provision/sync"
2324

2425
corev1 "k8s.io/api/core/v1"
26+
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
27+
"k8s.io/apimachinery/pkg/types"
2528

2629
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
2730
"github.com/devfile/devworkspace-operator/pkg/constants"
@@ -71,8 +74,35 @@ func (p *CommonStorageProvisioner) ProvisionStorage(podAdditions *v1alpha1.PodAd
7174
return nil
7275
}
7376

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
76106
}
77107

78108
// rewriteContainerVolumeMounts rewrites the VolumeMounts in a set of PodAdditions according to the 'common' PVC strategy

pkg/provision/storage/shared.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"k8s.io/apimachinery/pkg/api/resource"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828
"k8s.io/apimachinery/pkg/types"
29+
"sigs.k8s.io/controller-runtime/pkg/client"
2930

3031
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
3132
"github.com/devfile/devworkspace-operator/pkg/config"
@@ -235,3 +236,21 @@ func checkForExistingCommonPVC(namespace string, api sync.ClusterAPI) (string, e
235236
}
236237
return existingPVC.Name, nil
237238
}
239+
240+
// getSharedPVCWorkspaceCount returns the total number of workspaces which are using a shared PVC
241+
// (i.e the workspaces storage-class attribute is set to "common", "async", or unset which defaults to "common")
242+
func getSharedPVCWorkspaceCount(namespace string, api sync.ClusterAPI) (total int, err error) {
243+
workspaces := &dw.DevWorkspaceList{}
244+
err = api.Client.List(api.Ctx, workspaces, &client.ListOptions{Namespace: namespace})
245+
if err != nil {
246+
return 0, err
247+
}
248+
for _, workspace := range workspaces.Items {
249+
storageClass := workspace.Spec.Template.Attributes.GetString(constants.DevWorkspaceStorageTypeAttribute, nil)
250+
// Note, if the storageClass attribute isin't set (ie. storageClass == ""), then the storage class being used is "common"
251+
if storageClass == constants.AsyncStorageClassType || storageClass == constants.CommonStorageClassType || storageClass == "" {
252+
total++
253+
}
254+
}
255+
return total, nil
256+
}

0 commit comments

Comments
 (0)