job.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 k8s
  15. import (
  16. "yunion.io/x/jsonutils"
  17. )
  18. type JobTemplateOptions struct {
  19. K8sLabelOptions
  20. K8sPodTemplateOptions
  21. Parallelism int64 `help:"Specifies the maximum desired number of pods the job should run at any given time"`
  22. }
  23. func (o JobTemplateOptions) Params(name string) (*jsonutils.JSONDict, error) {
  24. params := jsonutils.NewDict()
  25. o.K8sPodTemplateOptions.setContainerName(name)
  26. if err := o.K8sPodTemplateOptions.Attach(params); err != nil {
  27. return nil, err
  28. }
  29. if o.Parallelism > 0 {
  30. params.Add(jsonutils.NewInt(o.Parallelism), "parallelism")
  31. }
  32. return params, nil
  33. }
  34. func (o JobTemplateOptions) Attach(params *jsonutils.JSONDict, name string, key ...string) error {
  35. ret, err := o.Params(name)
  36. if err != nil {
  37. return err
  38. }
  39. if len(key) == 0 {
  40. params.Update(ret)
  41. } else {
  42. params.Add(ret, key...)
  43. }
  44. return nil
  45. }
  46. type JobCreateOptions struct {
  47. NamespaceWithClusterOptions
  48. JobTemplateOptions
  49. NAME string `help:"Name of job"`
  50. }
  51. func (o JobCreateOptions) Params() (jsonutils.JSONObject, error) {
  52. params := o.NamespaceWithClusterOptions.Params()
  53. if err := o.JobTemplateOptions.Attach(params, o.NAME); err != nil {
  54. return nil, err
  55. }
  56. params.Add(jsonutils.NewString(o.NAME), "name")
  57. return params, nil
  58. }
  59. type CronJobCreateOptions struct {
  60. JobTemplateOptions
  61. NamespaceWithClusterOptions
  62. NAME string `help:"Name of cronjob"`
  63. Schedule string `help:"The chedule in Cron format, e.g. '*/10 * * * *'" required:"true"`
  64. }
  65. func (o CronJobCreateOptions) Params() (jsonutils.JSONObject, error) {
  66. params := o.NamespaceWithClusterOptions.Params()
  67. if err := o.JobTemplateOptions.Attach(params, o.NAME, "jobTemplate", "spec"); err != nil {
  68. return nil, err
  69. }
  70. params.Add(jsonutils.NewString(o.NAME), "name")
  71. params.Add(jsonutils.NewString(o.Schedule), "schedule")
  72. return params, nil
  73. }