kube.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package kube
  2. import (
  3. "errors"
  4. "text/template"
  5. "github.com/tal-tech/go-zero/tools/goctl/util"
  6. "github.com/urfave/cli"
  7. )
  8. const (
  9. category = "kube"
  10. deployTemplateFile = "deployment.tpl"
  11. jobTemplateFile = "job.tpl"
  12. basePort = 30000
  13. portLimit = 32767
  14. )
  15. var errUnknownServiceType = errors.New("unknown service type")
  16. type (
  17. ServiceType string
  18. KubeRequest struct {
  19. Env string
  20. ServiceName string
  21. ServiceType ServiceType
  22. Namespace string
  23. Schedule string
  24. Replicas int
  25. RevisionHistoryLimit int
  26. Port int
  27. LimitCpu int
  28. LimitMem int
  29. RequestCpu int
  30. RequestMem int
  31. SuccessfulJobsHistoryLimit int
  32. HpaMinReplicas int
  33. HpaMaxReplicas int
  34. }
  35. Deployment struct {
  36. Name string
  37. Namespace string
  38. Image string
  39. Secret string
  40. Replicas int
  41. Revisions int
  42. Port int
  43. NodePort int
  44. UseNodePort bool
  45. RequestCpu int
  46. RequestMem int
  47. LimitCpu int
  48. LimitMem int
  49. MinReplicas int
  50. MaxReplicas int
  51. }
  52. )
  53. func DeploymentCommand(c *cli.Context) error {
  54. nodePort := c.Int("nodePort")
  55. // 0 to disable the nodePort type
  56. if nodePort != 0 && (nodePort < basePort || nodePort > portLimit) {
  57. return errors.New("nodePort should be between 30000 and 32767")
  58. }
  59. text, err := util.LoadTemplate(category, deployTemplateFile, deploymentTemplate)
  60. if err != nil {
  61. return err
  62. }
  63. out, err := util.CreateIfNotExist(c.String("o"))
  64. if err != nil {
  65. return err
  66. }
  67. defer out.Close()
  68. t := template.Must(template.New("deploymentTemplate").Parse(text))
  69. return t.Execute(out, Deployment{
  70. Name: c.String("name"),
  71. Namespace: c.String("namespace"),
  72. Image: c.String("image"),
  73. Secret: c.String("secret"),
  74. Replicas: c.Int("replicas"),
  75. Revisions: c.Int("revisions"),
  76. Port: c.Int("port"),
  77. NodePort: nodePort,
  78. UseNodePort: nodePort > 0,
  79. RequestCpu: c.Int("requestCpu"),
  80. RequestMem: c.Int("requestMem"),
  81. LimitCpu: c.Int("limitCpu"),
  82. LimitMem: c.Int("limitMem"),
  83. MinReplicas: c.Int("minReplicas"),
  84. MaxReplicas: c.Int("maxReplicas"),
  85. })
  86. }
  87. func GenTemplates(_ *cli.Context) error {
  88. return util.InitTemplates(category, map[string]string{
  89. deployTemplateFile: deploymentTemplate,
  90. jobTemplateFile: jobTmeplate,
  91. })
  92. }