flow_template.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package approve_former
  2. import (
  3. "bytes"
  4. "github.com/pkg/errors"
  5. "text/template"
  6. )
  7. const workflowTemplateTpl = `
  8. [[- $workflowTemplateName := .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. buffer := &bytes.Buffer{}
  56. err = tpl.Execute(buffer, templateParams)
  57. if err != nil {
  58. return "", errors.New(err.Error())
  59. }
  60. return buffer.String(), nil
  61. }