-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.go
259 lines (218 loc) · 7.74 KB
/
helpers.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
package cluster
import (
"errors"
"fmt"
"strings"
"time"
"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/golang/protobuf/ptypes"
v1 "github.com/stackrox/infra/generated/api/v1"
"github.com/stackrox/infra/slack"
)
func getClusterIDFromWorkflow(workflow *v1alpha1.Workflow) string {
clusterID := GetClusterID(workflow)
if clusterID == "" {
// Prior workflows used a direct mapping from Argo workflow name to Infra cluster ID
clusterID = workflow.GetName()
}
return clusterID
}
// clusterFromWorkflow converts an Argo workflow into an infra cluster.
func clusterFromWorkflow(workflow v1alpha1.Workflow) *v1.Cluster {
cluster := &v1.Cluster{
ID: getClusterIDFromWorkflow(&workflow),
Status: workflowStatus(workflow.Status),
Flavor: GetFlavor(&workflow),
Owner: GetOwner(&workflow),
Lifespan: GetLifespan(&workflow),
Description: GetDescription(&workflow),
}
cluster.CreatedOn, _ = ptypes.TimestampProto(workflow.Status.StartedAt.Time.UTC())
if !workflow.Status.FinishedAt.Time.IsZero() {
cluster.DestroyedOn, _ = ptypes.TimestampProto(workflow.Status.FinishedAt.Time.UTC())
}
return cluster
}
func isWorkflowExpired(workflow v1alpha1.Workflow) bool {
lifespan, _ := ptypes.Duration(GetLifespan(&workflow))
workflowExpiryTime := workflow.Status.StartedAt.Time.Add(lifespan)
return time.Now().After(workflowExpiryTime)
}
func isNearingExpiry(workflow v1alpha1.Workflow) bool {
lifespan, _ := ptypes.Duration(GetLifespan(&workflow))
workflowExpiryTime := workflow.Status.StartedAt.Time.Add(lifespan)
return time.Now().Add(nearExpiry).After(workflowExpiryTime)
}
type metaCluster struct {
v1.Cluster
EventID string
Expired bool
NearingExpiry bool
Slack slack.Status
SlackDM bool
}
type artifactData struct {
Name string
Description string
Tags map[string]struct{}
Data []byte
}
// metaClusterFromWorkflow() converts an Argo workflow into an infra cluster
// with additional, non-cluster, metadata.
func (s *clusterImpl) metaClusterFromWorkflow(workflow v1alpha1.Workflow) (*metaCluster, error) {
cluster := clusterFromWorkflow(workflow)
expired := isWorkflowExpired(workflow)
nearingExpiry := isNearingExpiry(workflow)
cluster, err := s.getClusterDetailsFromArtifacts(cluster, workflow)
if err != nil {
return nil, err
}
return &metaCluster{
Cluster: *cluster,
Slack: slack.Status(GetSlack(&workflow)),
SlackDM: GetSlackDM(&workflow),
Expired: expired,
NearingExpiry: nearingExpiry,
EventID: GetEventID(&workflow),
}, nil
}
// getClusterDetailsFromArtifacts - get those cluster details that are stored by workflow artifacts.
func (s *clusterImpl) getClusterDetailsFromArtifacts(cluster *v1.Cluster, workflow v1alpha1.Workflow) (*v1.Cluster, error) {
flavorMetadata := make(map[string]*v1.FlavorArtifact)
flavor, _, found := s.registry.Get(cluster.Flavor)
if found && flavor.Artifacts != nil {
flavorMetadata = flavor.Artifacts
}
for _, nodeStatus := range workflow.Status.Nodes {
if nodeStatus.Outputs == nil {
continue
}
for _, artifact := range nodeStatus.Outputs.Artifacts {
if artifact.GCS == nil {
continue
}
// The only artifact of concern are those explicity defined in
// flavors.yaml artifacts section.
if meta, found := flavorMetadata[artifact.Name]; found {
// And only artifacts that are tagged with url or connect.
if _, foundURL := meta.Tags[artifactTagURL]; !foundURL {
if _, foundConnect := meta.Tags[artifactTagConnect]; !foundConnect {
continue
}
}
bucket, key := handleArtifactMigration(workflow, artifact)
if bucket == "" || key == "" {
continue
}
contents, err := s.signer.Contents(bucket, key)
if err != nil {
return nil, err
}
if _, found := meta.Tags[artifactTagURL]; found {
cluster.URL = strings.TrimSpace(string(contents))
}
if _, found := meta.Tags[artifactTagConnect]; found {
cluster.Connect = string(contents)
}
}
}
}
return cluster, nil
}
func handleArtifactMigration(workflow v1alpha1.Workflow, artifact v1alpha1.Artifact) (string, string) {
var bucket string
var key string
if workflow.Status.ArtifactRepositoryRef != nil &&
workflow.Status.ArtifactRepositoryRef.ArtifactRepository.GCS != nil &&
workflow.Status.ArtifactRepositoryRef.ArtifactRepository.GCS.Bucket != "" {
bucket = workflow.Status.ArtifactRepositoryRef.ArtifactRepository.GCS.Bucket
} else if artifact.GCS != nil && artifact.GCS.Bucket != "" {
bucket = artifact.GCS.Bucket
}
if artifact.GCS != nil && artifact.GCS.Key != "" {
key = artifact.GCS.Key
}
if bucket == "" || key == "" {
log.Warnw("cannot figure out bucket for artifact, possibly an upgrade issue, not fatal",
"workflow-name", workflow.Name,
"artifact", artifact,
"artifact-repository", workflow.Status.ArtifactRepositoryRef,
)
return "", ""
}
return bucket, key
}
func workflowStatus(workflowStatus v1alpha1.WorkflowStatus) v1.Status {
// https://godoc.org/github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1#WorkflowStatus
switch workflowStatus.Phase {
case v1alpha1.WorkflowFailed, v1alpha1.WorkflowError:
return v1.Status_FAILED
case v1alpha1.WorkflowSucceeded:
return v1.Status_FINISHED
case v1alpha1.WorkflowPending:
return v1.Status_CREATING
case v1alpha1.WorkflowRunning:
// https://godoc.org/github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1#Nodes
for _, node := range workflowStatus.Nodes {
// https://godoc.org/github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1#NodeType
if node.Type == v1alpha1.NodeTypePod {
if strings.Contains(node.Message, "ImagePullBackOff") {
return v1.Status_FAILED
}
if strings.Contains(node.Message, "ErrImagePull") {
return v1.Status_FAILED
}
if strings.Contains(node.Message, "Pod was active on the node longer than the specified deadline") {
return v1.Status_FAILED
}
} else if node.Type == v1alpha1.NodeTypeSuspend {
switch node.Phase {
case v1alpha1.NodeError, v1alpha1.NodeFailed, v1alpha1.NodeSkipped:
panic("a suspend should not be able to fail?")
case v1alpha1.NodeRunning, v1alpha1.NodePending:
return v1.Status_READY
}
}
if node.GetName() == "destroy" || node.IsExitNode() {
return v1.Status_DESTROYING
}
if node.GetName() == "create" {
switch node.Phase {
case v1alpha1.NodeError, v1alpha1.NodeFailed, v1alpha1.NodeSkipped:
return v1.Status_FAILED
case v1alpha1.NodeRunning, v1alpha1.NodePending:
return v1.Status_CREATING
}
}
}
// If no "create" or "destroy"/onExit node active, then we're ready.
return v1.Status_READY
case "":
return v1.Status_CREATING
}
panic("unknown situation")
}
// Returns an error with details of an aberrant condition if detected, nil otherwise.
// Intended to provide failure details to a user via slack post.
func workflowFailureDetails(workflowStatus v1alpha1.WorkflowStatus) error {
switch workflowStatus.Phase {
case v1alpha1.WorkflowRunning, v1alpha1.WorkflowFailed:
for _, node := range workflowStatus.Nodes {
if node.Type == v1alpha1.NodeTypePod {
if strings.Contains(node.Message, "ImagePullBackOff") {
msg := fmt.Sprintf("Workflow node `%s` has encountered an image pull back-off.", node.Name)
return errors.New(msg)
}
if strings.Contains(node.Message, "ErrImagePull") {
msg := fmt.Sprintf("Workflow node `%s` has encountered an image pull error.", node.Name)
return errors.New(msg)
}
if strings.Contains(node.Message, "Pod was active on the node longer than the specified deadline") {
msg := fmt.Sprintf("Workflow node `%s` has timed out.", node.Name)
return errors.New(msg)
}
}
}
}
return errors.New("")
}