Repository: dell/csi-baremetal
Pull request #961 (2023-02-14 03:39:22)

Bugfix issue 959 correct volume cr size
Align volumeCR size to MB when it is not LVG SC

codecov[bot]: # [Codecov](https://codecov.io/gh/dell/csi-baremetal/pull/961?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dell) Report
Base: **72.05**% // Head: **72.01**% // Decreases project coverage by **`-0.04%`** :warning:
> Coverage data is based on head [(`c6265de`)](https://codecov.io/gh/dell/csi-baremetal/pull/961?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dell) compared to base [(`0274df5`)](https://codecov.io/gh/dell/csi-baremetal/commit/0274df51eb13efde38d97f897c6e651130552b07?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dell).
> Patch coverage: 50.00% of modified lines in pull request are covered.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master     #961      +/-   ##
==========================================
- Coverage   72.05%   72.01%   -0.04%     
==========================================
  Files          62       62              
  Lines        8034     8044      +10     
==========================================
+ Hits         5789     5793       +4     
- Misses       1995     2001       +6     
  Partials      250      250              
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `72.01% <50.00%> (-0.04%)` | :arrow_down: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dell#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/dell/csi-baremetal/pull/961?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dell) | Coverage Δ | |
|---|---|---|
| [pkg/base/capacityplanner/common.go](https://codecov.io/gh/dell/csi-baremetal/pull/961?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dell#diff-cGtnL2Jhc2UvY2FwYWNpdHlwbGFubmVyL2NvbW1vbi5nbw==) | `55.00% <0.00%> (-23.58%)` | :arrow_down: |
| [pkg/common/volume\_operations.go](https://codecov.io/gh/dell/csi-baremetal/pull/961?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dell#diff-cGtnL2NvbW1vbi92b2x1bWVfb3BlcmF0aW9ucy5nbw==) | `82.66% <100.00%> (+0.14%)` | :arrow_up: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dell). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dell)

</details>

[:umbrella: View full report at Codecov](https://codecov.io/gh/dell/csi-baremetal/pull/961?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dell).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dell).

yanxiucai: Passed both the csi-custom-ci and csi-custom-acceptance tests

Patch:
diff --git a/pkg/base/capacityplanner/common.go b/pkg/base/capacityplanner/common.go
index 84c8bca..b531fd0 100644
--- a/pkg/base/capacityplanner/common.go
+++ b/pkg/base/capacityplanner/common.go
@@ -39,6 +39,15 @@ func AlignSizeByPE(size int64) int64 {
 	return size + alignement
 }
 
+// AlignSizeByMB make size aligned with MB
+func AlignSizeByMB(size int64) int64 {
+	reminder := size % (int64(util.MBYTE))
+	if reminder != 0 {
+		return size - reminder
+	}
+	return size
+}
+
 // SubtractLVMMetadataSize subtracts LVM metadata size from raw drive size
 func SubtractLVMMetadataSize(size int64) int64 {
 	reminder := size % DefaultPESize
diff --git a/pkg/common/volume_operations.go b/pkg/common/volume_operations.go
index 163dfd9..d0f22b4 100644
--- a/pkg/common/volume_operations.go
+++ b/pkg/common/volume_operations.go
@@ -206,7 +206,7 @@ func (vo *VolumeOperationsImpl) handleVolumeCreation(ctx context.Context, log *l
 		allocatedBytes = capacityplanner.AlignSizeByPE(v.Size)
 		locationType = apiV1.LocationTypeLVM
 	} else {
-		allocatedBytes = ac.Spec.Size
+		allocatedBytes = capacityplanner.AlignSizeByMB(ac.Spec.Size)
 		locationType = apiV1.LocationTypeDrive
 	}
 
@@ -241,7 +241,12 @@ func (vo *VolumeOperationsImpl) handleVolumeCreation(ctx context.Context, log *l
 	vo.cache.Set(v.Id, podNamespace)
 
 	// decrease AC size
-	ac.Spec.Size -= allocatedBytes
+	if util.IsStorageClassLVG(sc) {
+		ac.Spec.Size -= allocatedBytes
+	} else {
+		ac.Spec.Size = 0
+	}
+
 	if err = vo.k8sClient.UpdateCR(ctx, ac); err != nil {
 		log.Errorf("Unable to set size for AC %s to %d, error: %v", ac.Name, ac.Spec.Size, err)
 	}


Test patch:
diff --git a/pkg/base/capacityplanner/common_test.go b/pkg/base/capacityplanner/common_test.go
index d02dc14..cd7865e 100644
--- a/pkg/base/capacityplanner/common_test.go
+++ b/pkg/base/capacityplanner/common_test.go
@@ -64,3 +64,50 @@ func TestSubtractLVMMetadataSize(t *testing.T) {
 		})
 	}
 }
+
+func TestAlignSizeByMB(t *testing.T) {
+	type args struct {
+		size int64
+	}
+	tests := []struct {
+		name string
+		args args
+		want int64
+	}{
+		{
+			name: "500MB",
+			args: args{
+				size: 500000000,
+			},
+			want: 499122176,
+		},
+		{
+			name: "10GB",
+			args: args{
+				size: 10000000000,
+			},
+			want: 9999220736,
+		},
+		{
+			name: "500MiB",
+			args: args{
+				size: 524288000,
+			},
+			want: 524288000,
+		},
+		{
+			name: "10GiB",
+			args: args{
+				size: 10737418240,
+			},
+			want: 10737418240,
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if got := AlignSizeByMB(tt.args.size); got != tt.want {
+				t.Errorf("AlignSizeMB() = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}