kube.go 2.3 KB

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