-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathmain.go
More file actions
141 lines (121 loc) · 4.1 KB
/
Copy pathmain.go
File metadata and controls
141 lines (121 loc) · 4.1 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright 2021 Synology Inc.
*/
package main
import (
"os"
"os/signal"
"syscall"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"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"
"github.com/SynologyOpenSource/synology-csi/pkg/utils/hostexec"
)
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
multipathForUC = true
// Locations is tools and directories
chrootDir = ""
iscsiadmPath = ""
multipathPath = ""
multipathdPath = ""
nvmePath = ""
)
var rootCmd = &cobra.Command{
Use: "synology-csi-driver",
Short: "Synology CSI Driver",
SilenceUsage: true,
RunE: func(_ *cobra.Command, _ []string) error {
if webapiDebug {
logger.WebapiDebug = true
logLevel = "debug"
}
logger.Init(logLevel)
if !multipathForUC {
driver.MultipathEnabled = false
}
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 command executor
cmdMap := map[string]string{
"iscsiadm": iscsiadmPath,
"multipath": multipathPath,
"multipathd": multipathdPath,
"nvme": nvmePath,
}
cmdExecutor, err := hostexec.New(cmdMap, chrootDir)
if err != nil {
log.Errorf("Failed to create command executor: %v", err)
return err
}
tools := driver.NewTools(cmdExecutor)
// 3. Create and Run the Driver
drv, err := driver.NewControllerAndNodeDriver(csiNodeID, csiEndpoint, dsmService, tools)
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() {
rootCmd.FParseErrWhitelist.UnknownFlags = true
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.PersistentFlags().BoolVar(&multipathForUC, "multipath", multipathForUC, "Set to 'false' to disable multipath for UC")
cmd.PersistentFlags().StringVar(&chrootDir, "chroot-dir", chrootDir, "Host directory to chroot into (empty disables chroot)")
cmd.PersistentFlags().StringVar(&iscsiadmPath, "iscsiadm-path", iscsiadmPath, "Full path of iscsiadm executable")
cmd.PersistentFlags().StringVar(&multipathPath, "multipath-path", multipathPath, "Full path of multipath executable")
cmd.PersistentFlags().StringVar(&multipathdPath, "multipathd-path", multipathdPath, "Full path of multipathd executable")
cmd.PersistentFlags().StringVar(&nvmePath, "nvme-path", nvmePath, "Full path of nvme executable")
cmd.MarkFlagRequired("endpoint")
cmd.MarkFlagRequired("client-info")
cmd.Flags().SortFlags = false
cmd.PersistentFlags().SortFlags = false
}