@@ -2,33 +2,35 @@ package main
22
33import (
44 "context"
5+ "errors"
56 "flag"
67 "fmt"
78 "os"
89 "os/signal"
9- "syscall"
1010 "time"
1111
12+ "github.com/SwissDataScienceCenter/csi-rclone/pkg/common"
1213 "github.com/SwissDataScienceCenter/csi-rclone/pkg/metrics"
1314 "github.com/SwissDataScienceCenter/csi-rclone/pkg/rclone"
1415 "github.com/spf13/cobra"
16+ "github.com/spf13/pflag"
1517 "k8s.io/klog"
16- mountUtils "k8s.io/mount-utils"
1718)
1819
19- var (
20- endpoint string
21- nodeID string
22- cacheDir string
23- cacheSize string
24- meters []metrics. Observable
25- )
20+ func exitOnError ( err error ) {
21+ // ParseFlags uses errors to return some status information, ignore it here.
22+ if err != nil && ! errors . Is ( err , pflag . ErrHelp ) {
23+ klog . Error ( err . Error ())
24+ os . Exit ( 1 )
25+ }
26+ }
2627
2728func init () {
28- flag .Set ("logtostderr" , "true" )
29+ exitOnError ( flag .Set ("logtostderr" , "true" ) )
2930}
3031
3132func main () {
33+ var meters []metrics.Observable
3234 metricsServerConfig := metrics.ServerConfig {
3335 Host : "localhost" ,
3436 Port : 9090 ,
@@ -37,123 +39,49 @@ func main() {
3739 ShutdownTimeout : 5 * time .Second ,
3840 Enabled : false ,
3941 }
42+ nodeServerConfig := rclone.NodeServerConfig {}
43+ controllerServerConfig := rclone.ControllerServerConfig {}
4044
4145 root := & cobra.Command {
4246 Use : "rclone" ,
4347 Short : "CSI based rclone driver" ,
4448 }
49+ // Allow flags to be defined in subcommands, they will be reported at the Execute() step, with the help printed
50+ // before exiting.
51+ root .FParseErrWhitelist .UnknownFlags = true
52+
4553 metricsServerConfig .CommandLineParameters (root )
4654
4755 runCmd := & cobra.Command {
4856 Use : "run" ,
4957 Short : "Start the CSI driver." ,
5058 }
51- root .AddCommand (runCmd )
59+ exitOnError (nodeServerConfig .CommandLineParameters (runCmd , & meters ))
60+ exitOnError (controllerServerConfig .CommandLineParameters (runCmd , & meters ))
5261
53- runNode := & cobra.Command {
54- Use : "node" ,
55- Short : "Start the CSI driver node service - expected to run in a daemonset on every node." ,
56- Run : func (cmd * cobra.Command , args []string ) {
57- handleNode ()
58- },
59- }
60- runNode .PersistentFlags ().StringVar (& nodeID , "nodeid" , "" , "node id" )
61- runNode .MarkPersistentFlagRequired ("nodeid" )
62- runNode .PersistentFlags ().StringVar (& endpoint , "endpoint" , "" , "CSI endpoint" )
63- runNode .MarkPersistentFlagRequired ("endpoint" )
64- runNode .PersistentFlags ().StringVar (& cacheDir , "cachedir" , "" , "cache dir" )
65- runNode .PersistentFlags ().StringVar (& cacheSize , "cachesize" , "" , "cache size" )
66- runCmd .AddCommand (runNode )
67- runController := & cobra.Command {
68- Use : "controller" ,
69- Short : "Start the CSI driver controller." ,
70- Run : func (cmd * cobra.Command , args []string ) {
71- handleController ()
72- },
73- }
74- runController .PersistentFlags ().StringVar (& nodeID , "nodeid" , "" , "node id" )
75- runController .MarkPersistentFlagRequired ("nodeid" )
76- runController .PersistentFlags ().StringVar (& endpoint , "endpoint" , "" , "CSI endpoint" )
77- runController .MarkPersistentFlagRequired ("endpoint" )
78- runCmd .AddCommand (runController )
62+ root .AddCommand (runCmd )
7963
8064 versionCmd := & cobra.Command {
8165 Use : "version" ,
8266 Short : "Prints information about this version of csi rclone plugin" ,
8367 Run : func (cmd * cobra.Command , args []string ) {
84- fmt .Printf ("csi-rclone plugin Version: %s" , rclone .DriverVersion )
68+ fmt .Printf ("csi-rclone plugin Version: %s\n " , rclone .DriverVersion )
8569 },
8670 }
8771 root .AddCommand (versionCmd )
8872
89- root .ParseFlags (os .Args [1 :])
73+ exitOnError ( root .ParseFlags (os .Args [1 :]) )
9074
9175 if metricsServerConfig .Enabled {
9276 // Gracefully exit the metrics background servers
93- ctx , stop := signal .NotifyContext (context .Background (), syscall . SIGTERM , syscall . SIGINT )
77+ ctx , stop := signal .NotifyContext (context .Background (), common . InterruptSignals ... )
9478 defer stop ()
9579
9680 metricsServer := metricsServerConfig .NewServer (ctx , & meters )
9781 go metricsServer .ListenAndServe ()
9882 }
9983
100- if err := root .Execute (); err != nil {
101- fmt .Fprintf (os .Stderr , "%s" , err .Error ())
102- os .Exit (1 )
103- }
84+ exitOnError (root .Execute ())
10485
10586 os .Exit (0 )
10687}
107-
108- func handleNode () {
109- err := unmountOldVols ()
110- if err != nil {
111- klog .Warningf ("There was an error when trying to unmount old volumes: %v" , err )
112- }
113- d := rclone .NewDriver (nodeID , endpoint )
114- ns , err := rclone .NewNodeServer (d .CSIDriver , cacheDir , cacheSize )
115- if err != nil {
116- panic (err )
117- }
118- meters = append (meters , ns .Metrics ()... )
119- d .WithNodeServer (ns )
120- err = d .Run ()
121- if err != nil {
122- panic (err )
123- }
124- }
125-
126- func handleController () {
127- d := rclone .NewDriver (nodeID , endpoint )
128- cs := rclone .NewControllerServer (d .CSIDriver )
129- meters = append (meters , cs .Metrics ()... )
130- d .WithControllerServer (cs )
131- err := d .Run ()
132- if err != nil {
133- panic (err )
134- }
135- }
136-
137- // unmountOldVols is used to unmount volumes after a restart on a node
138- func unmountOldVols () error {
139- const mountType = "fuse.rclone"
140- const unmountTimeout = time .Second * 5
141- klog .Info ("Checking for existing mounts" )
142- mounter := mountUtils.Mounter {}
143- mounts , err := mounter .List ()
144- if err != nil {
145- return err
146- }
147- for _ , mount := range mounts {
148- if mount .Type != mountType {
149- continue
150- }
151- err := mounter .UnmountWithForce (mount .Path , unmountTimeout )
152- if err != nil {
153- klog .Warningf ("Failed to unmount %s because of %v." , mount .Path , err )
154- continue
155- }
156- klog .Infof ("Sucessfully unmounted %s" , mount .Path )
157- }
158- return nil
159- }
0 commit comments