Skip to content
This repository was archived by the owner on Mar 22, 2018. It is now read-only.

Commit 3e2a042

Browse files
committed
Standardize on a single data structure to load config file
1 parent 3e2c87c commit 3e2a042

2 files changed

Lines changed: 27 additions & 59 deletions

File tree

pkg/flexvolume/cinder_client.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ import (
2020
"fmt"
2121
"os"
2222

23+
openstack_provider "git.openstack.org/openstack/openstack-cloud-controller-manager/pkg/cloudprovider/providers/openstack"
2324
"github.com/golang/glog"
2425
"github.com/gophercloud/gophercloud"
2526
"github.com/gophercloud/gophercloud/openstack"
2627
"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/volumeactions"
2728
"github.com/gophercloud/gophercloud/openstack/blockstorage/v2/volumes"
28-
"github.com/scalingdata/gcfg"
29+
gcfg "gopkg.in/gcfg.v1"
2930
)
3031

3132
type cinderClient struct {
@@ -34,18 +35,7 @@ type cinderClient struct {
3435
}
3536

3637
type openStackConfig struct {
37-
Global struct {
38-
AuthUrl string `gcfg:"auth-url"`
39-
Username string `gcfg:"username"`
40-
UserId string `gcfg:"user-id"`
41-
Password string `gcfg:"password"`
42-
ApiKey string `gcfg:"api-key"`
43-
TenantId string `gcfg:"tenant-id"`
44-
TenantName string `gcfg:"tenant-name"`
45-
DomainId string `gcfg:"domain-id"`
46-
DomainName string `gcfg:"domain-name"`
47-
Region string `gcfg:"region"`
48-
}
38+
openstack_provider.Config
4939
RBD struct {
5040
Keyring string `gcfg:"keyring"`
5141
}
@@ -57,9 +47,10 @@ func (cfg openStackConfig) toAuthOptions() gophercloud.AuthOptions {
5747
Username: cfg.Global.Username,
5848
UserID: cfg.Global.UserId,
5949
Password: cfg.Global.Password,
60-
//APIKey: cfg.Global.ApiKey,
61-
TenantID: cfg.Global.TenantId,
62-
TenantName: cfg.Global.TenantName,
50+
TenantID: cfg.Global.TenantId,
51+
TenantName: cfg.Global.TenantName,
52+
DomainID: cfg.Global.DomainId,
53+
DomainName: cfg.Global.DomainName,
6354

6455
// Persistent service, so we need to be able to renew tokens.
6556
AllowReauth: true,

pkg/volume/cinder/volumeservice/connection.go

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -31,39 +31,29 @@ import (
3131
tokens3 "github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
3232
"gopkg.in/gcfg.v1"
3333

34+
openstack_provider "git.openstack.org/openstack/openstack-cloud-controller-manager/pkg/cloudprovider/providers/openstack"
35+
3436
"github.com/golang/glog"
3537
netutil "k8s.io/apimachinery/pkg/util/net"
3638
certutil "k8s.io/client-go/util/cert"
3739
)
3840

3941
type cinderConfig struct {
40-
Global cinderConfigGlobal
41-
}
42-
43-
type cinderConfigGlobal struct {
44-
CinderEndpoint string `gcfg:"cinder-endpoint"`
45-
AuthURL string `gcfg:"auth-url"`
46-
Username string
47-
UserID string `gcfg:"user-id"`
48-
Password string
49-
TenantID string `gcfg:"tenant-id"`
50-
TenantName string `gcfg:"tenant-name"`
51-
TrustID string `gcfg:"trust-id"`
52-
DomainID string `gcfg:"domain-id"`
53-
DomainName string `gcfg:"domain-name"`
54-
Region string
55-
CAFile string `gcfg:"ca-file"`
42+
openstack_provider.Config
43+
Cinder struct {
44+
Endpoint string `gcfg:"endpoint"`
45+
}
5646
}
5747

5848
func (cfg cinderConfig) toAuthOptions() gophercloud.AuthOptions {
5949
return gophercloud.AuthOptions{
60-
IdentityEndpoint: cfg.Global.AuthURL,
50+
IdentityEndpoint: cfg.Global.AuthUrl,
6151
Username: cfg.Global.Username,
62-
UserID: cfg.Global.UserID,
52+
UserID: cfg.Global.UserId,
6353
Password: cfg.Global.Password,
64-
TenantID: cfg.Global.TenantID,
54+
TenantID: cfg.Global.TenantId,
6555
TenantName: cfg.Global.TenantName,
66-
DomainID: cfg.Global.DomainID,
56+
DomainID: cfg.Global.DomainId,
6757
DomainName: cfg.Global.DomainName,
6858

6959
// Persistent service, so we need to be able to renew tokens.
@@ -73,11 +63,11 @@ func (cfg cinderConfig) toAuthOptions() gophercloud.AuthOptions {
7363

7464
func (cfg cinderConfig) toAuth3Options() tokens3.AuthOptions {
7565
return tokens3.AuthOptions{
76-
IdentityEndpoint: cfg.Global.AuthURL,
66+
IdentityEndpoint: cfg.Global.AuthUrl,
7767
Username: cfg.Global.Username,
78-
UserID: cfg.Global.UserID,
68+
UserID: cfg.Global.UserId,
7969
Password: cfg.Global.Password,
80-
DomainID: cfg.Global.DomainID,
70+
DomainID: cfg.Global.DomainId,
8171
DomainName: cfg.Global.DomainName,
8272
AllowReauth: true,
8373
}
@@ -90,22 +80,13 @@ func getConfigFromEnv() cinderConfig {
9080
return cinderConfig{}
9181
}
9282

93-
return cinderConfig{
94-
Global: cinderConfigGlobal{
95-
AuthURL: authURL,
96-
Username: os.Getenv("OS_USERNAME"),
97-
Password: os.Getenv("OS_PASSWORD"), // TODO: Replace with secret
98-
TenantID: os.Getenv("OS_TENANT_ID"),
99-
Region: os.Getenv("OS_REGION_NAME"),
100-
DomainName: os.Getenv("OS_USER_DOMAIN_NAME"),
101-
},
102-
}
83+
return cinderConfig{}
10384
}
10485

10586
func getConfig(configFilePath string) (cinderConfig, error) {
87+
config := getConfigFromEnv()
10688
if configFilePath != "" {
10789
var configFile *os.File
108-
var config cinderConfig
10990
configFile, err := os.Open(configFilePath)
11091
if err != nil {
11192
glog.Fatalf("Couldn't open configuration %s: %#v",
@@ -122,10 +103,6 @@ func getConfig(configFilePath string) (cinderConfig, error) {
122103
}
123104
return config, nil
124105
}
125-
envConfig := getConfigFromEnv()
126-
if envConfig != (cinderConfig{}) {
127-
return envConfig, nil
128-
}
129106

130107
// Pass explicit nil so plugins can actually check for nil. See
131108
// "Why is my nil error value not equal to nil?" in golang.org/doc/faq.
@@ -135,7 +112,7 @@ func getConfig(configFilePath string) (cinderConfig, error) {
135112
}
136113

137114
func getKeystoneVolumeService(cfg cinderConfig) (*gophercloud.ServiceClient, error) {
138-
provider, err := openstack.NewClient(cfg.Global.AuthURL)
115+
provider, err := openstack.NewClient(cfg.Global.AuthUrl)
139116
if err != nil {
140117
return nil, err
141118
}
@@ -150,10 +127,10 @@ func getKeystoneVolumeService(cfg cinderConfig) (*gophercloud.ServiceClient, err
150127
provider.HTTPClient.Transport = netutil.SetOldTransportDefaults(&http.Transport{TLSClientConfig: config})
151128

152129
}
153-
if cfg.Global.TrustID != "" {
130+
if cfg.Global.TrustId != "" {
154131
opts := cfg.toAuth3Options()
155132
authOptsExt := trusts.AuthOptsExt{
156-
TrustID: cfg.Global.TrustID,
133+
TrustID: cfg.Global.TrustId,
157134
AuthOptionsBuilder: &opts,
158135
}
159136
err = openstack.AuthenticateV3(provider, authOptsExt, gophercloud.EndpointOpts{})
@@ -185,7 +162,7 @@ func getNoAuthVolumeService(cfg cinderConfig) (*gophercloud.ServiceClient, error
185162
}
186163

187164
client, err := noauth.NewBlockStorageNoAuth(provider, noauth.EndpointOpts{
188-
CinderEndpoint: cfg.Global.CinderEndpoint,
165+
CinderEndpoint: cfg.Cinder.Endpoint,
189166
})
190167
if err != nil {
191168
return nil, fmt.Errorf("failed to get volume service: %v", err)
@@ -202,7 +179,7 @@ func GetVolumeService(configFilePath string) (*gophercloud.ServiceClient, error)
202179
return nil, err
203180
}
204181

205-
if config.Global.CinderEndpoint != "" {
182+
if config.Cinder.Endpoint != "" {
206183
return getNoAuthVolumeService(config)
207184
}
208185
return getKeystoneVolumeService(config)

0 commit comments

Comments
 (0)