kube.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package kube
  2. import (
  3. "errors"
  4. "text/template"
  5. "github.com/tal-tech/go-zero/tools/goctl/util"
  6. "github.com/urfave/cli"
  7. )
  8. const (
  9. category = "kube"
  10. deployTemplateFile = "deployment.tpl"
  11. jobTemplateFile = "job.tpl"
  12. basePort = 30000
  13. portLimit = 32767
  14. )
  15. type Deployment struct {
  16. Name string
  17. Namespace string
  18. Image string
  19. Secret string
  20. Replicas int
  21. Revisions int
  22. Port int
  23. NodePort int
  24. UseNodePort bool
  25. RequestCpu int
  26. RequestMem int
  27. LimitCpu int
  28. LimitMem int
  29. MinReplicas int
  30. MaxReplicas int
  31. }
  32. func DeploymentCommand(c *cli.Context) error {
  33. nodePort := c.Int("nodePort")
  34. // 0 to disable the nodePort type
  35. if nodePort != 0 && (nodePort < basePort || nodePort > portLimit) {
  36. return errors.New("nodePort should be between 30000 and 32767")
  37. }
  38. text, err := util.LoadTemplate(category, deployTemplateFile, deploymentTemplate)
  39. if err != nil {
  40. return err
  41. }
  42. out, err := util.CreateIfNotExist(c.String("o"))
  43. if err != nil {
  44. return err
  45. }
  46. defer out.Close()
  47. t := template.Must(template.New("deploymentTemplate").Parse(text))
  48. return t.Execute(out, Deployment{
  49. Name: c.String("name"),
  50. Namespace: c.String("namespace"),
  51. Image: c.String("image"),
  52. Secret: c.String("secret"),
  53. Replicas: c.Int("replicas"),
  54. Revisions: c.Int("revisions"),
  55. Port: c.Int("port"),
  56. NodePort: nodePort,
  57. UseNodePort: nodePort > 0,
  58. RequestCpu: c.Int("requestCpu"),
  59. RequestMem: c.Int("requestMem"),
  60. LimitCpu: c.Int("limitCpu"),
  61. LimitMem: c.Int("limitMem"),
  62. MinReplicas: c.Int("minReplicas"),
  63. MaxReplicas: c.Int("maxReplicas"),
  64. })
  65. }
  66. func Category() string {
  67. return category
  68. }
  69. func Clean() error {
  70. return util.Clean(category)
  71. }
  72. func GenTemplates(_ *cli.Context) error {
  73. return util.InitTemplates(category, map[string]string{
  74. deployTemplateFile: deploymentTemplate,
  75. jobTemplateFile: jobTmeplate,
  76. })
  77. }
  78. func RevertTemplate(name string) error {
  79. return util.CreateTemplate(category, name, deploymentTemplate)
  80. }
  81. func Update() error {
  82. err := Clean()
  83. if err != nil {
  84. return err
  85. }
  86. return util.InitTemplates(category, map[string]string{
  87. deployTemplateFile: deploymentTemplate,
  88. jobTemplateFile: jobTmeplate,
  89. })
  90. }