Skip to content

Commit

Permalink
Overwrite extraTags with extraVolumeTags only if unset
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishenzie committed Oct 1, 2020
1 parent 2d2e955 commit 9b99308
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func main() {
drv, err := driver.NewDriver(
driver.WithEndpoint(options.ServerOptions.Endpoint),
driver.WithExtraTags(options.ControllerOptions.ExtraTags),
driver.WithExtraVolumeTags(options.ControllerOptions.ExtraVolumeTags),
driver.WithMode(options.DriverMode),
driver.WithVolumeAttachLimit(options.NodeOptions.VolumeAttachLimit),
driver.WithKubernetesClusterID(options.ControllerOptions.KubernetesClusterID),
Expand Down
5 changes: 4 additions & 1 deletion cmd/options/controller_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ type ControllerOptions struct {
// ExtraTags is a map of tags that will be attached to each dynamically provisioned
// resource.
ExtraTags map[string]string
// ExtraVolumeTags is a map of tags that will be attached to each dynamically provisioned
// volume.
ExtraVolumeTags map[string]string
// ID of the kubernetes cluster.
KubernetesClusterID string
}

func (s *ControllerOptions) AddFlags(fs *flag.FlagSet) {
fs.Var(cliflag.NewMapStringString(&s.ExtraTags), "extra-tags", "Extra tags to attach to each dynamically provisioned resource. It is a comma separated list of key value pairs like '<key1>=<value1>,<key2>=<value2>'")
fs.Var(cliflag.NewMapStringString(&s.ExtraTags), "extra-volume-tags", "DEPRECATED: Please use --extra-tags instead. 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>'")
fs.Var(cliflag.NewMapStringString(&s.ExtraVolumeTags), "extra-volume-tags", "DEPRECATED: Please use --extra-tags instead. 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>'")
fs.StringVar(&s.KubernetesClusterID, "k8s-tag-cluster-id", "", "ID of the Kubernetes cluster used for tagging provisioned EBS volumes (optional).")
}
8 changes: 8 additions & 0 deletions pkg/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ func WithExtraTags(extraTags map[string]string) func(*DriverOptions) {
}
}

func WithExtraVolumeTags(extraVolumeTags map[string]string) func(*DriverOptions) {
return func(o *DriverOptions) {
if o.extraTags == nil {
o.extraTags = extraVolumeTags
}
}
}

func WithMode(mode Mode) func(*DriverOptions) {
return func(o *DriverOptions) {
o.mode = mode
Expand Down
20 changes: 20 additions & 0 deletions pkg/driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ func TestWithExtraTags(t *testing.T) {
}
}

func TestWithExtraVolumeTags(t *testing.T) {
value := map[string]string{"foo": "bar"}
options := &DriverOptions{}
WithExtraVolumeTags(value)(options)
if !reflect.DeepEqual(options.extraTags, value) {
t.Fatalf("expected extraTags option got set to %+v but is set to %+v", value, options.extraTags)
}
}

func TestWithExtraVolumeTagsNoOverwrite(t *testing.T) {
extraTagsValue := map[string]string{"foo": "bar"}
options := &DriverOptions{}
WithExtraTags(extraTagsValue)(options)
extraVolumeTagsValue := map[string]string{"baz": "qux"}
WithExtraVolumeTags(extraVolumeTagsValue)(options)
if !reflect.DeepEqual(options.extraTags, extraTagsValue) {
t.Fatalf("expected extraTags option got set to %+v but is set to %+v", extraTagsValue, options.extraTags)
}
}

func TestWithMode(t *testing.T) {
value := Mode("mode")
options := &DriverOptions{}
Expand Down

0 comments on commit 9b99308

Please sign in to comment.