hnsendpoint.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //go:build windows
  2. package hcsshim
  3. import (
  4. "github.com/Microsoft/hcsshim/internal/hns"
  5. )
  6. // HNSEndpoint represents a network endpoint in HNS
  7. type HNSEndpoint = hns.HNSEndpoint
  8. // HNSEndpointStats represent the stats for an networkendpoint in HNS
  9. type HNSEndpointStats = hns.EndpointStats
  10. // Namespace represents a Compartment.
  11. type Namespace = hns.Namespace
  12. // SystemType represents the type of the system on which actions are done
  13. type SystemType string
  14. // SystemType const
  15. const (
  16. ContainerType SystemType = "Container"
  17. VirtualMachineType SystemType = "VirtualMachine"
  18. HostType SystemType = "Host"
  19. )
  20. // EndpointAttachDetachRequest is the structure used to send request to the container to modify the system
  21. // Supported resource types are Network and Request Types are Add/Remove
  22. type EndpointAttachDetachRequest = hns.EndpointAttachDetachRequest
  23. // EndpointResquestResponse is object to get the endpoint request response
  24. type EndpointResquestResponse = hns.EndpointResquestResponse
  25. // HNSEndpointRequest makes a HNS call to modify/query a network endpoint
  26. func HNSEndpointRequest(method, path, request string) (*HNSEndpoint, error) {
  27. return hns.HNSEndpointRequest(method, path, request)
  28. }
  29. // HNSListEndpointRequest makes a HNS call to query the list of available endpoints
  30. func HNSListEndpointRequest() ([]HNSEndpoint, error) {
  31. return hns.HNSListEndpointRequest()
  32. }
  33. // HotAttachEndpoint makes a HCS Call to attach the endpoint to the container
  34. func HotAttachEndpoint(containerID string, endpointID string) error {
  35. endpoint, err := GetHNSEndpointByID(endpointID)
  36. if err != nil {
  37. return err
  38. }
  39. isAttached, err := endpoint.IsAttached(containerID)
  40. if isAttached {
  41. return err
  42. }
  43. return modifyNetworkEndpoint(containerID, endpointID, Add)
  44. }
  45. // HotDetachEndpoint makes a HCS Call to detach the endpoint from the container
  46. func HotDetachEndpoint(containerID string, endpointID string) error {
  47. endpoint, err := GetHNSEndpointByID(endpointID)
  48. if err != nil {
  49. return err
  50. }
  51. isAttached, err := endpoint.IsAttached(containerID)
  52. if !isAttached {
  53. return err
  54. }
  55. return modifyNetworkEndpoint(containerID, endpointID, Remove)
  56. }
  57. // ModifyContainer corresponding to the container id, by sending a request
  58. func modifyContainer(id string, request *ResourceModificationRequestResponse) error {
  59. container, err := OpenContainer(id)
  60. if err != nil {
  61. if IsNotExist(err) {
  62. return ErrComputeSystemDoesNotExist
  63. }
  64. return getInnerError(err)
  65. }
  66. defer container.Close()
  67. err = container.Modify(request)
  68. if err != nil {
  69. if IsNotSupported(err) {
  70. return ErrPlatformNotSupported
  71. }
  72. return getInnerError(err)
  73. }
  74. return nil
  75. }
  76. func modifyNetworkEndpoint(containerID string, endpointID string, request RequestType) error {
  77. requestMessage := &ResourceModificationRequestResponse{
  78. Resource: Network,
  79. Request: request,
  80. Data: endpointID,
  81. }
  82. err := modifyContainer(containerID, requestMessage)
  83. if err != nil {
  84. return err
  85. }
  86. return nil
  87. }
  88. // GetHNSEndpointByID get the Endpoint by ID
  89. func GetHNSEndpointByID(endpointID string) (*HNSEndpoint, error) {
  90. return hns.GetHNSEndpointByID(endpointID)
  91. }
  92. // GetHNSEndpointByName gets the endpoint filtered by Name
  93. func GetHNSEndpointByName(endpointName string) (*HNSEndpoint, error) {
  94. return hns.GetHNSEndpointByName(endpointName)
  95. }
  96. // GetHNSEndpointStats gets the endpoint stats by ID
  97. func GetHNSEndpointStats(endpointName string) (*HNSEndpointStats, error) {
  98. return hns.GetHNSEndpointStats(endpointName)
  99. }