diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index 0173a60f..b3c94326 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -4,6 +4,8 @@ const ( DefaultIAMEndPoint = "https://iam.cloud.ibm.com" DefaultVolumesPerNode = 4 + TimeDelayInMin float64 = 15 + KPEncryptionAlgorithm = "AES256" // https://github.com/IBM/ibm-cos-sdk-go/blob/master/service/s3/api.go#L9130-L9136 S3FS = "s3fs" diff --git a/pkg/driver/nodeserver.go b/pkg/driver/nodeserver.go index b9d4f6db..bfe7bcbf 100644 --- a/pkg/driver/nodeserver.go +++ b/pkg/driver/nodeserver.go @@ -12,6 +12,7 @@ package driver import ( "fmt" + "time" "github.com/IBM/ibm-object-csi-driver/pkg/constants" "github.com/IBM/ibm-object-csi-driver/pkg/mounter" @@ -24,13 +25,21 @@ import ( "k8s.io/klog/v2" ) +type NodeVolStats struct { + setTime time.Time + // capAvailable int64 + capTotal int64 + capUsed int64 +} + // Implements Node Server csi.NodeServer type nodeServer struct { *S3Driver - Stats utils.StatsUtils - NodeID string - Mounter mounter.NewMounterFactory - MounterUtils mounterUtils.MounterUtils + Stats utils.StatsUtils + NodeID string + Mounter mounter.NewMounterFactory + MounterUtils mounterUtils.MounterUtils + VolumeIDAndTimeMap map[string]NodeVolStats } func (ns *nodeServer) NodeStageVolume(_ context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) { @@ -214,31 +223,41 @@ func (ns *nodeServer) NodeGetVolumeStats(_ context.Context, req *csi.NodeGetVolu }, nil } - totalCap, err := ns.Stats.GetTotalCapacityFromPV(volumeID) - if err != nil { - return nil, err - } + timeSetInMap := ns.VolumeIDAndTimeMap[volumeID].setTime + if timeSetInMap.Second() == 0 || time.Since(timeSetInMap).Minutes() >= constants.TimeDelayInMin { + totalCap, err := ns.Stats.GetTotalCapacityFromPV(volumeID) + if err != nil { + return nil, err + } - capAsInt64, converted := totalCap.AsInt64() - if !converted { - capAsInt64 = capacity - } - klog.Info("NodeGetVolumeStats: Total Capacity of Volume: ", capAsInt64) + capAsInt64, converted := totalCap.AsInt64() + if !converted { + capAsInt64 = capacity + } + klog.Info("NodeGetVolumeStats: Total Capacity of Volume: ", capAsInt64) - capUsed, err := ns.Stats.GetBucketUsage(volumeID) - if err != nil { - return nil, err - } + capUsed, err := ns.Stats.GetBucketUsage(volumeID) + if err != nil { + return nil, err + } + + // Since `capAvailable` can be negative and K8s will roundoff from int64 to uint64 resulting in misleading value + // capAvailable := capAsInt64 - capUsed - // Since `capAvailable` can be negative and K8s will roundoff from int64 to uint64 resulting in misleading value - // capAvailable := capAsInt64 - capUsed + ns.VolumeIDAndTimeMap[volumeID] = NodeVolStats{ + setTime: time.Now(), + // capAvailable: capAvailable, + capTotal: capAsInt64, + capUsed: capUsed, + } + } resp := &csi.NodeGetVolumeStatsResponse{ Usage: []*csi.VolumeUsage{ { - // Available: capAvailable, - Total: capAsInt64, - Used: capUsed, + // Available: ns.VolumeIDAndTimeMap[volumeID].capAvailable, + Total: ns.VolumeIDAndTimeMap[volumeID].capTotal, + Used: ns.VolumeIDAndTimeMap[volumeID].capUsed, Unit: csi.VolumeUsage_BYTES, }, { diff --git a/pkg/driver/nodeserver_test.go b/pkg/driver/nodeserver_test.go index 13700632..016c99a9 100644 --- a/pkg/driver/nodeserver_test.go +++ b/pkg/driver/nodeserver_test.go @@ -528,7 +528,8 @@ func TestNodeGetVolumeStats(t *testing.T) { t.Log("Testcase being executed", zap.String("testcase", tc.testCaseName)) nodeServer := nodeServer{ - Stats: tc.driverStatsUtils, + Stats: tc.driverStatsUtils, + VolumeIDAndTimeMap: map[string]NodeVolStats{}, } actualResp, actualErr := nodeServer.NodeGetVolumeStats(ctx, tc.req) diff --git a/pkg/driver/s3-driver.go b/pkg/driver/s3-driver.go index b90d986e..a92788b2 100644 --- a/pkg/driver/s3-driver.go +++ b/pkg/driver/s3-driver.go @@ -131,11 +131,12 @@ func newControllerServer(d *S3Driver, statsUtil pkgUtils.StatsUtils, s3cosSessio func newNodeServer(d *S3Driver, statsUtil pkgUtils.StatsUtils, nodeID string, mountObj mounter.NewMounterFactory, mounterUtil mounterUtils.MounterUtils) *nodeServer { return &nodeServer{ - S3Driver: d, - Stats: statsUtil, - NodeID: nodeID, - Mounter: mountObj, - MounterUtils: mounterUtil, + S3Driver: d, + Stats: statsUtil, + NodeID: nodeID, + Mounter: mountObj, + MounterUtils: mounterUtil, + VolumeIDAndTimeMap: map[string]NodeVolStats{}, } }