-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathmain.go
349 lines (301 loc) · 14.2 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
Copyright 2017 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 (
"context"
"flag"
"fmt"
"net/http"
"os"
"time"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/workqueue"
"k8s.io/component-base/featuregate"
"k8s.io/component-base/logs"
logsapi "k8s.io/component-base/logs/api/v1"
_ "k8s.io/component-base/logs/json/register"
"k8s.io/component-base/metrics/legacyregistry"
_ "k8s.io/component-base/metrics/prometheus/clientgo/leaderelection" // register leader election in the default legacy registry
_ "k8s.io/component-base/metrics/prometheus/workqueue" // register work queues in the default legacy registry
csitrans "k8s.io/csi-translation-lib"
"k8s.io/klog/v2"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/kubernetes-csi/csi-lib-utils/connection"
"github.com/kubernetes-csi/csi-lib-utils/leaderelection"
"github.com/kubernetes-csi/csi-lib-utils/metrics"
"github.com/kubernetes-csi/csi-lib-utils/rpc"
"github.com/kubernetes-csi/external-attacher/pkg/attacher"
"github.com/kubernetes-csi/external-attacher/pkg/controller"
"google.golang.org/grpc"
)
const (
// Default timeout of short CSI calls like GetPluginInfo
csiTimeout = time.Second
)
// Command line flags
var (
kubeconfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig file. Required only when running out of cluster.")
resync = flag.Duration("resync", 10*time.Minute, "Resync interval of the controller.")
csiAddress = flag.String("csi-address", "/run/csi/socket", "Address of the CSI driver socket.")
showVersion = flag.Bool("version", false, "Show version.")
timeout = flag.Duration("timeout", 15*time.Second, "Timeout for waiting for attaching or detaching the volume.")
workerThreads = flag.Uint("worker-threads", 10, "Number of attacher worker threads")
maxEntries = flag.Int("max-entries", 0, "Max entries per each page in volume lister call, 0 means no limit.")
retryIntervalStart = flag.Duration("retry-interval-start", time.Second, "Initial retry interval of failed create volume or deletion. It doubles with each failure, up to retry-interval-max.")
retryIntervalMax = flag.Duration("retry-interval-max", 5*time.Minute, "Maximum retry interval of failed create volume or deletion.")
enableLeaderElection = flag.Bool("leader-election", false, "Enable leader election.")
leaderElectionNamespace = flag.String("leader-election-namespace", "", "Namespace where the leader election resource lives. Defaults to the pod namespace if not set.")
leaderElectionLeaseDuration = flag.Duration("leader-election-lease-duration", 15*time.Second, "Duration, in seconds, that non-leader candidates will wait to force acquire leadership. Defaults to 15 seconds.")
leaderElectionRenewDeadline = flag.Duration("leader-election-renew-deadline", 10*time.Second, "Duration, in seconds, that the acting leader will retry refreshing leadership before giving up. Defaults to 10 seconds.")
leaderElectionRetryPeriod = flag.Duration("leader-election-retry-period", 5*time.Second, "Duration, in seconds, the LeaderElector clients should wait between tries of actions. Defaults to 5 seconds.")
defaultFSType = flag.String("default-fstype", "", "The default filesystem type of the volume to publish. Defaults to empty string")
reconcileSync = flag.Duration("reconcile-sync", 1*time.Minute, "Resync interval of the VolumeAttachment reconciler.")
metricsAddress = flag.String("metrics-address", "", "(deprecated) The TCP network address where the prometheus metrics endpoint will listen (example: `:8080`). The default is empty string, which means metrics endpoint is disabled. Only one of `--metrics-address` and `--http-endpoint` can be set.")
httpEndpoint = flag.String("http-endpoint", "", "The TCP network address where the HTTP server for diagnostics, including metrics and leader election health check, will listen (example: `:8080`). The default is empty string, which means the server is disabled. Only one of `--metrics-address` and `--http-endpoint` can be set.")
metricsPath = flag.String("metrics-path", "/metrics", "The HTTP path where prometheus metrics will be exposed. Default is `/metrics`.")
kubeAPIQPS = flag.Float64("kube-api-qps", 5, "QPS to use while communicating with the kubernetes apiserver. Defaults to 5.0.")
kubeAPIBurst = flag.Int("kube-api-burst", 10, "Burst to use while communicating with the kubernetes apiserver. Defaults to 10.")
maxGRPCLogLength = flag.Int("max-grpc-log-length", -1, "The maximum amount of characters logged for every grpc responses. Defaults to no limit")
)
var (
version = "unknown"
)
func main() {
fg := featuregate.NewFeatureGate()
logsapi.AddFeatureGates(fg)
c := logsapi.NewLoggingConfiguration()
logsapi.AddGoFlags(c, flag.CommandLine)
logs.InitLogs()
flag.Parse()
logger := klog.Background()
if err := logsapi.ValidateAndApply(c, fg); err != nil {
logger.Error(err, "LoggingConfiguration is invalid")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
if *showVersion {
fmt.Println(os.Args[0], version)
return
}
logger.Info("Version", "version", version)
if *metricsAddress != "" && *httpEndpoint != "" {
logger.Error(nil, "Only one of `--metrics-address` and `--http-endpoint` can be set")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
addr := *metricsAddress
if addr == "" {
addr = *httpEndpoint
}
// Create the client config. Use kubeconfig if given, otherwise assume in-cluster.
config, err := buildConfig(*kubeconfig)
if err != nil {
logger.Error(err, "Failed to build a Kubernetes config")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
config.QPS = (float32)(*kubeAPIQPS)
config.Burst = *kubeAPIBurst
config.ContentType = runtime.ContentTypeProtobuf
if *workerThreads == 0 {
logger.Error(nil, "Option -worker-threads must be greater than zero")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
logger.Error(err, "Failed to create a Clientset")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
factory := informers.NewSharedInformerFactory(clientset, *resync)
var handler controller.Handler
metricsManager := metrics.NewCSIMetricsManager("" /* driverName */)
// Connect to CSI.
connection.SetMaxGRPCLogLength(*maxGRPCLogLength)
ctx := context.Background()
csiConn, err := connection.Connect(ctx, *csiAddress, metricsManager, connection.OnConnectionLoss(connection.ExitOnConnectionLoss()))
if err != nil {
logger.Error(err, "Failed to connect to the CSI driver", "csiAddress", *csiAddress)
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
err = rpc.ProbeForever(ctx, csiConn, *timeout)
if err != nil {
logger.Error(err, "Failed to probe the CSI driver")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
// Find driver name.
cancelationCtx, cancel := context.WithTimeout(ctx, csiTimeout)
cancelationCtx = klog.NewContext(cancelationCtx, logger)
defer cancel()
csiAttacher, err := rpc.GetDriverName(cancelationCtx, csiConn)
if err != nil {
logger.Error(err, "Failed to get the CSI driver name")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
logger = klog.LoggerWithValues(logger, "driver", csiAttacher)
logger.V(2).Info("CSI driver name")
translator := csitrans.New()
if translator.IsMigratedCSIDriverByName(csiAttacher) {
metricsManager = metrics.NewCSIMetricsManagerWithOptions(csiAttacher, metrics.WithMigration())
migratedCsiClient, err := connection.Connect(ctx, *csiAddress, metricsManager, connection.OnConnectionLoss(connection.ExitOnConnectionLoss()))
if err != nil {
logger.Error(err, "Failed to connect to the CSI driver", "csiAddress", *csiAddress, "migrated", true)
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
csiConn.Close()
csiConn = migratedCsiClient
err = rpc.ProbeForever(ctx, csiConn, *timeout)
if err != nil {
logger.Error(err, "Failed to probe the CSI driver", "migrated", true)
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
}
// Add default legacy registry so that metrics manager serves Go runtime and process metrics.
// Also registers the `k8s.io/component-base/` work queue and leader election metrics we anonymously import.
metricsManager.WithAdditionalRegistry(legacyregistry.DefaultGatherer)
// Prepare http endpoint for metrics + leader election healthz
mux := http.NewServeMux()
if addr != "" {
metricsManager.RegisterToServer(mux, *metricsPath)
metricsManager.SetDriverName(csiAttacher)
go func() {
logger.Info("ServeMux listening", "address", addr, "metricsPath", *metricsPath)
err := http.ListenAndServe(addr, mux)
if err != nil {
logger.Error(err, "Failed to start HTTP server at specified address and metrics path", "address", addr, "metricsPath", *metricsPath)
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
}()
}
cancelationCtx, cancel = context.WithTimeout(ctx, csiTimeout)
cancelationCtx = klog.NewContext(cancelationCtx, logger)
defer cancel()
supportsService, err := supportsPluginControllerService(cancelationCtx, csiConn)
if err != nil {
logger.Error(err, "Failed to check if the CSI Driver supports the CONTROLLER_SERVICE")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
var (
supportsAttach bool
supportsReadOnly bool
supportsListVolumesPublishedNodes bool
supportsSingleNodeMultiWriter bool
)
if !supportsService {
handler = controller.NewTrivialHandler(clientset)
logger.V(2).Info("CSI driver does not support Plugin Controller Service, using trivial handler")
} else {
supportsAttach, supportsReadOnly, supportsListVolumesPublishedNodes, supportsSingleNodeMultiWriter, err = supportsControllerCapabilities(cancelationCtx, csiConn)
if err != nil {
logger.Error(err, "Failed to controller capability check")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
if supportsAttach {
pvLister := factory.Core().V1().PersistentVolumes().Lister()
vaLister := factory.Storage().V1().VolumeAttachments().Lister()
csiNodeLister := factory.Storage().V1().CSINodes().Lister()
volAttacher := attacher.NewAttacher(csiConn)
CSIVolumeLister := attacher.NewVolumeLister(csiConn, *maxEntries)
handler = controller.NewCSIHandler(
clientset,
csiAttacher,
volAttacher,
CSIVolumeLister,
pvLister,
csiNodeLister,
vaLister,
timeout,
supportsReadOnly,
supportsSingleNodeMultiWriter,
csitrans.New(),
*defaultFSType,
)
logger.V(2).Info("CSI driver supports ControllerPublishUnpublish, using real CSI handler")
} else {
handler = controller.NewTrivialHandler(clientset)
logger.V(2).Info("CSI driver does not support ControllerPublishUnpublish, using trivial handler")
}
}
if supportsListVolumesPublishedNodes {
logger.V(2).Info("CSI driver supports list volumes published nodes. Using capability to reconcile volume attachment objects with actual backend state")
}
ctrl := controller.NewCSIAttachController(
logger,
clientset,
csiAttacher,
handler,
factory.Storage().V1().VolumeAttachments(),
factory.Core().V1().PersistentVolumes(),
workqueue.NewItemExponentialFailureRateLimiter(*retryIntervalStart, *retryIntervalMax),
workqueue.NewItemExponentialFailureRateLimiter(*retryIntervalStart, *retryIntervalMax),
supportsListVolumesPublishedNodes,
*reconcileSync,
)
run := func(ctx context.Context) {
stopCh := ctx.Done()
factory.Start(stopCh)
ctrl.Run(ctx, int(*workerThreads))
}
if !*enableLeaderElection {
run(klog.NewContext(context.Background(), logger))
} else {
// Create a new clientset for leader election. When the attacher
// gets busy and its client gets throttled, the leader election
// can proceed without issues.
leClientset, err := kubernetes.NewForConfig(config)
if err != nil {
logger.Error(err, "Failed to create leaderelection client")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
// Name of config map with leader election lock
lockName := "external-attacher-leader-" + csiAttacher
le := leaderelection.NewLeaderElection(leClientset, lockName, run)
if *httpEndpoint != "" {
le.PrepareHealthCheck(mux, leaderelection.DefaultHealthCheckTimeout)
}
if *leaderElectionNamespace != "" {
le.WithNamespace(*leaderElectionNamespace)
}
le.WithLeaseDuration(*leaderElectionLeaseDuration)
le.WithRenewDeadline(*leaderElectionRenewDeadline)
le.WithRetryPeriod(*leaderElectionRetryPeriod)
if err := le.Run(); err != nil {
logger.Error(err, "Failed to initialize leader election")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
}
}
func buildConfig(kubeconfig string) (*rest.Config, error) {
if kubeconfig != "" {
return clientcmd.BuildConfigFromFlags("", kubeconfig)
}
return rest.InClusterConfig()
}
func supportsControllerCapabilities(ctx context.Context, csiConn *grpc.ClientConn) (bool, bool, bool, bool, error) {
caps, err := rpc.GetControllerCapabilities(ctx, csiConn)
if err != nil {
return false, false, false, false, err
}
supportsControllerPublish := caps[csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME]
supportsPublishReadOnly := caps[csi.ControllerServiceCapability_RPC_PUBLISH_READONLY]
supportsListVolumesPublishedNodes := caps[csi.ControllerServiceCapability_RPC_LIST_VOLUMES] && caps[csi.ControllerServiceCapability_RPC_LIST_VOLUMES_PUBLISHED_NODES]
supportsSingleNodeMultiWriter := caps[csi.ControllerServiceCapability_RPC_SINGLE_NODE_MULTI_WRITER]
return supportsControllerPublish, supportsPublishReadOnly, supportsListVolumesPublishedNodes, supportsSingleNodeMultiWriter, nil
}
func supportsPluginControllerService(ctx context.Context, csiConn *grpc.ClientConn) (bool, error) {
caps, err := rpc.GetPluginCapabilities(ctx, csiConn)
if err != nil {
return false, err
}
return caps[csi.PluginCapability_Service_CONTROLLER_SERVICE], nil
}