hnsV1.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. for _, endpoint := range endpoints {
  68. equal := false
  69. if endpoint.IPAddress != nil {
  70. equal = endpoint.IPAddress.String() == ip
  71. }
  72. if equal && strings.EqualFold(endpoint.VirtualNetwork, hnsnetwork.Id) {
  73. return &endpointsInfo{
  74. ip: endpoint.IPAddress.String(),
  75. isLocal: !endpoint.IsRemoteEndpoint,
  76. macAddress: endpoint.MacAddress,
  77. hnsID: endpoint.Id,
  78. hns: hns,
  79. }, nil
  80. }
  81. }
  82. return nil, fmt.Errorf("Endpoint %v not found on network %s", ip, networkName)
  83. }
  84. func (hns hnsV1) createEndpoint(ep *endpointsInfo, networkName string) (*endpointsInfo, error) {
  85. hnsNetwork, err := hcsshim.GetHNSNetworkByName(networkName)
  86. if err != nil {
  87. return nil, err
  88. }
  89. hnsEndpoint := &hcsshim.HNSEndpoint{
  90. MacAddress: ep.macAddress,
  91. IPAddress: net.ParseIP(ep.ip),
  92. }
  93. var createdEndpoint *hcsshim.HNSEndpoint
  94. if !ep.isLocal {
  95. if len(ep.providerAddress) != 0 {
  96. paPolicy := hcsshim.PaPolicy{
  97. Type: hcsshim.PA,
  98. PA: ep.providerAddress,
  99. }
  100. paPolicyJson, err := json.Marshal(paPolicy)
  101. if err != nil {
  102. return nil, err
  103. }
  104. hnsEndpoint.Policies = append(hnsEndpoint.Policies, paPolicyJson)
  105. }
  106. createdEndpoint, err = hnsNetwork.CreateRemoteEndpoint(hnsEndpoint)
  107. if err != nil {
  108. return nil, err
  109. }
  110. } else {
  111. createdEndpoint, err = hnsNetwork.CreateEndpoint(hnsEndpoint)
  112. if err != nil {
  113. return nil, fmt.Errorf("Local endpoint creation failed: %v", err)
  114. }
  115. }
  116. return &endpointsInfo{
  117. ip: createdEndpoint.IPAddress.String(),
  118. isLocal: createdEndpoint.IsRemoteEndpoint,
  119. macAddress: createdEndpoint.MacAddress,
  120. hnsID: createdEndpoint.Id,
  121. providerAddress: ep.providerAddress, //TODO get from createdEndpoint
  122. hns: hns,
  123. }, nil
  124. }
  125. func (hns hnsV1) deleteEndpoint(hnsID string) error {
  126. hnsendpoint, err := hcsshim.GetHNSEndpointByID(hnsID)
  127. if err != nil {
  128. return err
  129. }
  130. _, err = hnsendpoint.Delete()
  131. if err == nil {
  132. klog.V(3).Infof("Remote endpoint resource deleted id %s", hnsID)
  133. }
  134. return err
  135. }
  136. func (hns hnsV1) getLoadBalancer(endpoints []endpointsInfo, flags loadBalancerFlags, sourceVip string, vip string, protocol uint16, internalPort uint16, externalPort uint16) (*loadBalancerInfo, error) {
  137. plists, err := hcsshim.HNSListPolicyListRequest()
  138. if err != nil {
  139. return nil, err
  140. }
  141. if flags.isDSR {
  142. klog.V(3).Info("DSR is not supported in V1. Using non DSR instead")
  143. }
  144. for _, plist := range plists {
  145. if len(plist.EndpointReferences) != len(endpoints) {
  146. continue
  147. }
  148. // Validate if input meets any of the policy lists
  149. elbPolicy := hcsshim.ELBPolicy{}
  150. if err = json.Unmarshal(plist.Policies[0], &elbPolicy); err != nil {
  151. continue
  152. }
  153. if elbPolicy.Protocol == protocol && elbPolicy.InternalPort == internalPort && elbPolicy.ExternalPort == externalPort && elbPolicy.ILB == flags.isILB {
  154. if len(vip) > 0 {
  155. if len(elbPolicy.VIPs) == 0 || elbPolicy.VIPs[0] != vip {
  156. continue
  157. }
  158. } else if len(elbPolicy.VIPs) != 0 {
  159. continue
  160. }
  161. LogJson(plist, "Found existing Hns loadbalancer policy resource", 1)
  162. return &loadBalancerInfo{
  163. hnsID: plist.ID,
  164. }, nil
  165. }
  166. }
  167. var hnsEndpoints []hcsshim.HNSEndpoint
  168. for _, ep := range endpoints {
  169. endpoint, err := hcsshim.GetHNSEndpointByID(ep.hnsID)
  170. if err != nil {
  171. return nil, err
  172. }
  173. hnsEndpoints = append(hnsEndpoints, *endpoint)
  174. }
  175. lb, err := hcsshim.AddLoadBalancer(
  176. hnsEndpoints,
  177. flags.isILB,
  178. sourceVip,
  179. vip,
  180. protocol,
  181. internalPort,
  182. externalPort,
  183. )
  184. if err == nil {
  185. LogJson(lb, "Hns loadbalancer policy resource", 1)
  186. } else {
  187. return nil, err
  188. }
  189. return &loadBalancerInfo{
  190. hnsID: lb.ID,
  191. }, err
  192. }
  193. func (hns hnsV1) deleteLoadBalancer(hnsID string) error {
  194. if len(hnsID) == 0 {
  195. // Return silently
  196. return nil
  197. }
  198. // Cleanup HNS policies
  199. hnsloadBalancer, err := hcsshim.GetPolicyListByID(hnsID)
  200. if err != nil {
  201. return err
  202. }
  203. LogJson(hnsloadBalancer, "Removing Policy", 2)
  204. _, err = hnsloadBalancer.Delete()
  205. return err
  206. }