hnspolicylist.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //go:build windows
  2. package hns
  3. import (
  4. "encoding/json"
  5. "github.com/sirupsen/logrus"
  6. )
  7. // RoutePolicy is a structure defining schema for Route based Policy
  8. type RoutePolicy struct {
  9. Policy
  10. DestinationPrefix string `json:"DestinationPrefix,omitempty"`
  11. NextHop string `json:"NextHop,omitempty"`
  12. EncapEnabled bool `json:"NeedEncap,omitempty"`
  13. }
  14. // ELBPolicy is a structure defining schema for ELB LoadBalancing based Policy
  15. type ELBPolicy struct {
  16. LBPolicy
  17. SourceVIP string `json:"SourceVIP,omitempty"`
  18. VIPs []string `json:"VIPs,omitempty"`
  19. ILB bool `json:"ILB,omitempty"`
  20. DSR bool `json:"IsDSR,omitempty"`
  21. }
  22. // LBPolicy is a structure defining schema for LoadBalancing based Policy
  23. type LBPolicy struct {
  24. Policy
  25. Protocol uint16 `json:"Protocol,omitempty"`
  26. InternalPort uint16
  27. ExternalPort uint16
  28. }
  29. // PolicyList is a structure defining schema for Policy list request
  30. type PolicyList struct {
  31. ID string `json:"ID,omitempty"`
  32. EndpointReferences []string `json:"References,omitempty"`
  33. Policies []json.RawMessage `json:"Policies,omitempty"`
  34. }
  35. // HNSPolicyListRequest makes a call into HNS to update/query a single network
  36. func HNSPolicyListRequest(method, path, request string) (*PolicyList, error) {
  37. var policy PolicyList
  38. err := hnsCall(method, "/policylists/"+path, request, &policy)
  39. if err != nil {
  40. return nil, err
  41. }
  42. return &policy, nil
  43. }
  44. // HNSListPolicyListRequest gets all the policy list
  45. func HNSListPolicyListRequest() ([]PolicyList, error) {
  46. var plist []PolicyList
  47. err := hnsCall("GET", "/policylists/", "", &plist)
  48. if err != nil {
  49. return nil, err
  50. }
  51. return plist, nil
  52. }
  53. // PolicyListRequest makes a HNS call to modify/query a network policy list
  54. func PolicyListRequest(method, path, request string) (*PolicyList, error) {
  55. policylist := &PolicyList{}
  56. err := hnsCall(method, "/policylists/"+path, request, &policylist)
  57. if err != nil {
  58. return nil, err
  59. }
  60. return policylist, nil
  61. }
  62. // GetPolicyListByID get the policy list by ID
  63. func GetPolicyListByID(policyListID string) (*PolicyList, error) {
  64. return PolicyListRequest("GET", policyListID, "")
  65. }
  66. // Create PolicyList by sending PolicyListRequest to HNS.
  67. func (policylist *PolicyList) Create() (*PolicyList, error) {
  68. operation := "Create"
  69. title := "hcsshim::PolicyList::" + operation
  70. logrus.Debugf(title+" id=%s", policylist.ID)
  71. jsonString, err := json.Marshal(policylist)
  72. if err != nil {
  73. return nil, err
  74. }
  75. return PolicyListRequest("POST", "", string(jsonString))
  76. }
  77. // Delete deletes PolicyList
  78. func (policylist *PolicyList) Delete() (*PolicyList, error) {
  79. operation := "Delete"
  80. title := "hcsshim::PolicyList::" + operation
  81. logrus.Debugf(title+" id=%s", policylist.ID)
  82. return PolicyListRequest("DELETE", policylist.ID, "")
  83. }
  84. // AddEndpoint add an endpoint to a Policy List
  85. func (policylist *PolicyList) AddEndpoint(endpoint *HNSEndpoint) (*PolicyList, error) {
  86. operation := "AddEndpoint"
  87. title := "hcsshim::PolicyList::" + operation
  88. logrus.Debugf(title+" id=%s, endpointId:%s", policylist.ID, endpoint.Id)
  89. _, err := policylist.Delete()
  90. if err != nil {
  91. return nil, err
  92. }
  93. // Add Endpoint to the Existing List
  94. policylist.EndpointReferences = append(policylist.EndpointReferences, "/endpoints/"+endpoint.Id)
  95. return policylist.Create()
  96. }
  97. // RemoveEndpoint removes an endpoint from the Policy List
  98. func (policylist *PolicyList) RemoveEndpoint(endpoint *HNSEndpoint) (*PolicyList, error) {
  99. operation := "RemoveEndpoint"
  100. title := "hcsshim::PolicyList::" + operation
  101. logrus.Debugf(title+" id=%s, endpointId:%s", policylist.ID, endpoint.Id)
  102. _, err := policylist.Delete()
  103. if err != nil {
  104. return nil, err
  105. }
  106. elementToRemove := "/endpoints/" + endpoint.Id
  107. var references []string
  108. for _, endpointReference := range policylist.EndpointReferences {
  109. if endpointReference == elementToRemove {
  110. continue
  111. }
  112. references = append(references, endpointReference)
  113. }
  114. policylist.EndpointReferences = references
  115. return policylist.Create()
  116. }
  117. // AddLoadBalancer policy list for the specified endpoints
  118. func AddLoadBalancer(endpoints []HNSEndpoint, isILB bool, sourceVIP, vip string, protocol uint16, internalPort uint16, externalPort uint16) (*PolicyList, error) {
  119. operation := "AddLoadBalancer"
  120. title := "hcsshim::PolicyList::" + operation
  121. logrus.Debugf(title+" endpointId=%v, isILB=%v, sourceVIP=%s, vip=%s, protocol=%v, internalPort=%v, externalPort=%v", endpoints, isILB, sourceVIP, vip, protocol, internalPort, externalPort)
  122. policylist := &PolicyList{}
  123. elbPolicy := &ELBPolicy{
  124. SourceVIP: sourceVIP,
  125. ILB: isILB,
  126. }
  127. if len(vip) > 0 {
  128. elbPolicy.VIPs = []string{vip}
  129. }
  130. elbPolicy.Type = ExternalLoadBalancer
  131. elbPolicy.Protocol = protocol
  132. elbPolicy.InternalPort = internalPort
  133. elbPolicy.ExternalPort = externalPort
  134. for _, endpoint := range endpoints {
  135. policylist.EndpointReferences = append(policylist.EndpointReferences, "/endpoints/"+endpoint.Id)
  136. }
  137. jsonString, err := json.Marshal(elbPolicy)
  138. if err != nil {
  139. return nil, err
  140. }
  141. policylist.Policies = append(policylist.Policies, jsonString)
  142. return policylist.Create()
  143. }
  144. // AddRoute adds route policy list for the specified endpoints
  145. func AddRoute(endpoints []HNSEndpoint, destinationPrefix string, nextHop string, encapEnabled bool) (*PolicyList, error) {
  146. operation := "AddRoute"
  147. title := "hcsshim::PolicyList::" + operation
  148. logrus.Debugf(title+" destinationPrefix:%s", destinationPrefix)
  149. policylist := &PolicyList{}
  150. rPolicy := &RoutePolicy{
  151. DestinationPrefix: destinationPrefix,
  152. NextHop: nextHop,
  153. EncapEnabled: encapEnabled,
  154. }
  155. rPolicy.Type = Route
  156. for _, endpoint := range endpoints {
  157. policylist.EndpointReferences = append(policylist.EndpointReferences, "/endpoints/"+endpoint.Id)
  158. }
  159. jsonString, err := json.Marshal(rPolicy)
  160. if err != nil {
  161. return nil, err
  162. }
  163. policylist.Policies = append(policylist.Policies, jsonString)
  164. return policylist.Create()
  165. }