kube.go 3.5 KB

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