-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
208 lines (157 loc) · 6.67 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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
package main
import (
"bytes"
"encoding/base64"
"flag"
"log"
"net/url"
"os"
"strconv"
"github.com/danitso/clouddk-csi-driver/driver"
"github.com/danitso/terraform-provider-clouddk/clouddk"
)
const (
// envAPIEndpoint specifies the name of the environment variable containing the Cloud.dk API endpoint.
envAPIEndpoint = "CLOUDDK_API_ENDPOINT"
// envAPIKey specifies the name of the environment variable containing the Cloud.dk API key.
envAPIKey = "CLOUDDK_API_KEY"
// envCSIEndpointKey specifies the name of the environment variable containing the CSI endpoint.
envCSIEndpointKey = "CLOUDDK_CSI_ENDPOINT"
// envNodeID specifies the name of the environment variable containing the node identifier.
envNodeID = "CLOUDDK_NODE_ID"
// envServerMemory specifies the name of the environment variable containing the amount of memory per storage server.
envServerMemory = "CLOUDDK_SERVER_MEMORY"
// envServerProcessors specifies the name of the environment variable containing the number of processors per storage server.
envServerProcessors = "CLOUDDK_SERVER_PROCESSORS"
// envSSHPrivateKey specifies the name of the environment variable containing the Base64 encoded private key for SSH connections.
envSSHPrivateKey = "CLOUDDK_SSH_PRIVATE_KEY"
// envSSHPublicKey specifies the name of the environment variable containing the Base64 encoded public key for SSH connections.
envSSHPublicKey = "CLOUDDK_SSH_PUBLIC_KEY"
// flagAPIEndpoint specifies the name of the command line option containing the Cloud.dk API endpoint.
flagAPIEndpoint = "api-endpoint"
// flagAPIKey specifies the name of the command line option containing the Cloud.dk API key.
flagAPIKey = "api-key"
// flagCSIEndpoint specifies the name of the command line option containing the CSI endpoint.
flagCSIEndpoint = "csi-endpoint"
// flagNodeID specifies the name of the command line option containing the node identifier.
flagNodeID = "node-id"
// flagServerMemory specifies the name of the command line option containing the amount of memory per storage server.
flagServerMemory = "server-memory"
// flagServerProcessors specifies the name of the command line option containing the number of processors per storage server.
flagServerProcessors = "server-processors"
// flagSSHPrivateKey specifies the name of the command line option containing the Base64 encoded private key for SSH connections.
flagSSHPrivateKey = "ssh-private-key"
// flagSSHPublicKey specifies the name of the command line option containing the Base64 encoded public key for SSH connections.
flagSSHPublicKey = "ssh-public-key"
)
func main() {
// Parse the environment variables and command line flags.
var (
apiEndpointEnv = os.Getenv(envAPIEndpoint)
apiKeyEnv = os.Getenv(envAPIKey)
csiEndpointEnv = os.Getenv(envCSIEndpointKey)
nodeIDEnv = os.Getenv(envNodeID)
serverMemoryEnv = os.Getenv(envServerMemory)
serverProcessorsEnv = os.Getenv(envServerProcessors)
sshPrivateKeyEnv = os.Getenv(envSSHPrivateKey)
sshPublicKeyEnv = os.Getenv(envSSHPublicKey)
)
if apiEndpointEnv == "" {
apiEndpointEnv = "https://api.cloud.dk/v1"
}
if csiEndpointEnv == "" {
csiEndpointEnv = "unix:///var/lib/kubelet/plugins/" + driver.DriverName + "/csi.sock"
}
serverMemory := 4096
serverProcessors := 2
if serverMemoryEnv != "" {
i, err := strconv.Atoi(serverMemoryEnv)
if err != nil {
log.Fatalln(err)
}
serverMemory = i
}
if serverProcessorsEnv != "" {
i, err := strconv.Atoi(serverProcessorsEnv)
if err != nil {
log.Fatalln(err)
}
serverProcessors = i
}
var (
apiEndpointFlag = flag.String(flagAPIEndpoint, apiEndpointEnv, "The API endpoint")
apiKeyFlag = flag.String(flagAPIKey, apiKeyEnv, "The API key")
csiEndpointFlag = flag.String(flagCSIEndpoint, csiEndpointEnv, "The CSI endpoint")
nodeIDFlag = flag.String(flagNodeID, nodeIDEnv, "The node id")
serverMemoryFlag = flag.Int(flagServerMemory, serverMemory, "The minimum amount of memory per storage server")
serverProcessorsFlag = flag.Int(flagServerProcessors, serverProcessors, "The minimum number of processors per storage server")
sshPrivateKeyFlag = flag.String(flagSSHPrivateKey, sshPrivateKeyEnv, "The Base64 encoded private key for SSH connections")
sshPublicKeyFlag = flag.String(flagSSHPublicKey, sshPublicKeyEnv, "The Base64 encoded public key for SSH connections")
)
flag.Parse()
// Verify that all the required properties are defined and appear to be valid.
if *apiEndpointFlag == "" {
log.Fatalln("You must specify an API endpoint (-api-endpoint or CLOUDDK_API_ENDPOINT)")
}
_, err := url.ParseRequestURI(*apiEndpointFlag)
if err != nil {
log.Fatalln(err)
}
if *apiKeyFlag == "" {
log.Fatalln("You must specify an API key (-api-key or CLOUDDK_API_KEY)")
}
if *csiEndpointFlag == "" {
log.Fatalln("You must specify a CSI endpoint (-csi-endpoint or CLOUDDK_CSI_ENDPOINT)")
}
if *nodeIDFlag == "" {
log.Fatalln("You must specify a node id (-node-id or CLOUDDK_NODE_ID)")
}
if *serverMemoryFlag < 1 {
log.Fatalln("You must specify the minimum amount of memory per storage server (-server-memory or CLOUDDK_SERVER_MEMORY)")
}
if *serverProcessorsFlag < 1 {
log.Fatalln("You must specify the minimum number of processors per storage server (-server-processors or CLOUDDK_SERVER_PROCESSORS)")
}
if *sshPrivateKeyFlag == "" {
log.Fatalln("You must specify a private SSH key (-ssh-private-key or CLOUDDK_SSH_PRIVATE_KEY)")
}
if *sshPublicKeyFlag == "" {
log.Fatalln("You must specify a public SSH key (-ssh-public-key or CLOUDDK_SSH_PUBLIC_KEY)")
}
// Decode the private and public SSH keys.
if *sshPrivateKeyFlag != "" {
key, err := base64.StdEncoding.DecodeString(*sshPrivateKeyFlag)
if err != nil {
log.Fatalln(err)
}
*sshPrivateKeyFlag = bytes.NewBuffer(key).String()
}
if *sshPublicKeyFlag != "" {
key, err := base64.StdEncoding.DecodeString(*sshPublicKeyFlag)
if err != nil {
log.Fatalln(err)
}
*sshPublicKeyFlag = bytes.NewBuffer(key).String()
}
// Initialize the driver.
c := driver.Configuration{
ClientSettings: &clouddk.ClientSettings{
Endpoint: *apiEndpointFlag,
Key: *apiKeyFlag,
},
Endpoint: *csiEndpointFlag,
NodeID: *nodeIDFlag,
PrivateKey: *sshPrivateKeyFlag,
PublicKey: *sshPublicKeyFlag,
ServerMemory: *serverMemoryFlag,
ServerProcessors: *serverProcessorsFlag,
}
drv, err := driver.NewDriver(&c)
if err != nil {
log.Fatalln(err)
}
drv.Run()
}