-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
294 lines (247 loc) · 9.1 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
*
* Copyright © 2021-2023 Dell Inc. or its subsidiaries. All Rights Reserved.
*
* 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 main
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/dell/csi-powerstore/v2/core"
"github.com/dell/csi-powerstore/v2/pkg/common"
"github.com/dell/csi-powerstore/v2/pkg/common/fs"
"github.com/dell/csi-powerstore/v2/pkg/controller"
"github.com/dell/csi-powerstore/v2/pkg/identity"
"github.com/dell/csi-powerstore/v2/pkg/interceptors"
"github.com/dell/csi-powerstore/v2/pkg/node"
"github.com/dell/csi-powerstore/v2/pkg/tracer"
"github.com/dell/gocsi"
csictx "github.com/dell/gocsi/context"
"github.com/dell/gofsutil"
"github.com/fsnotify/fsnotify"
grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
"github.com/opentracing/opentracing-go"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/uber/jaeger-client-go/config"
"google.golang.org/grpc"
)
//go:generate go generate ../../core
func init() {
// We set X_CSI_DEBUG to false, because we don't want gocsi to override our logging level
_ = os.Setenv(common.EnvGOCSIDebug, "false")
// Enable X_CSI_REQ_LOGGING and X_CSI_REP_LOGGING to see gRPC request information
_ = os.Setenv(gocsi.EnvVarReqLogging, "true")
_ = os.Setenv(gocsi.EnvVarRepLogging, "true")
paramsPath, ok := csictx.LookupEnv(context.Background(), common.EnvConfigParamsFilePath)
if !ok {
log.Warnf("config path X_CSI_POWERSTORE_CONFIG_PARAMS_PATH is not specified")
}
if name, ok := csictx.LookupEnv(context.Background(), common.EnvDriverName); ok {
common.Name = name
}
paramsViper := viper.New()
paramsViper.SetConfigFile(paramsPath)
paramsViper.SetConfigType("yaml")
err := paramsViper.ReadInConfig()
// if unable to read configuration file, default values will be used in updateDriverConfigParams
if err != nil {
log.WithError(err).Error("unable to read config file, using default values")
}
paramsViper.WatchConfig()
paramsViper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Params config file changed:", e.Name)
updateDriverConfigParams(paramsViper)
})
updateDriverConfigParams(paramsViper)
// If we don't set this env gocsi will overwrite log level with default Info level
err = os.Setenv(gocsi.EnvVarLogLevel, log.GetLevel().String())
if err != nil {
log.WithError(err).Errorf("unable to set env variable %s", gocsi.EnvVarDebug)
}
}
func main() {
f := &fs.Fs{Util: &gofsutil.FS{}}
common.RmSockFile(f)
identityService := identity.NewIdentityService(common.Name, core.SemVer, common.Manifest)
var controllerService *controller.Service
var nodeService *node.Service
mode := csictx.Getenv(context.Background(), gocsi.EnvVarMode)
configPath, ok := csictx.LookupEnv(context.Background(), common.EnvArrayConfigFilePath)
if !ok {
log.Fatalf("config path X_CSI_POWERSTORE_CONFIG_PATH is not specified")
}
if name, ok := csictx.LookupEnv(context.Background(), common.EnvDriverName); ok {
common.Name = name
}
common.SetAPIPort(context.Background())
if strings.EqualFold(mode, "controller") {
cs := &controller.Service{
Fs: f,
}
err := cs.UpdateArrays(configPath, f)
if err != nil {
log.Fatalf("couldn't initialize arrays in controller service: %s", err.Error())
}
err = cs.Init()
if err != nil {
log.Fatalf("couldn't create controller service: %s", err.Error())
}
if cs.K8sVisibilityAutoRegistration {
err = cs.RegisterK8sCluster(f)
if err != nil {
log.Errorf("couldn't register to arrays in controller service: %s", err.Error())
}
}
controllerService = cs
} else if strings.EqualFold(mode, "node") {
ns := &node.Service{
Fs: f,
}
err := ns.UpdateArrays(configPath, f)
if err != nil {
log.Fatalf("couldn't initialize arrays in node service: %s", err.Error())
}
err = ns.Init()
if err != nil {
log.Fatalf("couldn't create node service: %s", err.Error())
}
nodeService = ns
}
viper.SetConfigFile(configPath)
viper.SetConfigType("yaml")
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
log.Println("Config file changed:", e.Name)
if strings.EqualFold(mode, "controller") {
err := controllerService.UpdateArrays(configPath, f)
if err != nil {
log.Fatalf("couldn't initialize arrays in controller service: %s", err.Error())
}
if controllerService.K8sVisibilityAutoRegistration {
err = controllerService.RegisterK8sCluster(f)
if err != nil {
log.Errorf("couldn't register to arrays in controller service: %s", err.Error())
}
}
} else if strings.EqualFold(mode, "node") {
err := nodeService.UpdateArrays(configPath, f)
if err != nil {
log.Fatalf("couldn't initialize arrays in node service: %s", err.Error())
}
}
})
interList := []grpc.UnaryServerInterceptor{
interceptors.NewCustomSerialLock(mode),
interceptors.NewRewriteRequestIDInterceptor(),
}
if enableTracing, ok := csictx.LookupEnv(context.Background(), common.EnvDebugEnableTracing); ok && enableTracing != "" {
log.Infof("Detected debug flag. Enabling Interceptors..")
t, closer, err := tracer.NewTracer(&config.Configuration{})
if err != nil {
log.Fatalf("couldn't create tracer for Jaeger: %s", err.Error())
}
defer closer.Close() // #nosec G307
opentracing.SetGlobalTracer(t)
interList = append(interList, grpc_opentracing.UnaryServerInterceptor(grpc_opentracing.WithTracer(t)))
}
gocsi.Run(context.Background(), common.Name,
"A PowerStore Container Storage Interface (CSI) Driver",
usage,
&gocsi.StoragePlugin{
Controller: controllerService,
Identity: identityService,
Node: nodeService,
Interceptors: interList,
RegisterAdditionalServers: controllerService.RegisterAdditionalServers,
EnvVars: []string{
// Enable request validation.
gocsi.EnvVarSpecReqValidation + "=true",
// Enable serial volume access.
gocsi.EnvVarSerialVolAccess + "=true",
},
})
}
func updateDriverConfigParams(v *viper.Viper) {
logLevelParam := "CSI_LOG_LEVEL"
logFormatParam := "CSI_LOG_FORMAT"
logFormat := strings.ToLower(v.GetString(logFormatParam))
fmt.Printf("Read CSI_LOG_FORMAT from log configuration file, format: %s\n", logFormat)
// Use JSON logger as default
if !strings.EqualFold(logFormat, "text") {
log.SetFormatter(&log.JSONFormatter{
TimestampFormat: time.RFC3339Nano,
})
} else {
log.SetFormatter(&log.TextFormatter{})
}
level := log.DebugLevel
if v.IsSet(logLevelParam) {
logLevel := v.GetString(logLevelParam)
if logLevel != "" {
logLevel = strings.ToLower(logLevel)
fmt.Printf("Read CSI_LOG_LEVEL from log configuration file, level: %s\n", logLevel)
var err error
l, err := log.ParseLevel(logLevel)
if err != nil {
log.WithError(err).Errorf("LOG_LEVEL %s value not recognized, setting to default error: %s ", logLevel, err.Error())
} else {
level = l
}
}
}
log.SetLevel(level)
}
const usage = `
X_CSI_POWERSTORE_INSECURE
Specifies that the PowerStore's hostname and certificate chain
should not be verified.
The default value is false.
X_CSI_POWERSTORE_NODE_ID_PATH
Specifies the name of the text file contents of which will
be appended to the node ID
X_CSI_POWERSTORE_KUBE_NODE_NAME
Specifies the name of the kubernetes node
X_CSI_POWERSTORE_NODE_NAME_PREFIX
Specifies prefix which will be used when registering node
on PowerStore array
X_CSI_POWERSTORE_NODE_CHROOT_PATH
Specifies path to chroot where to execute iSCSI commands
X_CSI_POWERSTORE_TMP_DIR
Specifies path to the folder which will be used for csi-powerstore temporary files
X_CSI_FC_PORTS_FILTER_FILE_PATH
Specifies path to the file which provide list of WWPN which
should be used by the driver for FC connection on this node
example content of the file:
21:00:00:29:ff:48:9f:6e,21:00:00:29:ff:48:9f:6e
If file does not exist, empty or in invalid format,
then the driver will use all available FC ports
X_CSI_POWERSTORE_THROTTLING_RATE_LIMIT
Specifies a number of concurrent requests to one storage API
X_CSI_POWERSTORE_ENABLE_CHAP
Specifies whether driver should set CHAP credentials in the ISCSI
node database at the time of node plugin boot
X_CSI_POWERSTORE_EXTERNAL_ACCESS
Specifies an IP of the additional router you wish to add for nfs export
Used to provide NFS volumes behind NAT
X_CSI_POWERSTORE_CONFIG_PATH
Specifies the filepath to PowerStore arrays config file which will be used
for connection to PowerStore arrays
X_CSI_REPLICATION_CONTEXT_PREFIX
Enables sidecars to read required information from volume context
X_CSI_REPLICATION_PREFIX
Used as a prefix to find out if replication is enabled
`