Skip to content

Commit b675d19

Browse files
sunnylovestiramisuSneha-at
authored andcommitted
Clean up code
1 parent 8613f0f commit b675d19

File tree

12 files changed

+282
-48
lines changed

12 files changed

+282
-48
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ else
4848
endif
4949

5050
build-container: require-GCE_PD_CSI_STAGING_IMAGE require-GCE_PD_CSI_STAGING_VERSION init-buildx
51-
$(DOCKER) buildx build --progress=plain --platform=linux --progress=plain \
51+
$(DOCKER) buildx build --platform=linux --progress=plain \
5252
-t $(STAGINGIMAGE):$(STAGINGVERSION) \
5353
--build-arg BUILDPLATFORM=linux \
5454
--build-arg STAGINGVERSION=$(STAGINGVERSION) \

pkg/common/constants.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ const (
3232

3333
// Label that is set on a disk when it is used by a 'multi-zone' VolumeHandle
3434
MultiZoneLabel = "goog-gke-multi-zone"
35+
36+
// Data cache mode
37+
DataCacheModeWriteBack = "writeback"
38+
DataCacheModeWriteThrough = "writethrough"
3539
)

pkg/common/parameters.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package common
1818

1919
import (
2020
"fmt"
21+
"strconv"
2122
"strings"
2223

2324
"k8s.io/klog/v2"
@@ -75,9 +76,8 @@ const (
7576
)
7677

7778
type DataCacheParameters struct {
78-
// Values: {string}
79+
// Values: {string} in int64 form
7980
// Default: ""
80-
// Example: "25Gi"
8181
DataCacheSize string
8282
// Values: writethrough, writeback
8383
// Default: writethrough
@@ -151,10 +151,10 @@ func ExtractAndDefaultParameters(parameters map[string]string, driverName string
151151
ResourceTags: make(map[string]string), // Default
152152
}
153153

154-
// Set data cache feature default
154+
// Set data cache mode default
155155
d := DataCacheParameters{}
156-
if enableDataCache {
157-
d.DataCacheMode = "writethrough"
156+
if enableDataCache && parameters[ParameterKeyDataCacheSize] != "" {
157+
d.DataCacheMode = DataCacheModeWriteThrough
158158
}
159159

160160
for k, v := range extraVolumeLabels {
@@ -245,12 +245,20 @@ func ExtractAndDefaultParameters(parameters map[string]string, driverName string
245245
return p, d, fmt.Errorf("parameters contains invalid option %q", ParameterKeyDataCacheSize)
246246
}
247247
// TODO: need to parse or validate the string
248-
d.DataCacheSize = v
248+
249+
paramDataCacheSize, err := ConvertGiStringToInt64(v)
250+
if err != nil {
251+
return p, d, fmt.Errorf("parameters contain invalid dataCacheSize parameter: %w", err)
252+
}
253+
d.DataCacheSize = strconv.FormatInt(paramDataCacheSize, 10)
249254
klog.V(2).Infof("====== Data cache size is %v ======", v)
250255
case ParameterKeyDataCacheMode:
251256
if !enableDataCache {
252257
return p, d, fmt.Errorf("parameters contains invalid option %q", ParameterKeyDataCacheSize)
253258
}
259+
if err := ValidateDataCacheMode(v); err != nil {
260+
return p, d, fmt.Errorf("parameters contains invalid option: %w", err)
261+
}
254262
d.DataCacheMode = v
255263
klog.V(2).Infof("====== Data cache mode is %v ======", v)
256264
case ParameterKeyResourceTags:

pkg/common/parameters_test.go

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ import (
2525

2626
func TestExtractAndDefaultParameters(t *testing.T) {
2727
tests := []struct {
28-
name string
29-
parameters map[string]string
30-
labels map[string]string
31-
enableStoragePools bool
32-
extraTags map[string]string
33-
expectParams DiskParameters
34-
expectErr bool
28+
name string
29+
parameters map[string]string
30+
labels map[string]string
31+
enableStoragePools bool
32+
enableDataCache bool
33+
extraTags map[string]string
34+
expectParams DiskParameters
35+
expectDataCacheParams DataCacheParameters
36+
expectErr bool
3537
}{
3638
{
3739
name: "defaults",
@@ -350,11 +352,58 @@ func TestExtractAndDefaultParameters(t *testing.T) {
350352
labels: map[string]string{},
351353
expectErr: true,
352354
},
355+
{
356+
name: "data cache parameters - set default cache mode",
357+
enableDataCache: true,
358+
parameters: map[string]string{ParameterKeyType: "pd-balanced", ParameterKeyReplicationType: "none", ParameterKeyDiskEncryptionKmsKey: "foo/key", ParameterKeyLabels: "key1=value1,key2=value2", ParameterKeyDataCacheSize: "1234Gi"},
359+
labels: map[string]string{},
360+
expectParams: DiskParameters{
361+
DiskType: "pd-balanced",
362+
ReplicationType: "none",
363+
DiskEncryptionKMSKey: "foo/key",
364+
Tags: map[string]string{},
365+
Labels: map[string]string{
366+
"key1": "value1",
367+
"key2": "value2",
368+
},
369+
},
370+
expectDataCacheParams: DataCacheParameters{
371+
DataCacheMode: DataCacheModeWriteThrough,
372+
DataCacheSize: "1234",
373+
},
374+
},
375+
{
376+
name: "data cache parameters",
377+
enableDataCache: true,
378+
parameters: map[string]string{ParameterKeyType: "pd-balanced", ParameterKeyReplicationType: "none", ParameterKeyDiskEncryptionKmsKey: "foo/key", ParameterKeyLabels: "key1=value1,key2=value2", ParameterKeyDataCacheSize: "1234Gi", ParameterKeyDataCacheMode: DataCacheModeWriteBack},
379+
labels: map[string]string{},
380+
expectParams: DiskParameters{
381+
DiskType: "pd-balanced",
382+
ReplicationType: "none",
383+
DiskEncryptionKMSKey: "foo/key",
384+
Tags: map[string]string{},
385+
Labels: map[string]string{
386+
"key1": "value1",
387+
"key2": "value2",
388+
},
389+
},
390+
expectDataCacheParams: DataCacheParameters{
391+
DataCacheMode: DataCacheModeWriteBack,
392+
DataCacheSize: "1234",
393+
},
394+
},
395+
{
396+
name: "data cache parameters - enableDataCache is false",
397+
enableDataCache: false,
398+
parameters: map[string]string{ParameterKeyType: "pd-balanced", ParameterKeyReplicationType: "none", ParameterKeyDiskEncryptionKmsKey: "foo/key", ParameterKeyLabels: "key1=value1,key2=value2", ParameterKeyDataCacheSize: "1234Gi", ParameterKeyDataCacheMode: DataCacheModeWriteBack},
399+
labels: map[string]string{},
400+
expectErr: true,
401+
},
353402
}
354403

355404
for _, tc := range tests {
356405
t.Run(tc.name, func(t *testing.T) {
357-
p, _, err := ExtractAndDefaultParameters(tc.parameters, "testDriver", tc.labels, tc.enableStoragePools, false, tc.extraTags)
406+
p, d, err := ExtractAndDefaultParameters(tc.parameters, "testDriver", tc.labels, tc.enableStoragePools, tc.enableDataCache, tc.extraTags)
358407
if gotErr := err != nil; gotErr != tc.expectErr {
359408
t.Fatalf("ExtractAndDefaultParameters(%+v) = %v; expectedErr: %v", tc.parameters, err, tc.expectErr)
360409
}
@@ -365,6 +414,10 @@ func TestExtractAndDefaultParameters(t *testing.T) {
365414
if diff := cmp.Diff(p, tc.expectParams); diff != "" {
366415
t.Errorf("ExtractAndDefaultParameters(%+v): -want, +got \n%s", tc.parameters, diff)
367416
}
417+
418+
if diff := cmp.Diff(d, tc.expectDataCacheParams); diff != "" {
419+
t.Errorf("ExtractAndDefaultParameters(%+v): -want, +got \n%s", tc.parameters, diff)
420+
}
368421
})
369422
}
370423
}

pkg/common/runcmd.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ import (
44
"fmt"
55
"os/exec"
66
"strings"
7-
/*
8-
"bufio"
9-
"io"
10-
"io/ioutil"
11-
*/
127

138
"k8s.io/klog/v2"
149
)
@@ -36,25 +31,4 @@ func RunCommand(cmd string, args ...string) ([]byte, error) {
3631
return output, fmt.Errorf("%s %s failed: %w; output: %s", cmd, strings.Join(args, " "), err, string(output))
3732
}
3833
return output, nil
39-
/*
40-
pipe, _ := execCmd.StdoutPipe()
41-
if err := execCmd.Start(); err != nil {
42-
klog.Errorf("====== Failed execCmd.Start() ======")
43-
}
44-
outStr := ""
45-
go func(p io.ReadCloser) {
46-
reader := bufio.NewReader(pipe)
47-
klog.V(2).Infof("====== Start ioutil.ReadAll ======")
48-
b, err := ioutil.ReadAll(reader)
49-
if err != nil {
50-
klog.Errorf("====== Failed ioutil.ReadAll(reader) %v ======", err)
51-
}
52-
klog.V(2).Infof("%v \n", string(b))
53-
klog.V(2).Infof("====== End ioutil.ReadAll ======")
54-
}(pipe)
55-
56-
if err := execCmd.Wait(); err != nil {
57-
klog.Errorf("====== Failed execCmd.Wait(): %v ======", err)
58-
}
59-
return []byte(outStr), nil*/
6034
}

pkg/common/utils.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ var (
9292
http.StatusNotFound: codes.NotFound,
9393
}
9494

95+
validDataCacheMode = []string{DataCacheModeWriteBack, DataCacheModeWriteThrough}
96+
9597
// Regular expressions for validating parent_id, key and value of a resource tag.
9698
regexParent = regexp.MustCompile(`(^[1-9][0-9]{0,31}$)|(^[a-z][a-z0-9-]{4,28}[a-z0-9]$)`)
9799
regexKey = regexp.MustCompile(`^[a-zA-Z0-9]([0-9A-Za-z_.-]{0,61}[a-zA-Z0-9])?$`)
@@ -381,6 +383,15 @@ func ConvertMiStringToInt64(str string) (int64, error) {
381383
return volumehelpers.RoundUpToMiB(quantity)
382384
}
383385

386+
// ConvertGiStringToInt64 converts a GiB string to int64
387+
func ConvertGiStringToInt64(str string) (int64, error) {
388+
quantity, err := resource.ParseQuantity(str)
389+
if err != nil {
390+
return -1, err
391+
}
392+
return volumehelpers.RoundUpToGiB(quantity)
393+
}
394+
384395
// ConvertStringToBool converts a string to a boolean.
385396
func ConvertStringToBool(str string) (bool, error) {
386397
switch strings.ToLower(str) {
@@ -578,6 +589,22 @@ func VolumeIdAsMultiZone(volumeId string) (string, error) {
578589
return strings.Join(splitId, "/"), nil
579590
}
580591

592+
func StringInSlice(s string, list []string) bool {
593+
for _, v := range list {
594+
if v == s {
595+
return true
596+
}
597+
}
598+
return false
599+
}
600+
601+
func ValidateDataCacheMode(s string) error {
602+
if StringInSlice(s, validDataCacheMode) {
603+
return nil
604+
}
605+
return fmt.Errorf("invalid data-cache-mode %s. Only \"writeback\" and \"writethrough\" is a valid input", s)
606+
}
607+
581608
// NewLimiter returns a token bucket based request rate limiter after initializing
582609
// the passed values for limit, burst (or token bucket) size. If opted for emptyBucket
583610
// all initial tokens are reserved for the first burst.

0 commit comments

Comments
 (0)