Skip to content

Commit

Permalink
Queue reconcile for all common storage-type workspaces on PVC deletion
Browse files Browse the repository at this point in the history
To make sure workspaces that are in an errored state due to PVC cleanup
failing are removed when the common PVC is deleted (e.g. when all
DevWorkspaces are deleted), add a watch and enqueue reconciles for all
common-storage workspaces when the common PVC is deleted.

Signed-off-by: Angel Misevski <[email protected]>
  • Loading branch information
amisevsk committed Jul 4, 2022
1 parent 225fbf1 commit a2e149f
Showing 1 changed file with 39 additions and 17 deletions.
56 changes: 39 additions & 17 deletions controllers/workspace/devworkspace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,28 +636,49 @@ func (r *DevWorkspaceReconciler) getWorkspaceId(ctx context.Context, workspace *
}

// Mapping the pod to the devworkspace
func dwRelatedPodsHandler() handler.EventHandler {
podToDW := func(obj client.Object) []reconcile.Request {
labels := obj.GetLabels()
if _, ok := labels[constants.DevWorkspaceNameLabel]; !ok {
return nil
}
func dwRelatedPodsHandler(obj client.Object) []reconcile.Request {
labels := obj.GetLabels()
if _, ok := labels[constants.DevWorkspaceNameLabel]; !ok {
return []reconcile.Request{}
}

//If the dewworkspace label does not exist, do no reconcile
if _, ok := labels[constants.DevWorkspaceIDLabel]; !ok {
return nil
}
//If the dewworkspace label does not exist, do no reconcile
if _, ok := labels[constants.DevWorkspaceIDLabel]; !ok {
return []reconcile.Request{}
}

return []reconcile.Request{
{
return []reconcile.Request{
{
NamespacedName: types.NamespacedName{
Name: labels[constants.DevWorkspaceNameLabel],
Namespace: obj.GetNamespace(),
},
},
}
}

func (r *DevWorkspaceReconciler) dwPVCHandler(obj client.Object) []reconcile.Request {
if obj.GetName() != config.Workspace.PVCName || obj.GetDeletionTimestamp() == nil {
// We're looking for a deleted common PVC
return []reconcile.Request{}
}
dwList := &dw.DevWorkspaceList{}
if err := r.Client.List(context.Background(), dwList); err != nil {
return []reconcile.Request{}
}
var reconciles []reconcile.Request
for _, workspace := range dwList.Items {
storageType := workspace.Spec.Template.Attributes.GetString(constants.DevWorkspaceStorageTypeAttribute, nil)
if storageType == constants.CommonStorageClassType || storageType == "" {
reconciles = append(reconciles, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: labels[constants.DevWorkspaceNameLabel],
Namespace: obj.GetNamespace(),
Name: workspace.GetName(),
Namespace: workspace.GetNamespace(),
},
},
})
}
}
return handler.EnqueueRequestsFromMapFunc(podToDW)
return reconciles
}

func (r *DevWorkspaceReconciler) SetupWithManager(mgr ctrl.Manager) error {
Expand Down Expand Up @@ -688,7 +709,8 @@ func (r *DevWorkspaceReconciler) SetupWithManager(mgr ctrl.Manager) error {
Owns(&corev1.ConfigMap{}).
Owns(&corev1.Secret{}).
Owns(&corev1.ServiceAccount{}).
Watches(&source.Kind{Type: &corev1.Pod{}}, dwRelatedPodsHandler()).
Watches(&source.Kind{Type: &corev1.Pod{}}, handler.EnqueueRequestsFromMapFunc(dwRelatedPodsHandler)).
Watches(&source.Kind{Type: &corev1.PersistentVolumeClaim{}}, handler.EnqueueRequestsFromMapFunc(r.dwPVCHandler)).
Watches(&source.Kind{Type: &controllerv1alpha1.DevWorkspaceOperatorConfig{}}, handler.EnqueueRequestsFromMapFunc(emptyMapper), configWatcher).
WithEventFilter(predicates).
WithEventFilter(podPredicates).
Expand Down

0 comments on commit a2e149f

Please sign in to comment.