forked from argoproj/argo-workflows
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archived_workflow_server.go
293 lines (255 loc) · 9.23 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package workflowarchive
import (
"context"
"fmt"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
apierr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"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"
"github.com/argoproj/argo-workflows/v3/workflow/util"
)
const disableValueListRetrievalKeyPattern = "DISABLE_VALUE_LIST_RETRIEVAL_KEY_PATTERN"
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
namePrefix := req.NamePrefix
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 := ""
name := ""
minStartedAt := time.Time{}
maxStartedAt := time.Time{}
showRemainingItemCount := false
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, "metadata.name=") {
name = strings.TrimPrefix(selector, "metadata.name=")
} 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 if strings.HasPrefix(selector, "ext.showRemainingItemCount") {
showRemainingItemCount, err = strconv.ParseBool(strings.TrimPrefix(selector, "ext.showRemainingItemCount="))
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, fmt.Sprintf("Permission denied, you are not allowed to list workflows in namespace \"%s\". Maybe you want to specify a namespace with `listOptions.fieldSelector=metadata.namespace=your-ns`?", namespace))
}
// 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, name, namePrefix, minStartedAt, maxStartedAt, requirements, limitWithMore, offset)
if err != nil {
return nil, err
}
meta := metav1.ListMeta{}
if showRemainingItemCount && !loadAll {
total, err := w.wfArchive.CountWorkflows(namespace, name, namePrefix, minStartedAt, maxStartedAt, requirements)
if err != nil {
return nil, err
}
var count = total - int64(offset) - int64(items.Len())
if len(items) > limit {
count = count + 1
}
if count < 0 {
count = 0
}
meta.RemainingItemCount = &count
}
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
}
func (w *archivedWorkflowServer) ListArchivedWorkflowLabelKeys(ctx context.Context, req *workflowarchivepkg.ListArchivedWorkflowLabelKeysRequest) (*wfv1.LabelKeys, error) {
labelkeys, err := w.wfArchive.ListWorkflowsLabelKeys()
if err != nil {
return nil, err
}
return labelkeys, nil
}
func matchLabelKeyPattern(key string) bool {
pattern, _ := os.LookupEnv(disableValueListRetrievalKeyPattern)
if pattern == "" {
return false
}
match, _ := regexp.MatchString(pattern, key)
return match
}
func (w *archivedWorkflowServer) ListArchivedWorkflowLabelValues(ctx context.Context, req *workflowarchivepkg.ListArchivedWorkflowLabelValuesRequest) (*wfv1.LabelValues, error) {
options := req.ListOptions
requirements, err := labels.ParseToRequirements(options.LabelSelector)
if err != nil {
return nil, err
}
if len(requirements) != 1 {
return nil, fmt.Errorf("only allow 1 labelRequirement, found %v", len(requirements))
}
key := ""
requirement := requirements[0]
if requirement.Operator() == selection.Exists {
key = requirement.Key()
} else {
return nil, fmt.Errorf("operation %v is not supported", requirement.Operator())
}
if matchLabelKeyPattern(key) {
log.WithFields(log.Fields{"labelKey": key}).Info("Skipping retrieving the list of values for label key")
return &wfv1.LabelValues{Items: []string{}}, nil
}
labels, err := w.wfArchive.ListWorkflowsLabelValues(key)
if err != nil {
return nil, err
}
if labels == nil {
return nil, status.Error(codes.NotFound, "not found")
}
return labels, err
}
func (w *archivedWorkflowServer) ResubmitArchivedWorkflow(ctx context.Context, req *workflowarchivepkg.ResubmitArchivedWorkflowRequest) (*wfv1.Workflow, error) {
wfClient := auth.GetWfClient(ctx)
wf, err := w.GetArchivedWorkflow(ctx, &workflowarchivepkg.GetArchivedWorkflowRequest{Uid: req.Uid})
if err != nil {
return nil, err
}
newWF, err := util.FormulateResubmitWorkflow(wf, req.Memoized, nil)
if err != nil {
return nil, err
}
created, err := util.SubmitWorkflow(ctx, wfClient.ArgoprojV1alpha1().Workflows(req.Namespace), wfClient, req.Namespace, newWF, &wfv1.SubmitOpts{})
if err != nil {
return nil, err
}
return created, nil
}
func (w *archivedWorkflowServer) RetryArchivedWorkflow(ctx context.Context, req *workflowarchivepkg.RetryArchivedWorkflowRequest) (*wfv1.Workflow, error) {
wfClient := auth.GetWfClient(ctx)
kubeClient := auth.GetKubeClient(ctx)
wf, err := w.GetArchivedWorkflow(ctx, &workflowarchivepkg.GetArchivedWorkflowRequest{Uid: req.Uid})
if err != nil {
return nil, err
}
_, err = wfClient.ArgoprojV1alpha1().Workflows(req.Namespace).Get(ctx, wf.Name, metav1.GetOptions{})
if apierr.IsNotFound(err) {
wf, podsToDelete, err := util.FormulateRetryWorkflow(ctx, wf, req.RestartSuccessful, req.NodeFieldSelector, nil)
if err != nil {
return nil, err
}
for _, podName := range podsToDelete {
log.WithFields(log.Fields{"podDeleted": podName}).Info("Deleting pod")
err := kubeClient.CoreV1().Pods(wf.Namespace).Delete(ctx, podName, metav1.DeleteOptions{})
if err != nil && !apierr.IsNotFound(err) {
return nil, err
}
}
wf.ObjectMeta.ResourceVersion = ""
wf.ObjectMeta.UID = ""
result, err := wfClient.ArgoprojV1alpha1().Workflows(req.Namespace).Create(ctx, wf, metav1.CreateOptions{})
if err != nil {
return nil, err
}
return result, nil
}
if err == nil {
return nil, status.Error(codes.AlreadyExists, "Workflow already exists on cluster, use argo retry {name} instead")
}
return nil, err
}