Skip to content

Commit

Permalink
move flag option to azurefile package
Browse files Browse the repository at this point in the history
Signed-off-by: Fan Shang Xiang <[email protected]>
  • Loading branch information
MartinForReal committed Dec 25, 2023
1 parent 6337ecd commit dad3e43
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 98 deletions.
45 changes: 13 additions & 32 deletions pkg/azurefile/azurefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,35 +195,6 @@ var (
retriableErrors = []string{accountNotProvisioned, tooManyRequests, shareBeingDeleted, clientThrottled}
)

// DriverOptions defines driver parameters specified in driver deployment
type DriverOptions struct {
NodeID string
DriverName string
CloudConfigSecretName string
CloudConfigSecretNamespace string
CustomUserAgent string
UserAgentSuffix string
AllowEmptyCloudConfig bool
AllowInlineVolumeKeyAccessWithIdentity bool
EnableVHDDiskFeature bool
EnableVolumeMountGroup bool
EnableGetVolumeStats bool
AppendMountErrorHelpLink bool
MountPermissions uint64
FSGroupChangePolicy string
KubeAPIQPS float64
KubeAPIBurst int
EnableWindowsHostProcess bool
AppendClosetimeoOption bool
AppendNoShareSockOption bool
AppendNoResvPortOption bool
AppendActimeoOption bool
SkipMatchingTagCacheExpireInMinutes int
VolStatsCacheExpireInMinutes int
PrintVolumeStatsCallLogs bool
SasTokenExpirationMinutes int
}

// Driver implements all interfaces of CSI drivers
type Driver struct {
csicommon.CSIDriver
Expand Down Expand Up @@ -280,6 +251,9 @@ type Driver struct {
sasTokenExpirationMinutes int
// azcopy for provide exec mock for ut
azcopy *fileutil.Azcopy

kubeconfig string
endpoint string
}

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

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

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

if *&d.NodeID == "" {
// nodeid is not needed in controller component
klog.Warning("nodeid is empty")
}

userAgent := GetUserAgent(d.Name, d.customUserAgent, d.userAgentSuffix)
klog.V(2).Infof("driver userAgent: %s", userAgent)
d.cloud, err = getCloudProvider(context.Background(), kubeconfig, d.NodeID, d.cloudConfigSecretName, d.cloudConfigSecretNamespace, userAgent, d.allowEmptyCloudConfig, d.enableWindowsHostProcess, d.kubeAPIQPS, d.kubeAPIBurst)
d.cloud, err = getCloudProvider(context.Background(), d.kubeconfig, d.NodeID, d.cloudConfigSecretName, d.cloudConfigSecretNamespace, userAgent, d.allowEmptyCloudConfig, d.enableWindowsHostProcess, d.kubeAPIQPS, d.kubeAPIBurst)
if err != nil {
klog.Fatalf("failed to get Azure Cloud Provider, error: %v", err)
}
Expand Down Expand Up @@ -420,7 +401,7 @@ func (d *Driver) Run(ctx context.Context, endpoint, kubeconfig string) error {
csi.RegisterNodeServer(server, d)
d.server = server

listener, err := csicommon.ListenEndpoint(endpoint)
listener, err := csicommon.ListenEndpoint(d.endpoint)
if err != nil {
klog.Fatalf("failed to listen endpoint: %v", err)
}
Expand Down
86 changes: 86 additions & 0 deletions pkg/azurefile/azurefile_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright 2019 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 azurefile

import "flag"

// DriverOptions defines driver parameters specified in driver deployment
type DriverOptions struct {
NodeID string
DriverName string
CloudConfigSecretName string
CloudConfigSecretNamespace string
CustomUserAgent string
UserAgentSuffix string
AllowEmptyCloudConfig bool
AllowInlineVolumeKeyAccessWithIdentity bool
EnableVHDDiskFeature bool
EnableVolumeMountGroup bool
EnableGetVolumeStats bool
AppendMountErrorHelpLink bool
MountPermissions uint64
FSGroupChangePolicy string
KubeAPIQPS float64
KubeAPIBurst int
EnableWindowsHostProcess bool
AppendClosetimeoOption bool
AppendNoShareSockOption bool
AppendNoResvPortOption bool
AppendActimeoOption bool
SkipMatchingTagCacheExpireInMinutes int
VolStatsCacheExpireInMinutes int
PrintVolumeStatsCallLogs bool
SasTokenExpirationMinutes int
KubeConfig string
Endpoint string
}

func (o *DriverOptions) AddFlags() *flag.FlagSet {
if o == nil {
return nil
}
fs := flag.NewFlagSet("", flag.ExitOnError)
fs.StringVar(&o.NodeID, "nodeid", "", "node id")
fs.StringVar(&o.DriverName, "drivername", DefaultDriverName, "name of the driver")
fs.StringVar(&o.CloudConfigSecretName, "cloud-config-secret-name", "azure-cloud-provider", "secret name of cloud config")
fs.StringVar(&o.CloudConfigSecretNamespace, "cloud-config-secret-namespace", "kube-system", "secret namespace of cloud config")
fs.StringVar(&o.CustomUserAgent, "custom-user-agent", "", "custom userAgent")
fs.StringVar(&o.UserAgentSuffix, "user-agent-suffix", "", "userAgent suffix")
fs.BoolVar(&o.AllowEmptyCloudConfig, "allow-empty-cloud-config", true, "allow running driver without cloud config")
fs.BoolVar(&o.AllowInlineVolumeKeyAccessWithIdentity, "allow-inline-volume-key-access-with-identity", false, "allow accessing storage account key using cluster identity for inline volume")
fs.BoolVar(&o.EnableVHDDiskFeature, "enable-vhd", true, "enable VHD disk feature (experimental)")
fs.BoolVar(&o.EnableVolumeMountGroup, "enable-volume-mount-group", true, "indicates whether enabling VOLUME_MOUNT_GROUP")
fs.BoolVar(&o.EnableGetVolumeStats, "enable-get-volume-stats", true, "allow GET_VOLUME_STATS on agent node")
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.")
fs.Uint64Var(&o.MountPermissions, "mount-permissions", 0777, "mounted folder permissions")
fs.StringVar(&o.FSGroupChangePolicy, "fsgroup-change-policy", "", "indicates how the volume's ownership will be changed by the driver, OnRootMismatch is the default value")
fs.Float64Var(&o.KubeAPIQPS, "kube-api-qps", 25.0, "QPS to use while communicating with the kubernetes apiserver.")
fs.IntVar(&o.KubeAPIBurst, "kube-api-burst", 50, "Burst to use while communicating with the kubernetes apiserver.")
fs.BoolVar(&o.EnableWindowsHostProcess, "enable-windows-host-process", false, "enable windows host process")
fs.BoolVar(&o.AppendClosetimeoOption, "append-closetimeo-option", false, "Whether appending closetimeo=0 option to smb mount command")
fs.BoolVar(&o.AppendNoShareSockOption, "append-nosharesock-option", true, "Whether appending nosharesock option to smb mount command")
fs.BoolVar(&o.AppendNoResvPortOption, "append-noresvport-option", true, "Whether appending noresvport option to nfs mount command")
fs.BoolVar(&o.AppendActimeoOption, "append-actimeo-option", true, "Whether appending actimeo=0 option to nfs mount command")
fs.IntVar(&o.SkipMatchingTagCacheExpireInMinutes, "skip-matching-tag-cache-expire-in-minutes", 30, "The cache expire time in minutes for skipMatchingTagCache")
fs.IntVar(&o.VolStatsCacheExpireInMinutes, "vol-stats-cache-expire-in-minutes", 10, "The cache expire time in minutes for volume stats cache")
fs.BoolVar(&o.PrintVolumeStatsCallLogs, "print-volume-stats-call-logs", false, "Whether to print volume statfs call logs with log level 2")
fs.IntVar(&o.SasTokenExpirationMinutes, "sas-token-expiration-minutes", 1440, "sas token expiration minutes during volume cloning")
fs.StringVar(&o.KubeConfig, "kubeconfig", "", "Absolute path to the kubeconfig file. Required only when running out of cluster.")
fs.StringVar(&o.Endpoint, "endpoint", "unix://tmp/csi.sock", "CSI endpoint")

return fs
}
37 changes: 37 additions & 0 deletions pkg/azurefile/azurefile_options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright 2019 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 azurefile

import (
"flag"
"reflect"
"testing"
)

func TestDriverOptions_AddFlags(t *testing.T) {
o := &DriverOptions{}
typeInfo := reflect.TypeOf(*o)

got := o.AddFlags()
count := 0
got.VisitAll(func(f *flag.Flag) {
count++
})
if count != typeInfo.NumField() {
t.Errorf("DriverOptions.AddFlags() = %v, want %v", count, typeInfo.NumField())
}
}
6 changes: 4 additions & 2 deletions pkg/azurefile/azurefile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ func TestNewFakeDriver(t *testing.T) {
driverOptions := DriverOptions{
NodeID: fakeNodeID,
DriverName: DefaultDriverName,
Endpoint: "tcp://127.0.0.1:0",
KubeConfig: "",
}
d := NewDriver(&driverOptions)
assert.NotNil(t, d)
Expand Down Expand Up @@ -1058,7 +1060,7 @@ func TestRun(t *testing.T) {
time.Sleep(1 * time.Second)
cancelFn()
}()
if err := d.Run(ctx, "tcp://127.0.0.1:0", ""); err != nil {
if err := d.Run(ctx); err != nil {
t.Error(err.Error())
}

Expand Down Expand Up @@ -1093,7 +1095,7 @@ func TestRun(t *testing.T) {
}()
d.cloud = &azure.Cloud{}
d.NodeID = ""
if err := d.Run(ctx, "tcp://127.0.0.1:0", ""); err != nil {
if err := d.Run(ctx); err != nil {
t.Error(err.Error())
}
},
Expand Down
71 changes: 8 additions & 63 deletions pkg/azurefileplugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,94 +33,39 @@ import (

func init() {
klog.InitFlags(nil)
driverOptions.AddFlags().VisitAll(func(f *flag.Flag) {
flag.CommandLine.Var(f.Value, f.Name, f.Usage)
})
}

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

func main() {
flag.Parse()
if *version {
info, err := azurefile.GetVersionYAML(*driverName)
info, err := azurefile.GetVersionYAML(driverOptions.DriverName)
if err != nil {
klog.Fatalln(err)
}
fmt.Println(info) // nolint
os.Exit(0)
}

if *nodeID == "" {
// nodeid is not needed in controller component
klog.Warning("nodeid is empty")
}

exportMetrics()
handle()
os.Exit(0)
}

func handle() {
driverOptions := azurefile.DriverOptions{
NodeID: *nodeID,
DriverName: *driverName,
CloudConfigSecretName: *cloudConfigSecretName,
CloudConfigSecretNamespace: *cloudConfigSecretNamespace,
CustomUserAgent: *customUserAgent,
UserAgentSuffix: *userAgentSuffix,
AllowEmptyCloudConfig: *allowEmptyCloudConfig,
EnableVolumeMountGroup: *enableVolumeMountGroup,
EnableGetVolumeStats: *enableGetVolumeStats,
MountPermissions: *mountPermissions,
AllowInlineVolumeKeyAccessWithIdentity: *allowInlineVolumeKeyAccessWithIdentity,
FSGroupChangePolicy: *fsGroupChangePolicy,
EnableVHDDiskFeature: *enableVHDDiskFeature,
AppendMountErrorHelpLink: *appendMountErrorHelpLink,
KubeAPIQPS: *kubeAPIQPS,
KubeAPIBurst: *kubeAPIBurst,
EnableWindowsHostProcess: *enableWindowsHostProcess,
AppendClosetimeoOption: *appendClosetimeoOption,
AppendNoShareSockOption: *appendNoShareSockOption,
AppendNoResvPortOption: *appendNoResvPortOption,
AppendActimeoOption: *appendActimeoOption,
SkipMatchingTagCacheExpireInMinutes: *skipMatchingTagCacheExpireInMinutes,
VolStatsCacheExpireInMinutes: *volStatsCacheExpireInMinutes,
PrintVolumeStatsCallLogs: *printVolumeStatsCallLogs,
SasTokenExpirationMinutes: *sasTokenExpirationMinutes,
}
driver := azurefile.NewDriver(&driverOptions)
if driver == nil {
klog.Fatalln("Failed to initialize azurefile CSI Driver")
}
if err := driver.Run(context.Background(), *endpoint, *kubeconfig); err != nil {
if err := driver.Run(context.Background()); err != nil {
klog.Fatalln(err)
}
}
Expand Down
4 changes: 3 additions & 1 deletion test/e2e/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,13 @@ var _ = ginkgo.BeforeSuite(func(ctx ginkgo.SpecContext) {
driverOptions := azurefile.DriverOptions{
NodeID: os.Getenv("nodeid"),
DriverName: azurefile.DefaultDriverName,
Endpoint: fmt.Sprintf("unix:///tmp/csi-%s.sock", uuid.NewUUID().String()),
KubeConfig: kubeconfig,
}
azurefileDriver = azurefile.NewDriver(&driverOptions)
go func() {
os.Setenv("AZURE_CREDENTIAL_FILE", credentials.TempAzureCredentialFilePath)
err := azurefileDriver.Run(context.Background(), fmt.Sprintf("unix:///tmp/csi-%s.sock", uuid.NewUUID().String()), kubeconfig)
err := azurefileDriver.Run(context.Background())
gomega.Expect(err).NotTo(gomega.HaveOccurred())
}()
}
Expand Down

0 comments on commit dad3e43

Please sign in to comment.