/Godeps/_workspace/src/github.com/rackspace/gophercloud/openstack/compute/v2/extensions/schedulerhints/requests.go

https://gitlab.com/vectorci/kubernetes · Go · 134 lines · 82 code · 25 blank · 27 comment · 23 complexity · be37c29f4427e7cd2a905168ecaf304e MD5 · raw file

  1. package schedulerhints
  2. import (
  3. "fmt"
  4. "net"
  5. "regexp"
  6. "strings"
  7. "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
  8. )
  9. // SchedulerHints represents a set of scheduling hints that are passed to the
  10. // OpenStack scheduler
  11. type SchedulerHints struct {
  12. // Group specifies a Server Group to place the instance in.
  13. Group string
  14. // DifferentHost will place the instance on a compute node that does not
  15. // host the given instances.
  16. DifferentHost []string
  17. // SameHost will place the instance on a compute node that hosts the given
  18. // instances.
  19. SameHost []string
  20. // Query is a conditional statement that results in compute nodes able to
  21. // host the instance.
  22. Query []interface{}
  23. // TargetCell specifies a cell name where the instance will be placed.
  24. TargetCell string
  25. // BuildNearHostIP specifies a subnet of compute nodes to host the instance.
  26. BuildNearHostIP string
  27. }
  28. // SchedulerHintsBuilder builds the scheduler hints into a serializable format.
  29. type SchedulerHintsBuilder interface {
  30. ToServerSchedulerHintsMap() (map[string]interface{}, error)
  31. }
  32. // ToServerSchedulerHintsMap builds the scheduler hints into a serializable format.
  33. func (opts SchedulerHints) ToServerSchedulerHintsMap() (map[string]interface{}, error) {
  34. sh := make(map[string]interface{})
  35. uuidRegex, _ := regexp.Compile("^[a-z0-9]{8}-[a-z0-9]{4}-[1-5][a-z0-9]{3}-[a-z0-9]{4}-[a-z0-9]{12}$")
  36. if opts.Group != "" {
  37. if !uuidRegex.MatchString(opts.Group) {
  38. return nil, fmt.Errorf("Group must be a UUID")
  39. }
  40. sh["group"] = opts.Group
  41. }
  42. if len(opts.DifferentHost) > 0 {
  43. for _, diffHost := range opts.DifferentHost {
  44. if !uuidRegex.MatchString(diffHost) {
  45. return nil, fmt.Errorf("The hosts in DifferentHost must be in UUID format.")
  46. }
  47. }
  48. sh["different_host"] = opts.DifferentHost
  49. }
  50. if len(opts.SameHost) > 0 {
  51. for _, sameHost := range opts.SameHost {
  52. if !uuidRegex.MatchString(sameHost) {
  53. return nil, fmt.Errorf("The hosts in SameHost must be in UUID format.")
  54. }
  55. }
  56. sh["same_host"] = opts.SameHost
  57. }
  58. /* Query can be something simple like:
  59. [">=", "$free_ram_mb", 1024]
  60. Or more complex like:
  61. ['and',
  62. ['>=', '$free_ram_mb', 1024],
  63. ['>=', '$free_disk_mb', 200 * 1024]
  64. ]
  65. Because of the possible complexity, just make sure the length is a minimum of 3.
  66. */
  67. if len(opts.Query) > 0 {
  68. if len(opts.Query) < 3 {
  69. return nil, fmt.Errorf("Query must be a conditional statement in the format of [op,variable,value]")
  70. }
  71. sh["query"] = opts.Query
  72. }
  73. if opts.TargetCell != "" {
  74. sh["target_cell"] = opts.TargetCell
  75. }
  76. if opts.BuildNearHostIP != "" {
  77. if _, _, err := net.ParseCIDR(opts.BuildNearHostIP); err != nil {
  78. return nil, fmt.Errorf("BuildNearHostIP must be a valid subnet in the form 192.168.1.1/24")
  79. }
  80. ipParts := strings.Split(opts.BuildNearHostIP, "/")
  81. sh["build_near_host_ip"] = ipParts[0]
  82. sh["cidr"] = "/" + ipParts[1]
  83. }
  84. return sh, nil
  85. }
  86. // CreateOptsExt adds a SchedulerHints option to the base CreateOpts.
  87. type CreateOptsExt struct {
  88. servers.CreateOptsBuilder
  89. // SchedulerHints provides a set of hints to the scheduler.
  90. SchedulerHints SchedulerHintsBuilder
  91. }
  92. // ToServerCreateMap adds the SchedulerHints option to the base server creation options.
  93. func (opts CreateOptsExt) ToServerCreateMap() (map[string]interface{}, error) {
  94. base, err := opts.CreateOptsBuilder.ToServerCreateMap()
  95. if err != nil {
  96. return nil, err
  97. }
  98. schedulerHints, err := opts.SchedulerHints.ToServerSchedulerHintsMap()
  99. if err != nil {
  100. return nil, err
  101. }
  102. if len(schedulerHints) == 0 {
  103. return base, nil
  104. }
  105. base["os:scheduler_hints"] = schedulerHints
  106. return base, nil
  107. }