Skip to content

Commit dad3e43

Browse files
committed
move flag option to azurefile package
Signed-off-by: Fan Shang Xiang <[email protected]>
1 parent 6337ecd commit dad3e43

File tree

6 files changed

+151
-98
lines changed

6 files changed

+151
-98
lines changed

pkg/azurefile/azurefile.go

+13-32
Original file line numberDiff line numberDiff line change
@@ -195,35 +195,6 @@ var (
195195
retriableErrors = []string{accountNotProvisioned, tooManyRequests, shareBeingDeleted, clientThrottled}
196196
)
197197

198-
// DriverOptions defines driver parameters specified in driver deployment
199-
type DriverOptions struct {
200-
NodeID string
201-
DriverName string
202-
CloudConfigSecretName string
203-
CloudConfigSecretNamespace string
204-
CustomUserAgent string
205-
UserAgentSuffix string
206-
AllowEmptyCloudConfig bool
207-
AllowInlineVolumeKeyAccessWithIdentity bool
208-
EnableVHDDiskFeature bool
209-
EnableVolumeMountGroup bool
210-
EnableGetVolumeStats bool
211-
AppendMountErrorHelpLink bool
212-
MountPermissions uint64
213-
FSGroupChangePolicy string
214-
KubeAPIQPS float64
215-
KubeAPIBurst int
216-
EnableWindowsHostProcess bool
217-
AppendClosetimeoOption bool
218-
AppendNoShareSockOption bool
219-
AppendNoResvPortOption bool
220-
AppendActimeoOption bool
221-
SkipMatchingTagCacheExpireInMinutes int
222-
VolStatsCacheExpireInMinutes int
223-
PrintVolumeStatsCallLogs bool
224-
SasTokenExpirationMinutes int
225-
}
226-
227198
// Driver implements all interfaces of CSI drivers
228199
type Driver struct {
229200
csicommon.CSIDriver
@@ -280,6 +251,9 @@ type Driver struct {
280251
sasTokenExpirationMinutes int
281252
// azcopy for provide exec mock for ut
282253
azcopy *fileutil.Azcopy
254+
255+
kubeconfig string
256+
endpoint string
283257
}
284258

285259
// NewDriver Creates a NewCSIDriver object. Assumes vendor version is equal to driver version &
@@ -314,6 +288,8 @@ func NewDriver(options *DriverOptions) *Driver {
314288
driver.subnetLockMap = newLockMap()
315289
driver.volumeLocks = newVolumeLocks()
316290
driver.azcopy = &fileutil.Azcopy{}
291+
driver.kubeconfig = options.KubeConfig
292+
driver.endpoint = options.Endpoint
317293

318294
var err error
319295
getter := func(key string) (interface{}, error) { return nil, nil }
@@ -356,16 +332,21 @@ func NewDriver(options *DriverOptions) *Driver {
356332
}
357333

358334
// Run driver initialization
359-
func (d *Driver) Run(ctx context.Context, endpoint, kubeconfig string) error {
335+
func (d *Driver) Run(ctx context.Context) error {
360336
versionMeta, err := GetVersionYAML(d.Name)
361337
if err != nil {
362338
klog.Fatalf("%v", err)
363339
}
364340
klog.Infof("\nDRIVER INFORMATION:\n-------------------\n%s\n\nStreaming logs below:", versionMeta)
365341

342+
if *&d.NodeID == "" {
343+
// nodeid is not needed in controller component
344+
klog.Warning("nodeid is empty")
345+
}
346+
366347
userAgent := GetUserAgent(d.Name, d.customUserAgent, d.userAgentSuffix)
367348
klog.V(2).Infof("driver userAgent: %s", userAgent)
368-
d.cloud, err = getCloudProvider(context.Background(), kubeconfig, d.NodeID, d.cloudConfigSecretName, d.cloudConfigSecretNamespace, userAgent, d.allowEmptyCloudConfig, d.enableWindowsHostProcess, d.kubeAPIQPS, d.kubeAPIBurst)
349+
d.cloud, err = getCloudProvider(context.Background(), d.kubeconfig, d.NodeID, d.cloudConfigSecretName, d.cloudConfigSecretNamespace, userAgent, d.allowEmptyCloudConfig, d.enableWindowsHostProcess, d.kubeAPIQPS, d.kubeAPIBurst)
369350
if err != nil {
370351
klog.Fatalf("failed to get Azure Cloud Provider, error: %v", err)
371352
}
@@ -420,7 +401,7 @@ func (d *Driver) Run(ctx context.Context, endpoint, kubeconfig string) error {
420401
csi.RegisterNodeServer(server, d)
421402
d.server = server
422403

423-
listener, err := csicommon.ListenEndpoint(endpoint)
404+
listener, err := csicommon.ListenEndpoint(d.endpoint)
424405
if err != nil {
425406
klog.Fatalf("failed to listen endpoint: %v", err)
426407
}

pkg/azurefile/azurefile_options.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package azurefile
18+
19+
import "flag"
20+
21+
// DriverOptions defines driver parameters specified in driver deployment
22+
type DriverOptions struct {
23+
NodeID string
24+
DriverName string
25+
CloudConfigSecretName string
26+
CloudConfigSecretNamespace string
27+
CustomUserAgent string
28+
UserAgentSuffix string
29+
AllowEmptyCloudConfig bool
30+
AllowInlineVolumeKeyAccessWithIdentity bool
31+
EnableVHDDiskFeature bool
32+
EnableVolumeMountGroup bool
33+
EnableGetVolumeStats bool
34+
AppendMountErrorHelpLink bool
35+
MountPermissions uint64
36+
FSGroupChangePolicy string
37+
KubeAPIQPS float64
38+
KubeAPIBurst int
39+
EnableWindowsHostProcess bool
40+
AppendClosetimeoOption bool
41+
AppendNoShareSockOption bool
42+
AppendNoResvPortOption bool
43+
AppendActimeoOption bool
44+
SkipMatchingTagCacheExpireInMinutes int
45+
VolStatsCacheExpireInMinutes int
46+
PrintVolumeStatsCallLogs bool
47+
SasTokenExpirationMinutes int
48+
KubeConfig string
49+
Endpoint string
50+
}
51+
52+
func (o *DriverOptions) AddFlags() *flag.FlagSet {
53+
if o == nil {
54+
return nil
55+
}
56+
fs := flag.NewFlagSet("", flag.ExitOnError)
57+
fs.StringVar(&o.NodeID, "nodeid", "", "node id")
58+
fs.StringVar(&o.DriverName, "drivername", DefaultDriverName, "name of the driver")
59+
fs.StringVar(&o.CloudConfigSecretName, "cloud-config-secret-name", "azure-cloud-provider", "secret name of cloud config")
60+
fs.StringVar(&o.CloudConfigSecretNamespace, "cloud-config-secret-namespace", "kube-system", "secret namespace of cloud config")
61+
fs.StringVar(&o.CustomUserAgent, "custom-user-agent", "", "custom userAgent")
62+
fs.StringVar(&o.UserAgentSuffix, "user-agent-suffix", "", "userAgent suffix")
63+
fs.BoolVar(&o.AllowEmptyCloudConfig, "allow-empty-cloud-config", true, "allow running driver without cloud config")
64+
fs.BoolVar(&o.AllowInlineVolumeKeyAccessWithIdentity, "allow-inline-volume-key-access-with-identity", false, "allow accessing storage account key using cluster identity for inline volume")
65+
fs.BoolVar(&o.EnableVHDDiskFeature, "enable-vhd", true, "enable VHD disk feature (experimental)")
66+
fs.BoolVar(&o.EnableVolumeMountGroup, "enable-volume-mount-group", true, "indicates whether enabling VOLUME_MOUNT_GROUP")
67+
fs.BoolVar(&o.EnableGetVolumeStats, "enable-get-volume-stats", true, "allow GET_VOLUME_STATS on agent node")
68+
fs.BoolVar(&o.AppendMountErrorHelpLink, "append-mount-error-help-link", true, "Whether to include a link for help with mount errors when a mount error occurs.")
69+
fs.Uint64Var(&o.MountPermissions, "mount-permissions", 0777, "mounted folder permissions")
70+
fs.StringVar(&o.FSGroupChangePolicy, "fsgroup-change-policy", "", "indicates how the volume's ownership will be changed by the driver, OnRootMismatch is the default value")
71+
fs.Float64Var(&o.KubeAPIQPS, "kube-api-qps", 25.0, "QPS to use while communicating with the kubernetes apiserver.")
72+
fs.IntVar(&o.KubeAPIBurst, "kube-api-burst", 50, "Burst to use while communicating with the kubernetes apiserver.")
73+
fs.BoolVar(&o.EnableWindowsHostProcess, "enable-windows-host-process", false, "enable windows host process")
74+
fs.BoolVar(&o.AppendClosetimeoOption, "append-closetimeo-option", false, "Whether appending closetimeo=0 option to smb mount command")
75+
fs.BoolVar(&o.AppendNoShareSockOption, "append-nosharesock-option", true, "Whether appending nosharesock option to smb mount command")
76+
fs.BoolVar(&o.AppendNoResvPortOption, "append-noresvport-option", true, "Whether appending noresvport option to nfs mount command")
77+
fs.BoolVar(&o.AppendActimeoOption, "append-actimeo-option", true, "Whether appending actimeo=0 option to nfs mount command")
78+
fs.IntVar(&o.SkipMatchingTagCacheExpireInMinutes, "skip-matching-tag-cache-expire-in-minutes", 30, "The cache expire time in minutes for skipMatchingTagCache")
79+
fs.IntVar(&o.VolStatsCacheExpireInMinutes, "vol-stats-cache-expire-in-minutes", 10, "The cache expire time in minutes for volume stats cache")
80+
fs.BoolVar(&o.PrintVolumeStatsCallLogs, "print-volume-stats-call-logs", false, "Whether to print volume statfs call logs with log level 2")
81+
fs.IntVar(&o.SasTokenExpirationMinutes, "sas-token-expiration-minutes", 1440, "sas token expiration minutes during volume cloning")
82+
fs.StringVar(&o.KubeConfig, "kubeconfig", "", "Absolute path to the kubeconfig file. Required only when running out of cluster.")
83+
fs.StringVar(&o.Endpoint, "endpoint", "unix://tmp/csi.sock", "CSI endpoint")
84+
85+
return fs
86+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package azurefile
18+
19+
import (
20+
"flag"
21+
"reflect"
22+
"testing"
23+
)
24+
25+
func TestDriverOptions_AddFlags(t *testing.T) {
26+
o := &DriverOptions{}
27+
typeInfo := reflect.TypeOf(*o)
28+
29+
got := o.AddFlags()
30+
count := 0
31+
got.VisitAll(func(f *flag.Flag) {
32+
count++
33+
})
34+
if count != typeInfo.NumField() {
35+
t.Errorf("DriverOptions.AddFlags() = %v, want %v", count, typeInfo.NumField())
36+
}
37+
}

pkg/azurefile/azurefile_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ func TestNewFakeDriver(t *testing.T) {
9797
driverOptions := DriverOptions{
9898
NodeID: fakeNodeID,
9999
DriverName: DefaultDriverName,
100+
Endpoint: "tcp://127.0.0.1:0",
101+
KubeConfig: "",
100102
}
101103
d := NewDriver(&driverOptions)
102104
assert.NotNil(t, d)
@@ -1058,7 +1060,7 @@ func TestRun(t *testing.T) {
10581060
time.Sleep(1 * time.Second)
10591061
cancelFn()
10601062
}()
1061-
if err := d.Run(ctx, "tcp://127.0.0.1:0", ""); err != nil {
1063+
if err := d.Run(ctx); err != nil {
10621064
t.Error(err.Error())
10631065
}
10641066

@@ -1093,7 +1095,7 @@ func TestRun(t *testing.T) {
10931095
}()
10941096
d.cloud = &azure.Cloud{}
10951097
d.NodeID = ""
1096-
if err := d.Run(ctx, "tcp://127.0.0.1:0", ""); err != nil {
1098+
if err := d.Run(ctx); err != nil {
10971099
t.Error(err.Error())
10981100
}
10991101
},

pkg/azurefileplugin/main.go

+8-63
Original file line numberDiff line numberDiff line change
@@ -33,94 +33,39 @@ import (
3333

3434
func init() {
3535
klog.InitFlags(nil)
36+
driverOptions.AddFlags().VisitAll(func(f *flag.Flag) {
37+
flag.CommandLine.Var(f.Value, f.Name, f.Usage)
38+
})
3639
}
3740

3841
var (
39-
endpoint = flag.String("endpoint", "unix://tmp/csi.sock", "CSI endpoint")
40-
nodeID = flag.String("nodeid", "", "node id")
41-
version = flag.Bool("version", false, "Print the version and exit.")
42-
metricsAddress = flag.String("metrics-address", "", "export the metrics")
43-
kubeconfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig file. Required only when running out of cluster.")
44-
driverName = flag.String("drivername", azurefile.DefaultDriverName, "name of the driver")
45-
cloudConfigSecretName = flag.String("cloud-config-secret-name", "azure-cloud-provider", "secret name of cloud config")
46-
cloudConfigSecretNamespace = flag.String("cloud-config-secret-namespace", "kube-system", "secret namespace of cloud config")
47-
customUserAgent = flag.String("custom-user-agent", "", "custom userAgent")
48-
userAgentSuffix = flag.String("user-agent-suffix", "", "userAgent suffix")
49-
allowEmptyCloudConfig = flag.Bool("allow-empty-cloud-config", true, "allow running driver without cloud config")
50-
enableVolumeMountGroup = flag.Bool("enable-volume-mount-group", true, "indicates whether enabling VOLUME_MOUNT_GROUP")
51-
enableGetVolumeStats = flag.Bool("enable-get-volume-stats", true, "allow GET_VOLUME_STATS on agent node")
52-
mountPermissions = flag.Uint64("mount-permissions", 0777, "mounted folder permissions")
53-
allowInlineVolumeKeyAccessWithIdentity = flag.Bool("allow-inline-volume-key-access-with-identity", false, "allow accessing storage account key using cluster identity for inline volume")
54-
fsGroupChangePolicy = flag.String("fsgroup-change-policy", "", "indicates how the volume's ownership will be changed by the driver, OnRootMismatch is the default value")
55-
enableVHDDiskFeature = flag.Bool("enable-vhd", true, "enable VHD disk feature (experimental)")
56-
kubeAPIQPS = flag.Float64("kube-api-qps", 25.0, "QPS to use while communicating with the kubernetes apiserver.")
57-
kubeAPIBurst = flag.Int("kube-api-burst", 50, "Burst to use while communicating with the kubernetes apiserver.")
58-
appendMountErrorHelpLink = flag.Bool("append-mount-error-help-link", true, "Whether to include a link for help with mount errors when a mount error occurs.")
59-
enableWindowsHostProcess = flag.Bool("enable-windows-host-process", false, "enable windows host process")
60-
appendClosetimeoOption = flag.Bool("append-closetimeo-option", false, "Whether appending closetimeo=0 option to smb mount command")
61-
appendNoShareSockOption = flag.Bool("append-nosharesock-option", true, "Whether appending nosharesock option to smb mount command")
62-
appendNoResvPortOption = flag.Bool("append-noresvport-option", true, "Whether appending noresvport option to nfs mount command")
63-
appendActimeoOption = flag.Bool("append-actimeo-option", true, "Whether appending actimeo=0 option to nfs mount command")
64-
skipMatchingTagCacheExpireInMinutes = flag.Int("skip-matching-tag-cache-expire-in-minutes", 30, "The cache expire time in minutes for skipMatchingTagCache")
65-
volStatsCacheExpireInMinutes = flag.Int("vol-stats-cache-expire-in-minutes", 10, "The cache expire time in minutes for volume stats cache")
66-
printVolumeStatsCallLogs = flag.Bool("print-volume-stats-call-logs", false, "Whether to print volume statfs call logs with log level 2")
67-
sasTokenExpirationMinutes = flag.Int("sas-token-expiration-minutes", 1440, "sas token expiration minutes during volume cloning")
42+
version = flag.Bool("version", false, "Print the version and exit.")
43+
metricsAddress = flag.String("metrics-address", "", "export the metrics")
44+
driverOptions azurefile.DriverOptions
6845
)
6946

7047
func main() {
7148
flag.Parse()
7249
if *version {
73-
info, err := azurefile.GetVersionYAML(*driverName)
50+
info, err := azurefile.GetVersionYAML(driverOptions.DriverName)
7451
if err != nil {
7552
klog.Fatalln(err)
7653
}
7754
fmt.Println(info) // nolint
7855
os.Exit(0)
7956
}
8057

81-
if *nodeID == "" {
82-
// nodeid is not needed in controller component
83-
klog.Warning("nodeid is empty")
84-
}
85-
8658
exportMetrics()
8759
handle()
8860
os.Exit(0)
8961
}
9062

9163
func handle() {
92-
driverOptions := azurefile.DriverOptions{
93-
NodeID: *nodeID,
94-
DriverName: *driverName,
95-
CloudConfigSecretName: *cloudConfigSecretName,
96-
CloudConfigSecretNamespace: *cloudConfigSecretNamespace,
97-
CustomUserAgent: *customUserAgent,
98-
UserAgentSuffix: *userAgentSuffix,
99-
AllowEmptyCloudConfig: *allowEmptyCloudConfig,
100-
EnableVolumeMountGroup: *enableVolumeMountGroup,
101-
EnableGetVolumeStats: *enableGetVolumeStats,
102-
MountPermissions: *mountPermissions,
103-
AllowInlineVolumeKeyAccessWithIdentity: *allowInlineVolumeKeyAccessWithIdentity,
104-
FSGroupChangePolicy: *fsGroupChangePolicy,
105-
EnableVHDDiskFeature: *enableVHDDiskFeature,
106-
AppendMountErrorHelpLink: *appendMountErrorHelpLink,
107-
KubeAPIQPS: *kubeAPIQPS,
108-
KubeAPIBurst: *kubeAPIBurst,
109-
EnableWindowsHostProcess: *enableWindowsHostProcess,
110-
AppendClosetimeoOption: *appendClosetimeoOption,
111-
AppendNoShareSockOption: *appendNoShareSockOption,
112-
AppendNoResvPortOption: *appendNoResvPortOption,
113-
AppendActimeoOption: *appendActimeoOption,
114-
SkipMatchingTagCacheExpireInMinutes: *skipMatchingTagCacheExpireInMinutes,
115-
VolStatsCacheExpireInMinutes: *volStatsCacheExpireInMinutes,
116-
PrintVolumeStatsCallLogs: *printVolumeStatsCallLogs,
117-
SasTokenExpirationMinutes: *sasTokenExpirationMinutes,
118-
}
11964
driver := azurefile.NewDriver(&driverOptions)
12065
if driver == nil {
12166
klog.Fatalln("Failed to initialize azurefile CSI Driver")
12267
}
123-
if err := driver.Run(context.Background(), *endpoint, *kubeconfig); err != nil {
68+
if err := driver.Run(context.Background()); err != nil {
12469
klog.Fatalln(err)
12570
}
12671
}

test/e2e/suite_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,13 @@ var _ = ginkgo.BeforeSuite(func(ctx ginkgo.SpecContext) {
146146
driverOptions := azurefile.DriverOptions{
147147
NodeID: os.Getenv("nodeid"),
148148
DriverName: azurefile.DefaultDriverName,
149+
Endpoint: fmt.Sprintf("unix:///tmp/csi-%s.sock", uuid.NewUUID().String()),
150+
KubeConfig: kubeconfig,
149151
}
150152
azurefileDriver = azurefile.NewDriver(&driverOptions)
151153
go func() {
152154
os.Setenv("AZURE_CREDENTIAL_FILE", credentials.TempAzureCredentialFilePath)
153-
err := azurefileDriver.Run(context.Background(), fmt.Sprintf("unix:///tmp/csi-%s.sock", uuid.NewUUID().String()), kubeconfig)
155+
err := azurefileDriver.Run(context.Background())
154156
gomega.Expect(err).NotTo(gomega.HaveOccurred())
155157
}()
156158
}

0 commit comments

Comments
 (0)