Skip to content

Commit 820fae3

Browse files
authored
Merge pull request #284 from dvonthenen/feature/ipaddybbynetworkname
Allow Internal/External status.Address Filtering by VM Network Name
2 parents c1b02f4 + f86cc6d commit 820fae3

9 files changed

+163
-108
lines changed

pkg/cloudprovider/vsphere/cloud.go

+6-18
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
cloudprovider "k8s.io/cloud-provider"
2727

2828
"k8s.io/cloud-provider-vsphere/pkg/cloudprovider/vsphere/server"
29-
vcfg "k8s.io/cloud-provider-vsphere/pkg/common/config"
3029
cm "k8s.io/cloud-provider-vsphere/pkg/common/connectionmanager"
3130
k8s "k8s.io/cloud-provider-vsphere/pkg/common/kubernetes"
3231
)
@@ -41,22 +40,17 @@ const (
4140

4241
func init() {
4342
cloudprovider.RegisterCloudProvider(ProviderName, func(config io.Reader) (cloudprovider.Interface, error) {
44-
cfg, err := vcfg.ReadConfig(config)
45-
if err != nil {
46-
return nil, err
47-
}
48-
4943
cpiConfig, err := ReadCPIConfig(config)
5044
if err != nil {
5145
return nil, err
5246
}
53-
return newVSphere(cfg, cpiConfig, true)
47+
return newVSphere(cpiConfig, true)
5448
})
5549
}
5650

5751
// Creates new Controller node interface and returns
58-
func newVSphere(cfg *vcfg.Config, cpiCfg *CPIConfig, finalize ...bool) (*VSphere, error) {
59-
vs, err := buildVSphereFromConfig(cfg, cpiCfg)
52+
func newVSphere(cfg *CPIConfig, finalize ...bool) (*VSphere, error) {
53+
vs, err := buildVSphereFromConfig(cfg)
6054
if err != nil {
6155
return nil, err
6256
}
@@ -75,7 +69,7 @@ func (vs *VSphere) Initialize(clientBuilder cloudprovider.ControllerClientBuilde
7569

7670
vs.informMgr = k8s.NewInformer(client, true)
7771

78-
connMgr := cm.NewConnectionManager(vs.cfg, vs.informMgr, client)
72+
connMgr := cm.NewConnectionManager(&vs.cfg.Config, vs.informMgr, client)
7973
vs.connectionManager = connMgr
8074
vs.nodeManager.connectionManager = connMgr
8175

@@ -149,17 +143,11 @@ func (vs *VSphere) HasClusterID() bool {
149143
}
150144

151145
// Initializes vSphere from vSphere CloudProvider Configuration
152-
func buildVSphereFromConfig(cfg *vcfg.Config, cpiCfg *CPIConfig) (*VSphere, error) {
153-
nm := &NodeManager{
154-
nodeNameMap: make(map[string]*NodeInfo),
155-
nodeUUIDMap: make(map[string]*NodeInfo),
156-
nodeRegUUIDMap: make(map[string]*v1.Node),
157-
vcList: make(map[string]*VCenterInfo),
158-
}
146+
func buildVSphereFromConfig(cfg *CPIConfig) (*VSphere, error) {
147+
nm := newNodeManager(cfg, nil)
159148

160149
vs := VSphere{
161150
cfg: cfg,
162-
cpiCfg: cpiCfg,
163151
nodeManager: nm,
164152
instances: newInstances(nm),
165153
zones: newZones(nm, cfg.Labels.Zone, cfg.Labels.Region),

pkg/cloudprovider/vsphere/config.go

+18-14
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,30 @@ import (
2424
"gopkg.in/gcfg.v1"
2525
)
2626

27-
// CPIConfig is used to read and store information (related only to the CPI) from the cloud configuration file
28-
type CPIConfig struct {
29-
Nodes struct {
30-
// IP address on VirtualMachine's network interfaces included in the fields' CIDRs
31-
// that will be used in respective status.addresses fields.
32-
InternalNetworkSubnetCIDR string `gcfg:"internal-network-subnet-cidr"`
33-
ExternalNetworkSubnetCIDR string `gcfg:"external-network-subnet-cidr"`
34-
}
35-
}
36-
37-
// FromEnv initializes the provided configuratoin object with values
27+
// FromCPIEnv initializes the provided configuratoin object with values
3828
// obtained from environment variables. If an environment variable is set
3929
// for a property that's already initialized, the environment variable's value
4030
// takes precedence.
41-
func (cfg *CPIConfig) FromEnv() {
31+
func (cfg *CPIConfig) FromCPIEnv() error {
32+
if err := cfg.FromEnv(); err != nil {
33+
return err
34+
}
35+
4236
if v := os.Getenv("VSPHERE_NODES_INTERNAL_NETWORK_SUBNET_CIDR"); v != "" {
4337
cfg.Nodes.InternalNetworkSubnetCIDR = v
4438
}
45-
4639
if v := os.Getenv("VSPHERE_NODES_EXTERNAL_NETWORK_SUBNET_CIDR"); v != "" {
4740
cfg.Nodes.ExternalNetworkSubnetCIDR = v
4841
}
42+
43+
if v := os.Getenv("VSPHERE_NODES_INTERNAL_VM_NETWORK_NAME"); v != "" {
44+
cfg.Nodes.InternalVMNetworkName = v
45+
}
46+
if v := os.Getenv("VSPHERE_NODES_EXTERNAL_VM_NETWORK_NAME"); v != "" {
47+
cfg.Nodes.ExternalVMNetworkName = v
48+
}
49+
50+
return nil
4951
}
5052

5153
// ReadCPIConfig parses vSphere cloud config file and stores it into CPIConfig.
@@ -62,7 +64,9 @@ func ReadCPIConfig(config io.Reader) (*CPIConfig, error) {
6264
}
6365

6466
// Env Vars should override config file entries if present
65-
cfg.FromEnv()
67+
if err := cfg.FromCPIEnv(); err != nil {
68+
return nil, err
69+
}
6670

6771
return cfg, nil
6872
}
+43-18
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
/*
22
Copyright 2019 The Kubernetes Authors.
3-
43
Licensed under the Apache License, Version 2.0 (the "License");
54
you may not use this file except in compliance with the License.
65
You may obtain a copy of the License at
7-
86
http://www.apache.org/licenses/LICENSE-2.0
9-
107
Unless required by applicable law or agreed to in writing, software
118
distributed under the License is distributed on an "AS IS" BASIS,
129
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -17,48 +14,76 @@ limitations under the License.
1714
package vsphere
1815

1916
import (
20-
"os"
2117
"strings"
2218
"testing"
2319
)
2420

25-
const basicConfig = `
21+
const subnetCidrConfig = `
22+
[Global]
23+
server = 0.0.0.0
24+
port = 443
25+
user = user
26+
password = password
27+
insecure-flag = true
28+
datacenters = us-west
29+
ca-file = /some/path/to/a/ca.pem
30+
31+
[Nodes]
32+
internal-network-subnet-cidr = "192.0.2.0/24"
33+
external-network-subnet-cidr = "198.51.100.0/24"
34+
`
35+
36+
const networkNameConfig = `
37+
[Global]
38+
server = 0.0.0.0
39+
port = 443
40+
user = user
41+
password = password
42+
insecure-flag = true
43+
datacenters = us-west
44+
ca-file = /some/path/to/a/ca.pem
45+
2646
[Nodes]
27-
internal-network-subnet-cidr = 192.0.2.0/24
28-
external-network-subnet-cidr = 198.51.100.0/24
47+
internal-vm-network-name = "Internal K8s Traffic"
48+
external-vm-network-name = "External/Outbound Traffic"
2949
`
3050

31-
func TestReadConfigGlobal(t *testing.T) {
51+
func TestReadConfigSubnetCidr(t *testing.T) {
3252
_, err := ReadCPIConfig(nil)
3353
if err == nil {
3454
t.Errorf("Should fail when no config is provided: %s", err)
3555
}
3656

37-
cfg, err := ReadCPIConfig(strings.NewReader(basicConfig))
57+
cfg, err := ReadCPIConfig(strings.NewReader(subnetCidrConfig))
3858
if err != nil {
3959
t.Fatalf("Should succeed when a valid config is provided: %s", err)
4060
}
4161

4262
if cfg.Nodes.InternalNetworkSubnetCIDR != "192.0.2.0/24" {
43-
t.Errorf("incorrect vcenter ip: %s", cfg.Nodes.InternalNetworkSubnetCIDR)
63+
t.Errorf("incorrect internal network subnet cidr: %s", cfg.Nodes.InternalNetworkSubnetCIDR)
4464
}
4565

4666
if cfg.Nodes.ExternalNetworkSubnetCIDR != "198.51.100.0/24" {
47-
t.Errorf("incorrect datacenter: %s", cfg.Nodes.ExternalNetworkSubnetCIDR)
67+
t.Errorf("incorrect external network subnet cidr: %s", cfg.Nodes.ExternalNetworkSubnetCIDR)
4868
}
4969
}
5070

51-
func TestEnvOverridesFile(t *testing.T) {
52-
subnet := "203.0.113.0/24"
53-
os.Setenv("VSPHERE_NODES_INTERNAL_NETWORK_SUBNET_CIDR", subnet)
54-
defer os.Unsetenv("VSPHERE_NODES_INTERNAL_NETWORK_SUBNET_CIDR")
71+
func TestReadConfigNetworkName(t *testing.T) {
72+
_, err := ReadCPIConfig(nil)
73+
if err == nil {
74+
t.Errorf("Should fail when no config is provided: %s", err)
75+
}
5576

56-
cfg, err := ReadCPIConfig(strings.NewReader(basicConfig))
77+
cfg, err := ReadCPIConfig(strings.NewReader(networkNameConfig))
5778
if err != nil {
5879
t.Fatalf("Should succeed when a valid config is provided: %s", err)
5980
}
6081

61-
if cfg.Nodes.InternalNetworkSubnetCIDR != subnet {
62-
t.Errorf("expected subnet: \"%s\", got: \"%s\"", subnet, cfg.Nodes.InternalNetworkSubnetCIDR)
82+
if cfg.Nodes.InternalVMNetworkName != "Internal K8s Traffic" {
83+
t.Errorf("incorrect internal vm network name: %s", cfg.Nodes.InternalVMNetworkName)
84+
}
85+
86+
if cfg.Nodes.ExternalVMNetworkName != "External/Outbound Traffic" {
87+
t.Errorf("incorrect internal vm network name: %s", cfg.Nodes.ExternalVMNetworkName)
6388
}
6489
}

pkg/cloudprovider/vsphere/instances_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
v1 "k8s.io/api/core/v1"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828
"k8s.io/apimachinery/pkg/types"
29-
clientv1 "k8s.io/client-go/listers/core/v1"
3029
v1helper "k8s.io/cloud-provider/node/helpers"
3130

3231
cm "k8s.io/cloud-provider-vsphere/pkg/common/connectionmanager"
@@ -36,8 +35,8 @@ type MyNodeManager struct {
3635
NodeManager
3736
}
3837

39-
func newMyNodeManager(cm *cm.ConnectionManager, lister clientv1.NodeLister) *MyNodeManager {
40-
return &MyNodeManager{*newNodeManager(cm, lister)}
38+
func newMyNodeManager(cm *cm.ConnectionManager) *MyNodeManager {
39+
return &MyNodeManager{*newNodeManager(nil, cm)}
4140
}
4241

4342
// Used to populate the networking info
@@ -76,7 +75,7 @@ func TestInstance(t *testing.T) {
7675
* Setup
7776
*/
7877
connMgr := cm.NewConnectionManager(cfg, nil, nil)
79-
nm := newMyNodeManager(connMgr, nil)
78+
nm := newMyNodeManager(connMgr)
8079
instances := newInstances(&nm.NodeManager)
8180

8281
vm := simulator.Map.Any("VirtualMachine").(*simulator.VirtualMachine)
@@ -155,7 +154,7 @@ func TestInvalidInstance(t *testing.T) {
155154
* Setup
156155
*/
157156
connMgr := cm.NewConnectionManager(cfg, nil, nil)
158-
nm := newMyNodeManager(connMgr, nil)
157+
nm := newMyNodeManager(connMgr)
159158
instances := newInstances(&nm.NodeManager)
160159

161160
name := "" //junk name

0 commit comments

Comments
 (0)