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

Commit 4689d43

Browse files
committed
Ability to specify OS_* variables for OpenStack configuration
When we convert the OpenStack cloud provider to run in an external process, we should be able to use kubernetes Secrets capability to inject the OS_* variables. This way we can specify the cloud configuration as a configmap, specify secrets for the userid/password information. The configmap can be mounted as a file. the secrets can be made available as environment variables. the external controller itself can run as a pod/daemonset. For backward compat, we preload all the OS_* variables, if anything is in the config file, then that overrides the environment variables.
1 parent 07c5696 commit 4689d43

2 files changed

Lines changed: 61 additions & 52 deletions

File tree

pkg/cloudprovider/providers/openstack/openstack.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"io"
2424
"io/ioutil"
2525
"net/http"
26+
"os"
2627
"regexp"
2728
"strings"
2829
"time"
@@ -180,12 +181,50 @@ func (cfg Config) toAuth3Options() tokens3.AuthOptions {
180181
}
181182
}
182183

184+
// configFromEnv allows setting up credentials etc using the
185+
// standard OS_* OpenStack client environment variables.
186+
func configFromEnv() (cfg Config, ok bool) {
187+
cfg.Global.AuthUrl = os.Getenv("OS_AUTH_URL")
188+
cfg.Global.Username = os.Getenv("OS_USERNAME")
189+
cfg.Global.Password = os.Getenv("OS_PASSWORD")
190+
cfg.Global.Region = os.Getenv("OS_REGION_NAME")
191+
192+
cfg.Global.TenantId = os.Getenv("OS_TENANT_ID")
193+
if cfg.Global.TenantId == "" {
194+
cfg.Global.TenantId = os.Getenv("OS_PROJECT_ID")
195+
}
196+
cfg.Global.TenantName = os.Getenv("OS_TENANT_NAME")
197+
if cfg.Global.TenantName == "" {
198+
cfg.Global.TenantName = os.Getenv("OS_PROJECT_NAME")
199+
}
200+
201+
cfg.Global.DomainId = os.Getenv("OS_DOMAIN_ID")
202+
if cfg.Global.DomainId == "" {
203+
cfg.Global.DomainId = os.Getenv("OS_USER_DOMAIN_ID")
204+
}
205+
cfg.Global.DomainName = os.Getenv("OS_DOMAIN_NAME")
206+
if cfg.Global.DomainName == "" {
207+
cfg.Global.DomainName = os.Getenv("OS_USER_DOMAIN_NAME")
208+
}
209+
210+
ok = cfg.Global.AuthUrl != "" &&
211+
cfg.Global.Username != "" &&
212+
cfg.Global.Password != "" &&
213+
(cfg.Global.TenantId != "" || cfg.Global.TenantName != "" ||
214+
cfg.Global.DomainId != "" || cfg.Global.DomainName != "")
215+
216+
cfg.Metadata.SearchOrder = fmt.Sprintf("%s,%s", configDriveID, metadataID)
217+
cfg.BlockStorage.BSVersion = "auto"
218+
219+
return
220+
}
221+
183222
func readConfig(config io.Reader) (Config, error) {
184223
if config == nil {
185224
return Config{}, fmt.Errorf("no OpenStack cloud provider config file given")
186225
}
187226

188-
var cfg Config
227+
cfg, _ := configFromEnv()
189228

190229
// Set default values for config params
191230
cfg.BlockStorage.BSVersion = "auto"

pkg/cloudprovider/providers/openstack/openstack_test.go

Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,17 @@ func TestReadConfig(t *testing.T) {
9090
t.Errorf("Should fail when no config is provided: %s", err)
9191
}
9292

93+
os.Setenv("OS_PASSWORD", "mypass")
94+
defer os.Unsetenv("OS_PASSWORD")
95+
96+
os.Setenv("OS_TENANT_NAME", "admin")
97+
defer os.Unsetenv("OS_TENANT_NAME")
98+
9399
cfg, err := readConfig(strings.NewReader(`
94100
[Global]
95101
auth-url = http://auth.url
96-
username = user
102+
user-id = user
103+
tenant-name = demo
97104
[LoadBalancer]
98105
create-monitor = yes
99106
monitor-delay = 1m
@@ -113,6 +120,19 @@ func TestReadConfig(t *testing.T) {
113120
t.Errorf("incorrect authurl: %s", cfg.Global.AuthUrl)
114121
}
115122

123+
if cfg.Global.UserId != "user" {
124+
t.Errorf("incorrect userid: %s", cfg.Global.UserId)
125+
}
126+
127+
if cfg.Global.Password != "mypass" {
128+
t.Errorf("incorrect password: %s", cfg.Global.Password)
129+
}
130+
131+
// config file wins over environment variable
132+
if cfg.Global.TenantName != "demo" {
133+
t.Errorf("incorrect tenant name: %s", cfg.Global.TenantName)
134+
}
135+
116136
if !cfg.LoadBalancer.CreateMonitor {
117137
t.Errorf("incorrect lb.createmonitor: %t", cfg.LoadBalancer.CreateMonitor)
118138
}
@@ -377,56 +397,6 @@ func TestNodeAddresses(t *testing.T) {
377397
}
378398
}
379399

380-
// This allows acceptance testing against an existing OpenStack
381-
// install, using the standard OS_* OpenStack client environment
382-
// variables.
383-
// FIXME: it would be better to hermetically test against canned JSON
384-
// requests/responses.
385-
func configFromEnv() (cfg Config, ok bool) {
386-
cfg.Global.AuthUrl = os.Getenv("OS_AUTH_URL")
387-
388-
cfg.Global.TenantId = os.Getenv("OS_TENANT_ID")
389-
// Rax/nova _insists_ that we don't specify both tenant ID and name
390-
if cfg.Global.TenantId == "" {
391-
cfg.Global.TenantName = os.Getenv("OS_TENANT_NAME")
392-
}
393-
394-
cfg.Global.Username = os.Getenv("OS_USERNAME")
395-
cfg.Global.Password = os.Getenv("OS_PASSWORD")
396-
cfg.Global.Region = os.Getenv("OS_REGION_NAME")
397-
398-
cfg.Global.TenantName = os.Getenv("OS_TENANT_NAME")
399-
if cfg.Global.TenantName == "" {
400-
cfg.Global.TenantName = os.Getenv("OS_PROJECT_NAME")
401-
}
402-
403-
cfg.Global.TenantId = os.Getenv("OS_TENANT_ID")
404-
if cfg.Global.TenantId == "" {
405-
cfg.Global.TenantId = os.Getenv("OS_PROJECT_ID")
406-
}
407-
408-
cfg.Global.DomainId = os.Getenv("OS_DOMAIN_ID")
409-
if cfg.Global.DomainId == "" {
410-
cfg.Global.DomainId = os.Getenv("OS_USER_DOMAIN_ID")
411-
}
412-
413-
cfg.Global.DomainName = os.Getenv("OS_DOMAIN_NAME")
414-
if cfg.Global.DomainName == "" {
415-
cfg.Global.DomainName = os.Getenv("OS_USER_DOMAIN_NAME")
416-
}
417-
418-
ok = (cfg.Global.AuthUrl != "" &&
419-
cfg.Global.Username != "" &&
420-
cfg.Global.Password != "" &&
421-
(cfg.Global.TenantId != "" || cfg.Global.TenantName != "" ||
422-
cfg.Global.DomainId != "" || cfg.Global.DomainName != ""))
423-
424-
cfg.Metadata.SearchOrder = fmt.Sprintf("%s,%s", configDriveID, metadataID)
425-
cfg.BlockStorage.BSVersion = "auto"
426-
427-
return
428-
}
429-
430400
func TestNewOpenStack(t *testing.T) {
431401
cfg, ok := configFromEnv()
432402
if !ok {

0 commit comments

Comments
 (0)