config.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package vsphere
  14. import (
  15. "errors"
  16. "fmt"
  17. "io"
  18. "os"
  19. "gopkg.in/gcfg.v1"
  20. "k8s.io/kubernetes/test/e2e/framework"
  21. )
  22. const (
  23. vSphereConfFileEnvVar = "VSPHERE_CONF_FILE"
  24. )
  25. var (
  26. confFileLocation = os.Getenv(vSphereConfFileEnvVar)
  27. )
  28. // Config represents vSphere configuration
  29. type Config struct {
  30. Username string
  31. Password string
  32. Hostname string
  33. Port string
  34. Datacenters string
  35. RoundTripperCount uint
  36. DefaultDatastore string
  37. Folder string
  38. }
  39. // ConfigFile represents the content of vsphere.conf file.
  40. // Users specify the configuration of one or more vSphere instances in vsphere.conf where
  41. // the Kubernetes master and worker nodes are running.
  42. type ConfigFile struct {
  43. Global struct {
  44. // vCenter username.
  45. User string `gcfg:"user"`
  46. // vCenter password in clear text.
  47. Password string `gcfg:"password"`
  48. // vCenter port.
  49. VCenterPort string `gcfg:"port"`
  50. // True if vCenter uses self-signed cert.
  51. InsecureFlag bool `gcfg:"insecure-flag"`
  52. // Datacenter in which VMs are located.
  53. Datacenters string `gcfg:"datacenters"`
  54. // Soap round tripper count (retries = RoundTripper - 1)
  55. RoundTripperCount uint `gcfg:"soap-roundtrip-count"`
  56. }
  57. VirtualCenter map[string]*Config
  58. Network struct {
  59. // PublicNetwork is name of the network the VMs are joined to.
  60. PublicNetwork string `gcfg:"public-network"`
  61. }
  62. Disk struct {
  63. // SCSIControllerType defines SCSI controller to be used.
  64. SCSIControllerType string `dcfg:"scsicontrollertype"`
  65. }
  66. // Endpoint used to create volumes
  67. Workspace struct {
  68. VCenterIP string `gcfg:"server"`
  69. Datacenter string `gcfg:"datacenter"`
  70. Folder string `gcfg:"folder"`
  71. DefaultDatastore string `gcfg:"default-datastore"`
  72. ResourcePoolPath string `gcfg:"resourcepool-path"`
  73. }
  74. }
  75. // GetVSphereInstances parses vsphere.conf and returns VSphere instances
  76. func GetVSphereInstances() (map[string]*VSphere, error) {
  77. cfg, err := getConfig()
  78. if err != nil {
  79. return nil, err
  80. }
  81. return populateInstanceMap(cfg)
  82. }
  83. func getConfig() (*ConfigFile, error) {
  84. if confFileLocation == "" {
  85. if framework.TestContext.CloudConfig.ConfigFile == "" {
  86. return nil, fmt.Errorf("env variable 'VSPHERE_CONF_FILE' is not set, and no config-file specified")
  87. }
  88. confFileLocation = framework.TestContext.CloudConfig.ConfigFile
  89. }
  90. confFile, err := os.Open(confFileLocation)
  91. if err != nil {
  92. return nil, err
  93. }
  94. defer confFile.Close()
  95. cfg, err := readConfig(confFile)
  96. if err != nil {
  97. return nil, err
  98. }
  99. return &cfg, nil
  100. }
  101. // readConfig parses vSphere cloud config file into ConfigFile.
  102. func readConfig(config io.Reader) (ConfigFile, error) {
  103. if config == nil {
  104. err := fmt.Errorf("no vSphere cloud provider config file given")
  105. return ConfigFile{}, err
  106. }
  107. var cfg ConfigFile
  108. err := gcfg.ReadInto(&cfg, config)
  109. return cfg, err
  110. }
  111. func populateInstanceMap(cfg *ConfigFile) (map[string]*VSphere, error) {
  112. vsphereInstances := make(map[string]*VSphere)
  113. if cfg.Workspace.VCenterIP == "" || cfg.Workspace.DefaultDatastore == "" || cfg.Workspace.Folder == "" || cfg.Workspace.Datacenter == "" {
  114. msg := fmt.Sprintf("All fields in workspace are mandatory."+
  115. " vsphere.conf does not have the workspace specified correctly. cfg.Workspace: %+v", cfg.Workspace)
  116. framework.Logf(msg)
  117. return nil, errors.New(msg)
  118. }
  119. for vcServer, vcConfig := range cfg.VirtualCenter {
  120. framework.Logf("Initializing vc server %s", vcServer)
  121. if vcServer == "" {
  122. framework.Logf("vsphere.conf does not have the VirtualCenter IP address specified")
  123. return nil, errors.New("vsphere.conf does not have the VirtualCenter IP address specified")
  124. }
  125. vcConfig.Hostname = vcServer
  126. if vcConfig.Username == "" {
  127. vcConfig.Username = cfg.Global.User
  128. }
  129. if vcConfig.Password == "" {
  130. vcConfig.Password = cfg.Global.Password
  131. }
  132. if vcConfig.Username == "" {
  133. msg := fmt.Sprintf("vcConfig.Username is empty for vc %s!", vcServer)
  134. framework.Logf(msg)
  135. return nil, errors.New(msg)
  136. }
  137. if vcConfig.Password == "" {
  138. msg := fmt.Sprintf("vcConfig.Password is empty for vc %s!", vcServer)
  139. framework.Logf(msg)
  140. return nil, errors.New(msg)
  141. }
  142. if vcConfig.Port == "" {
  143. vcConfig.Port = cfg.Global.VCenterPort
  144. }
  145. if vcConfig.Datacenters == "" && cfg.Global.Datacenters != "" {
  146. vcConfig.Datacenters = cfg.Global.Datacenters
  147. }
  148. if vcConfig.RoundTripperCount == 0 {
  149. vcConfig.RoundTripperCount = cfg.Global.RoundTripperCount
  150. }
  151. vcConfig.DefaultDatastore = cfg.Workspace.DefaultDatastore
  152. vcConfig.Folder = cfg.Workspace.Folder
  153. vsphereIns := VSphere{
  154. Config: vcConfig,
  155. }
  156. vsphereInstances[vcServer] = &vsphereIns
  157. }
  158. framework.Logf("ConfigFile %v \n vSphere instances %v", cfg, vsphereInstances)
  159. return vsphereInstances, nil
  160. }