forked from argoproj/argo-workflows
/
workflow_archive.go
239 lines (220 loc) · 6.63 KB
/
workflow_archive.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
package sqldb
import (
"context"
"encoding/json"
"fmt"
"time"
log "github.com/sirupsen/logrus"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"upper.io/db.v3"
"upper.io/db.v3/lib/sqlbuilder"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo/util/instanceid"
)
const archiveTableName = "argo_archived_workflows"
const archiveLabelsTableName = archiveTableName + "_labels"
type archivedWorkflowMetadata struct {
ClusterName string `db:"clustername"`
InstanceID string `db:"instanceid"`
UID string `db:"uid"`
Name string `db:"name"`
Namespace string `db:"namespace"`
Phase wfv1.NodePhase `db:"phase"`
StartedAt time.Time `db:"startedat"`
FinishedAt time.Time `db:"finishedat"`
}
type archivedWorkflowRecord struct {
archivedWorkflowMetadata
Workflow string `db:"workflow"`
}
type archivedWorkflowLabelRecord struct {
ClusterName string `db:"clustername"`
UID string `db:"uid"`
// Why is this called "name" not "key"? Key is an SQL reserved word.
Key string `db:"name"`
Value string `db:"value"`
}
type WorkflowArchive interface {
ArchiveWorkflow(wf *wfv1.Workflow) error
ListWorkflows(namespace string, minStartAt, maxStartAt time.Time, labelRequirements labels.Requirements, limit, offset int) (wfv1.Workflows, error)
GetWorkflow(uid string) (*wfv1.Workflow, error)
DeleteWorkflow(uid string) error
DeleteExpiredWorkflows(ttl time.Duration) error
}
type workflowArchive struct {
session sqlbuilder.Database
clusterName string
managedNamespace string
instanceIDService instanceid.Service
dbType dbType
}
// NewWorkflowArchive returns a new workflowArchive
func NewWorkflowArchive(session sqlbuilder.Database, clusterName, managedNamespace string, instanceIDService instanceid.Service) WorkflowArchive {
return &workflowArchive{session: session, clusterName: clusterName, managedNamespace: managedNamespace, instanceIDService: instanceIDService, dbType: dbTypeFor(session)}
}
func (r *workflowArchive) ArchiveWorkflow(wf *wfv1.Workflow) error {
logCtx := log.WithFields(log.Fields{"uid": wf.UID, "labels": wf.GetLabels()})
logCtx.Debug("Archiving workflow")
workflow, err := json.Marshal(wf)
if err != nil {
return err
}
return r.session.Tx(context.Background(), func(sess sqlbuilder.Tx) error {
_, err := sess.
DeleteFrom(archiveTableName).
Where(r.clusterManagedNamespaceAndInstanceID()).
And(db.Cond{"uid": wf.UID}).
Exec()
if err != nil {
return err
}
_, err = sess.Collection(archiveTableName).
Insert(&archivedWorkflowRecord{
archivedWorkflowMetadata: archivedWorkflowMetadata{
ClusterName: r.clusterName,
InstanceID: r.instanceIDService.InstanceID(),
UID: string(wf.UID),
Name: wf.Name,
Namespace: wf.Namespace,
Phase: wf.Status.Phase,
StartedAt: wf.Status.StartedAt.Time,
FinishedAt: wf.Status.FinishedAt.Time,
},
Workflow: string(workflow),
})
if err != nil {
return err
}
// insert the labels
for key, value := range wf.GetLabels() {
_, err := sess.Collection(archiveLabelsTableName).
Insert(&archivedWorkflowLabelRecord{
ClusterName: r.clusterName,
UID: string(wf.UID),
Key: key,
Value: value,
})
if err != nil {
return err
}
}
return nil
})
}
func (r *workflowArchive) ListWorkflows(namespace string, minStartedAt, maxStartedAt time.Time, labelRequirements labels.Requirements, limit int, offset int) (wfv1.Workflows, error) {
var archivedWfs []archivedWorkflowMetadata
clause, err := labelsClause(r.dbType, labelRequirements)
if err != nil {
return nil, err
}
err = r.session.
Select("name", "namespace", "uid", "phase", "startedat", "finishedat").
From(archiveTableName).
Where(r.clusterManagedNamespaceAndInstanceID()).
And(namespaceEqual(namespace)).
And(startedAtClause(minStartedAt, maxStartedAt)).
And(clause).
OrderBy("-startedat").
Limit(limit).
Offset(offset).
All(&archivedWfs)
if err != nil {
return nil, err
}
wfs := make(wfv1.Workflows, len(archivedWfs))
for i, md := range archivedWfs {
wfs[i] = wfv1.Workflow{
ObjectMeta: v1.ObjectMeta{
Name: md.Name,
Namespace: md.Namespace,
UID: types.UID(md.UID),
CreationTimestamp: v1.Time{Time: md.StartedAt},
},
Status: wfv1.WorkflowStatus{
Phase: md.Phase,
StartedAt: v1.Time{Time: md.StartedAt},
FinishedAt: v1.Time{Time: md.FinishedAt},
},
}
}
return wfs, nil
}
func (r *workflowArchive) clusterManagedNamespaceAndInstanceID() db.Compound {
return db.And(
db.Cond{"clustername": r.clusterName},
namespaceEqual(r.managedNamespace),
db.Cond{"instanceid": r.instanceIDService.InstanceID()},
)
}
func startedAtClause(from, to time.Time) db.Compound {
var conds []db.Compound
if !from.IsZero() {
conds = append(conds, db.Cond{"startedat > ": from})
}
if !to.IsZero() {
conds = append(conds, db.Cond{"startedat < ": to})
}
return db.And(conds...)
}
func namespaceEqual(namespace string) db.Cond {
if namespace == "" {
return db.Cond{}
} else {
return db.Cond{"namespace": namespace}
}
}
func (r *workflowArchive) GetWorkflow(uid string) (*wfv1.Workflow, error) {
archivedWf := &archivedWorkflowRecord{}
err := r.session.
Select("workflow").
From(archiveTableName).
Where(r.clusterManagedNamespaceAndInstanceID()).
And(db.Cond{"uid": uid}).
One(archivedWf)
if err != nil {
if err == db.ErrNoMoreRows {
return nil, nil
}
return nil, err
}
var wf *wfv1.Workflow
err = json.Unmarshal([]byte(archivedWf.Workflow), &wf)
if err != nil {
return nil, err
}
return wf, nil
}
func (r *workflowArchive) DeleteWorkflow(uid string) error {
rs, err := r.session.
DeleteFrom(archiveTableName).
Where(r.clusterManagedNamespaceAndInstanceID()).
And(db.Cond{"uid": uid}).
Exec()
if err != nil {
return err
}
rowsAffected, err := rs.RowsAffected()
if err != nil {
return err
}
log.WithFields(log.Fields{"uid": uid, "rowsAffected": rowsAffected}).Debug("Deleted archived workflow")
return nil
}
func (r *workflowArchive) DeleteExpiredWorkflows(ttl time.Duration) error {
rs, err := r.session.
DeleteFrom(archiveTableName).
Where(r.clusterManagedNamespaceAndInstanceID()).
And(fmt.Sprintf("finishedat < current_timestamp - interval '%d' second", int(ttl.Seconds()))).
Exec()
if err != nil {
return err
}
rowsAffected, err := rs.RowsAffected()
if err != nil {
return err
}
log.WithFields(log.Fields{"rowsAffected": rowsAffected}).Info("Deleted archived workflows")
return nil
}