flow_template.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package approve_former
  2. import (
  3. "bytes"
  4. "github.com/pkg/errors"
  5. "text/template"
  6. )
  7. const workflowTemplateTpl = `
  8. [[- $workflowTemplateName := .real_workflow_name -]]
  9. [[- $rootChildren := .children -]]
  10. apiVersion: argoproj.io/v1alpha1
  11. kind: WorkflowTemplate
  12. metadata:
  13. name: [[ $workflowTemplateName ]]
  14. spec:
  15. entrypoint: approve-flow
  16. arguments:
  17. parameters:
  18. - name: object
  19. value: {}
  20. templates:
  21. - name: approve-flow
  22. inputs:
  23. parameters:
  24. - name: object
  25. dag:
  26. tasks:
  27. [[ template "template" $rootChildren ]]
  28. - name: approval
  29. suspend: {}
  30. inputs:
  31. parameters:
  32. - name: approve
  33. default: '通过'
  34. enum:
  35. - '通过'
  36. - '拒绝'
  37. - '忽略'
  38. outputs:
  39. parameters:
  40. - name: approve
  41. valueFrom:
  42. supplied: {}
  43. [[- define "template" -]]
  44. - name: node
  45. template: approval
  46. [[- end -]]
  47. `
  48. func ParseWorkflowTemplate(name string, templateParams map[string]any) (string, error) {
  49. tpl := template.New(name)
  50. tpl.Delims("[[", "]]")
  51. tpl, err := tpl.Parse(workflowTemplateTpl)
  52. if err != nil {
  53. return "", errors.New(err.Error())
  54. }
  55. templateParams["real_workflow_name"] = name
  56. buffer := &bytes.Buffer{}
  57. err = tpl.Execute(buffer, templateParams)
  58. if err != nil {
  59. return "", errors.New(err.Error())
  60. }
  61. return buffer.String(), nil
  62. }