forked from SynologyOpenSource/synology-csi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (90 loc) · 2.7 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* Copyright 2021 Synology Inc.
*/
package main
import (
"os"
"os/signal"
"syscall"
"github.com/spf13/cobra"
log "github.com/sirupsen/logrus"
"github.com/SynologyOpenSource/synology-csi/pkg/driver"
"github.com/SynologyOpenSource/synology-csi/pkg/dsm/common"
"github.com/SynologyOpenSource/synology-csi/pkg/dsm/service"
"github.com/SynologyOpenSource/synology-csi/pkg/logger"
)
var (
// CSI options
csiNodeID = "CSINode"
csiEndpoint = "unix:///var/lib/kubelet/plugins/" + driver.DriverName + "/csi.sock"
csiClientInfoPath = "/etc/synology/client-info.yml"
// Logging
logLevel = "info"
webapiDebug = false
)
var rootCmd = &cobra.Command{
Use: "synology-csi-driver",
Short: "Synology CSI Driver",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if webapiDebug {
logger.WebapiDebug = true
logLevel = "debug"
}
logger.Init(logLevel)
err := driverStart()
if err != nil {
log.Errorf("Failed to driverStart(): %v", err)
return err
}
return nil
},
}
func driverStart() error {
log.Infof("CSI Options = {%s, %s, %s}", csiNodeID, csiEndpoint, csiClientInfoPath)
dsmService := service.NewDsmService()
// 1. Login DSMs by given ClientInfo
info, err := common.LoadConfig(csiClientInfoPath)
if err != nil {
log.Errorf("Failed to read config: %v", err)
return err
}
for _, client := range info.Clients {
err := dsmService.AddDsm(client)
if err != nil {
log.Errorf("Failed to add DSM: %s, error: %v", client.Host, err)
}
}
defer dsmService.RemoveAllDsms()
// 2. Create and Run the Driver
drv, err := driver.NewControllerAndNodeDriver(csiNodeID, csiEndpoint, dsmService)
if err != nil {
log.Errorf("Failed to create driver: %v", err)
return err
}
drv.Activate()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
// Block until a signal is received.
<-c
log.Infof("Shutting down.")
return nil
}
func main() {
addFlags(rootCmd)
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
os.Exit(0)
}
func addFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringVar(&csiNodeID, "nodeid", csiNodeID, "Node ID")
cmd.PersistentFlags().StringVarP(&csiEndpoint, "endpoint", "e", csiEndpoint, "CSI endpoint")
cmd.PersistentFlags().StringVarP(&csiClientInfoPath, "client-info", "f", csiClientInfoPath, "Path of Synology config yaml file")
cmd.PersistentFlags().StringVar(&logLevel, "log-level", logLevel, "Log level (debug, info, warn, error, fatal)")
cmd.PersistentFlags().BoolVarP(&webapiDebug, "debug", "d", webapiDebug, "Enable webapi debugging logs")
cmd.MarkFlagRequired("endpoint")
cmd.MarkFlagRequired("client-info")
cmd.Flags().SortFlags = false
cmd.PersistentFlags().SortFlags = false
}