-
Notifications
You must be signed in to change notification settings - Fork 57
/
main.go
164 lines (137 loc) · 5.07 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
/*
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"
"errors"
"flag"
"fmt"
"os"
"github.com/ianschenck/envflag"
"github.com/linode/linodego"
"go.uber.org/automaxprocs/maxprocs"
"k8s.io/klog/v2"
"github.com/linode/linode-blockstorage-csi-driver/internal/driver"
cryptsetupclient "github.com/linode/linode-blockstorage-csi-driver/pkg/cryptsetup-client"
devicemanager "github.com/linode/linode-blockstorage-csi-driver/pkg/device-manager"
filesystem "github.com/linode/linode-blockstorage-csi-driver/pkg/filesystem"
linodeclient "github.com/linode/linode-blockstorage-csi-driver/pkg/linode-client"
"github.com/linode/linode-blockstorage-csi-driver/pkg/logger"
mountmanager "github.com/linode/linode-blockstorage-csi-driver/pkg/mount-manager"
)
var vendorVersion string // set by the linker
type configuration struct {
// The UNIX socket to listen on for RPC requests.
csiEndpoint string
// Linode personal access token, used to make requests to the Linode
// API.
linodeToken string
// Linode API URL.
linodeURL string
// Optional label prefix to use when creating new Linode Block Storage
// Volumes.
volumeLabelPrefix string
// Name of the current node, when running as the node plugin.
//
// Deprecated: This is not needed as the CSI driver now uses the Linode
// Metadata Service to source information about the current
// node/instance. It will be removed in a future change.
nodeName string
// Flag to enable metrics
enableMetrics string
// Flag to specify the port on which the http server will run
metricsPort string
}
func loadConfig() configuration {
var cfg configuration
envflag.StringVar(&cfg.csiEndpoint, "CSI_ENDPOINT", "unix:/tmp/csi.sock", "Path to the CSI endpoint socket")
envflag.StringVar(&cfg.linodeToken, "LINODE_TOKEN", "", "Linode API token")
envflag.StringVar(&cfg.linodeURL, "LINODE_URL", linodego.APIHost, "Linode API URL")
envflag.StringVar(&cfg.volumeLabelPrefix, "LINODE_VOLUME_LABEL_PREFIX", "", "Linode Block Storage volume label prefix")
envflag.StringVar(&cfg.nodeName, "NODE_NAME", "", "Name of the current node") // deprecated
envflag.StringVar(&cfg.enableMetrics, "ENABLE_METRICS", "", "This flag conditionally runs the metrics servers")
envflag.StringVar(&cfg.metricsPort, "METRICS_PORT", "8081", "This flag specifies the port on which the metrics https server will run")
envflag.Parse()
return cfg
}
func main() {
// Create a base context with the logger
ctx := context.Background()
log := logger.NewLogger(ctx)
ctx = context.WithValue(ctx, logger.LoggerKey{}, log)
klog.InitFlags(nil)
if err := flag.Set("logtostderr", "true"); err != nil {
log.Error(err, "Fatal error")
os.Exit(0)
}
flag.Parse()
maxProcs(log)
if err := handle(ctx); err != nil {
log.Error(err, "Fatal error")
os.Exit(1)
}
os.Exit(0)
}
func maxProcs(log *logger.Logger) {
undoMaxprocs, maxprocsError := maxprocs.Set(maxprocs.Logger(func(msg string, keysAndValues ...interface{}) {
log.Klogr.WithValues("component", "maxprocs", "version", maxprocs.Version).V(2).Info(fmt.Sprintf(msg, keysAndValues...))
}))
defer undoMaxprocs()
if maxprocsError != nil {
log.Error(maxprocsError, "Failed to set GOMAXPROCS")
}
}
func handle(ctx context.Context) error {
log := logger.GetLogger(ctx)
if vendorVersion == "" {
return errors.New("vendorVersion must be set at compile time")
}
log.V(4).Info("Driver vendor version", "version", vendorVersion)
cfg := loadConfig()
if cfg.linodeToken == "" {
return errors.New("linode token required")
}
linodeDriver := driver.GetLinodeDriver(ctx)
// Initialize Linode Driver (Move setup to main?)
uaPrefix := fmt.Sprintf("LinodeCSI/%s", vendorVersion)
cloudProvider, err := linodeclient.NewLinodeClient(cfg.linodeToken, uaPrefix, cfg.linodeURL)
if err != nil {
return fmt.Errorf("failed to set up linode client: %w", err)
}
mounter := mountmanager.NewSafeMounter()
fileSystem := filesystem.NewFileSystem()
deviceUtils := devicemanager.NewDeviceUtils(fileSystem, mounter.Exec)
cryptSetup := cryptsetupclient.NewCryptSetup()
encrypt := driver.NewLuksEncryption(mounter.Exec, fileSystem, cryptSetup)
nodeMetadata, err := driver.GetNodeMetadata(ctx, cloudProvider, fileSystem)
if err != nil {
return fmt.Errorf("failed to get node metadata: %w", err)
}
if err := linodeDriver.SetupLinodeDriver(
ctx,
cloudProvider,
mounter,
deviceUtils,
nodeMetadata,
driver.Name,
vendorVersion,
cfg.volumeLabelPrefix,
encrypt,
cfg.enableMetrics,
cfg.metricsPort,
); err != nil {
return fmt.Errorf("setup driver: %w", err)
}
linodeDriver.Run(ctx, cfg.csiEndpoint)
return nil
}