This repository has been archived by the owner on Apr 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
162 lines (137 loc) · 3.71 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"csi/backend"
"csi/driver"
"encoding/json"
"flag"
"io/ioutil"
"net"
"os"
"path/filepath"
"runtime/debug"
"time"
"utils/log"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
const (
configFile = "/etc/huawei/csi.json"
controllerLogFile = "huawei-csi-controller"
nodeLogFile = "huawei-csi-node"
csiLogFile = "huawei-csi"
csiVersion = "2.2.12"
defaultDriverName = "csi.huawei.com"
)
var (
endpoint = flag.String("endpoint",
"/var/lib/kubelet/plugins/huawei.csi.driver/csi.sock",
"CSI endpoint")
controller = flag.Bool("controller",
false,
"Run as a controller service")
controllerFlagFile = flag.String("controller-flag-file",
"/var/lib/kubelet/plugins/huawei.csi.driver/provider_running",
"The flag file path to specify controller service. Privilege is higher than controller")
driverName = flag.String("driver-name",
defaultDriverName,
"CSI driver name")
containerized = flag.Bool("containerized",
false,
"Run as a containerized service")
backendUpdateInterval = flag.Int("backend-update-interval",
60,
"The interval seconds to update backends status. Default is 60 seconds")
config CSIConfig
)
type CSIConfig struct {
Backends []map[string]interface{} `json:"backends"`
}
func init() {
_ = flag.Set("log_dir", "/var/log/huawei")
flag.Parse()
data, err := ioutil.ReadFile(configFile)
if err != nil {
logrus.Fatalf("Read config file %s error: %v", configFile, err)
}
err = json.Unmarshal(data, &config)
if err != nil {
logrus.Fatalf("Unmarshal config file %s error: %v", configFile, err)
}
if len(config.Backends) <= 0 {
logrus.Fatalf("Must configure at least one backend")
}
if *containerized {
*controllerFlagFile = ""
}
var logFilePrefix string
if len(*controllerFlagFile) > 0 {
logFilePrefix = csiLogFile
} else if *controller {
logFilePrefix = controllerLogFile
} else {
logFilePrefix = nodeLogFile
}
err = log.Init(map[string]string{
"logFilePrefix": logFilePrefix,
})
if err != nil {
logrus.Fatalf("Init log error: %v", err)
}
}
func updateBackendCapabilities() {
err := backend.SyncUpdateCapabilities()
if err != nil {
log.Fatalf("Update backend capabilities error: %v", err)
}
ticker := time.NewTicker(time.Second * time.Duration(*backendUpdateInterval))
for range ticker.C {
backend.AsyncUpdateCapabilities(*controllerFlagFile)
}
}
func main() {
defer func() {
if r := recover(); r != nil {
log.Errorf("Runtime error caught in main routine: %v", r)
log.Errorf("%s", debug.Stack())
}
log.Flush()
log.Close()
}()
if *controller || *controllerFlagFile != "" {
err := backend.RegisterBackend(config.Backends, true)
if err != nil {
log.Fatalf("Register backends error: %v", err)
}
go updateBackendCapabilities()
} else {
err := backend.RegisterBackend(config.Backends, false)
if err != nil {
log.Fatalf("Register backends error: %v", err)
}
}
endpointDir := filepath.Dir(*endpoint)
_, err := os.Stat(endpointDir)
if err != nil && os.IsNotExist(err) {
os.Mkdir(endpointDir, 0755)
} else {
_, err := os.Stat(*endpoint)
if err == nil {
log.Infof("Gonna remove old sock file %s", *endpoint)
os.Remove(*endpoint)
}
}
listener, err := net.Listen("unix", *endpoint)
if err != nil {
log.Fatalf("Listen on %s error: %v", *endpoint, err)
}
d := driver.NewDriver(*driverName, csiVersion)
server := grpc.NewServer()
csi.RegisterIdentityServer(server, d)
csi.RegisterControllerServer(server, d)
csi.RegisterNodeServer(server, d)
log.Infof("Starting Huawei CSI driver, listening on %s", *endpoint)
if err := server.Serve(listener); err != nil {
log.Fatalf("Start Huawei CSI driver error: %v", err)
}
}