Skip to content

Commit

Permalink
Create constants for keys used in map lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheng Pan committed Feb 14, 2019
1 parent d961621 commit bd10db8
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 52 deletions.
42 changes: 42 additions & 0 deletions pkg/driver/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package driver

// constants of keys in PublishContext
const (
// devicePathKey represents key for device path in PublishContext
// devicePath is the device path where the volume is attached to
DevicePathKey = "devicePath"
)

// constants of keys in volume parameters
const (
// FsTypeKey represents key for filesystem type
FsTypeKey = "fsType"

// VolumeTypeKey represents key for volume type
VolumeTypeKey = "type"

// IopsPerGBKey represents key for IOPS per GB
IopsPerGBKey = "iopsPerGB"

// EncryptedKey represents key for whether filesystem is encrypted
EncryptedKey = "encrypted"

// KmsKeyId represents key for KMS encryption key
KmsKeyIdKey = "kmsKeyId"
)
14 changes: 7 additions & 7 deletions pkg/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
}

volumeParams := req.GetParameters()
fsType := volumeParams["fsType"]
fsType := volumeParams[FsTypeKey]

// volume exists already
if disk != nil {
Expand All @@ -97,10 +97,10 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)

// create a new volume
zone := pickAvailabilityZone(req.GetAccessibilityRequirements())
volumeType := volumeParams["type"]
volumeType := volumeParams[VolumeTypeKey]
iopsPerGB := 0
if volumeType == cloud.VolumeTypeIO1 {
iopsPerGB, err = strconv.Atoi(volumeParams["iopsPerGB"])
iopsPerGB, err = strconv.Atoi(volumeParams[IopsPerGBKey])
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "Could not parse invalid iopsPerGB: %v", err)
}
Expand All @@ -110,9 +110,9 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
isEncrypted bool
kmsKeyId string
)
if volumeParams["encrypted"] == "true" {
if volumeParams[EncryptedKey] == "true" {
isEncrypted = true
kmsKeyId = volumeParams["kmsKeyId"]
kmsKeyId = volumeParams[KmsKeyIdKey]
}

opts := &cloud.DiskOptions{
Expand Down Expand Up @@ -192,7 +192,7 @@ func (d *Driver) ControllerPublishVolume(ctx context.Context, req *csi.Controlle
}
klog.V(5).Infof("ControllerPublishVolume: volume %s attached to node %s through device %s", volumeID, nodeID, devicePath)

pvInfo := map[string]string{"devicePath": devicePath}
pvInfo := map[string]string{DevicePathKey: devicePath}
return &csi.ControllerPublishVolumeResponse{PublishContext: pvInfo}, nil
}

Expand Down Expand Up @@ -328,7 +328,7 @@ func newCreateVolumeResponse(disk *cloud.Disk) *csi.CreateVolumeResponse {
VolumeId: disk.VolumeID,
CapacityBytes: util.GiBToBytes(disk.CapacityGiB),
VolumeContext: map[string]string{
"fsType": disk.FsType,
FsTypeKey: disk.FsType,
},
AccessibleTopology: []*csi.Topology{
{
Expand Down
42 changes: 21 additions & 21 deletions pkg/driver/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestCreateVolume(t *testing.T) {
expVol: &csi.Volume{
CapacityBytes: stdVolSize,
VolumeId: "vol-test",
VolumeContext: map[string]string{"fsType": ""},
VolumeContext: map[string]string{FsTypeKey: ""},
},
},
{
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestCreateVolume(t *testing.T) {
expVol: &csi.Volume{
CapacityBytes: stdVolSize,
VolumeId: "vol-test",
VolumeContext: map[string]string{"fsType": ""},
VolumeContext: map[string]string{FsTypeKey: ""},
},
},
{
Expand Down Expand Up @@ -124,7 +124,7 @@ func TestCreateVolume(t *testing.T) {
expVol: &csi.Volume{
CapacityBytes: cloud.DefaultVolumeSize,
VolumeId: "vol-test",
VolumeContext: map[string]string{"fsType": ""},
VolumeContext: map[string]string{FsTypeKey: ""},
},
},
{
Expand All @@ -138,7 +138,7 @@ func TestCreateVolume(t *testing.T) {
expVol: &csi.Volume{
CapacityBytes: 2147483648, // 1 GiB + 1 byte = 2 GiB
VolumeId: "vol-test",
VolumeContext: map[string]string{"fsType": ""},
VolumeContext: map[string]string{FsTypeKey: ""},
},
},
{
Expand All @@ -147,12 +147,12 @@ func TestCreateVolume(t *testing.T) {
Name: "vol-test",
CapacityRange: stdCapRange,
VolumeCapabilities: stdVolCap,
Parameters: map[string]string{"fsType": defaultFsType},
Parameters: map[string]string{FsTypeKey: defaultFsType},
},
expVol: &csi.Volume{
CapacityBytes: stdVolSize,
VolumeId: "vol-test",
VolumeContext: map[string]string{"fsType": defaultFsType},
VolumeContext: map[string]string{FsTypeKey: defaultFsType},
},
},
{
Expand All @@ -162,14 +162,14 @@ func TestCreateVolume(t *testing.T) {
CapacityRange: stdCapRange,
VolumeCapabilities: stdVolCap,
Parameters: map[string]string{
"type": cloud.VolumeTypeIO1,
"iopsPerGB": "5",
VolumeTypeKey: cloud.VolumeTypeIO1,
IopsPerGBKey: "5",
},
},
expVol: &csi.Volume{
CapacityBytes: stdVolSize,
VolumeId: "vol-test",
VolumeContext: map[string]string{"fsType": ""},
VolumeContext: map[string]string{FsTypeKey: ""},
},
},
{
Expand All @@ -179,46 +179,46 @@ func TestCreateVolume(t *testing.T) {
CapacityRange: stdCapRange,
VolumeCapabilities: stdVolCap,
Parameters: map[string]string{
"type": cloud.VolumeTypeSC1,
VolumeTypeKey: cloud.VolumeTypeSC1,
},
},
expVol: &csi.Volume{
CapacityBytes: stdVolSize,
VolumeId: "vol-test",
VolumeContext: map[string]string{"fsType": ""},
VolumeContext: map[string]string{FsTypeKey: ""},
},
},
{
name: "success with volume encrpytion",
name: "success with volume encryption",
req: &csi.CreateVolumeRequest{
Name: "vol-test",
CapacityRange: stdCapRange,
VolumeCapabilities: stdVolCap,
Parameters: map[string]string{
"encrypted": "true",
EncryptedKey: "true",
},
},
expVol: &csi.Volume{
CapacityBytes: stdVolSize,
VolumeId: "vol-test",
VolumeContext: map[string]string{"fsType": ""},
VolumeContext: map[string]string{FsTypeKey: ""},
},
},
{
name: "success with volume encrpytion with KMS key",
name: "success with volume encryption with KMS key",
req: &csi.CreateVolumeRequest{
Name: "vol-test",
CapacityRange: stdCapRange,
VolumeCapabilities: stdVolCap,
Parameters: map[string]string{
"encrypted": "true",
"kmsKeyId": "arn:aws:kms:us-east-1:012345678910:key/abcd1234-a123-456a-a12b-a123b4cd56ef",
EncryptedKey: "true",
KmsKeyIdKey: "arn:aws:kms:us-east-1:012345678910:key/abcd1234-a123-456a-a12b-a123b4cd56ef",
},
},
expVol: &csi.Volume{
CapacityBytes: stdVolSize,
VolumeId: "vol-test",
VolumeContext: map[string]string{"fsType": ""},
VolumeContext: map[string]string{FsTypeKey: ""},
},
},
{
Expand All @@ -228,7 +228,7 @@ func TestCreateVolume(t *testing.T) {
CapacityRange: stdCapRange,
VolumeCapabilities: stdVolCap,
Parameters: map[string]string{
"fsType": expFsType,
FsTypeKey: expFsType,
},
AccessibilityRequirements: &csi.TopologyRequirement{
Requisite: []*csi.Topology{
Expand All @@ -243,7 +243,7 @@ func TestCreateVolume(t *testing.T) {
CapacityRange: stdCapRange,
VolumeCapabilities: stdVolCap,
Parameters: map[string]string{
"fsType": expFsType,
FsTypeKey: expFsType,
},
AccessibilityRequirements: &csi.TopologyRequirement{
Requisite: []*csi.Topology{
Expand All @@ -256,7 +256,7 @@ func TestCreateVolume(t *testing.T) {
expVol: &csi.Volume{
CapacityBytes: stdVolSize,
VolumeId: "vol-test",
VolumeContext: map[string]string{"fsType": expFsType},
VolumeContext: map[string]string{FsTypeKey: expFsType},
AccessibleTopology: []*csi.Topology{
{
Segments: map[string]string{TopologyKey: expZone},
Expand Down
16 changes: 10 additions & 6 deletions pkg/driver/internal/inflight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/util"
)

type testRequest struct {
Expand All @@ -38,12 +39,15 @@ var stdVolCap = []*csi.VolumeCapability{
},
},
}
var stdVolSize = int64(5 * 1024 * 1024 * 1024)
var stdCapRange = &csi.CapacityRange{RequiredBytes: stdVolSize}
var stdParams = map[string]string{
"fsType": "ext3",
"volumeType": "gp2",
}

var (
stdVolSize = int64(5 * util.GiB)
stdCapRange = &csi.CapacityRange{RequiredBytes: stdVolSize}
stdParams = map[string]string{
"key1": "value1",
"key2": "value2",
}
)

func TestInFlight(t *testing.T) {
testCases := []struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe
d.inFlight.Delete(req)
}()

source, ok := req.PublishContext["devicePath"]
source, ok := req.PublishContext[DevicePathKey]
if !ok {
return nil, status.Error(codes.InvalidArgument, "Device path not provided")
}
Expand Down Expand Up @@ -121,7 +121,7 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe

// Get fs type that the volume will be formatted with
attributes := req.GetVolumeContext()
fsType, exists := attributes["fsType"]
fsType, exists := attributes[FsTypeKey]
if !exists || fsType == "" {
fsType = defaultFsType
}
Expand Down
Loading

0 comments on commit bd10db8

Please sign in to comment.