schedulers.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package compute
  15. import (
  16. "yunion.io/x/onecloud/pkg/apis/scheduler"
  17. "yunion.io/x/onecloud/pkg/mcclient"
  18. )
  19. type SchedulerTestBaseOptions struct {
  20. ServerConfigs
  21. Mem int `help:"Memory size (MB), default 512" metavar:"MEMORY" default:"512"`
  22. Ncpu int `help:"#CPU cores of VM server, default 1" default:"1" metavar:"<SERVER_CPU_COUNT>"`
  23. Sku string `help:"Server SKU instance type"`
  24. Log bool `help:"Record to schedule history"`
  25. Cdrom string `help:"ISO image ID" metavar:"IMAGE_ID"`
  26. }
  27. func (o SchedulerTestBaseOptions) data(s *mcclient.ClientSession) (*scheduler.ServerConfig, error) {
  28. config, err := o.ServerConfigs.Data()
  29. if err != nil {
  30. return nil, err
  31. }
  32. data := new(scheduler.ServerConfig)
  33. data.ServerConfigs = config
  34. data.Project = o.Project
  35. if o.Mem > 0 {
  36. data.Memory = o.Mem
  37. }
  38. if o.Ncpu > 0 {
  39. data.Ncpu = o.Ncpu
  40. }
  41. if o.Sku != "" {
  42. data.InstanceType = o.Sku
  43. }
  44. if o.Cdrom != "" {
  45. data.Cdrom = o.Cdrom
  46. }
  47. return data, nil
  48. }
  49. func (o SchedulerTestBaseOptions) options() *scheduler.ScheduleBaseConfig {
  50. opt := new(scheduler.ScheduleBaseConfig)
  51. opt.RecordLog = o.Log
  52. return opt
  53. }
  54. type SchedulerTestOptions struct {
  55. SchedulerTestBaseOptions
  56. SuggestionLimit int64 `help:"Number of schedule candidate informations" default:"50"`
  57. SuggestionAll bool `help:"Show all schedule candidate informations"`
  58. Details bool `help:"Show suggestion details"`
  59. }
  60. func (o *SchedulerTestOptions) Params(s *mcclient.ClientSession) (*scheduler.ScheduleInput, error) {
  61. data, err := o.data(s)
  62. if err != nil {
  63. return nil, err
  64. }
  65. opts := o.options()
  66. input := new(scheduler.ScheduleInput)
  67. input.ServerConfig = *data
  68. input.ScheduleBaseConfig = *opts
  69. input.SuggestionLimit = o.SuggestionLimit
  70. input.SuggestionAll = o.SuggestionAll
  71. input.Details = o.Details
  72. return input, nil
  73. }
  74. type SchedulerForecastOptions struct {
  75. SchedulerTestBaseOptions
  76. }
  77. func (o SchedulerForecastOptions) Params(s *mcclient.ClientSession) (*scheduler.ScheduleInput, error) {
  78. data, err := o.data(s)
  79. if err != nil {
  80. return nil, err
  81. }
  82. opts := o.options()
  83. input := new(scheduler.ScheduleInput)
  84. input.ServerConfig = *data
  85. input.ScheduleBaseConfig = *opts
  86. return input, nil
  87. }