Skip to content

Commit

Permalink
Updates based on the Travis' feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
davidvonthenen committed Aug 28, 2019
1 parent 44b759e commit 61fcc6c
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 6 deletions.
6 changes: 4 additions & 2 deletions pkg/common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,11 @@ func (cfg *Config) FromEnv() error {
return nil
}

// IsSecretInfoProvided returns true if a global secret has been set
// IsSecretInfoProvided returns true if k8s secret is set or using generic CO secret method.
// If both k8s secret and generic CO both are true, we don't know which to use, so return false.
func (cfg *Config) IsSecretInfoProvided() bool {
return !((cfg.Global.SecretName == "" || cfg.Global.SecretNamespace == "") && cfg.Global.SecretsDirectory == "")
return (cfg.Global.SecretName != "" && cfg.Global.SecretNamespace != "" && cfg.Global.SecretsDirectory == "") ||
(cfg.Global.SecretName == "" && cfg.Global.SecretNamespace == "" && cfg.Global.SecretsDirectory != "")
}

func validateIPFamily(value string) ([]string, error) {
Expand Down
65 changes: 65 additions & 0 deletions pkg/common/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ datacenters = us-west
ca-file = /some/path/to/a/ca.pem
`

const twoVCsUsingSecretConfig = `
[Global]
port = 443
insecure-flag = true
[VirtualCenter "us-west@10.0.0.1"]
datacenters = "vic0dc"
service-account = "us-west-svcacct"
secret-name = "us-west-secret"
secret-namespace = "kube-system"
# port, insecure-flag will be used from Global section.
[VirtualCenter "us-east@10.0.0.2"]
datacenters = "vic1dc"
service-account = "us-east-svcacct"
secret-name = "us-east-secret"
secret-namespace = "kube-system"
# port, insecure-flag will be used from Global section.
`

func TestReadConfigGlobal(t *testing.T) {
_, err := ReadConfig(nil)
if err == nil {
Expand Down Expand Up @@ -134,3 +154,48 @@ func TestIPFamilies(t *testing.T) {
t.Errorf("Invalid ipv4,ipv7 but successful")
}
}

func TestConnectionLabels(t *testing.T) {
cfg, err := ReadConfig(strings.NewReader(twoVCsUsingSecretConfig))
if err != nil {
t.Fatalf("Should succeed when a valid config is provided: %s", err)
}

if cfg.IsSecretInfoProvided() {
t.Error("IsSecretInfoProvided should not be set.")
}

vcConfig1 := cfg.VirtualCenter["us-west@10.0.0.1"]
if vcConfig1 == nil {
t.Fatalf("Should return a valid vcConfig1")
}
if !vcConfig1.IsSecretInfoProvided() {
t.Error("vcConfig1.IsSecretInfoProvided() should be set.")
}
if !strings.EqualFold(vcConfig1.VCenterIP, "10.0.0.1") {
t.Errorf("vcConfig1 VCenterIP should be 10.0.0.1 but actual=%s", vcConfig1.VCenterIP)
}
if !strings.EqualFold(vcConfig1.ConnectionLabel, "us-west") {
t.Errorf("vcConfig1 ConnectionLabel should be us-west but actual=%s", vcConfig1.ConnectionLabel)
}
if !strings.EqualFold(vcConfig1.SecretHolder, "us-west-secret_kube-system") {
t.Errorf("vcConfig1 SecretHolder should be us-west-secret_kube-system but actual=%s", vcConfig1.SecretHolder)
}

vcConfig2 := cfg.VirtualCenter["us-east@10.0.0.2"]
if vcConfig2 == nil {
t.Fatalf("Should return a valid vcConfig2")
}
if !vcConfig2.IsSecretInfoProvided() {
t.Error("vcConfig2.IsSecretInfoProvided() should be set.")
}
if !strings.EqualFold(vcConfig2.VCenterIP, "10.0.0.2") {
t.Errorf("vcConfig2 VCenterIP should be 10.0.0.2 but actual=%s", vcConfig2.VCenterIP)
}
if !strings.EqualFold(vcConfig2.ConnectionLabel, "us-east") {
t.Errorf("vcConfig2 ConnectionLabel should be us-east but actual=%s", vcConfig2.ConnectionLabel)
}
if !strings.EqualFold(vcConfig2.SecretHolder, "us-east-secret_kube-system") {
t.Errorf("vcConfig2 SecretHolder should be us-east@10.0.0.2 but actual=%s", vcConfig2.SecretHolder)
}
}
2 changes: 1 addition & 1 deletion pkg/common/config/consts_and_errors.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2018 The Kubernetes Authors.
Copyright 2019 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.
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/config/virtualcenterconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ func (vcc *VirtualCenterConfig) ReturnVirtualCenterConfigKey() string {

// IsSecretInfoProvided returns true if the secret per VC has been configured
func (vcc *VirtualCenterConfig) IsSecretInfoProvided() bool {
return !(vcc.ServiceAccount == "" || vcc.SecretName == "" || vcc.SecretNamespace == "")
return vcc.ServiceAccount != "" && vcc.SecretName != "" && vcc.SecretNamespace != ""
}
3 changes: 3 additions & 0 deletions pkg/common/connectionmanager/connectionmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ var (
// 2. Update the credentials
// 3. Connects again to vCenter with fetched credentials
func (connMgr *ConnectionManager) Connect(ctx context.Context, vcInstance *VSphereInstance) error {
clientLock.Lock()
defer clientLock.Unlock()

err := vcInstance.Conn.Connect(ctx)
if err == nil {
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/connectionmanager/consts_and_errors.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2018 The Kubernetes Authors.
Copyright 2019 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.
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/credentialmanager/consts_and_errors.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2018 The Kubernetes Authors.
Copyright 2019 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.
Expand Down

0 comments on commit 61fcc6c

Please sign in to comment.