-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
archived_workflow_server.go
148 lines (131 loc) · 4.53 KB
/
archived_workflow_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package workflowarchive
import (
"context"
"fmt"
"sort"
"strconv"
"strings"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"github.com/argoproj/argo-workflows/v3/persist/sqldb"
workflowarchivepkg "github.com/argoproj/argo-workflows/v3/pkg/apiclient/workflowarchive"
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo-workflows/v3/server/auth"
)
type archivedWorkflowServer struct {
wfArchive sqldb.WorkflowArchive
}
// NewWorkflowArchiveServer returns a new archivedWorkflowServer
func NewWorkflowArchiveServer(wfArchive sqldb.WorkflowArchive) workflowarchivepkg.ArchivedWorkflowServiceServer {
return &archivedWorkflowServer{wfArchive: wfArchive}
}
func (w *archivedWorkflowServer) ListArchivedWorkflows(ctx context.Context, req *workflowarchivepkg.ListArchivedWorkflowsRequest) (*wfv1.WorkflowList, error) {
options := req.ListOptions
if options == nil {
options = &metav1.ListOptions{}
}
if options.Continue == "" {
options.Continue = "0"
}
limit := int(options.Limit)
offset, err := strconv.Atoi(options.Continue)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "listOptions.continue must be int")
}
if offset < 0 {
return nil, status.Error(codes.InvalidArgument, "listOptions.continue must >= 0")
}
namespace := ""
minStartedAt := time.Time{}
maxStartedAt := time.Time{}
for _, selector := range strings.Split(options.FieldSelector, ",") {
if len(selector) == 0 {
continue
}
if strings.HasPrefix(selector, "metadata.namespace=") {
namespace = strings.TrimPrefix(selector, "metadata.namespace=")
} else if strings.HasPrefix(selector, "spec.startedAt>") {
minStartedAt, err = time.Parse(time.RFC3339, strings.TrimPrefix(selector, "spec.startedAt>"))
if err != nil {
return nil, err
}
} else if strings.HasPrefix(selector, "spec.startedAt<") {
maxStartedAt, err = time.Parse(time.RFC3339, strings.TrimPrefix(selector, "spec.startedAt<"))
if err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("unsupported requirement %s", selector)
}
}
requirements, err := labels.ParseToRequirements(options.LabelSelector)
if err != nil {
return nil, err
}
allowed, err := auth.CanI(ctx, "list", workflow.WorkflowPlural, namespace, "")
if err != nil {
return nil, err
}
if !allowed {
return nil, status.Error(codes.PermissionDenied, "permission denied")
}
// When the zero value is passed, we should treat this as returning all results
// to align ourselves with the behavior of the `List` endpoints in the Kubernetes API
loadAll := limit == 0
limitWithMore := 0
if !loadAll {
// Attempt to load 1 more record than we actually need as an easy way to determine whether or not more
// records exist than we're currently requesting
limitWithMore = limit + 1
}
items, err := w.wfArchive.ListWorkflows(namespace, minStartedAt, maxStartedAt, requirements, limitWithMore, offset)
if err != nil {
return nil, err
}
meta := metav1.ListMeta{}
if !loadAll && len(items) > limit {
items = items[0:limit]
meta.Continue = fmt.Sprintf("%v", offset+limit)
}
sort.Sort(items)
return &wfv1.WorkflowList{ListMeta: meta, Items: items}, nil
}
func (w *archivedWorkflowServer) GetArchivedWorkflow(ctx context.Context, req *workflowarchivepkg.GetArchivedWorkflowRequest) (*wfv1.Workflow, error) {
wf, err := w.wfArchive.GetWorkflow(req.Uid)
if err != nil {
return nil, err
}
if wf == nil {
return nil, status.Error(codes.NotFound, "not found")
}
allowed, err := auth.CanI(ctx, "get", workflow.WorkflowPlural, wf.Namespace, wf.Name)
if err != nil {
return nil, err
}
if !allowed {
return nil, status.Error(codes.PermissionDenied, "permission denied")
}
return wf, err
}
func (w *archivedWorkflowServer) DeleteArchivedWorkflow(ctx context.Context, req *workflowarchivepkg.DeleteArchivedWorkflowRequest) (*workflowarchivepkg.ArchivedWorkflowDeletedResponse, error) {
wf, err := w.GetArchivedWorkflow(ctx, &workflowarchivepkg.GetArchivedWorkflowRequest{Uid: req.Uid})
if err != nil {
return nil, err
}
allowed, err := auth.CanI(ctx, "delete", workflow.WorkflowPlural, wf.Namespace, wf.Name)
if err != nil {
return nil, err
}
if !allowed {
return nil, status.Error(codes.PermissionDenied, "permission denied")
}
err = w.wfArchive.DeleteWorkflow(req.Uid)
if err != nil {
return nil, err
}
return &workflowarchivepkg.ArchivedWorkflowDeletedResponse{}, nil
}