diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index 5aaa9a82b..73da3a425 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -255,11 +255,18 @@ func (cfg *Config) FromEnv() error { // ReadConfig parses vSphere cloud config file and stores it into VSphereConfig. // Environment variables are also checked func ReadConfig(byConfig []byte) (*Config, error) { + var cfg *Config + if len(byConfig) == 0 { return nil, fmt.Errorf("Invalid YAML/INI file") } - cfg, err := ReadConfigYAML(byConfig) + // Check to see if yaml file before calling parser to prevent unnecessary error messages. + isYAML, err := isConfigYaml(byConfig) + if isYAML { + cfg, err = ReadConfigYAML(byConfig) + } + if err != nil { klog.Warningf("ReadConfigYAML failed: %s", err) diff --git a/pkg/common/config/config_yaml.go b/pkg/common/config/config_yaml.go index 56a67b3e1..f6e33b428 100644 --- a/pkg/common/config/config_yaml.go +++ b/pkg/common/config/config_yaml.go @@ -227,3 +227,14 @@ func ReadConfigYAML(byConfig []byte) (*Config, error) { return cfg.CreateConfig(), nil } + +func isConfigYaml(byConfig []byte) (bool, error) { + cfg := CommonConfigYAML{ + Vcenter: make(map[string]*VirtualCenterConfigYAML), + } + + if err := yaml.Unmarshal(byConfig, &cfg); err != nil { + return false, err + } + return true, nil +} diff --git a/pkg/common/config/config_yaml_test.go b/pkg/common/config/config_yaml_test.go index 5d3ce799e..afa138225 100644 --- a/pkg/common/config/config_yaml_test.go +++ b/pkg/common/config/config_yaml_test.go @@ -65,6 +65,17 @@ vcenter: secretNamespace: kube-system ` +const badConfigYAML = ` +global: + server: 0.0.0.0 + port: 443 + user: password: + insecureFlag: true + datacenters: + - us-west + caFile: /some/path/to/a/ca.pem +` + func TestReadConfigYAMLGlobal(t *testing.T) { _, err := ReadConfigYAML([]byte("")) if err == nil { @@ -137,3 +148,17 @@ func TestTenantRefsYAML(t *testing.T) { t.Errorf("vcConfig3 SecretRef should be kube-system/eu-secret but actual=%s", vcConfig3.SecretRef) } } + +func TestIsConfigYAML(t *testing.T) { + if ok, _ := isConfigYaml([]byte(basicConfigYAML)); !ok { + t.Error("YAML config should be valid") + } + + if ok, _ := isConfigYaml([]byte(basicConfigINI)); ok { + t.Error("INI config should be invalid") + } + + if ok, _ := isConfigYaml([]byte(badConfigYAML)); ok { + t.Error("Bad config YAML config should be invalid") + } +}