vmware.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package network
  15. import (
  16. "fmt"
  17. "log"
  18. "net"
  19. )
  20. func ProcessVMwareNetconf(config map[string]string) ([]InterfaceGenerator, error) {
  21. log.Println("Processing VMware network config")
  22. log.Println("Parsing nameservers")
  23. var nameservers []net.IP
  24. for i := 0; ; i++ {
  25. if ipStr, ok := config[fmt.Sprintf("dns.server.%d", i)]; ok {
  26. if ip := net.ParseIP(ipStr); ip != nil {
  27. nameservers = append(nameservers, ip)
  28. } else {
  29. return nil, fmt.Errorf("invalid nameserver: %q", ipStr)
  30. }
  31. } else {
  32. break
  33. }
  34. }
  35. log.Printf("Parsed %d nameservers", len(nameservers))
  36. log.Println("Parsing search domains")
  37. var domains []string
  38. for i := 0; ; i++ {
  39. if domain, ok := config[fmt.Sprintf("dns.domain.%d", i)]; ok {
  40. domains = append(domains, domain)
  41. } else {
  42. break
  43. }
  44. }
  45. log.Printf("Parsed %d search domains", len(domains))
  46. var interfaces []InterfaceGenerator
  47. for i := 0; ; i++ {
  48. var addresses []net.IPNet
  49. var routes []route
  50. var err error
  51. var dhcp bool
  52. iface := &physicalInterface{}
  53. log.Printf("Proccessing interface %d", i)
  54. log.Println("Processing DHCP")
  55. if dhcp, err = processDHCPConfig(config, fmt.Sprintf("interface.%d.", i)); err != nil {
  56. return nil, err
  57. }
  58. log.Println("Processing addresses")
  59. if as, err := processAddressConfig(config, fmt.Sprintf("interface.%d.", i)); err == nil {
  60. addresses = append(addresses, as...)
  61. } else {
  62. return nil, err
  63. }
  64. log.Println("Processing routes")
  65. if rs, err := processRouteConfig(config, fmt.Sprintf("interface.%d.", i)); err == nil {
  66. routes = append(routes, rs...)
  67. } else {
  68. return nil, err
  69. }
  70. if mac, ok := config[fmt.Sprintf("interface.%d.mac", i)]; ok {
  71. log.Printf("Parsing interface %d MAC address: %q", i, mac)
  72. if hwaddr, err := net.ParseMAC(mac); err == nil {
  73. iface.hwaddr = hwaddr
  74. } else {
  75. return nil, fmt.Errorf("error while parsing MAC address: %v", err)
  76. }
  77. }
  78. if name, ok := config[fmt.Sprintf("interface.%d.name", i)]; ok {
  79. log.Printf("Parsing interface %d name: %q", i, name)
  80. iface.name = name
  81. }
  82. if len(addresses) > 0 || len(routes) > 0 {
  83. iface.config = configMethodStatic{
  84. hwaddress: iface.hwaddr,
  85. addresses: addresses,
  86. nameservers: nameservers,
  87. domains: domains,
  88. routes: routes,
  89. }
  90. } else if dhcp {
  91. iface.config = configMethodDHCP{
  92. hwaddress: iface.hwaddr,
  93. }
  94. } else {
  95. break
  96. }
  97. interfaces = append(interfaces, iface)
  98. }
  99. return interfaces, nil
  100. }
  101. func processAddressConfig(config map[string]string, prefix string) (addresses []net.IPNet, err error) {
  102. for a := 0; ; a++ {
  103. prefix := fmt.Sprintf("%sip.%d.", prefix, a)
  104. addressStr, ok := config[prefix+"address"]
  105. if !ok {
  106. break
  107. }
  108. ip, network, err := net.ParseCIDR(addressStr)
  109. if err != nil {
  110. return nil, fmt.Errorf("invalid address: %q", addressStr)
  111. }
  112. addresses = append(addresses, net.IPNet{
  113. IP: ip,
  114. Mask: network.Mask,
  115. })
  116. }
  117. return
  118. }
  119. func processRouteConfig(config map[string]string, prefix string) (routes []route, err error) {
  120. for r := 0; ; r++ {
  121. prefix := fmt.Sprintf("%sroute.%d.", prefix, r)
  122. gatewayStr, gok := config[prefix+"gateway"]
  123. destinationStr, dok := config[prefix+"destination"]
  124. if gok && !dok {
  125. return nil, fmt.Errorf("missing destination key")
  126. } else if !gok && dok {
  127. return nil, fmt.Errorf("missing gateway key")
  128. } else if !gok && !dok {
  129. break
  130. }
  131. gateway := net.ParseIP(gatewayStr)
  132. if gateway == nil {
  133. return nil, fmt.Errorf("invalid gateway: %q", gatewayStr)
  134. }
  135. _, destination, err := net.ParseCIDR(destinationStr)
  136. if err != nil {
  137. return nil, err
  138. }
  139. routes = append(routes, route{
  140. destination: *destination,
  141. gateway: gateway,
  142. })
  143. }
  144. return
  145. }
  146. func processDHCPConfig(config map[string]string, prefix string) (dhcp bool, err error) {
  147. dhcpStr, ok := config[prefix+"dhcp"]
  148. if !ok {
  149. return false, nil
  150. }
  151. switch dhcpStr {
  152. case "yes":
  153. return true, nil
  154. case "no":
  155. return false, nil
  156. default:
  157. return false, fmt.Errorf("invalid DHCP option: %q", dhcpStr)
  158. }
  159. }