Skip to content

Commit 1deb25d

Browse files
committed
Added vsphere yaml cloud config support.
Updates
1 parent d37cbfe commit 1deb25d

File tree

6 files changed

+60
-71
lines changed

6 files changed

+60
-71
lines changed

pkg/controller/vsphere/actuator_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,10 @@ func TestMachineEvents(t *testing.T) {
115115
g.Expect(k8sClient.Delete(context.Background(), testNamespace)).To(Succeed())
116116
}()
117117

118+
credentialsSecretName := "test"
118119
credentialsSecret := corev1.Secret{
119120
ObjectMeta: metav1.ObjectMeta{
120-
Name: "test",
121+
Name: credentialsSecretName,
121122
Namespace: testNamespaceName,
122123
},
123124
Data: map[string][]byte{
@@ -131,7 +132,7 @@ func TestMachineEvents(t *testing.T) {
131132
g.Expect(k8sClient.Delete(context.Background(), &credentialsSecret)).To(Succeed())
132133
}()
133134

134-
testConfig := fmt.Sprintf(testConfigFmt, port)
135+
testConfig := fmt.Sprintf(testConfigFmt, port, credentialsSecretName, testNamespaceName)
135136
configMap := &corev1.ConfigMap{
136137
ObjectMeta: metav1.ObjectMeta{
137138
Name: "testname",

pkg/controller/vsphere/machine_scope.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
apicorev1 "k8s.io/api/core/v1"
1212
apimachineryerrors "k8s.io/apimachinery/pkg/api/errors"
1313
apimachineryutilerrors "k8s.io/apimachinery/pkg/util/errors"
14+
vsphere "k8s.io/cloud-provider-vsphere/pkg/common/config"
1415
"k8s.io/klog/v2"
1516
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
1617
)
@@ -39,7 +40,7 @@ type machineScope struct {
3940
// client reader that bypasses the manager's cache
4041
apiReader runtimeclient.Reader
4142
// vSphere cloud-provider config
42-
vSphereConfig *vSphereConfig
43+
vSphereConfig *vsphere.Config
4344
// machine resource
4445
machine *machinev1.Machine
4546
providerSpec *machinev1.VSphereMachineProviderSpec
@@ -78,10 +79,10 @@ func newMachineScope(params machineScopeParams) (*machineScope, error) {
7879
return nil, fmt.Errorf("%v: no workspace provided", params.machine.GetName())
7980
}
8081

81-
server := fmt.Sprintf("%s:%s", providerSpec.Workspace.Server, getPortFromConfig(vSphereConfig))
82+
server := fmt.Sprintf("%s:%s", providerSpec.Workspace.Server, getVCenterPortFromConfig(vSphereConfig, providerSpec.Workspace.Server))
8283
authSession, err := session.GetOrCreate(params.Context,
8384
server, providerSpec.Workspace.Datacenter,
84-
user, password, getInsecureFlagFromConfig(vSphereConfig))
85+
user, password, getVCenterInsecureFlagFromConfig(vSphereConfig, providerSpec.Workspace.Server))
8586
if err != nil {
8687
return nil, fmt.Errorf("failed to create vSphere session: %w", err)
8788
}

pkg/controller/vsphere/machine_scope_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,10 @@ func TestPatchMachine(t *testing.T) {
335335
g.Expect(k8sClient.Delete(ctx, testNamespace)).To(Succeed())
336336
}()
337337

338+
credentialsSecretName := "test"
338339
credentialsSecret := &corev1.Secret{
339340
ObjectMeta: metav1.ObjectMeta{
340-
Name: "test",
341+
Name: credentialsSecretName,
341342
Namespace: testNamespaceName,
342343
},
343344
Data: map[string][]byte{
@@ -351,7 +352,7 @@ func TestPatchMachine(t *testing.T) {
351352
g.Expect(k8sClient.Delete(ctx, credentialsSecret)).To(Succeed())
352353
}()
353354

354-
testConfig := fmt.Sprintf(testConfigFmt, port)
355+
testConfig := fmt.Sprintf(testConfigFmt, port, credentialsSecretName, testNamespaceName)
355356
configMap := &corev1.ConfigMap{
356357
ObjectMeta: metav1.ObjectMeta{
357358
Name: "testname",

pkg/controller/vsphere/reconciler_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4040
apimachinerytypes "k8s.io/apimachinery/pkg/types"
4141
"k8s.io/client-go/kubernetes/scheme"
42+
vsphere "k8s.io/cloud-provider-vsphere/pkg/common/config"
4243
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
4344
"sigs.k8s.io/controller-runtime/pkg/client/fake"
4445

@@ -1763,9 +1764,10 @@ func TestDelete(t *testing.T) {
17631764

17641765
password, _ := server.URL.User.Password()
17651766

1767+
credentialsSecretName := "test"
17661768
secret := &corev1.Secret{
17671769
ObjectMeta: metav1.ObjectMeta{
1768-
Name: "test",
1770+
Name: credentialsSecretName,
17691771
Namespace: ns,
17701772
},
17711773
Data: map[string][]byte{
@@ -1774,7 +1776,7 @@ func TestDelete(t *testing.T) {
17741776
},
17751777
}
17761778

1777-
testConfig := fmt.Sprintf(testConfigFmt, port)
1779+
testConfig := fmt.Sprintf(testConfigFmt, port, credentialsSecretName, ns)
17781780
configMap := &corev1.ConfigMap{
17791781
ObjectMeta: metav1.ObjectMeta{
17801782
Name: "testName",
@@ -2272,9 +2274,10 @@ func TestCreate(t *testing.T) {
22722274
vm.Name = vmName
22732275
vm.Config.Version = minimumHWVersionString
22742276

2277+
credentialsSecretName := "test"
22752278
credentialsSecret := &corev1.Secret{
22762279
ObjectMeta: metav1.ObjectMeta{
2277-
Name: "test",
2280+
Name: credentialsSecretName,
22782281
Namespace: namespace,
22792282
},
22802283
Data: map[string][]byte{
@@ -2283,7 +2286,7 @@ func TestCreate(t *testing.T) {
22832286
},
22842287
}
22852288

2286-
testConfig := fmt.Sprintf(testConfigFmt, port)
2289+
testConfig := fmt.Sprintf(testConfigFmt, port, credentialsSecretName, namespace)
22872290
configMap := &corev1.ConfigMap{
22882291
ObjectMeta: metav1.ObjectMeta{
22892292
Name: "testName",
@@ -2687,9 +2690,10 @@ func TestUpdate(t *testing.T) {
26872690
instanceUUID := "a5764857-ae35-34dc-8f25-a9c9e73aa898"
26882691
vm.Config.InstanceUuid = instanceUUID
26892692

2693+
credentialsSecretName := "test"
26902694
credentialsSecret := &corev1.Secret{
26912695
ObjectMeta: metav1.ObjectMeta{
2692-
Name: "test",
2696+
Name: credentialsSecretName,
26932697
Namespace: namespace,
26942698
},
26952699
Data: map[string][]byte{
@@ -2698,7 +2702,7 @@ func TestUpdate(t *testing.T) {
26982702
},
26992703
}
27002704

2701-
testConfig := fmt.Sprintf(testConfigFmt, port)
2705+
testConfig := fmt.Sprintf(testConfigFmt, port, credentialsSecretName, namespace)
27022706
configMap := &corev1.ConfigMap{
27032707
ObjectMeta: metav1.ObjectMeta{
27042708
Name: "testName",
@@ -3019,8 +3023,8 @@ func TestReconcileMachineWithCloudState(t *testing.T) {
30193023
providerStatus: &machinev1.VSphereMachineProviderStatus{
30203024
TaskRef: task.Reference().Value,
30213025
},
3022-
vSphereConfig: &vSphereConfig{
3023-
Labels: Labels{
3026+
vSphereConfig: &vsphere.Config{
3027+
Labels: vsphere.Labels{
30243028
Zone: zoneKey,
30253029
Region: regionKey,
30263030
},

pkg/controller/vsphere/util.go

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ import (
88
"net/http"
99
"strings"
1010

11-
"gopkg.in/gcfg.v1"
12-
1311
configv1 "github.com/openshift/api/config/v1"
1412
machinev1 "github.com/openshift/api/machine/v1beta1"
1513
corev1 "k8s.io/api/core/v1"
1614
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1715
"k8s.io/apimachinery/pkg/fields"
1816
"k8s.io/apimachinery/pkg/runtime"
17+
vsphere "k8s.io/cloud-provider-vsphere/pkg/common/config"
1918
"k8s.io/klog/v2"
2019
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
2120
)
@@ -25,35 +24,6 @@ const (
2524
OpenshiftConfigNamespace = "openshift-config"
2625
)
2726

28-
// vSphereConfig is a copy of the Kubernetes vSphere cloud provider config type
29-
// that contains the fields we need. Unfortunately, we can't easily import
30-
// either the legacy or newer cloud provider code here, so we're just
31-
// duplicating part of the type and parsing it ourselves using the same gcfg
32-
// library for now.
33-
type vSphereConfig struct {
34-
// Global is the vSphere cloud provider's global configuration.
35-
Labels Labels `gcfg:"Labels"`
36-
// Global is the vSphere cloud provider's global configuration.
37-
Global Global `gcfg:"Global"`
38-
}
39-
40-
// Labels is the vSphere cloud provider's zone and region configuration.
41-
type Labels struct {
42-
// Zone is the zone in which VMs are created/located.
43-
Zone string `gcfg:"zone"`
44-
// Region is the region in which VMs are created/located.
45-
Region string `gcfg:"region"`
46-
}
47-
48-
// Global is the vSphere cloud provider's global configuration.
49-
type Global struct {
50-
// Port is the port on which the vSphere endpoint is listening.
51-
// Defaults to 443.
52-
// Has string type because we need empty string value for formatting
53-
Port string `gcfg:"port"`
54-
InsecureFlag string `gcfg:"insecure-flag"`
55-
}
56-
5727
func getInfrastructure(c runtimeclient.Reader) (*configv1.Infrastructure, error) {
5828
if c == nil {
5929
return nil, errors.New("no API reader -- will not fetch infrastructure config")
@@ -69,7 +39,7 @@ func getInfrastructure(c runtimeclient.Reader) (*configv1.Infrastructure, error)
6939
return infra, nil
7040
}
7141

72-
func getVSphereConfig(c runtimeclient.Reader, configNamespace string) (*vSphereConfig, error) {
42+
func getVSphereConfig(c runtimeclient.Reader, configNamespace string) (*vsphere.Config, error) {
7343
if c == nil {
7444
return nil, errors.New("no API reader -- will not fetch vSphere config")
7545
}
@@ -104,13 +74,7 @@ func getVSphereConfig(c runtimeclient.Reader, configNamespace string) (*vSphereC
10474
)
10575
}
10676

107-
var vcfg vSphereConfig
108-
109-
if err := gcfg.FatalOnly(gcfg.ReadStringInto(&vcfg, cloudConfig)); err != nil {
110-
return nil, err
111-
}
112-
113-
return &vcfg, nil
77+
return vsphere.ReadConfig([]byte(cloudConfig))
11478
}
11579

11680
func setConditions(condition metav1.Condition, conditions []metav1.Condition) []metav1.Condition {
@@ -169,18 +133,33 @@ func conditionFailed() metav1.Condition {
169133
}
170134
}
171135

172-
func getPortFromConfig(config *vSphereConfig) string {
136+
func getVCenterPortFromConfig(config *vsphere.Config, vcenter string) string {
173137
if config != nil {
174-
return config.Global.Port
138+
for _, vc := range config.VirtualCenter {
139+
if vc.VCenterIP == vcenter {
140+
if len(vc.VCenterPort) > 0 {
141+
return vc.VCenterPort
142+
} else {
143+
return config.Global.VCenterPort
144+
}
145+
}
146+
}
175147
}
176148
return ""
177149
}
178150

179-
// getInsecureFlagFromConfig get insecure flag from config and default to false
180-
func getInsecureFlagFromConfig(config *vSphereConfig) bool {
181-
if config != nil && config.Global.InsecureFlag == "1" {
182-
return true
151+
func getVCenterInsecureFlagFromConfig(config *vsphere.Config, vcenter string) bool {
152+
if config != nil {
153+
for _, vc := range config.VirtualCenter {
154+
if vc.VCenterIP == vcenter {
155+
return vc.InsecureFlag
156+
}
157+
}
158+
159+
// Either vCenter is not found or config is missing.
160+
return config.Global.InsecureFlag
183161
}
162+
184163
return false
185164
}
186165

pkg/controller/vsphere/util_test.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,23 @@ const (
1515
testRegion = "testRegion"
1616
testZone = "testZone"
1717
testPort = "443"
18-
testInsecureFlag = "1"
18+
testInsecureFlag = true
1919
openshiftConfigNamespaceForTest = "openshift-config-test"
2020
testConfigFmt = `
21-
[Labels]
22-
zone = "testZone"
23-
region = "testRegion"
24-
[Global]
25-
port = %s
26-
insecure-flag="1"
21+
[Labels]
22+
zone = "testZone"
23+
region = "testRegion"
24+
[VirtualCenter "127.0.0.1"]
25+
port = %s
26+
[Global]
27+
insecure-flag="1"
28+
secret-name = "%s"
29+
secret-namespace = "%s"
2730
`
2831
)
2932

3033
func TestGetVSphereConfig(t *testing.T) {
31-
testConfig := fmt.Sprintf(testConfigFmt, "443")
34+
testConfig := fmt.Sprintf(testConfigFmt, "443", "test", "test-namespace")
3235
configMap := &corev1.ConfigMap{
3336
ObjectMeta: metav1.ObjectMeta{
3437
Name: "testName",
@@ -66,11 +69,11 @@ func TestGetVSphereConfig(t *testing.T) {
6669
t.Errorf("Expected zone %s, got %s", testZone, vSphereConfig.Labels.Zone)
6770
}
6871

69-
if vSphereConfig.Global.Port != testPort {
70-
t.Errorf("Expected zone %s, got %s", testZone, vSphereConfig.Global.Port)
72+
if vSphereConfig.Global.VCenterPort != testPort {
73+
t.Errorf("Expected zone %s, got %s", testZone, vSphereConfig.Global.VCenterPort)
7174
}
7275

7376
if vSphereConfig.Global.InsecureFlag != testInsecureFlag {
74-
t.Errorf("Expected insecure flag %s, got %s", testInsecureFlag, vSphereConfig.Global.InsecureFlag)
77+
t.Errorf("Expected insecure flag %t, got %t", testInsecureFlag, vSphereConfig.Global.InsecureFlag)
7578
}
7679
}

0 commit comments

Comments
 (0)