forked from kubernetes-sigs/alibaba-cloud-csi-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
180 lines (157 loc) · 5.37 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
/*
Copyright 2019 The Kubernetes Authors.
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 (
"flag"
"io"
"os"
"path"
"strings"
"time"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/agent"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/cpfs"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/disk"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/local"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/lvm"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mem"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/nas"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/om"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/oss"
log "github.com/sirupsen/logrus"
)
func init() {
flag.Set("logtostderr", "true")
}
const (
// LogfilePrefix prefix of log file
LogfilePrefix = "/var/log/alicloud/"
// MBSIZE MB size
MBSIZE = 1024 * 1024
// TypePluginDISK DISK type plugin
TypePluginDISK = "diskplugin.csi.alibabacloud.com"
// TypePluginNAS NAS type plugin
TypePluginNAS = "nasplugin.csi.alibabacloud.com"
// TypePluginOSS OSS type plugin
TypePluginOSS = "ossplugin.csi.alibabacloud.com"
// TypePluginCPFS CPFS type plugin
TypePluginCPFS = "cpfsplugin.csi.alibabacloud.com"
// TypePluginLVM LVM type plugin
TypePluginLVM = "lvmplugin.csi.alibabacloud.com"
// TypePluginMEM memory type plugin
TypePluginMEM = "memplugin.csi.alibabacloud.com"
// TypePluginLOCAL local type plugin
TypePluginLOCAL = "yodaplugin.csi.alibabacloud.com"
// ExtenderAgent agent component
ExtenderAgent = "agent"
)
// BRANCH is CSI Driver Branch
var BRANCH = ""
// VERSION is CSI Driver Version
var VERSION = ""
// COMMITID is CSI Driver CommitID
var COMMITID = ""
// BUILDTIME is CSI Driver Buildtime
var BUILDTIME = ""
var (
endpoint = flag.String("endpoint", "unix://tmp/csi.sock", "CSI endpoint")
nodeID = flag.String("nodeid", "", "node id")
runAsController = flag.Bool("run-as-controller", false, "Only run as controller service")
driver = flag.String("driver", TypePluginDISK, "CSI Driver")
rootDir = flag.String("rootdir", "/var/lib/kubelet/csi-plugins", "Kubernetes root directory")
)
// Nas CSI Plugin
func main() {
flag.Parse()
// set log config
setLogAttribute(*driver)
if err := createPersistentStorage(path.Join(*rootDir, *driver, "controller")); err != nil {
log.Errorf("failed to create persistent storage for controller: %v", err)
os.Exit(1)
}
if err := createPersistentStorage(path.Join(*rootDir, *driver, "node")); err != nil {
log.Errorf("failed to create persistent storage for node: %v", err)
os.Exit(1)
}
// Storage devops
go om.StorageOM()
drivername := *driver
log.Infof("CSI Driver Name: %s, %s, %s", drivername, *nodeID, *endpoint)
log.Infof("CSI Driver Branch: %s, Version: %s, Build time: %s\n", BRANCH, VERSION, BUILDTIME)
if drivername == TypePluginNAS {
driver := nas.NewDriver(*nodeID, *endpoint)
driver.Run()
} else if drivername == TypePluginOSS {
driver := oss.NewDriver(*nodeID, *endpoint)
driver.Run()
} else if drivername == TypePluginDISK {
driver := disk.NewDriver(*nodeID, *endpoint, *runAsController)
driver.Run()
} else if drivername == TypePluginLVM {
driver := lvm.NewDriver(*nodeID, *endpoint)
driver.Run()
} else if drivername == TypePluginCPFS {
driver := cpfs.NewDriver(*nodeID, *endpoint)
driver.Run()
} else if drivername == TypePluginMEM {
driver := mem.NewDriver(*nodeID, *endpoint)
driver.Run()
} else if drivername == TypePluginLOCAL {
driver := local.NewDriver(*nodeID, *endpoint)
driver.Run()
} else if drivername == ExtenderAgent {
queryServer := agent.NewAgent()
queryServer.RunAgent()
} else {
log.Errorf("CSI start failed, not support driver: %s", drivername)
}
os.Exit(0)
}
func createPersistentStorage(persistentStoragePath string) error {
log.Infof("Create Stroage Path: %s", persistentStoragePath)
return os.MkdirAll(persistentStoragePath, os.FileMode(0755))
}
// rotate log file by 2M bytes
// default print log to stdout and file both.
func setLogAttribute(driver string) {
logType := os.Getenv("LOG_TYPE")
logType = strings.ToLower(logType)
if logType != "stdout" && logType != "host" {
logType = "both"
}
if logType == "stdout" {
return
}
os.MkdirAll(LogfilePrefix, os.FileMode(0755))
logFile := LogfilePrefix + driver + ".log"
f, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
os.Exit(1)
}
// rotate the log file if too large
if fi, err := f.Stat(); err == nil && fi.Size() > 2*MBSIZE {
f.Close()
timeStr := time.Now().Format("-2006-01-02-15:04:05")
timedLogfile := LogfilePrefix + driver + timeStr + ".log"
os.Rename(logFile, timedLogfile)
f, err = os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
os.Exit(1)
}
}
if logType == "both" {
mw := io.MultiWriter(os.Stdout, f)
log.SetOutput(mw)
} else {
log.SetOutput(f)
}
}