Skip to content

Commit 13bf502

Browse files
committed
Adding allowVolumeExpansion variable in Cloudstack CSI storageClass Syncer
1 parent ccaedca commit 13bf502

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

cmd/cloudstack-csi-sc-syncer/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var (
2121
label = flag.String("label", "app.kubernetes.io/managed-by="+agent, "")
2222
namePrefix = flag.String("namePrefix", "cloudstack-", "")
2323
delete = flag.Bool("delete", false, "Delete")
24+
volumeExpansion = flag.Bool("volumeExpansion", false, "VolumeExpansion")
2425
showVersion = flag.Bool("version", false, "Show version")
2526

2627
// Version is set by the build process
@@ -43,6 +44,7 @@ func main() {
4344
Label: *label,
4445
NamePrefix: *namePrefix,
4546
Delete: *delete,
47+
VolumeExpansion: *volumeExpansion,
4648
})
4749
if err != nil {
4850
log.Fatalf("Error: %v", err)

pkg/syncer/run.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ import (
1717
)
1818

1919
var (
20-
volBindingMode = storagev1.VolumeBindingWaitForFirstConsumer
21-
reclaimPolicy = corev1.PersistentVolumeReclaimDelete
22-
allowVolumeExpansion = false
20+
volBindingMode = storagev1.VolumeBindingWaitForFirstConsumer
21+
reclaimPolicy = corev1.PersistentVolumeReclaimDelete
2322
)
2423

2524
func (s syncer) Run(ctx context.Context) error {
@@ -85,6 +84,11 @@ func (s syncer) Run(ctx context.Context) error {
8584
}
8685
}
8786

87+
if s.volumeExpansion {
88+
allowVolumeExpansion := s.volumeExpansion
89+
log.Printf("Setting allowVolumeExpasion to %t...", allowVolumeExpansion)
90+
}
91+
8892
if len(errs) == 0 {
8993
return nil
9094
}
@@ -123,7 +127,7 @@ func (s syncer) syncOffering(ctx context.Context, offering *cloudstack.DiskOffer
123127
Provisioner: driver.DriverName,
124128
VolumeBindingMode: &volBindingMode,
125129
ReclaimPolicy: &reclaimPolicy,
126-
AllowVolumeExpansion: &allowVolumeExpansion,
130+
AllowVolumeExpansion: &s.volumeExpansion,
127131
Parameters: map[string]string{
128132
driver.DiskOfferingKey: offering.Id,
129133
},
@@ -134,9 +138,20 @@ func (s syncer) syncOffering(ctx context.Context, offering *cloudstack.DiskOffer
134138
return "", err
135139
}
136140

141+
// Check and update AllowVolumeExpansion if necessary
142+
if sc.AllowVolumeExpansion == nil || *sc.AllowVolumeExpansion != s.volumeExpansion {
143+
log.Printf("Updating AllowVolumeExpansion for storage class %s", sc.Name)
144+
sc.AllowVolumeExpansion = &s.volumeExpansion
145+
146+
_, err = s.k8sClient.StorageV1().StorageClasses().Update(ctx, sc, metav1.UpdateOptions{})
147+
if err != nil {
148+
return "", fmt.Errorf("failed to update AllowVolumeExpansion for storage class %s: %w", sc.Name, err)
149+
}
150+
}
151+
137152
// Storage class already exists
138153

139-
err = checkStorageClass(sc, offering.Id)
154+
err = checkStorageClass(sc, offering.Id, s.volumeExpansion)
140155
if err != nil {
141156
// Updates to provisioner, reclaimpolicy, volumeBindingMode and parameters are forbidden
142157
log.Printf("Storage class %s exists but it not compatible.", name)
@@ -159,7 +174,7 @@ func (s syncer) syncOffering(ctx context.Context, offering *cloudstack.DiskOffer
159174
return name, nil
160175
}
161176

162-
func checkStorageClass(sc *storagev1.StorageClass, expectedOfferingID string) error {
177+
func checkStorageClass(sc *storagev1.StorageClass, expectedOfferingID string, expectedVolumeExpansion bool) error {
163178
errs := make([]error, 0)
164179
diskOfferingID, ok := sc.Parameters[driver.DiskOfferingKey]
165180
if !ok {
@@ -174,8 +189,8 @@ func checkStorageClass(sc *storagev1.StorageClass, expectedOfferingID string) er
174189
if sc.VolumeBindingMode == nil || *sc.VolumeBindingMode != volBindingMode {
175190
errs = append(errs, errors.New("wrong VolumeBindingMode"))
176191
}
177-
if sc.AllowVolumeExpansion == nil || *sc.AllowVolumeExpansion != allowVolumeExpansion {
178-
errs = append(errs, errors.New("wrong AllowVolumeExpansion"))
192+
if sc.AllowVolumeExpansion == nil || *sc.AllowVolumeExpansion != expectedVolumeExpansion {
193+
errs = append(errs, fmt.Errorf("wrong AllowVolumeExpansion for storage class %s", sc.Name))
179194
}
180195

181196
if len(errs) > 0 {

pkg/syncer/syncer.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type Config struct {
2626
Label string
2727
NamePrefix string
2828
Delete bool
29+
VolumeExpansion bool
2930
}
3031

3132
// Syncer has a function Run which synchronizes CloudStack
@@ -36,11 +37,12 @@ type Syncer interface {
3637

3738
// syncer is Syncer implementation.
3839
type syncer struct {
39-
k8sClient *kubernetes.Clientset
40-
csClient *cloudstack.CloudStackClient
41-
labelsSet labels.Set
42-
namePrefix string
43-
delete bool
40+
k8sClient *kubernetes.Clientset
41+
csClient *cloudstack.CloudStackClient
42+
labelsSet labels.Set
43+
namePrefix string
44+
delete bool
45+
volumeExpansion bool
4446
}
4547

4648
func createK8sClient(kubeconfig, agent string) (*kubernetes.Clientset, error) {
@@ -96,10 +98,11 @@ func New(config Config) (Syncer, error) {
9698
}
9799

98100
return syncer{
99-
k8sClient: k8sClient,
100-
csClient: csClient,
101-
labelsSet: createLabelsSet(config.Label),
102-
namePrefix: config.NamePrefix,
103-
delete: config.Delete,
101+
k8sClient: k8sClient,
102+
csClient: csClient,
103+
labelsSet: createLabelsSet(config.Label),
104+
namePrefix: config.NamePrefix,
105+
delete: config.Delete,
106+
volumeExpansion: config.VolumeExpansion,
104107
}, nil
105108
}

0 commit comments

Comments
 (0)