-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.go
182 lines (152 loc) · 5.07 KB
/
runner.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
package runner
import (
"errors"
"strconv"
wfv1 "github.com/arunprasadmudaliar/trinity/api/v1"
"github.com/arunprasadmudaliar/trinity/pkg/utils"
batchv1 "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"github.com/sirupsen/logrus"
)
//Run will trigger the executor
func Run(config string, name string, ns string) {
cfg, err := clientcmd.BuildConfigFromFlags("", config)
if err != nil {
logrus.WithError(err).Fatal("Failed to build workflow client configuration")
}
kc, err := wfv1.NewForConfig(cfg)
if err != nil {
logrus.WithError(err).Fatal("Failed to create workflow client for given configuration")
}
workflow, err := kc.WorkFlows(ns).Get(name)
if err != nil {
logrus.Error(err)
}
var runid int
if len(workflow.Status.Runs) == 0 {
runid, _ = initialRun(kc, name, ns, workflow)
} else {
runid, _ = nextRun(kc, name, ns, workflow)
}
deployJob(config, name, ns, workflow, strconv.Itoa(runid))
}
func initialRun(kc *wfv1.WorkFlowClient, name string, namespace string, workflow *wfv1.Workflow) (int, error) {
init := wfv1.WorkflowStatus{
Runs: []wfv1.Workflowruns{
{
ID: 1,
Phase: "Running",
Tasks: []wfv1.TaskStatus{},
StartedAt: utils.Timestamp(),
EndedAt: "",
},
},
}
workflow.Status = init
workflow.Kind = "Workflow"
workflow.APIVersion = "trinity.cloudlego.com/v1"
_, err := kc.WorkFlows(namespace).Put(name, workflow)
if err != nil {
return -1, err
}
logrus.Infof("triggered run %d for workflow %s under namespace %s", 1, name, namespace)
return 0, nil
}
func nextRun(kc *wfv1.WorkFlowClient, name string, namespace string, workflow *wfv1.Workflow) (int, error) {
runid := len(workflow.Status.Runs)
runstatus := wfv1.Workflowruns{
ID: runid + 1,
Phase: "running",
Tasks: []wfv1.TaskStatus{},
StartedAt: utils.Timestamp(),
EndedAt: "",
}
workflow.Status.Runs = append(workflow.Status.Runs, runstatus)
workflow.Kind = "Workflow"
workflow.APIVersion = "trinity.cloudlego.com/v1"
_, err := kc.WorkFlows(namespace).Put(name, workflow)
if err != nil {
return -1, err
}
logrus.Infof("triggered run %d for workflow %s under namespace %s", runid+1, name, namespace)
return runid, nil
}
func deployJob(cfg string, name string, namespace string, workflow *wfv1.Workflow, runid string) {
kc, err := utils.Client(cfg)
if err != nil {
logrus.Error(err)
}
var minio *v1.Pod
var svc *v1.Service
var artifactEnabled bool
var creds wfv1.MinioCreds
//deploy minio to store artifacts
if workflow.Spec.StoreArtifacts {
artifactEnabled = true
creds = wfv1.MinioCreds{
AccessKey: utils.MinioCredential(),
SecretKey: utils.MinioCredential(),
}
minio, svc, err = utils.DeployMinio(kc, name, namespace, creds)
if err != nil {
logrus.WithError(err).Errorf("failed to initialize artifact store")
}
logrus.Info("artifact store is up and running")
}
for taskid, task := range workflow.Spec.Tasks {
//image := getImage((task.Command))
job, err := utils.CreateJob(kc, name, namespace, IMAGE, runid, strconv.Itoa(taskid), creds)
if err != nil {
logrus.Error(err)
}
logrus.Infof("executing task %s for workflow %s", task.Name, name)
//ch, err := utils.WatchJob(kc, name+"-task-"+strconv.Itoa(taskid), namespace)
ch, err := utils.WatchJob(kc, name+"-task-"+strconv.Itoa(taskid), namespace)
if err != nil {
logrus.Error(err)
}
for event := range ch.ResultChan() {
if event.Type == watch.Modified {
object := event.Object.(*batchv1.Job)
if len(object.Status.Conditions) > 0 {
if object.Status.Conditions[0].Type == "Complete" {
logrus.Infof("completed task %s for workflow %s", task.Name, name)
//removeJob(kc, name+"-task-"+strconv.Itoa(taskid), namespace)
removeJob(kc, job)
} else {
newerr := errors.New(object.Status.Conditions[0].Message)
logrus.WithError(newerr).Errorf("failed to execute task %s", name)
//removeJob(kc, name+"-task-"+strconv.Itoa(taskid), namespace)
removeJob(kc, job)
}
break
}
} else if event.Type == watch.Added {
logrus.Infof("waiting for task %s to complete", task.Name)
}
}
}
//Perform cleanup of artifactory storage
if artifactEnabled {
err := utils.DeleteMinio(kc, minio, svc)
if err != nil {
logrus.WithError(err).Errorf("failed to delete artifact store")
}
logrus.Info("artifact store was removed successfully")
}
}
//func removeJob(kc *kubernetes.Clientset, name string, namespace string) {
func removeJob(kc *kubernetes.Clientset, job *batchv1.Job) {
err := utils.DeleteJob(kc, job.ObjectMeta.Name, job.ObjectMeta.Namespace)
if err != nil {
logrus.WithError(err).Errorf("failed to remove job %s.Manual clean up required before next run.", job.ObjectMeta.Name)
}
err = utils.DeleteJobPod(kc, job.ObjectMeta.Name, job.ObjectMeta.Namespace)
if err != nil {
logrus.WithError(err).Errorf("failed to remove pod for job %s.Manual clean up required before next run.", job.ObjectMeta.Name)
}
logrus.Info("jobs and corresponding pods were removed successfully")
}