-
Notifications
You must be signed in to change notification settings - Fork 660
/
workflow.go
108 lines (102 loc) · 3.84 KB
/
workflow.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
package events
import (
"github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo/pkg/client/clientset/versioned"
"github.com/argoproj/argo/pkg/client/informers/externalversions"
litmusV1alpha1 "github.com/litmuschaos/chaos-operator/pkg/client/clientset/versioned/typed/litmuschaos/v1alpha1"
"github.com/litmuschaos/litmus/litmus-portal/backend/workflow-agent/pkg/k8s"
"github.com/litmuschaos/litmus/litmus-portal/backend/workflow-agent/pkg/types"
"github.com/sirupsen/logrus"
"k8s.io/client-go/tools/cache"
"time"
)
// 0 means no resync
const (
resyncPeriod time.Duration = 0
)
// initializes the Argo Workflow event watcher
func WorkflowEventWatcher(stopCh chan struct{}, stream chan types.WorkflowEvent) {
cfg, err := k8s.GetKubeConfig()
if err != nil {
logrus.WithError(err).Fatal("could not get config")
}
// ClientSet to create Informer
clientSet, err := versioned.NewForConfig(cfg)
if err != nil {
logrus.WithError(err).Fatal("could not generate dynamic client for config")
}
// Create a factory object to watch workflows
f := externalversions.NewSharedInformerFactory(clientSet, resyncPeriod)
informer := f.Argoproj().V1alpha1().Workflows().Informer()
// Start Event Watch
go startWatch(stopCh, informer, stream)
}
// handles the different workflow events - add, update and delete
func startWatch(stopCh <-chan struct{}, s cache.SharedIndexInformer, stream chan types.WorkflowEvent) {
startTime := time.Now().Unix()
handlers := cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
workflowEventHandler(obj, "ADD", stream, startTime)
},
UpdateFunc: func(oldObj, obj interface{}) {
workflowEventHandler(obj, "UPDATE", stream, startTime)
},
DeleteFunc: func(obj interface{}) {
workflowEventHandler(obj, "DELETE", stream, startTime)
},
}
s.AddEventHandler(handlers)
s.Run(stopCh)
}
// responsible for extracting the required data from the event and streaming
func workflowEventHandler(obj interface{}, eventType string, stream chan types.WorkflowEvent, startTime int64) {
workflowObj := obj.(*v1alpha1.Workflow)
if workflowObj.ObjectMeta.CreationTimestamp.Unix() < startTime {
return
}
cfg, err := k8s.GetKubeConfig()
if err != nil {
logrus.WithError(err).Fatal("could not get config")
}
chaosClient, err := litmusV1alpha1.NewForConfig(cfg)
if err != nil {
logrus.WithError(err).Fatal("could not get Chaos ClientSet")
}
nodes := make(map[string]types.Node)
logrus.Print("WORKFLOW EVENT ", workflowObj.UID, " ", eventType)
for _, nodeStatus := range workflowObj.Status.Nodes {
nodeType := string(nodeStatus.Type)
var cd *types.ChaosData = nil
// considering chaos workflow has only 1 artifact with manifest as raw data
if nodeStatus.Type == "Pod" && nodeStatus.Inputs != nil && len(nodeStatus.Inputs.Artifacts) == 1 {
//extracts chaos data
nodeType, cd, err = CheckChaosData(nodeStatus, chaosClient)
if err != nil {
logrus.WithError(err).Print("FAILED PARSING CHAOS ENGINE CRD")
}
}
details := types.Node{
Name: nodeStatus.DisplayName,
Phase: string(nodeStatus.Phase),
Type: nodeType,
StartedAt: StrConvTime(nodeStatus.StartedAt.Unix()),
FinishedAt: StrConvTime(nodeStatus.FinishedAt.Unix()),
Children: nodeStatus.Children,
ChaosExp: cd,
}
nodes[nodeStatus.ID] = details
}
workflow := types.WorkflowEvent{
EventType: eventType,
UID: string(workflowObj.ObjectMeta.UID),
Namespace: workflowObj.ObjectMeta.Namespace,
Name: workflowObj.ObjectMeta.Name,
CreationTimestamp: StrConvTime(workflowObj.ObjectMeta.CreationTimestamp.Unix()),
Phase: string(workflowObj.Status.Phase),
StartedAt: StrConvTime(workflowObj.Status.StartedAt.Unix()),
FinishedAt: StrConvTime(workflowObj.Status.FinishedAt.Unix()),
Nodes: nodes,
}
//stream
stream <- workflow
}