kube.go 3.3 KB

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