kube.go 3.2 KB

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