Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement different driver modes and AWS Region override for controller service #438

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
FROM golang:1.12.7-stretch as builder
WORKDIR /go/src/github.com/kubernetes-sigs/aws-ebs-csi-driver
ADD . .
RUN make
RUN make

FROM amazonlinux:2
RUN yum install ca-certificates e2fsprogs xfsprogs util-linux -y
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ verify:

.PHONY: test
test:
go test -v -race ./pkg/...
go test -v -race ./cmd/... ./pkg/...

.PHONY: test-sanity
test-sanity:
Expand Down
8 changes: 7 additions & 1 deletion aws-ebs-csi-driver/templates/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,11 @@ spec:
- name: ebs-plugin
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
args:
- controller
- --endpoint=$(CSI_ENDPOINT)
{{ include "aws-ebs-csi-driver.extra-volume-tags" . }}
- --logtostderr
- --v=5
{{ include "aws-ebs-csi-driver.extra-volume-tags" . }}
env:
- name: CSI_ENDPOINT
value: unix:///var/lib/csi/sockets/pluginproxy/csi.sock
Expand All @@ -241,6 +242,10 @@ spec:
name: aws-secret
key: access_key
optional: true
{{- if .Values.region }}
- name: AWS_REGION
value: {{ .Values.region }}
{{- end }}
volumeMounts:
- name: socket-dir
mountPath: /var/lib/csi/sockets/pluginproxy/
Expand Down Expand Up @@ -353,6 +358,7 @@ spec:
privileged: true
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
args:
- node
- --endpoint=$(CSI_ENDPOINT)
- --logtostderr
- --v=5
Expand Down
6 changes: 6 additions & 0 deletions aws-ebs-csi-driver/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,9 @@ affinity: {}
# key1: value1
# key2: value2
extraVolumeTags: {}

# AWS region to use. If not specified then the region will be looked up via the AWS EC2 metadata
# service.
# ---
# region: us-east-1
region: ""
32 changes: 6 additions & 26 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,20 @@ package main

import (
"flag"
"fmt"
"os"

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

"k8s.io/klog"
)

func main() {
var (
version bool
endpoint string
extraVolumeTags map[string]string
)

flag.BoolVar(&version, "version", false, "Print the version and exit.")
flag.StringVar(&endpoint, "endpoint", driver.DefaultCSIEndpoint, "CSI Endpoint")
flag.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()

if version {
info, err := driver.GetVersionJSON()
if err != nil {
klog.Fatalln(err)
}
fmt.Println(info)
os.Exit(0)
}
fs := flag.NewFlagSet("aws-ebs-csi-driver", flag.ExitOnError)
options := GetOptions(fs)

drv, err := driver.NewDriver(
driver.WithEndpoint(endpoint),
driver.WithExtraVolumeTags(extraVolumeTags),
driver.WithEndpoint(options.ServerOptions.Endpoint),
driver.WithExtraVolumeTags(options.ControllerOptions.ExtraVolumeTags),
driver.WithMode(options.DriverMode),
)
if err != nil {
klog.Fatalln(err)
Expand Down
110 changes: 110 additions & 0 deletions cmd/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
Copyright 2020 The Kubernetes Authors.
rfranzke marked this conversation as resolved.
Show resolved Hide resolved

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 main

import (
"flag"
"fmt"
"os"
"strings"

"github.com/kubernetes-sigs/aws-ebs-csi-driver/cmd/options"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver"

"k8s.io/klog"
)

// Options is the combined set of options for all operating modes.
type Options struct {
DriverMode driver.Mode

*options.ServerOptions
*options.ControllerOptions
*options.NodeOptions
}

// used for testing
var osExit = os.Exit

// GetOptions parses the command line options and returns a struct that contains
// the parsed options.
func GetOptions(fs *flag.FlagSet) *Options {
var (
version = fs.Bool("version", false, "Print the version and exit.")

args = os.Args[1:]
mode = driver.AllMode

serverOptions = options.ServerOptions{}
controllerOptions = options.ControllerOptions{}
nodeOptions = options.NodeOptions{}
)

serverOptions.AddFlags(fs)
klog.InitFlags(fs)

if len(os.Args) > 1 {
cmd := os.Args[1]

switch {
case cmd == string(driver.ControllerMode):
controllerOptions.AddFlags(fs)
args = os.Args[2:]
mode = driver.ControllerMode

case cmd == string(driver.NodeMode):
nodeOptions.AddFlags(fs)
args = os.Args[2:]
mode = driver.NodeMode

case cmd == string(driver.AllMode):
controllerOptions.AddFlags(fs)
nodeOptions.AddFlags(fs)
args = os.Args[2:]

case strings.HasPrefix(cmd, "-"):
controllerOptions.AddFlags(fs)
nodeOptions.AddFlags(fs)
args = os.Args[1:]

default:
fmt.Printf("unknown command: %s: expected %q, %q or %q", cmd, driver.ControllerMode, driver.NodeMode, driver.AllMode)
os.Exit(1)
}
}

if err := fs.Parse(args); err != nil {
panic(err)
}

if *version {
info, err := driver.GetVersionJSON()
if err != nil {
klog.Fatalln(err)
}
fmt.Println(info)
osExit(0)
}

return &Options{
DriverMode: mode,

ServerOptions: &serverOptions,
ControllerOptions: &controllerOptions,
NodeOptions: &nodeOptions,
}
}
34 changes: 34 additions & 0 deletions cmd/options/controller_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2020 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 options

import (
"flag"

cliflag "k8s.io/component-base/cli/flag"
)

// ControllerOptions contains options and configuration settings for the controller service.
type ControllerOptions struct {
// ExtraVolumeTags is a map of tags that will be attached to each dynamically provisioned
// volume.
ExtraVolumeTags map[string]string
}

func (s *ControllerOptions) AddFlags(fs *flag.FlagSet) {
fs.Var(cliflag.NewMapStringString(&s.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>'")
}
56 changes: 56 additions & 0 deletions cmd/options/controller_options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2020 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 options

import (
"flag"
"testing"
)

func TestControllerOptions(t *testing.T) {
testCases := []struct {
name string
flag string
found bool
}{
{
name: "lookup desired flag",
flag: "extra-volume-tags",
found: true,
},
{
name: "fail for non-desired flag",
flag: "some-other-flag",
found: false,
},
}

for _, tc := range testCases {
flagSet := flag.NewFlagSet("test-flagset", flag.ContinueOnError)
controllerOptions := &ControllerOptions{}

t.Run(tc.name, func(t *testing.T) {
controllerOptions.AddFlags(flagSet)

flag := flagSet.Lookup(tc.flag)
found := flag != nil
if found != tc.found {
t.Fatalf("result not equal\ngot:\n%v\nexpected:\n%v", found, tc.found)
}
})
}
}
26 changes: 26 additions & 0 deletions cmd/options/node_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2020 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 options

import (
"flag"
)

// NodeOptions contains options and configuration settings for the node service.
type NodeOptions struct{}

func (s *NodeOptions) AddFlags(fs *flag.FlagSet) {}
51 changes: 51 additions & 0 deletions cmd/options/node_options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2020 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 options

import (
"flag"
"testing"
)

func TestNodeOptions(t *testing.T) {
testCases := []struct {
name string
flag string
found bool
}{
{
name: "fail for non-desired flag",
flag: "some-flag",
found: false,
},
}

for _, tc := range testCases {
flagSet := flag.NewFlagSet("test-flagset", flag.ContinueOnError)
nodeOptions := &NodeOptions{}

t.Run(tc.name, func(t *testing.T) {
nodeOptions.AddFlags(flagSet)

flag := flagSet.Lookup(tc.flag)
found := flag != nil
if found != tc.found {
t.Fatalf("result not equal\ngot:\n%v\nexpected:\n%v", found, tc.found)
}
})
}
}
Loading