deployment.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 DeploymentCreateOptions struct {
  19. NamespaceWithClusterOptions
  20. K8sLabelOptions
  21. K8sPodTemplateOptions
  22. ServiceSpecOptions
  23. NAME string `help:"Name of deployment"`
  24. Replicas int64 `help:"Number of replicas for pods in this deployment"`
  25. }
  26. func (o DeploymentCreateOptions) Params() (jsonutils.JSONObject, error) {
  27. params := o.NamespaceWithClusterOptions.Params()
  28. o.K8sPodTemplateOptions.setContainerName(o.NAME)
  29. if err := o.K8sPodTemplateOptions.Attach(params); err != nil {
  30. return nil, err
  31. }
  32. if err := o.K8sLabelOptions.Attach(params); err != nil {
  33. return nil, err
  34. }
  35. if err := o.ServiceSpecOptions.Attach(params); err != nil {
  36. return nil, err
  37. }
  38. params.Add(jsonutils.NewString(o.NAME), "name")
  39. if o.Replicas > 1 {
  40. params.Add(jsonutils.NewInt(o.Replicas), "replicas")
  41. }
  42. return params, nil
  43. }
  44. type DeploymentUpdateOptions struct {
  45. NamespaceWithClusterOptions
  46. NAME string `help:"Name of deployment"`
  47. Image []string `help:"Image of container to set, e.g. 'default=nginx:latest'"`
  48. }
  49. func (o DeploymentUpdateOptions) GetId() string {
  50. return o.NAME
  51. }
  52. func (o DeploymentUpdateOptions) Params() (jsonutils.JSONObject, error) {
  53. params := o.NamespaceWithClusterOptions.Params()
  54. containers := jsonutils.NewArray()
  55. for _, img := range o.Image {
  56. parts, err := parseImage(img)
  57. if err != nil {
  58. return nil, err
  59. }
  60. containers.Add(parts)
  61. }
  62. params.Add(containers, "containers")
  63. return params, nil
  64. }