Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locate InternalIP and ExternalIP by vSphere Network name #271

Merged
merged 1 commit into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions pkg/cloudprovider/vsphere/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,18 @@ func init() {
if err != nil {
return nil, err
}
return newVSphere(cfg, true)

cpiConfig, err := ReadCPIConfig(config)
if err != nil {
return nil, err
}
return newVSphere(cfg, cpiConfig, true)
})
}

// Creates new Controller node interface and returns
func newVSphere(cfg *vcfg.Config, finalize ...bool) (*VSphere, error) {
vs, err := buildVSphereFromConfig(cfg)
func newVSphere(cfg *vcfg.Config, cpiCfg *CPIConfig, finalize ...bool) (*VSphere, error) {
vs, err := buildVSphereFromConfig(cfg, cpiCfg)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -144,7 +149,7 @@ func (vs *VSphere) HasClusterID() bool {
}

// Initializes vSphere from vSphere CloudProvider Configuration
func buildVSphereFromConfig(cfg *vcfg.Config) (*VSphere, error) {
func buildVSphereFromConfig(cfg *vcfg.Config, cpiCfg *CPIConfig) (*VSphere, error) {
nm := &NodeManager{
nodeNameMap: make(map[string]*NodeInfo),
nodeUUIDMap: make(map[string]*NodeInfo),
Expand All @@ -154,6 +159,7 @@ func buildVSphereFromConfig(cfg *vcfg.Config) (*VSphere, error) {

vs := VSphere{
cfg: cfg,
cpiCfg: cpiCfg,
nodeManager: nm,
instances: newInstances(nm),
zones: newZones(nm, cfg.Labels.Zone, cfg.Labels.Region),
Expand Down
68 changes: 68 additions & 0 deletions pkg/cloudprovider/vsphere/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2019New 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.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package vsphere

import (
"fmt"
"io"
"os"

"gopkg.in/gcfg.v1"
)

// CPIConfig is used to read and store information (related only to the CPI) from the cloud configuration file
type CPIConfig struct {
dvonthenen marked this conversation as resolved.
Show resolved Hide resolved
Nodes struct {
// IP address on VirtualMachine's network interfaces included in the fields' CIDRs
// that will be used in respective status.addresses fields.
InternalNetworkSubnetCIDR string `gcfg:"internal-network-subnet-cidr"`
ExternalNetworkSubnetCIDR string `gcfg:"external-network-subnet-cidr"`
}
}

// FromEnv initializes the provided configuratoin object with values
// obtained from environment variables. If an environment variable is set
// for a property that's already initialized, the environment variable's value
// takes precedence.
func (cfg *CPIConfig) FromEnv() {
if v := os.Getenv("VSPHERE_NODES_INTERNAL_NETWORK_SUBNET_CIDR"); v != "" {
cfg.Nodes.InternalNetworkSubnetCIDR = v
}

if v := os.Getenv("VSPHERE_NODES_EXTERNAL_NETWORK_SUBNET_CIDR"); v != "" {
cfg.Nodes.ExternalNetworkSubnetCIDR = v
}
}

// ReadCPIConfig parses vSphere cloud config file and stores it into CPIConfig.
// Environment variables are also checked
func ReadCPIConfig(config io.Reader) (*CPIConfig, error) {
if config == nil {
return nil, fmt.Errorf("no vSphere cloud provider config file given")
}

cfg := &CPIConfig{}

if err := gcfg.FatalOnly(gcfg.ReadInto(cfg, config)); err != nil {
return nil, err
}

// Env Vars should override config file entries if present
cfg.FromEnv()

return cfg, nil
}
64 changes: 64 additions & 0 deletions pkg/cloudprovider/vsphere/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
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.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package vsphere
zuzzas marked this conversation as resolved.
Show resolved Hide resolved

import (
"os"
"strings"
"testing"
)

const basicConfig = `
[Nodes]
internal-network-subnet-cidr = 192.0.2.0/24
external-network-subnet-cidr = 198.51.100.0/24
`

func TestReadConfigGlobal(t *testing.T) {
dvonthenen marked this conversation as resolved.
Show resolved Hide resolved
_, err := ReadCPIConfig(nil)
if err == nil {
t.Errorf("Should fail when no config is provided: %s", err)
}

cfg, err := ReadCPIConfig(strings.NewReader(basicConfig))
if err != nil {
t.Fatalf("Should succeed when a valid config is provided: %s", err)
}

if cfg.Nodes.InternalNetworkSubnetCIDR != "192.0.2.0/24" {
t.Errorf("incorrect vcenter ip: %s", cfg.Nodes.InternalNetworkSubnetCIDR)
}

if cfg.Nodes.ExternalNetworkSubnetCIDR != "198.51.100.0/24" {
t.Errorf("incorrect datacenter: %s", cfg.Nodes.ExternalNetworkSubnetCIDR)
}
}

func TestEnvOverridesFile(t *testing.T) {
subnet := "203.0.113.0/24"
os.Setenv("VSPHERE_NODES_INTERNAL_NETWORK_SUBNET_CIDR", subnet)
defer os.Unsetenv("VSPHERE_NODES_INTERNAL_NETWORK_SUBNET_CIDR")

cfg, err := ReadCPIConfig(strings.NewReader(basicConfig))
if err != nil {
t.Fatalf("Should succeed when a valid config is provided: %s", err)
}

if cfg.Nodes.InternalNetworkSubnetCIDR != subnet {
t.Errorf("expected subnet: \"%s\", got: \"%s\"", subnet, cfg.Nodes.InternalNetworkSubnetCIDR)
}
}
91 changes: 81 additions & 10 deletions pkg/cloudprovider/vsphere/nodemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,29 @@ func (nm *NodeManager) DiscoverNode(nodeID string, searchBy cm.FindVM) error {
klog.Warningf("Unable to find vcInstance for %s. Defaulting to ipv4.", tenantRef)
}

var internalNetworkSubnet *net.IPNet
var externalNetworkSubnet *net.IPNet

if nm.cpiCfg != nil {
if nm.cpiCfg.Nodes.InternalNetworkSubnetCIDR != "" {
_, internalNetworkSubnet, err = net.ParseCIDR(nm.cpiCfg.Nodes.InternalNetworkSubnetCIDR)
if err != nil {
return err
}
}
if nm.cpiCfg.Nodes.ExternalNetworkSubnetCIDR != "" {
_, externalNetworkSubnet, err = net.ParseCIDR(nm.cpiCfg.Nodes.ExternalNetworkSubnetCIDR)
if err != nil {
return err
}
}
}

var addressMatchingEnabled bool
if internalNetworkSubnet != nil || externalNetworkSubnet != nil {
addressMatchingEnabled = true
}

found := false
addrs := []v1.NodeAddress{}
for _, v := range oVM.Guest.Net {
Expand All @@ -204,23 +227,71 @@ func (nm *NodeManager) DiscoverNode(nodeID string, searchBy cm.FindVM) error {
for _, family := range ipFamily {
ips := returnIPsFromSpecificFamily(family, v.IpAddress)

for _, ip := range ips {
klog.V(2).Infof("Adding IP: %s", ip)
if addressMatchingEnabled {
zuzzas marked this conversation as resolved.
Show resolved Hide resolved
klog.V(2).Infof("Adding Hostname: %s", oVM.Guest.HostName)
v1helper.AddToNodeAddresses(&addrs,
v1.NodeAddress{
Type: v1.NodeExternalIP,
Address: ip,
}, v1.NodeAddress{
Type: v1.NodeInternalIP,
Address: ip,
}, v1.NodeAddress{
Type: v1.NodeHostName,
Address: oVM.Guest.HostName,
},
)

found = true
break
var internalIP string
var externalIP string
for _, ip := range ips {
parsedIP := net.ParseIP(ip)
if parsedIP == nil {
return fmt.Errorf("can't parse IP: %s", ip)
}

if internalIP == "" && internalNetworkSubnet != nil && internalNetworkSubnet.Contains(parsedIP) {
internalIP = ip
}

if externalIP == "" && externalNetworkSubnet != nil && externalNetworkSubnet.Contains(parsedIP) {
externalIP = ip
}
}

if internalIP != "" {
klog.V(2).Infof("Adding Internal IP: %s", internalIP)
v1helper.AddToNodeAddresses(&addrs,
v1.NodeAddress{
Type: v1.NodeInternalIP,
Address: internalIP,
},
)
found = true
}
if externalIP != "" {
klog.V(2).Infof("Adding External IP: %s", externalIP)
v1helper.AddToNodeAddresses(&addrs,
v1.NodeAddress{
Type: v1.NodeExternalIP,
Address: externalIP,
},
)
found = true
}

} else {
for _, ip := range ips {
klog.V(2).Infof("Adding IP: %s", ip)
v1helper.AddToNodeAddresses(&addrs,
v1.NodeAddress{
Type: v1.NodeExternalIP,
Address: ip,
}, v1.NodeAddress{
Type: v1.NodeInternalIP,
Address: ip,
}, v1.NodeAddress{
Type: v1.NodeHostName,
Address: oVM.Guest.HostName,
},
)
found = true
break
}
}

if found {
Expand Down
4 changes: 4 additions & 0 deletions pkg/cloudprovider/vsphere/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type GRPCServer interface {
// VSphere is an implementation of cloud provider Interface for VSphere.
type VSphere struct {
cfg *vcfg.Config
cpiCfg *CPIConfig
connectionManager *cm.ConnectionManager
nodeManager *NodeManager
informMgr *k8s.InformerManager
Expand Down Expand Up @@ -84,6 +85,9 @@ type NodeManager struct {
// NodeLister to track Node properties
nodeLister clientv1.NodeLister

// Reference to CPI-specific configuration
cpiCfg *CPIConfig

// Mutexes
nodeInfoLock sync.RWMutex
nodeRegInfoLock sync.RWMutex
Expand Down
11 changes: 7 additions & 4 deletions pkg/cloudprovider/vsphere/vsphere_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,12 @@ func configFromEnvOrSim(multiDc bool) (*vcfg.Config, func()) {

func TestNewVSphere(t *testing.T) {
cfg := &vcfg.Config{}
cpiCfg := &CPIConfig{}
if err := cfg.FromEnv(); err != nil {
t.Skipf("No config found in environment")
}

_, err := newVSphere(cfg)
_, err := newVSphere(cfg, cpiCfg)
if err != nil {
t.Fatalf("Failed to construct/authenticate vSphere: %s", err)
}
Expand All @@ -155,9 +156,10 @@ func TestNewVSphere(t *testing.T) {
func TestVSphereLogin(t *testing.T) {
cfg, cleanup := configFromEnvOrSim(false)
defer cleanup()
cpiCfg := &CPIConfig{}

// Create vSphere configuration object
vs, err := newVSphere(cfg)
vs, err := newVSphere(cfg, cpiCfg)
if err != nil {
t.Fatalf("Failed to construct/authenticate vSphere: %s", err)
}
Expand All @@ -184,13 +186,14 @@ func TestVSphereLogin(t *testing.T) {
func TestVSphereLoginByToken(t *testing.T) {
cfg, cleanup := configFromSim(false)
defer cleanup()
cpiCfg := &CPIConfig{}

// Configure for SAML token auth
cfg.Global.User = localhostCert
cfg.Global.Password = localhostKey

// Create vSphere configuration object
vs, err := newVSphere(cfg)
vs, err := newVSphere(cfg, cpiCfg)
if err != nil {
t.Fatalf("Failed to construct/authenticate vSphere: %s", err)
}
Expand Down Expand Up @@ -490,7 +493,7 @@ func TestSecretVSphereConfig(t *testing.T) {
t.Fatalf("readConfig: unexpected error returned: %v", err)
}
}
vs, err = buildVSphereFromConfig(cfg)
vs, err = buildVSphereFromConfig(cfg, &CPIConfig{})
if err != nil { // testcase.expectedError {
t.Fatalf("buildVSphereFromConfig: Should succeed when a valid config is provided: %v", err)
}
Expand Down