@@ -18,6 +18,7 @@ package syncer
1818
1919import (
2020 "context"
21+ "errors"
2122 "fmt"
2223 "os"
2324 "path/filepath"
@@ -1085,7 +1086,7 @@ func initStorageQuotaPeriodicSync(ctx context.Context, metadataSyncer *metadataS
10851086func syncStorageQuotaReserved (ctx context.Context ,
10861087 cnsOperatorClient client.Client , metadataSyncer * metadataSyncInformer ) {
10871088 log := logger .GetLogger (ctx )
1088- lastSyncTime := metav1 . NewTime ( time . Now ())
1089+
10891090 // fetching storagepolicyquota
10901091 log .Info ("syncStorageQuotaReserved: Sync started for storage quota" )
10911092 spqList := & storagepolicyv1alpha2.StoragePolicyQuotaList {}
@@ -1108,11 +1109,13 @@ func syncStorageQuotaReserved(ctx context.Context,
11081109 return
11091110 }
11101111 var totalStoragePolicyReserved map [string ]* resource.Quantity
1112+ var hasValidData bool
11111113 expectedReservedValues := []sqperiodicsyncv1alpha1.ExpectedReservedValues {}
11121114 log .Debug ("syncStorageQuotaReserved: iterate through namespaces and calcuate reserved values" )
11131115 // loop over namespaces and calcuate reserved values
11141116 for ns := range namespaces {
11151117 totalStoragePolicyReserved = make (map [string ]* resource.Quantity )
1118+ hasValidData = false
11161119 log .Debugf ("syncStorageQuotaReserved: processing storage quota sync for namespace %q" , ns )
11171120 // calculate VM service's storagepolicyusage reserved values for given namespace
11181121 spuVmServiceReserved , err := calculateVMServiceStoragePolicyUsageReservedForNamespace (ctx ,
@@ -1123,6 +1126,13 @@ func syncStorageQuotaReserved(ctx context.Context,
11231126 continue
11241127 }
11251128 if spuVmServiceReserved != nil {
1129+ if ! validateReservedValues (spuVmServiceReserved ) {
1130+ log .Errorf ("syncStorageQuotaReserved: error while calculating expected VmService StoragePolicyUsage" +
1131+ " reserved value for namespace %s, Error: %v" , ns ,
1132+ errors .New ("expected reserved is negative value" ))
1133+ continue
1134+ }
1135+ hasValidData = true
11261136 totalStoragePolicyReserved = mergeStoragePolicyReserved (spuVmServiceReserved , totalStoragePolicyReserved )
11271137 }
11281138
@@ -1134,6 +1144,12 @@ func syncStorageQuotaReserved(ctx context.Context,
11341144 continue
11351145 }
11361146 if pvcReserved != nil {
1147+ if ! validateReservedValues (pvcReserved ) {
1148+ log .Errorf ("syncStorageQuotaReserved: error while calculating expected PVC reserved value for" +
1149+ " given namespace %q, Error: %v" , ns , errors .New ("expected reserved is negative value" ))
1150+ continue
1151+ }
1152+ hasValidData = true
11371153 totalStoragePolicyReserved = mergeStoragePolicyReserved (pvcReserved , totalStoragePolicyReserved )
11381154 }
11391155
@@ -1145,6 +1161,12 @@ func syncStorageQuotaReserved(ctx context.Context,
11451161 continue
11461162 }
11471163 if vsReserved != nil {
1164+ if ! validateReservedValues (vsReserved ) {
1165+ log .Errorf ("syncStorageQuotaReserved: error while calculating expected VolumeSnapshot reserved value for" +
1166+ " given namespace %q, Error: %v" , ns , errors .New ("expected reserved is negative value" ))
1167+ continue
1168+ }
1169+ hasValidData = true
11481170 totalStoragePolicyReserved = mergeStoragePolicyReserved (vsReserved , totalStoragePolicyReserved )
11491171 }
11501172
@@ -1158,19 +1180,38 @@ func syncStorageQuotaReserved(ctx context.Context,
11581180 continue
11591181 }
11601182 if sprReserved != nil {
1183+ if ! validateReservedValues (sprReserved ) {
1184+ log .Errorf ("syncStorageQuotaReserved: error while calculating expected reserved value for" +
1185+ " StoragePolicyReservation in namespace %q, Error: %v" , ns ,
1186+ errors .New ("expected reserved is negative value" ))
1187+ continue
1188+ }
1189+ hasValidData = true
11611190 totalStoragePolicyReserved = mergeStoragePolicyReserved (sprReserved , totalStoragePolicyReserved )
11621191 }
11631192 }
1193+ if hasValidData && len (totalStoragePolicyReserved ) > 0 {
1194+ expectedReservedValues = append (expectedReservedValues , sqperiodicsyncv1alpha1.ExpectedReservedValues {
1195+ Namespace : ns ,
1196+ Reserved : totalStoragePolicyReserved ,
1197+ })
1198+ }
11641199
1165- expectedReservedValues = append (expectedReservedValues , sqperiodicsyncv1alpha1.ExpectedReservedValues {
1166- Namespace : ns ,
1167- Reserved : totalStoragePolicyReserved ,
1168- })
11691200 }
1201+ lastSyncTime := metav1 .NewTime (time .Now ())
11701202 updateStorageQuotaPeriodicSyncCR (ctx , cnsOperatorClient , expectedReservedValues , lastSyncTime )
11711203 log .Debug ("syncStorageQuotaReserved: updated the StorageQuotaPeriodicSync CR" )
11721204}
11731205
1206+ func validateReservedValues (reservedValueMap map [string ]* resource.Quantity ) bool {
1207+ for _ , policyReservedValue := range reservedValueMap {
1208+ if policyReservedValue .Cmp (resource .MustParse ("0" )) < 0 {
1209+ return false
1210+ }
1211+ }
1212+ return true
1213+ }
1214+
11741215// mergeStoragePolicyReserved will sum up and return the total reserved value
11751216func mergeStoragePolicyReserved (storagePolicyReserved ,
11761217 totalStoragepolicyReserved map [string ]* resource.Quantity ) map [string ]* resource.Quantity {
@@ -1207,7 +1248,8 @@ func calculateVMServiceStoragePolicyUsageReservedForNamespace(ctx context.Contex
12071248 storagePolicyUsage .Name , storagePolicyUsage .Namespace )
12081249 continue
12091250 }
1210- if storagePolicyUsage .Status .ResourceTypeLevelQuotaUsage == nil {
1251+ if storagePolicyUsage .Status .ResourceTypeLevelQuotaUsage == nil ||
1252+ storagePolicyUsage .Status .ResourceTypeLevelQuotaUsage .Reserved == nil {
12111253 log .Debugf ("calculateVMServiceStoragePolicyUsageReservedForNamespace: StoragePolicyUsage" +
12121254 " status not populated, continue processing other PVCs Name: %q, Namespace: %q" ,
12131255 storagePolicyUsage .Name , storagePolicyUsage .Namespace )
@@ -1266,17 +1308,27 @@ func calculatePVCReservedForNamespace(ctx context.Context, volumeMap map[string]
12661308 }
12671309 }
12681310 // check if pvc is in pending state
1311+ if pvc .Spec .Resources .Requests == nil {
1312+ log .Debugf ("calculatePVCReservedForNamespace: pvc resources not populated, continuing processing others" +
1313+ " Name: %q, Namespace: %q" , pvc .Name , pvc .Namespace )
1314+ continue
1315+ }
12691316 pvcSize := pvc .Spec .Resources .Requests [v1 .ResourceStorage ]
12701317 if pvc .Status .Phase == v1 .ClaimPending {
12711318 if storagePolicyID , ok := scToStoragePolicyIDMap [* pvc .Spec .StorageClassName ]; ok {
12721319 log .Debugf ("calculatePVCReservedForNamespace: pvc is in pending state," +
12731320 " Name: %q, Namespace: %q adding pvc capacity to reserved" , pvc .Name , pvc .Namespace )
12741321 storagePolicyToReservedMap [storagePolicyID ].Add (pvcSize )
12751322 }
1276- } else if pv , ok := volumeMap [pvc .Name ]; ok {
1323+ } else if pv , ok := volumeMap [pvc .Namespace + "$" + pvc .Name ]; ok {
1324+ if pv .Spec .Capacity == nil {
1325+ log .Debugf ("calculatePVCReservedForNamespace: pv capacity not populated, continuing" +
1326+ " Name: %q, Namespace: %q" , pvc .Name , pvc .Namespace )
1327+ continue
1328+ }
12771329 pvSize := pv .Spec .Capacity [v1 .ResourceStorage ]
12781330 // check if pvc is under expansion
1279- if ! pvcSize .Equal (pvSize ) {
1331+ if pvcSize .Cmp (pvSize ) > 0 {
12801332 log .Debugf ("calculatePVCReservedForNamespace: pvc size being expanded" +
12811333 " Name: %q, Namespace: %q adding pvc capacity to reserved" , pvc .Name , pvc .Namespace )
12821334 if storagePolicyID , ok := scToStoragePolicyIDMap [* pvc .Spec .StorageClassName ]; ok {
@@ -1310,7 +1362,7 @@ func calculateVolumeSnapshotReservedForNamespace(ctx context.Context,
13101362 " error: %v in namespace: %s" , err , namespace )
13111363 return nil , err
13121364 }
1313- pvcList := & v1. PersistentVolumeClaimList {}
1365+ pvcListFetched := false
13141366 volumeClaim := & v1.PersistentVolumeClaim {}
13151367 volumeMap := make (map [string ]* v1.PersistentVolumeClaim )
13161368 storagePolicyIdToReservedMap := make (map [string ]* resource.Quantity )
@@ -1328,7 +1380,7 @@ func calculateVolumeSnapshotReservedForNamespace(ctx context.Context,
13281380 } else {
13291381 log .Debugf ("calculateVolumeSnapshotReservedForNamespace: Processing VolumeSnapshot" +
13301382 " Name: %q, Namespace: %q" , vs .Name , vs .Namespace )
1331- if len ( pvcList . Items ) == 0 {
1383+ if ! pvcListFetched {
13321384 log .Debugf ("calculateVolumeSnapshotReservedForNamespace: Fetching PersistentVolumeClaim " +
13331385 " for Namespace: %q" , vs .Namespace )
13341386 pvcList , err := metadataSyncer .pvcLister .PersistentVolumeClaims (namespace ).List (labels .NewSelector ())
@@ -1337,12 +1389,28 @@ func calculateVolumeSnapshotReservedForNamespace(ctx context.Context,
13371389 " from namespace %q, Error %v" , namespace , err )
13381390 return nil , err
13391391 }
1392+ pvcListFetched = true
13401393 for _ , pvc := range pvcList {
1341- volumeMap [pvc .Name ] = pvc
1394+ volumeMap [pvc .Namespace + "$" + pvc . Name ] = pvc
13421395 }
13431396 }
13441397 if vs .Spec .Source .PersistentVolumeClaimName != nil {
1345- volumeClaim = volumeMap [* vs .Spec .Source .PersistentVolumeClaimName ]
1398+ volumeClaim = volumeMap [vs .Namespace + "$" + * vs .Spec .Source .PersistentVolumeClaimName ]
1399+ if volumeClaim == nil {
1400+ log .Debugf ("calculateVolumeSnapshotReservedForNamespace: PVC not found for VolumeSnapshot" +
1401+ " Name: %q, Namespace: %q, PVC: %q" , vs .Name , vs .Namespace , * vs .Spec .Source .PersistentVolumeClaimName )
1402+ continue
1403+ }
1404+ if volumeClaim .Spec .StorageClassName == nil {
1405+ log .Debugf ("calculateVolumeSnapshotReservedForNamespace: StorageClassName is nil for PVC" +
1406+ " Name: %q, Namespace: %q" , volumeClaim .Name , volumeClaim .Namespace )
1407+ continue
1408+ }
1409+ if volumeClaim .Spec .Resources .Requests == nil {
1410+ log .Debugf ("calculateVolumeSnapshotReservedForNamespace: PVC resources not populated" +
1411+ " Name: %q, Namespace: %q" , volumeClaim .Name , volumeClaim .Namespace )
1412+ continue
1413+ }
13461414 if spu , ok := scToStoragPolicyIdMap [* volumeClaim .Spec .StorageClassName ]; ok {
13471415 if storagePolicyIdToReservedMap [spu ] == nil {
13481416 storagePolicyIdToReservedMap [spu ] = resource .NewQuantity (0 , resource .BinarySI )
@@ -1567,7 +1635,7 @@ func fetchPVs(ctx context.Context, metadataSyncer *metadataSyncInformer) (map[st
15671635 volumeMap := make (map [string ]* v1.PersistentVolume )
15681636 for _ , pv := range pvList {
15691637 if pv .Spec .ClaimRef != nil && pv .Status .Phase != v1 .VolumePending {
1570- volumeMap [pv .Spec .ClaimRef .Name ] = pv
1638+ volumeMap [pv .Spec .ClaimRef .Namespace + "$" + pv . Spec . ClaimRef . Name ] = pv
15711639 }
15721640 }
15731641 return volumeMap , nil
0 commit comments