-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
211 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
Copyright 2018 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" | ||
"strings" | ||
|
||
"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 { | ||
CPI struct { | ||
// address options for each network type (internal and/or external) are mutually inclusive | ||
// e.g., if you specify "internal-network-name" and "internal-network-subnet-cidr" | ||
// only the addresses that match BOTH will match | ||
|
||
// VirtualMachine network interfaces backed by the following vSphere Networks | ||
// will be used in respective status.addresses fields. | ||
InternalNetworkName []string `gcfg:"internal-network-name"` | ||
ExternalNetworkName []string `gcfg:"external-network-name"` | ||
|
||
// IP addresses on VirtualMachine's network interfaces included in the fields' CIDRs | ||
// 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_INTERNAL_NETWORK_NAME"); v != "" { | ||
cfg.CPI.InternalNetworkName = strings.Split(v, ",") | ||
} | ||
|
||
if v := os.Getenv("VSPHERE_EXTERNAL_NETWORK_NAME"); v != "" { | ||
cfg.CPI.ExternalNetworkName = strings.Split(v, ",") | ||
} | ||
|
||
if v := os.Getenv("VSPHERE_INTERNAL_NETWORK_SUBNET_CIDR"); v != "" { | ||
cfg.CPI.InternalNetworkSubnetCIDR = strings.Split(v, ",") | ||
} | ||
|
||
if v := os.Getenv("VSPHERE_EXTERNAL_NETWORK_SUBNET_CIDR"); v != "" { | ||
cfg.CPI.ExternalNetworkSubnetCIDR = strings.Split(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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters