-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
209 lines (182 loc) · 5.86 KB
/
connection.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
/*
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 volumeservice
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net/http"
"os"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/gophercloud/openstack/blockstorage/noauth"
"github.com/gophercloud/gophercloud/openstack/identity/v3/extensions/trusts"
tokens3 "github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
"gopkg.in/gcfg.v1"
"github.com/golang/glog"
netutil "k8s.io/apimachinery/pkg/util/net"
certutil "k8s.io/client-go/util/cert"
)
type cinderConfig struct {
Global cinderConfigGlobal
}
type cinderConfigGlobal struct {
CinderEndpoint string `gcfg:"cinder-endpoint"`
AuthURL string `gcfg:"auth-url"`
Username string
UserID string `gcfg:"user-id"`
Password string
TenantID string `gcfg:"tenant-id"`
TenantName string `gcfg:"tenant-name"`
TrustID string `gcfg:"trust-id"`
DomainID string `gcfg:"domain-id"`
DomainName string `gcfg:"domain-name"`
Region string
CAFile string `gcfg:"ca-file"`
}
func (cfg cinderConfig) toAuthOptions() gophercloud.AuthOptions {
return gophercloud.AuthOptions{
IdentityEndpoint: cfg.Global.AuthURL,
Username: cfg.Global.Username,
UserID: cfg.Global.UserID,
Password: cfg.Global.Password,
TenantID: cfg.Global.TenantID,
TenantName: cfg.Global.TenantName,
DomainID: cfg.Global.DomainID,
DomainName: cfg.Global.DomainName,
// Persistent service, so we need to be able to renew tokens.
AllowReauth: true,
}
}
func (cfg cinderConfig) toAuth3Options() tokens3.AuthOptions {
return tokens3.AuthOptions{
IdentityEndpoint: cfg.Global.AuthURL,
Username: cfg.Global.Username,
UserID: cfg.Global.UserID,
Password: cfg.Global.Password,
DomainID: cfg.Global.DomainID,
DomainName: cfg.Global.DomainName,
AllowReauth: true,
}
}
func getConfigFromEnv() cinderConfig {
authURL := os.Getenv("OS_AUTH_URL")
if authURL == "" {
glog.Warning("OS_AUTH_URL missing from environment")
return cinderConfig{}
}
return cinderConfig{
Global: cinderConfigGlobal{
AuthURL: authURL,
Username: os.Getenv("OS_USERNAME"),
Password: os.Getenv("OS_PASSWORD"), // TODO: Replace with secret
TenantID: os.Getenv("OS_TENANT_ID"),
Region: os.Getenv("OS_REGION_NAME"),
DomainName: os.Getenv("OS_USER_DOMAIN_NAME"),
},
}
}
func getConfig(configFilePath string) (cinderConfig, error) {
if configFilePath != "" {
var configFile *os.File
var config cinderConfig
configFile, err := os.Open(configFilePath)
if err != nil {
glog.Fatalf("Couldn't open configuration %s: %#v",
configFilePath, err)
return cinderConfig{}, err
}
defer configFile.Close()
err = gcfg.ReadInto(&config, configFile)
if err != nil {
glog.Fatalf("Couldn't read configuration: %#v", err)
return cinderConfig{}, err
}
return config, nil
}
envConfig := getConfigFromEnv()
if envConfig != (cinderConfig{}) {
return envConfig, nil
}
// Pass explicit nil so plugins can actually check for nil. See
// "Why is my nil error value not equal to nil?" in golang.org/doc/faq.
glog.Fatal("Configuration missing: no config file specified and " +
"environment variables are not set.")
return cinderConfig{}, errors.New("Missing configuration")
}
func getKeystoneVolumeService(cfg cinderConfig) (*gophercloud.ServiceClient, error) {
provider, err := openstack.NewClient(cfg.Global.AuthURL)
if err != nil {
return nil, err
}
if cfg.Global.CAFile != "" {
var roots *x509.CertPool
roots, err = certutil.NewPool(cfg.Global.CAFile)
if err != nil {
return nil, err
}
config := &tls.Config{}
config.RootCAs = roots
provider.HTTPClient.Transport = netutil.SetOldTransportDefaults(&http.Transport{TLSClientConfig: config})
}
if cfg.Global.TrustID != "" {
opts := cfg.toAuth3Options()
authOptsExt := trusts.AuthOptsExt{
TrustID: cfg.Global.TrustID,
AuthOptionsBuilder: &opts,
}
err = openstack.AuthenticateV3(provider, authOptsExt, gophercloud.EndpointOpts{})
} else {
err = openstack.Authenticate(provider, cfg.toAuthOptions())
}
if err != nil {
return nil, err
}
volumeService, err := openstack.NewBlockStorageV2(provider,
gophercloud.EndpointOpts{
Region: cfg.Global.Region,
})
if err != nil {
return nil, fmt.Errorf("failed to get volume service: %v", err)
}
return volumeService, nil
}
func getNoAuthVolumeService(cfg cinderConfig) (*gophercloud.ServiceClient, error) {
provider, err := noauth.NewClient(gophercloud.AuthOptions{
Username: cfg.Global.Username,
TenantName: cfg.Global.TenantName,
})
if err != nil {
return nil, err
}
client, err := noauth.NewBlockStorageV2(provider, noauth.EndpointOpts{
CinderEndpoint: cfg.Global.CinderEndpoint,
})
if err != nil {
return nil, fmt.Errorf("failed to get volume service: %v", err)
}
return client, nil
}
// GetVolumeService returns a connected cinder client based on configuration
// specified in configFilePath or the environment.
func GetVolumeService(configFilePath string) (*gophercloud.ServiceClient, error) {
config, err := getConfig(configFilePath)
if err != nil {
return nil, err
}
if config.Global.CinderEndpoint != "" {
return getNoAuthVolumeService(config)
}
return getKeystoneVolumeService(config)
}