kube.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. ImagePullPolicy string
  44. }
  45. // DeploymentCommand is used to generate the kubernetes deployment yaml files.
  46. func deploymentCommand(_ *cobra.Command, _ []string) error {
  47. nodePort := varIntNodePort
  48. home := varStringHome
  49. remote := varStringRemote
  50. branch := varStringBranch
  51. if len(remote) > 0 {
  52. repo, _ := util.CloneIntoGitHome(remote, branch)
  53. if len(repo) > 0 {
  54. home = repo
  55. }
  56. }
  57. if len(home) > 0 {
  58. pathx.RegisterGoctlHome(home)
  59. }
  60. // 0 to disable the nodePort type
  61. if nodePort != 0 && (nodePort < basePort || nodePort > portLimit) {
  62. return errors.New("nodePort should be between 30000 and 32767")
  63. }
  64. text, err := pathx.LoadTemplate(category, deployTemplateFile, deploymentTemplate)
  65. if err != nil {
  66. return err
  67. }
  68. out, err := pathx.CreateIfNotExist(varStringO)
  69. if err != nil {
  70. return err
  71. }
  72. defer out.Close()
  73. t := template.Must(template.New("deploymentTemplate").Parse(text))
  74. err = t.Execute(out, Deployment{
  75. Name: varStringName,
  76. Namespace: varStringNamespace,
  77. Image: varStringImage,
  78. Secret: varStringSecret,
  79. Replicas: varIntReplicas,
  80. Revisions: varIntRevisions,
  81. Port: varIntPort,
  82. NodePort: nodePort,
  83. UseNodePort: nodePort > 0,
  84. RequestCpu: varIntRequestCpu,
  85. RequestMem: varIntRequestMem,
  86. LimitCpu: varIntLimitCpu,
  87. LimitMem: varIntLimitMem,
  88. MinReplicas: varIntMinReplicas,
  89. MaxReplicas: varIntMaxReplicas,
  90. ServiceAccount: varStringServiceAccount,
  91. ImagePullPolicy: varStringImagePullPolicy,
  92. })
  93. if err != nil {
  94. return err
  95. }
  96. fmt.Println(aurora.Green("Done."))
  97. return nil
  98. }
  99. // Category returns the category of the deployments.
  100. func Category() string {
  101. return category
  102. }
  103. // Clean cleans the generated deployment files.
  104. func Clean() error {
  105. return pathx.Clean(category)
  106. }
  107. // GenTemplates generates the deployment template files.
  108. func GenTemplates() error {
  109. return pathx.InitTemplates(category, map[string]string{
  110. deployTemplateFile: deploymentTemplate,
  111. jobTemplateFile: jobTemplate,
  112. })
  113. }
  114. // RevertTemplate reverts the given template file to the default value.
  115. func RevertTemplate(name string) error {
  116. return pathx.CreateTemplate(category, name, deploymentTemplate)
  117. }
  118. // Update updates the template files to the templates built in current goctl.
  119. func Update() error {
  120. err := Clean()
  121. if err != nil {
  122. return err
  123. }
  124. return pathx.InitTemplates(category, map[string]string{
  125. deployTemplateFile: deploymentTemplate,
  126. jobTemplateFile: jobTemplate,
  127. })
  128. }