Skip to content

Commit

Permalink
Add a cmdline option to add extra volume tags
Browse files Browse the repository at this point in the history
Add a new cli command option `--extra-volume-tags` which is a map of
string to string (syntax is similar to `--node-labels` for kubelet).  By
default, it's an empty map which maps the the current behavior.

If this option is not empty, when doing dynamic provisioning (i.e., in
`CreateVolume`), we always attach the extra volume tags when calling
`d.cloud.CreateDisk`.
  • Loading branch information
jieyu committed Aug 23, 2019
1 parent 5fb7b9d commit 0d92b86
Show file tree
Hide file tree
Showing 12 changed files with 508 additions and 73 deletions.
22 changes: 16 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,30 @@ limitations under the License.
package main

import (
"flag"
"fmt"
"os"

"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver"
"github.com/spf13/pflag"
cliflag "k8s.io/component-base/cli/flag"
"k8s.io/klog"
)

func main() {
var (
endpoint = flag.String("endpoint", "unix://tmp/csi.sock", "CSI Endpoint")
version = flag.Bool("version", false, "Print the version and exit.")
version bool
endpoint string
extraVolumeTags map[string]string
)

pflag.BoolVar(&version, "version", false, "Print the version and exit.")
pflag.StringVar(&endpoint, "endpoint", driver.DefaultCSIEndpoint, "CSI Endpoint")
pflag.Var(cliflag.NewMapStringString(&extraVolumeTags), "extra-volume-tags", "Extra volume tags to attach to each dynamically provisioned volume. It is a comma separated list of key value pairs like '<key1>=<value1>,<key2>=<value2>'")

klog.InitFlags(nil)
flag.Parse()
pflag.Parse()

if *version {
if version {
info, err := driver.GetVersionJSON()
if err != nil {
klog.Fatalln(err)
Expand All @@ -42,7 +49,10 @@ func main() {
os.Exit(0)
}

drv, err := driver.NewDriver(*endpoint)
drv, err := driver.NewDriver(
driver.WithEndpoint(endpoint),
driver.WithExtraVolumeTags(extraVolumeTags),
)
if err != nil {
klog.Fatalln(err)
}
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ require (
github.com/soheilhy/cmux v0.1.4 // indirect
github.com/spf13/afero v1.1.2 // indirect
github.com/spf13/cobra v0.0.3 // indirect
github.com/spf13/pflag v1.0.3 // indirect
github.com/spf13/pflag v1.0.3
github.com/tmc/grpc-websocket-proxy v0.0.0-20171017195756-830351dc03c6 // indirect
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8 // indirect
go.uber.org/atomic v1.3.2 // indirect
Expand All @@ -64,6 +64,7 @@ require (
k8s.io/api v0.0.0
k8s.io/apimachinery v0.0.0
k8s.io/client-go v0.0.0
k8s.io/component-base v0.0.0
k8s.io/klog v0.4.0
k8s.io/kubernetes v1.15.2
)
Expand Down
14 changes: 13 additions & 1 deletion pkg/cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,20 @@ var (
)

// AWS provisioning limits.
// Source: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSVolumeTypes.html
// Sources:
// http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSVolumeTypes.html
// https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html#tag-restrictions
const (
// MinTotalIOPS represents the minimum Input Output per second.
MinTotalIOPS = 100
// MaxTotalIOPS represents the maximum Input Output per second.
MaxTotalIOPS = 20000
// MaxNumTagsPerResource represents the maximum number of tags per AWS resource.
MaxNumTagsPerResource = 50
// MaxTagKeyLength represents the maximum key length for a tag.
MaxTagKeyLength = 128
// MaxTagValueLength represents the maximum value length for a tag.
MaxTagValueLength = 256
)

// Defaults
Expand All @@ -75,6 +83,10 @@ const (
VolumeNameTagKey = "CSIVolumeName"
// SnapshotNameTagKey is the key value that refers to the snapshot's name.
SnapshotNameTagKey = "CSIVolumeSnapshotName"
// KubernetesTagKeyPrefix is the prefix of the key value that is reserved for Kubernetes.
KubernetesTagKeyPrefix = "kubernetes.io"
// AWSTagKeyPrefix is the prefix of the key value that is reserved for AWS.
AWSTagKeyPrefix = "aws:"
)

var (
Expand Down
5 changes: 5 additions & 0 deletions pkg/driver/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ const (
// KmsKeyId represents key for KMS encryption key
KmsKeyIdKey = "kmskeyid"
)

// constants for default command line flag values
const (
DefaultCSIEndpoint = "unix://tmp/csi.sock"
)
18 changes: 14 additions & 4 deletions pkg/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,21 @@ var (

// controllerService represents the controller service of CSI driver
type controllerService struct {
cloud cloud.Cloud
cloud cloud.Cloud
driverOptions *DriverOptions
}

// newControllerService creates a new controller service
// it panics if failed to create the service
func newControllerService() controllerService {
func newControllerService(driverOptions *DriverOptions) controllerService {
cloud, err := cloud.NewCloud()
if err != nil {
panic(err)
}

return controllerService{
cloud: cloud,
cloud: cloud,
driverOptions: driverOptions,
}
}

Expand Down Expand Up @@ -140,9 +142,17 @@ func (d *controllerService) CreateVolume(ctx context.Context, req *csi.CreateVol

// create a new volume
zone := pickAvailabilityZone(req.GetAccessibilityRequirements())

volumeTags := map[string]string{
cloud.VolumeNameTagKey: volName,
}
for k, v := range d.driverOptions.extraVolumeTags {
volumeTags[k] = v
}

opts := &cloud.DiskOptions{
CapacityBytes: volSizeBytes,
Tags: map[string]string{cloud.VolumeNameTagKey: volName},
Tags: volumeTags,
VolumeType: volumeType,
IOPSPerGB: iopsPerGB,
AvailabilityZone: zone,
Expand Down
Loading

0 comments on commit 0d92b86

Please sign in to comment.