hcnglobals.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package hcn
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/Microsoft/hcsshim/internal/hcserror"
  6. "github.com/Microsoft/hcsshim/internal/interop"
  7. "github.com/sirupsen/logrus"
  8. )
  9. // Globals are all global properties of the HCN Service.
  10. type Globals struct {
  11. Version Version `json:"Version"`
  12. }
  13. // Version is the HCN Service version.
  14. type Version struct {
  15. Major int `json:"Major"`
  16. Minor int `json:"Minor"`
  17. }
  18. var (
  19. // HNSVersion1803 added ACL functionality.
  20. HNSVersion1803 = Version{Major: 7, Minor: 2}
  21. // V2ApiSupport allows the use of V2 Api calls and V2 Schema.
  22. V2ApiSupport = Version{Major: 9, Minor: 2}
  23. // Remote Subnet allows for Remote Subnet policies on Overlay networks
  24. RemoteSubnetVersion = Version{Major: 9, Minor: 2}
  25. // A Host Route policy allows for local container to local host communication Overlay networks
  26. HostRouteVersion = Version{Major: 9, Minor: 2}
  27. // HNS 10.2 allows for Direct Server Return for loadbalancing
  28. DSRVersion = Version{Major: 10, Minor: 2}
  29. )
  30. // GetGlobals returns the global properties of the HCN Service.
  31. func GetGlobals() (*Globals, error) {
  32. var version Version
  33. err := hnsCall("GET", "/globals/version", "", &version)
  34. if err != nil {
  35. return nil, err
  36. }
  37. globals := &Globals{
  38. Version: version,
  39. }
  40. return globals, nil
  41. }
  42. type hnsResponse struct {
  43. Success bool
  44. Error string
  45. Output json.RawMessage
  46. }
  47. func hnsCall(method, path, request string, returnResponse interface{}) error {
  48. var responseBuffer *uint16
  49. logrus.Debugf("[%s]=>[%s] Request : %s", method, path, request)
  50. err := _hnsCall(method, path, request, &responseBuffer)
  51. if err != nil {
  52. return hcserror.New(err, "hnsCall ", "")
  53. }
  54. response := interop.ConvertAndFreeCoTaskMemString(responseBuffer)
  55. hnsresponse := &hnsResponse{}
  56. if err = json.Unmarshal([]byte(response), &hnsresponse); err != nil {
  57. return err
  58. }
  59. if !hnsresponse.Success {
  60. return fmt.Errorf("HNS failed with error : %s", hnsresponse.Error)
  61. }
  62. if len(hnsresponse.Output) == 0 {
  63. return nil
  64. }
  65. logrus.Debugf("Network Response : %s", hnsresponse.Output)
  66. err = json.Unmarshal(hnsresponse.Output, returnResponse)
  67. if err != nil {
  68. return err
  69. }
  70. return nil
  71. }