hnsV2.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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/hcn"
  19. "k8s.io/klog"
  20. "strings"
  21. )
  22. type hnsV2 struct{}
  23. func (hns hnsV2) getNetworkByName(name string) (*hnsNetworkInfo, error) {
  24. hnsnetwork, err := hcn.GetNetworkByName(name)
  25. if err != nil {
  26. klog.Errorf("%v", err)
  27. return nil, err
  28. }
  29. var remoteSubnets []*remoteSubnetInfo
  30. for _, policy := range hnsnetwork.Policies {
  31. if policy.Type == hcn.RemoteSubnetRoute {
  32. policySettings := hcn.RemoteSubnetRoutePolicySetting{}
  33. err = json.Unmarshal(policy.Settings, &policySettings)
  34. if err != nil {
  35. return nil, fmt.Errorf("Failed to unmarshal Remote Subnet policy settings")
  36. }
  37. rs := &remoteSubnetInfo{
  38. destinationPrefix: policySettings.DestinationPrefix,
  39. isolationID: policySettings.IsolationId,
  40. providerAddress: policySettings.ProviderAddress,
  41. drMacAddress: policySettings.DistributedRouterMacAddress,
  42. }
  43. remoteSubnets = append(remoteSubnets, rs)
  44. }
  45. }
  46. return &hnsNetworkInfo{
  47. id: hnsnetwork.Id,
  48. name: hnsnetwork.Name,
  49. networkType: string(hnsnetwork.Type),
  50. remoteSubnets: remoteSubnets,
  51. }, nil
  52. }
  53. func (hns hnsV2) getEndpointByID(id string) (*endpointsInfo, error) {
  54. hnsendpoint, err := hcn.GetEndpointByID(id)
  55. if err != nil {
  56. return nil, err
  57. }
  58. return &endpointsInfo{ //TODO: fill out PA
  59. ip: hnsendpoint.IpConfigurations[0].IpAddress,
  60. isLocal: uint32(hnsendpoint.Flags&hcn.EndpointFlagsRemoteEndpoint) == 0, //TODO: Change isLocal to isRemote
  61. macAddress: hnsendpoint.MacAddress,
  62. hnsID: hnsendpoint.Id,
  63. hns: hns,
  64. }, nil
  65. }
  66. func (hns hnsV2) getEndpointByIpAddress(ip string, networkName string) (*endpointsInfo, error) {
  67. hnsnetwork, err := hcn.GetNetworkByName(networkName)
  68. if err != nil {
  69. klog.Errorf("%v", err)
  70. return nil, err
  71. }
  72. endpoints, err := hcn.ListEndpoints()
  73. for _, endpoint := range endpoints {
  74. equal := false
  75. if endpoint.IpConfigurations != nil && len(endpoint.IpConfigurations) > 0 {
  76. equal = endpoint.IpConfigurations[0].IpAddress == ip
  77. }
  78. if equal && strings.EqualFold(endpoint.HostComputeNetwork, hnsnetwork.Id) {
  79. return &endpointsInfo{
  80. ip: endpoint.IpConfigurations[0].IpAddress,
  81. isLocal: uint32(endpoint.Flags&hcn.EndpointFlagsRemoteEndpoint) == 0, //TODO: Change isLocal to isRemote
  82. macAddress: endpoint.MacAddress,
  83. hnsID: endpoint.Id,
  84. hns: hns,
  85. }, nil
  86. }
  87. }
  88. return nil, fmt.Errorf("Endpoint %v not found on network %s", ip, networkName)
  89. }
  90. func (hns hnsV2) createEndpoint(ep *endpointsInfo, networkName string) (*endpointsInfo, error) {
  91. hnsNetwork, err := hcn.GetNetworkByName(networkName)
  92. if err != nil {
  93. return nil, err
  94. }
  95. var flags hcn.EndpointFlags
  96. if !ep.isLocal {
  97. flags |= hcn.EndpointFlagsRemoteEndpoint
  98. }
  99. ipConfig := &hcn.IpConfig{
  100. IpAddress: ep.ip,
  101. }
  102. hnsEndpoint := &hcn.HostComputeEndpoint{
  103. IpConfigurations: []hcn.IpConfig{*ipConfig},
  104. MacAddress: ep.macAddress,
  105. Flags: flags,
  106. SchemaVersion: hcn.SchemaVersion{
  107. Major: 2,
  108. Minor: 0,
  109. },
  110. }
  111. var createdEndpoint *hcn.HostComputeEndpoint
  112. if !ep.isLocal {
  113. if len(ep.providerAddress) != 0 {
  114. policySettings := hcn.ProviderAddressEndpointPolicySetting{
  115. ProviderAddress: ep.providerAddress,
  116. }
  117. policySettingsJson, err := json.Marshal(policySettings)
  118. if err != nil {
  119. return nil, fmt.Errorf("PA Policy creation failed: %v", err)
  120. }
  121. paPolicy := hcn.EndpointPolicy{
  122. Type: hcn.NetworkProviderAddress,
  123. Settings: policySettingsJson,
  124. }
  125. hnsEndpoint.Policies = append(hnsEndpoint.Policies, paPolicy)
  126. }
  127. createdEndpoint, err = hnsNetwork.CreateRemoteEndpoint(hnsEndpoint)
  128. if err != nil {
  129. return nil, err
  130. }
  131. } else {
  132. createdEndpoint, err = hnsNetwork.CreateEndpoint(hnsEndpoint)
  133. if err != nil {
  134. return nil, err
  135. }
  136. }
  137. return &endpointsInfo{
  138. ip: createdEndpoint.IpConfigurations[0].IpAddress,
  139. isLocal: uint32(createdEndpoint.Flags&hcn.EndpointFlagsRemoteEndpoint) == 0,
  140. macAddress: createdEndpoint.MacAddress,
  141. hnsID: createdEndpoint.Id,
  142. providerAddress: ep.providerAddress, //TODO get from createdEndpoint
  143. hns: hns,
  144. }, nil
  145. }
  146. func (hns hnsV2) deleteEndpoint(hnsID string) error {
  147. hnsendpoint, err := hcn.GetEndpointByID(hnsID)
  148. if err != nil {
  149. return err
  150. }
  151. err = hnsendpoint.Delete()
  152. if err == nil {
  153. klog.V(3).Infof("Remote endpoint resource deleted id %s", hnsID)
  154. }
  155. return err
  156. }
  157. func (hns hnsV2) getLoadBalancer(endpoints []endpointsInfo, flags loadBalancerFlags, sourceVip string, vip string, protocol uint16, internalPort uint16, externalPort uint16) (*loadBalancerInfo, error) {
  158. plists, err := hcn.ListLoadBalancers()
  159. if err != nil {
  160. return nil, err
  161. }
  162. for _, plist := range plists {
  163. if len(plist.HostComputeEndpoints) != len(endpoints) {
  164. continue
  165. }
  166. // Validate if input meets any of the policy lists
  167. lbPortMapping := plist.PortMappings[0]
  168. if lbPortMapping.Protocol == uint32(protocol) && lbPortMapping.InternalPort == internalPort && lbPortMapping.ExternalPort == externalPort && (lbPortMapping.Flags&1 != 0) == flags.isILB {
  169. if len(vip) > 0 {
  170. if len(plist.FrontendVIPs) == 0 || plist.FrontendVIPs[0] != vip {
  171. continue
  172. }
  173. } else if len(plist.FrontendVIPs) != 0 {
  174. continue
  175. }
  176. LogJson(plist, "Found existing Hns loadbalancer policy resource", 1)
  177. return &loadBalancerInfo{
  178. hnsID: plist.Id,
  179. }, nil
  180. }
  181. }
  182. var hnsEndpoints []hcn.HostComputeEndpoint
  183. for _, ep := range endpoints {
  184. endpoint, err := hcn.GetEndpointByID(ep.hnsID)
  185. if err != nil {
  186. return nil, err
  187. }
  188. hnsEndpoints = append(hnsEndpoints, *endpoint)
  189. }
  190. vips := []string{}
  191. if len(vip) > 0 {
  192. vips = append(vips, vip)
  193. }
  194. lbPortMappingFlags := hcn.LoadBalancerPortMappingFlagsNone
  195. if flags.isILB {
  196. lbPortMappingFlags |= hcn.LoadBalancerPortMappingFlagsILB
  197. }
  198. if flags.useMUX {
  199. lbPortMappingFlags |= hcn.LoadBalancerPortMappingFlagsUseMux
  200. }
  201. if flags.preserveDIP {
  202. lbPortMappingFlags |= hcn.LoadBalancerPortMappingFlagsPreserveDIP
  203. }
  204. if flags.localRoutedVIP {
  205. lbPortMappingFlags |= hcn.LoadBalancerPortMappingFlagsLocalRoutedVIP
  206. }
  207. lbFlags := hcn.LoadBalancerFlagsNone
  208. if flags.isDSR {
  209. lbFlags |= hcn.LoadBalancerFlagsDSR
  210. }
  211. lb, err := hcn.AddLoadBalancer(
  212. hnsEndpoints,
  213. lbFlags,
  214. lbPortMappingFlags,
  215. sourceVip,
  216. vips,
  217. protocol,
  218. internalPort,
  219. externalPort,
  220. )
  221. if err != nil {
  222. return nil, err
  223. }
  224. LogJson(lb, "Hns loadbalancer policy resource", 1)
  225. return &loadBalancerInfo{
  226. hnsID: lb.Id,
  227. }, err
  228. }
  229. func (hns hnsV2) deleteLoadBalancer(hnsID string) error {
  230. lb, err := hcn.GetLoadBalancerByID(hnsID)
  231. if err != nil {
  232. // Return silently
  233. return nil
  234. }
  235. err = lb.Delete()
  236. return err
  237. }