Skip to content

Commit

Permalink
Put finalizers back if the are missing
Browse files Browse the repository at this point in the history
  • Loading branch information
mhmxs committed Nov 7, 2022
1 parent 2c85d12 commit d3fdc47
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
21 changes: 20 additions & 1 deletion controllers/pvc_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,28 @@ func (r *PVCReconciler) createPVC(config *discoblocksondatiov1.DiskConfig, pod *

if err = r.Create(ctx, topologySC); err != nil {
if !apierrors.IsAlreadyExists(err) {
logger.Error(err, "Failed to create StorageClass")
logger.Error(err, "Failed to create topology StorageClass")
return
}

logger.Info("Fetch topology StorageClass...")

if err = r.Client.Get(ctx, types.NamespacedName{Name: topologySC.Name}, topologySC); err != nil {
logger.Error(err, "Failed to fetch topology StorageClass")
return
}

scFinalizer := utils.RenderFinalizer(config.Name, config.Namespace)
if !controllerutil.ContainsFinalizer(topologySC, scFinalizer) {
controllerutil.AddFinalizer(topologySC, scFinalizer)

logger.Info("Update topology StorageClass finalizer...", "name", topologySC.Name)

if err = r.Client.Update(ctx, topologySC); err != nil {
logger.Error(err, "Failed to update topology StorageClass")
return
}
}
}

pvc.Spec.StorageClassName = &topologySC.Name
Expand Down
33 changes: 32 additions & 1 deletion mutators/pod_mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,26 @@ func (a *PodMutator) Handle(ctx context.Context, req admission.Request) admissio

logger.Info("PVC already exists")

finalizer := utils.RenderFinalizer(config.Name)

logger.Info("Fetch PVC...")

if err = a.Client.Get(ctx, types.NamespacedName{Name: pvc.Name, Namespace: pvc.Namespace}, pvc); err != nil {
logger.Error(err, "Unable to fetch PVC", "name", pvc.Name)
return admission.Errored(http.StatusInternalServerError, fmt.Errorf("unable to fetch PVC %s: %w", pvc.Name, err))
}

if !controllerutil.ContainsFinalizer(pvc, finalizer) {
controllerutil.AddFinalizer(pvc, finalizer)

logger.Info("Update PVC finalizer...", "name", pvc.Name)

if err = a.Client.Update(ctx, pvc); err != nil {
logger.Error(err, "Unable to update PVC finalizer", "name", pvc.Name)
return admission.Errored(http.StatusInternalServerError, fmt.Errorf("unable to update PVC finalizer %s: %w", pvc.Name, err))
}
}

if config.Spec.AvailabilityMode != discoblocksondatiov1.ReadWriteOnce {
label, err := labels.NewRequirement("discoblocks-parent", selection.Equals, []string{pvc.Name})
if err != nil {
Expand All @@ -254,10 +274,21 @@ func (a *PodMutator) Handle(ctx context.Context, req admission.Request) admissio
})

for i := range pvcs.Items {
if pvcs.Items[i].DeletionTimestamp != nil || !controllerutil.ContainsFinalizer(&pvcs.Items[i], utils.RenderFinalizer(config.Name)) {
if pvcs.Items[i].DeletionTimestamp != nil {
continue
}

if !controllerutil.ContainsFinalizer(&pvcs.Items[i], finalizer) {
controllerutil.AddFinalizer(&pvcs.Items[i], finalizer)

logger.Info("Update PVC child finalizer...", "name", pvcs.Items[i].Name)

if err = a.Client.Update(ctx, &pvcs.Items[i]); err != nil {
logger.Error(err, "Unable to update PVC finalizer", "name", pvcs.Items[i].Name)
return admission.Errored(http.StatusInternalServerError, fmt.Errorf("unable to update PVC finalizer %s: %w", pvcs.Items[i].Name, err))
}
}

if _, ok := pvcs.Items[i].Labels["discoblocks-index"]; !ok {
err = errors.New("volume index not found")
logger.Error(err, "Volume index not found")
Expand Down

0 comments on commit d3fdc47

Please sign in to comment.