-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
cluster_workflow_template_server.go
133 lines (113 loc) · 5.66 KB
/
cluster_workflow_template_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package clusterworkflowtemplate
import (
"context"
"fmt"
"sort"
"google.golang.org/grpc/codes"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterwftmplpkg "github.com/argoproj/argo-workflows/v3/pkg/apiclient/clusterworkflowtemplate"
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo-workflows/v3/server/auth"
"github.com/argoproj/argo-workflows/v3/util/instanceid"
"github.com/argoproj/argo-workflows/v3/workflow/creator"
"github.com/argoproj/argo-workflows/v3/workflow/templateresolution"
"github.com/argoproj/argo-workflows/v3/workflow/validate"
serverutils "github.com/argoproj/argo-workflows/v3/server/utils"
)
type ClusterWorkflowTemplateServer struct {
instanceIDService instanceid.Service
}
func NewClusterWorkflowTemplateServer(instanceID instanceid.Service) clusterwftmplpkg.ClusterWorkflowTemplateServiceServer {
return &ClusterWorkflowTemplateServer{instanceID}
}
func (cwts *ClusterWorkflowTemplateServer) CreateClusterWorkflowTemplate(ctx context.Context, req *clusterwftmplpkg.ClusterWorkflowTemplateCreateRequest) (*v1alpha1.ClusterWorkflowTemplate, error) {
wfClient := auth.GetWfClient(ctx)
if req.Template == nil {
return nil, serverutils.ToStatusError(fmt.Errorf("cluster workflow template was not found in the request body"), codes.InvalidArgument)
}
cwts.instanceIDService.Label(req.Template)
creator.Label(ctx, req.Template)
cwftmplGetter := templateresolution.WrapClusterWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().ClusterWorkflowTemplates())
err := validate.ValidateClusterWorkflowTemplate(nil, cwftmplGetter, req.Template, validate.ValidateOpts{})
if err != nil {
return nil, serverutils.ToStatusError(err, codes.InvalidArgument)
}
res, err := wfClient.ArgoprojV1alpha1().ClusterWorkflowTemplates().Create(ctx, req.Template, v1.CreateOptions{})
if err != nil {
return nil, serverutils.ToStatusError(err, codes.Internal)
}
return res, nil
}
func (cwts *ClusterWorkflowTemplateServer) GetClusterWorkflowTemplate(ctx context.Context, req *clusterwftmplpkg.ClusterWorkflowTemplateGetRequest) (*v1alpha1.ClusterWorkflowTemplate, error) {
wfTmpl, err := cwts.getTemplateAndValidate(ctx, req.Name)
if err != nil {
return nil, serverutils.ToStatusError(err, codes.Internal)
}
return wfTmpl, nil
}
func (cwts *ClusterWorkflowTemplateServer) getTemplateAndValidate(ctx context.Context, name string) (*v1alpha1.ClusterWorkflowTemplate, error) {
wfClient := auth.GetWfClient(ctx)
wfTmpl, err := wfClient.ArgoprojV1alpha1().ClusterWorkflowTemplates().Get(ctx, name, v1.GetOptions{})
if err != nil {
return nil, serverutils.ToStatusError(err, codes.Internal)
}
err = cwts.instanceIDService.Validate(wfTmpl)
if err != nil {
return nil, serverutils.ToStatusError(err, codes.InvalidArgument)
}
return wfTmpl, nil
}
func (cwts *ClusterWorkflowTemplateServer) ListClusterWorkflowTemplates(ctx context.Context, req *clusterwftmplpkg.ClusterWorkflowTemplateListRequest) (*v1alpha1.ClusterWorkflowTemplateList, error) {
wfClient := auth.GetWfClient(ctx)
options := &v1.ListOptions{}
if req.ListOptions != nil {
options = req.ListOptions
}
cwts.instanceIDService.With(options)
cwfList, err := wfClient.ArgoprojV1alpha1().ClusterWorkflowTemplates().List(ctx, *options)
if err != nil {
return nil, serverutils.ToStatusError(err, codes.Internal)
}
sort.Sort(cwfList.Items)
return cwfList, nil
}
func (cwts *ClusterWorkflowTemplateServer) DeleteClusterWorkflowTemplate(ctx context.Context, req *clusterwftmplpkg.ClusterWorkflowTemplateDeleteRequest) (*clusterwftmplpkg.ClusterWorkflowTemplateDeleteResponse, error) {
wfClient := auth.GetWfClient(ctx)
_, err := cwts.getTemplateAndValidate(ctx, req.Name)
if err != nil {
return nil, serverutils.ToStatusError(err, codes.InvalidArgument)
}
err = wfClient.ArgoprojV1alpha1().ClusterWorkflowTemplates().Delete(ctx, req.Name, v1.DeleteOptions{})
if err != nil {
return nil, serverutils.ToStatusError(err, codes.Internal)
}
return &clusterwftmplpkg.ClusterWorkflowTemplateDeleteResponse{}, nil
}
func (cwts *ClusterWorkflowTemplateServer) LintClusterWorkflowTemplate(ctx context.Context, req *clusterwftmplpkg.ClusterWorkflowTemplateLintRequest) (*v1alpha1.ClusterWorkflowTemplate, error) {
cwts.instanceIDService.Label(req.Template)
creator.Label(ctx, req.Template)
wfClient := auth.GetWfClient(ctx)
cwftmplGetter := templateresolution.WrapClusterWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().ClusterWorkflowTemplates())
err := validate.ValidateClusterWorkflowTemplate(nil, cwftmplGetter, req.Template, validate.ValidateOpts{Lint: true})
if err != nil {
return nil, serverutils.ToStatusError(err, codes.InvalidArgument)
}
return req.Template, nil
}
func (cwts *ClusterWorkflowTemplateServer) UpdateClusterWorkflowTemplate(ctx context.Context, req *clusterwftmplpkg.ClusterWorkflowTemplateUpdateRequest) (*v1alpha1.ClusterWorkflowTemplate, error) {
if req.Template == nil {
return nil, serverutils.ToStatusError(fmt.Errorf("ClusterWorkflowTemplate is not found in Request body"), codes.InvalidArgument)
}
err := cwts.instanceIDService.Validate(req.Template)
if err != nil {
return nil, serverutils.ToStatusError(err, codes.InvalidArgument)
}
wfClient := auth.GetWfClient(ctx)
cwftmplGetter := templateresolution.WrapClusterWorkflowTemplateInterface(wfClient.ArgoprojV1alpha1().ClusterWorkflowTemplates())
err = validate.ValidateClusterWorkflowTemplate(nil, cwftmplGetter, req.Template, validate.ValidateOpts{})
if err != nil {
return nil, serverutils.ToStatusError(err, codes.InvalidArgument)
}
res, err := wfClient.ArgoprojV1alpha1().ClusterWorkflowTemplates().Update(ctx, req.Template, v1.UpdateOptions{})
return res, serverutils.ToStatusError(err, codes.Internal)
}