Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mark the topolvm storageclass as default #210

Merged
merged 1 commit into from
Jun 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions controllers/topolvm_storageclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (s topolvmStorageClass) getName() string {
func (s topolvmStorageClass) ensureCreated(r *LVMClusterReconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error {

// one storage class for every deviceClass based on CR is created
topolvmStorageClasses := getTopolvmStorageClasses(lvmCluster)
topolvmStorageClasses := getTopolvmStorageClasses(r, ctx, lvmCluster)
for _, sc := range topolvmStorageClasses {

// we anticipate no edits to storage class
Expand Down Expand Up @@ -85,15 +85,36 @@ func (s topolvmStorageClass) updateStatus(r *LVMClusterReconciler, ctx context.C
return nil
}

func getTopolvmStorageClasses(lvmCluster *lvmv1alpha1.LVMCluster) []*storagev1.StorageClass {
sc := []*storagev1.StorageClass{}
func getTopolvmStorageClasses(r *LVMClusterReconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) []*storagev1.StorageClass {

const defaultSCAnnotation string = "storageclass.kubernetes.io/is-default-class"
allowVolumeExpansion := true
volumeBindingMode := storagev1.VolumeBindingWaitForFirstConsumer

defaultStorageClassName := ""
setDefaultStorageClass := true

// Mark the first lvmo storage class default if no other storageclasses exist on the cluster
scList := &storagev1.StorageClassList{}
err := r.Client.List(ctx, scList)

if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if else block can be removed and only for loop can be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to set a new var in the if block.

r.Log.Error(err, "failed to list storage classes. Not setting any storageclass as the default")
setDefaultStorageClass = false
} else {
for _, sc := range scList.Items {
v := sc.Annotations[defaultSCAnnotation]
if v == "true" {
defaultStorageClassName = sc.Name
break
}
}
}
sc := []*storagev1.StorageClass{}
for _, deviceClass := range lvmCluster.Spec.Storage.DeviceClasses {
scName := "odf-lvm-" + deviceClass.Name

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we define a new variable called scPrefix := "odf-lvm-" and use it everywhere in place of this string?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would need to be done in a separate PR.

storageClass := &storagev1.StorageClass{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("odf-lvm-%s", deviceClass.Name),
Name: scName,
Annotations: map[string]string{
"description": "Provides RWO and RWOP Filesystem & Block volumes",
},
Expand All @@ -106,6 +127,11 @@ func getTopolvmStorageClasses(lvmCluster *lvmv1alpha1.LVMCluster) []*storagev1.S
"csi.storage.k8s.io/fstype": TopolvmFilesystemType,
},
}
// reconcile will pick up any existing LVMO storage classes as well
if setDefaultStorageClass && (defaultStorageClassName == "" || defaultStorageClassName == scName) {
storageClass.Annotations[defaultSCAnnotation] = "true"
defaultStorageClassName = scName
}
sc = append(sc, storageClass)
}
return sc
Expand Down