-
Notifications
You must be signed in to change notification settings - Fork 241
/
main.go
268 lines (235 loc) · 8.89 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
/*
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"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"path"
"strconv"
"strings"
"sync"
"time"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/agent"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/cloud/metadata"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/disk"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/ens"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/features"
csilog "github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/log"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/metric"
"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/options"
_ "github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/options"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/oss"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/pov"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/utils"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/version"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
)
const (
// TypePluginSuffix is the suffix of all storage plugins.
TypePluginSuffix = "plugin.csi.alibabacloud.com"
// TypePluginVar is the yaml variable that needs to be replaced.
TypePluginVar = "driverplugin.csi.alibabacloud.com-replace"
// PluginServicePort default port is 11260.
PluginServicePort = "11260"
// ProvisionerServicePort default port is 11270.
ProvisionerServicePort = "11270"
// 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"
// TypePluginENS ENS type plugins
TypePluginENS = "ensplugin.csi.alibabacloud.com"
// TypePluginPOV POV type plugins
TypePluginPOV = "povplugin.csi.alibabacloud.com"
// ExtenderAgent agent component
ExtenderAgent = "agent"
)
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 (deprecated)")
runControllerService = flag.Bool("run-controller-service", true, "activate CSI controller service")
runNodeService = flag.Bool("run-node-service", true, "activate CSI node service")
driver = flag.String("driver", TypePluginDISK, "CSI Driver")
// Deprecated: rootDir is instead by KUBELET_ROOT_DIR env.
rootDir = flag.String("rootdir", "/var/lib/kubelet/csi-plugins", "Kubernetes root directory")
)
func main() {
csilog.RedirectLogrusToLogr(logrus.StandardLogger(), klog.Background())
flag.Var(features.FunctionalMutableFeatureGate, "feature-gates", "A set of key=value pairs that describe feature gates for alpha/experimental features. "+
"Options are:\n"+strings.Join(features.FunctionalMutableFeatureGate.KnownFeatures(), "\n"))
klog.InitFlags(nil)
flag.Parse()
serviceType := utils.GetServiceType(*runAsController, *runControllerService, *runNodeService)
// enable pprof analyse
pprofPort := os.Getenv("PPROF_PORT")
if pprofPort != "" {
if _, err := strconv.Atoi(pprofPort); err == nil {
klog.Infof("enable pprof & start port at %v", pprofPort)
go func() {
err := http.ListenAndServe(fmt.Sprintf("127.0.0.1:%v", pprofPort), nil)
klog.Errorf("start server err: %v", err)
}()
}
}
// setLogAttribute(logAttribute)
// log.AddHook(rotateHook(logAttribute))
klog.Infof("Multi CSI Driver Name: %s, nodeID: %s, endPoints: %s", *driver, *nodeID, *endpoint)
klog.Infof("CSI Driver, Version: %s, Build time: %s", version.VERSION, version.BUILDTIME)
multiDriverNames := *driver
driverNames := strings.Split(multiDriverNames, ",")
var wg sync.WaitGroup
// Storage devops
go om.StorageOM()
// initialize node metadata
meta := metadata.NewMetadata()
meta.EnableEcs(http.DefaultTransport)
cfg, err := clientcmd.BuildConfigFromFlags(options.MasterURL, options.Kubeconfig)
if err != nil {
klog.Warningf("newGlobalConfig: build kubeconfig failed: %v", err)
} else {
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
klog.Warningf("Error building kubernetes clientset: %v", err)
} else {
meta.EnableKubernetes(kubeClient)
}
}
ac := utils.GetAccessControl()
ecsClient := utils.NewEcsClient(ac)
stsClient := utils.NewStsClient(ac)
meta.EnableOpenAPI(ecsClient, stsClient)
for i, driverName := range driverNames {
if !strings.Contains(driverName, TypePluginSuffix) && driverName != ExtenderAgent {
driverNames[i] = joinCsiPluginSuffix(driverName)
}
}
for _, driverName := range driverNames {
wg.Add(1)
endPointName := replaceCsiEndpoint(driverName, *endpoint)
klog.Infof("CSI endpoint for driver %s: %s", driverName, endPointName)
if err := createPersistentStorage(path.Join(utils.KubeletRootDir, "/csi-plugins", driverName, "controller")); err != nil {
klog.Errorf("failed to create persistent storage for controller: %v", err)
os.Exit(1)
}
if err := createPersistentStorage(path.Join(utils.KubeletRootDir, "/csi-plugins", driverName, "node")); err != nil {
klog.Errorf("failed to create persistent storage for node: %v", err)
os.Exit(1)
}
switch driverName {
case TypePluginNAS:
go func(endPoint string) {
defer wg.Done()
driver := nas.NewDriver(meta, endPoint, serviceType)
driver.Run()
}(endPointName)
case TypePluginOSS:
go func(endPoint string) {
defer wg.Done()
driver := oss.NewDriver(endPoint, meta, serviceType)
driver.Run()
}(endPointName)
case TypePluginDISK:
go func(endPoint string) {
defer wg.Done()
driver := disk.NewDriver(meta, endPoint, serviceType)
driver.Run()
}(endPointName)
case TypePluginCPFS:
klog.Fatalf("%s is no longer supported, please switch to %s if you are using CPFS 2.0 protocol server", TypePluginCPFS, TypePluginNAS)
case TypePluginENS:
go func(endpoint string) {
defer wg.Done()
driver := ens.NewDriver(*nodeID, endpoint, serviceType)
driver.Run()
}(endPointName)
// TODO: remove entire rund-csi protocol 1.0
case ExtenderAgent:
go func() {
defer wg.Done()
queryServer := agent.NewAgent()
queryServer.RunAgent()
}()
case TypePluginPOV:
go func(endPoint string) {
defer wg.Done()
driver := pov.NewDriver(meta, endPoint, serviceType)
driver.Run()
}(endPointName)
default:
klog.Fatalf("CSI start failed, not support driver: %s", driverName)
}
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, unix.SIGTERM)
go func() {
s := <-c
klog.Infof("Got signal: %v, exiting...", s)
os.Exit(0)
}()
servicePort := os.Getenv("SERVICE_PORT")
if servicePort == "" {
servicePort = PluginServicePort
if serviceType&utils.Controller != 0 {
servicePort = ProvisionerServicePort
}
}
version.SetPrometheusVersion()
enableMetric := true
if os.Getenv("ENABLE_METRIC") == "false" {
enableMetric = false
}
klog.Info("CSI is running status.")
csiMux := http.NewServeMux()
csiMux.HandleFunc("/healthz", healthHandler)
klog.Infof("Metric listening on address: /healthz")
if enableMetric && serviceType&utils.Node != 0 {
metricHandler := metric.NewMetricHandler(driverNames, serviceType)
csiMux.Handle("/metrics", metricHandler)
klog.Infof("Metric listening on address: /metrics")
}
if err := http.ListenAndServe(fmt.Sprintf(":%s", servicePort), csiMux); err != nil {
klog.Fatalf("Service port listen and serve err:%s", err.Error())
}
wg.Wait()
}
func createPersistentStorage(persistentStoragePath string) error {
klog.Infof("Create Storage Path: %s", persistentStoragePath)
return os.MkdirAll(persistentStoragePath, os.FileMode(0755))
}
func joinCsiPluginSuffix(storageType string) string {
return storageType + TypePluginSuffix
}
func replaceCsiEndpoint(pluginType string, endPointName string) string {
return strings.Replace(endPointName, TypePluginVar, pluginType, -1)
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
time := time.Now()
message := "Liveness probe is OK, time:" + time.String()
w.Write([]byte(message))
}