workflow_template.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package client
  2. import (
  3. "github.com/argoproj/argo-workflows/v3/pkg/apiclient/workflowtemplate"
  4. "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
  5. "github.com/pkg/errors"
  6. )
  7. type CreateWorkflowTemplateParams struct {
  8. Namespace string
  9. TemplateDefinition string
  10. }
  11. // CreateWorkflowTemplate 创建工作流模板
  12. func (c *Client) CreateWorkflowTemplate(params CreateWorkflowTemplateParams) error {
  13. wft := new(v1alpha1.WorkflowTemplate)
  14. err := unmarshalYamlToJsonStruct([]byte(params.TemplateDefinition), wft)
  15. if err != nil {
  16. return err
  17. }
  18. _, err = c.workflowTemplateService.CreateWorkflowTemplate(c.ctx, &workflowtemplate.WorkflowTemplateCreateRequest{
  19. Namespace: params.Namespace,
  20. Template: wft,
  21. })
  22. if err != nil {
  23. return errors.New(err.Error())
  24. }
  25. return nil
  26. }
  27. type DeleteWorkflowTemplateParams struct {
  28. Namespace string
  29. Name string
  30. }
  31. // DeleteWorkflowTemplate 删除工作流模板
  32. func (c *Client) DeleteWorkflowTemplate(params DeleteWorkflowTemplateParams) error {
  33. _, err := c.workflowTemplateService.DeleteWorkflowTemplate(c.ctx, &workflowtemplate.WorkflowTemplateDeleteRequest{
  34. Namespace: params.Namespace,
  35. Name: params.Name,
  36. })
  37. if err != nil {
  38. return errors.New(err.Error())
  39. }
  40. return nil
  41. }
  42. type UpdateWorkflowTemplateParams struct {
  43. Namespace string
  44. Name string
  45. WorkflowTemplate *v1alpha1.WorkflowTemplate
  46. }
  47. // UpdateWorkflowTemplate 更新工作流模板
  48. func (c *Client) UpdateWorkflowTemplate(params UpdateWorkflowTemplateParams) error {
  49. _, err := c.workflowTemplateService.UpdateWorkflowTemplate(c.ctx, &workflowtemplate.WorkflowTemplateUpdateRequest{
  50. Namespace: params.Namespace,
  51. Template: params.WorkflowTemplate,
  52. })
  53. if err != nil {
  54. return errors.New(err.Error())
  55. }
  56. return nil
  57. }
  58. type GetWorkflowTemplatesInNamespaceParams struct {
  59. Namespace string
  60. }
  61. // GetWorkflowTemplatesInNamespace 获取命名空间下的工作流模板
  62. func (c *Client) GetWorkflowTemplatesInNamespace(params GetWorkflowTemplatesInNamespaceParams) (v1alpha1.WorkflowTemplates, error) {
  63. response, err := c.workflowTemplateService.ListWorkflowTemplates(c.ctx, &workflowtemplate.WorkflowTemplateListRequest{
  64. Namespace: params.Namespace,
  65. })
  66. if err != nil {
  67. return nil, errors.New(err.Error())
  68. }
  69. return response.Items, nil
  70. }
  71. type GetWorkflowTemplateParams struct {
  72. Namespace string
  73. Name string
  74. }
  75. // GetWorkflowTemplate 获取指定的工作流模板
  76. func (c *Client) GetWorkflowTemplate(params GetWorkflowTemplateParams) (*v1alpha1.WorkflowTemplate, error) {
  77. response, err := c.workflowTemplateService.GetWorkflowTemplate(c.ctx, &workflowtemplate.WorkflowTemplateGetRequest{
  78. Namespace: params.Namespace,
  79. Name: params.Name,
  80. })
  81. if err != nil {
  82. return nil, errors.New(err.Error())
  83. }
  84. return response, nil
  85. }
  86. type LintWorkflowTemplateParams struct {
  87. Namespace string
  88. TemplateDefinition string
  89. }
  90. // LintWorkflowTemplate 工作流模板定义语法检查
  91. func (c *Client) LintWorkflowTemplate(params LintWorkflowTemplateParams) error {
  92. wft := new(v1alpha1.WorkflowTemplate)
  93. err := unmarshalYamlToJsonStruct([]byte(params.TemplateDefinition), wft)
  94. if err != nil {
  95. return err
  96. }
  97. _, err = c.workflowTemplateService.LintWorkflowTemplate(c.ctx, &workflowtemplate.WorkflowTemplateLintRequest{
  98. Namespace: params.Namespace,
  99. Template: wft,
  100. })
  101. if err != nil {
  102. return errors.New(err.Error())
  103. }
  104. return nil
  105. }