Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksandr Zimin <[email protected]>
  • Loading branch information
AleksZimin committed May 8, 2024
1 parent b56d2e8 commit 85b6b3c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 36 deletions.
46 changes: 11 additions & 35 deletions images/controller/pkg/controller/nfs_storage_class_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,37 +353,8 @@ func reconcileStorageClassCreateFunc(
if created {
log.Info(fmt.Sprintf("[reconcileStorageClassCreateFunc] successfully create storage class, name: %s", newSC.Name))
} else {
log.Info(fmt.Sprintf("[reconcileStorageClassCreateFunc] a storage class %s already exists", newSC.Name))
diff := ""
for _, oldSC := range scList.Items {
if oldSC.Name == newSC.Name {
diff, err = GetSCDiff(&oldSC, newSC)
break
}
}
if err != nil {
log.Error(err, fmt.Sprintf("[reconcileStorageClassCreateFunc] Error occured while identifying the difference between the existed StorageClass %s and the new one", newSC.Name))
upError := updateNFSStorageClassPhase(ctx, cl, nsc, FailedStatusPhase, err.Error())
if upError != nil {
log.Error(upError, fmt.Sprintf("[reconcileStorageClassCreateFunc] unable to update the NFSStorageClass %s", nsc.Name))
}
return true, err
}
if diff != "" {
log.Info(fmt.Sprintf("[reconcileStorageClassCreateFunc] current Storage Class %s differs from the NFSStorageClass one. The Storage Class will be recreated", newSC.Name))
err := recreateStorageClass(ctx, cl, newSC)
if err != nil {
log.Error(err, fmt.Sprintf("[reconcileStorageClassCreateFunc] unable to recreate a Storage Class %s", newSC.Name))
upError := updateNFSStorageClassPhase(ctx, cl, nsc, FailedStatusPhase, err.Error())
if upError != nil {
log.Error(upError, fmt.Sprintf("[reconcileStorageClassCreateFunc] unable to update the NFSStorageClass %s", nsc.Name))
}
return true, err
}
log.Info(fmt.Sprintf("[reconcileStorageClassCreateFunc] a Storage Class %s was successfully recreated", newSC.Name))
} else {
log.Info(fmt.Sprintf("[reconcileStorageClassCreateFunc] the Storage Class %s is up-to-date", newSC.Name))
}
log.Warning(fmt.Sprintf("[reconcileLSCCreateFunc] Storage class %s already exists. Adding event to requeue.", newSC.Name))
return true, nil
}

return false, nil
Expand Down Expand Up @@ -434,7 +405,7 @@ func reconcileStorageClassUpdateFunc(
if diff != "" {
log.Info(fmt.Sprintf("[reconcileStorageClassUpdateFunc] current Storage Class LVMVolumeGroups do not match NFSStorageClass ones. The Storage Class %s will be recreated with new ones", nsc.Name))

err = recreateStorageClass(ctx, cl, newSC)
err = recreateStorageClass(ctx, cl, oldSC, newSC)
if err != nil {
log.Error(err, fmt.Sprintf("[reconcileStorageClassUpdateFunc] unable to recreate a Storage Class %s", newSC.Name))
upError := updateNFSStorageClassPhase(ctx, cl, nsc, FailedStatusPhase, err.Error())
Expand Down Expand Up @@ -622,14 +593,19 @@ func updateNFSStorageClassPhase(ctx context.Context, cl client.Client, nsc *v1al
return nil
}

func recreateStorageClass(ctx context.Context, cl client.Client, sc *v1.StorageClass) error {
err := deleteStorageClass(ctx, cl, sc)
func recreateStorageClass(ctx context.Context, cl client.Client, oldSC, newSC *v1.StorageClass) error {
// It is necessary to pass the original StorageClass to the delete operation because
// the deletion will not succeed if the fields in the StorageClass provided to delete
// differ from those currently in the cluster.
err := deleteStorageClass(ctx, cl, oldSC)
if err != nil {
err = fmt.Errorf("[recreateStorageClass] unable to delete a storage class %s: %s", oldSC.Name, err.Error())
return err
}

err = cl.Create(ctx, sc)
err = cl.Create(ctx, newSC)
if err != nil {
err = fmt.Errorf("[recreateStorageClass] unable to create a storage class %s: %s", newSC.Name, err.Error())
return err
}

Expand Down
2 changes: 1 addition & 1 deletion images/webhooks/src/handlers/scValidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func SCValidate(ctx context.Context, arReview *model.AdmissionReview, obj metav1
nil
} else {
klog.Infof("User %s is not allowed to manage storage classes with provisioner %s", arReview.UserInfo.Username, NFSStorageClassProvisioner)
return &kwhvalidating.ValidatorResult{Valid: false, Message: fmt.Sprintf("Manual operations with StorageClass with provisioner %s are not allowed. Please use NFSStorageClass instead.", NFSStorageClassProvisioner)},
return &kwhvalidating.ValidatorResult{Valid: false, Message: fmt.Sprintf("Manual operations with the StorageClass that uses the %s provisioner are not allowed. Please use NFSStorageClass instead.", NFSStorageClassProvisioner)},
nil
}
} else {
Expand Down

0 comments on commit 85b6b3c

Please sign in to comment.