hnsV1.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // +build windows
  2. /*
  3. Copyright 2018 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package winkernel
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "github.com/Microsoft/hcsshim"
  19. "k8s.io/klog"
  20. "net"
  21. "strings"
  22. )
  23. type HostNetworkService interface {
  24. getNetworkByName(name string) (*hnsNetworkInfo, error)
  25. getEndpointByID(id string) (*endpointsInfo, error)
  26. getEndpointByIpAddress(ip string, networkName string) (*endpointsInfo, error)
  27. createEndpoint(ep *endpointsInfo, networkName string) (*endpointsInfo, error)
  28. deleteEndpoint(hnsID string) error
  29. getLoadBalancer(endpoints []endpointsInfo, flags loadBalancerFlags, sourceVip string, vip string, protocol uint16, internalPort uint16, externalPort uint16) (*loadBalancerInfo, error)
  30. deleteLoadBalancer(hnsID string) error
  31. }
  32. // V1 HNS API
  33. type hnsV1 struct{}
  34. func (hns hnsV1) getNetworkByName(name string) (*hnsNetworkInfo, error) {
  35. hnsnetwork, err := hcsshim.GetHNSNetworkByName(name)
  36. if err != nil {
  37. klog.Errorf("%v", err)
  38. return nil, err
  39. }
  40. return &hnsNetworkInfo{
  41. id: hnsnetwork.Id,
  42. name: hnsnetwork.Name,
  43. networkType: hnsnetwork.Type,
  44. }, nil
  45. }
  46. func (hns hnsV1) getEndpointByID(id string) (*endpointsInfo, error) {
  47. hnsendpoint, err := hcsshim.GetHNSEndpointByID(id)
  48. if err != nil {
  49. klog.Errorf("%v", err)
  50. return nil, err
  51. }
  52. return &endpointsInfo{
  53. ip: hnsendpoint.IPAddress.String(),
  54. isLocal: !hnsendpoint.IsRemoteEndpoint, //TODO: Change isLocal to isRemote
  55. macAddress: hnsendpoint.MacAddress,
  56. hnsID: hnsendpoint.Id,
  57. hns: hns,
  58. }, nil
  59. }
  60. func (hns hnsV1) getEndpointByIpAddress(ip string, networkName string) (*endpointsInfo, error) {
  61. hnsnetwork, err := hcsshim.GetHNSNetworkByName(networkName)
  62. if err != nil {
  63. klog.Errorf("%v", err)
  64. return nil, err
  65. }
  66. endpoints, err := hcsshim.HNSListEndpointRequest()
  67. if err != nil {
  68. return nil, fmt.Errorf("Failed to list endpoints: %v", err)
  69. }
  70. for _, endpoint := range endpoints {
  71. equal := false
  72. if endpoint.IPAddress != nil {
  73. equal = endpoint.IPAddress.String() == ip
  74. }
  75. if equal && strings.EqualFold(endpoint.VirtualNetwork, hnsnetwork.Id) {
  76. return &endpointsInfo{
  77. ip: endpoint.IPAddress.String(),
  78. isLocal: !endpoint.IsRemoteEndpoint,
  79. macAddress: endpoint.MacAddress,
  80. hnsID: endpoint.Id,
  81. hns: hns,
  82. }, nil
  83. }
  84. }
  85. return nil, fmt.Errorf("Endpoint %v not found on network %s", ip, networkName)
  86. }
  87. func (hns hnsV1) createEndpoint(ep *endpointsInfo, networkName string) (*endpointsInfo, error) {
  88. hnsNetwork, err := hcsshim.GetHNSNetworkByName(networkName)
  89. if err != nil {
  90. return nil, err
  91. }
  92. hnsEndpoint := &hcsshim.HNSEndpoint{
  93. MacAddress: ep.macAddress,
  94. IPAddress: net.ParseIP(ep.ip),
  95. }
  96. var createdEndpoint *hcsshim.HNSEndpoint
  97. if !ep.isLocal {
  98. if len(ep.providerAddress) != 0 {
  99. paPolicy := hcsshim.PaPolicy{
  100. Type: hcsshim.PA,
  101. PA: ep.providerAddress,
  102. }
  103. paPolicyJson, err := json.Marshal(paPolicy)
  104. if err != nil {
  105. return nil, err
  106. }
  107. hnsEndpoint.Policies = append(hnsEndpoint.Policies, paPolicyJson)
  108. }
  109. createdEndpoint, err = hnsNetwork.CreateRemoteEndpoint(hnsEndpoint)
  110. if err != nil {
  111. return nil, err
  112. }
  113. } else {
  114. createdEndpoint, err = hnsNetwork.CreateEndpoint(hnsEndpoint)
  115. if err != nil {
  116. return nil, fmt.Errorf("Local endpoint creation failed: %v", err)
  117. }
  118. }
  119. return &endpointsInfo{
  120. ip: createdEndpoint.IPAddress.String(),
  121. isLocal: createdEndpoint.IsRemoteEndpoint,
  122. macAddress: createdEndpoint.MacAddress,
  123. hnsID: createdEndpoint.Id,
  124. providerAddress: ep.providerAddress, //TODO get from createdEndpoint
  125. hns: hns,
  126. }, nil
  127. }
  128. func (hns hnsV1) deleteEndpoint(hnsID string) error {
  129. hnsendpoint, err := hcsshim.GetHNSEndpointByID(hnsID)
  130. if err != nil {
  131. return err
  132. }
  133. _, err = hnsendpoint.Delete()
  134. if err == nil {
  135. klog.V(3).Infof("Remote endpoint resource deleted id %s", hnsID)
  136. }
  137. return err
  138. }
  139. func (hns hnsV1) getLoadBalancer(endpoints []endpointsInfo, flags loadBalancerFlags, sourceVip string, vip string, protocol uint16, internalPort uint16, externalPort uint16) (*loadBalancerInfo, error) {
  140. plists, err := hcsshim.HNSListPolicyListRequest()
  141. if err != nil {
  142. return nil, err
  143. }
  144. if flags.isDSR {
  145. klog.V(3).Info("DSR is not supported in V1. Using non DSR instead")
  146. }
  147. for _, plist := range plists {
  148. if len(plist.EndpointReferences) != len(endpoints) {
  149. continue
  150. }
  151. // Validate if input meets any of the policy lists
  152. elbPolicy := hcsshim.ELBPolicy{}
  153. if err = json.Unmarshal(plist.Policies[0], &elbPolicy); err != nil {
  154. continue
  155. }
  156. if elbPolicy.Protocol == protocol && elbPolicy.InternalPort == internalPort && elbPolicy.ExternalPort == externalPort && elbPolicy.ILB == flags.isILB {
  157. if len(vip) > 0 {
  158. if len(elbPolicy.VIPs) == 0 || elbPolicy.VIPs[0] != vip {
  159. continue
  160. }
  161. } else if len(elbPolicy.VIPs) != 0 {
  162. continue
  163. }
  164. LogJson(plist, "Found existing Hns loadbalancer policy resource", 1)
  165. return &loadBalancerInfo{
  166. hnsID: plist.ID,
  167. }, nil
  168. }
  169. }
  170. var hnsEndpoints []hcsshim.HNSEndpoint
  171. for _, ep := range endpoints {
  172. endpoint, err := hcsshim.GetHNSEndpointByID(ep.hnsID)
  173. if err != nil {
  174. return nil, err
  175. }
  176. hnsEndpoints = append(hnsEndpoints, *endpoint)
  177. }
  178. lb, err := hcsshim.AddLoadBalancer(
  179. hnsEndpoints,
  180. flags.isILB,
  181. sourceVip,
  182. vip,
  183. protocol,
  184. internalPort,
  185. externalPort,
  186. )
  187. if err == nil {
  188. LogJson(lb, "Hns loadbalancer policy resource", 1)
  189. } else {
  190. return nil, err
  191. }
  192. return &loadBalancerInfo{
  193. hnsID: lb.ID,
  194. }, err
  195. }
  196. func (hns hnsV1) deleteLoadBalancer(hnsID string) error {
  197. if len(hnsID) == 0 {
  198. // Return silently
  199. return nil
  200. }
  201. // Cleanup HNS policies
  202. hnsloadBalancer, err := hcsshim.GetPolicyListByID(hnsID)
  203. if err != nil {
  204. return err
  205. }
  206. LogJson(hnsloadBalancer, "Removing Policy", 2)
  207. _, err = hnsloadBalancer.Delete()
  208. return err
  209. }