kube.go 2.8 KB

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