kube.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // DeploymentCommand is used to generate the kubernetes deployment yaml files.
  35. func DeploymentCommand(c *cli.Context) error {
  36. nodePort := c.Int("nodePort")
  37. // 0 to disable the nodePort type
  38. if nodePort != 0 && (nodePort < basePort || nodePort > portLimit) {
  39. return errors.New("nodePort should be between 30000 and 32767")
  40. }
  41. text, err := util.LoadTemplate(category, deployTemplateFile, deploymentTemplate)
  42. if err != nil {
  43. return err
  44. }
  45. out, err := util.CreateIfNotExist(c.String("o"))
  46. if err != nil {
  47. return err
  48. }
  49. defer out.Close()
  50. t := template.Must(template.New("deploymentTemplate").Parse(text))
  51. err = t.Execute(out, Deployment{
  52. Name: c.String("name"),
  53. Namespace: c.String("namespace"),
  54. Image: c.String("image"),
  55. Secret: c.String("secret"),
  56. Replicas: c.Int("replicas"),
  57. Revisions: c.Int("revisions"),
  58. Port: c.Int("port"),
  59. NodePort: nodePort,
  60. UseNodePort: nodePort > 0,
  61. RequestCpu: c.Int("requestCpu"),
  62. RequestMem: c.Int("requestMem"),
  63. LimitCpu: c.Int("limitCpu"),
  64. LimitMem: c.Int("limitMem"),
  65. MinReplicas: c.Int("minReplicas"),
  66. MaxReplicas: c.Int("maxReplicas"),
  67. })
  68. if err != nil {
  69. return err
  70. }
  71. fmt.Println(aurora.Green("Done."))
  72. return nil
  73. }
  74. // Category returns the category of the deployments.
  75. func Category() string {
  76. return category
  77. }
  78. // Clean cleans the generated deployment files.
  79. func Clean() error {
  80. return util.Clean(category)
  81. }
  82. // GenTemplates generates the deployment template files.
  83. func GenTemplates(_ *cli.Context) error {
  84. return util.InitTemplates(category, map[string]string{
  85. deployTemplateFile: deploymentTemplate,
  86. jobTemplateFile: jobTmeplate,
  87. })
  88. }
  89. // RevertTemplate reverts the given template file to the default value.
  90. func RevertTemplate(name string) error {
  91. return util.CreateTemplate(category, name, deploymentTemplate)
  92. }
  93. // Update updates the template files to the templates built in current goctl.
  94. func Update() error {
  95. err := Clean()
  96. if err != nil {
  97. return err
  98. }
  99. return util.InitTemplates(category, map[string]string{
  100. deployTemplateFile: deploymentTemplate,
  101. jobTemplateFile: jobTmeplate,
  102. })
  103. }