kube.go 3.0 KB

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