forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statesyncer.go
159 lines (142 loc) · 5.02 KB
/
statesyncer.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
package pipelineexecution
import (
"context"
"github.com/rancher/norman/controller"
"github.com/rancher/rancher/pkg/pipeline/engine"
"github.com/rancher/rancher/pkg/pipeline/utils"
"github.com/rancher/rancher/pkg/ref"
"github.com/rancher/rancher/pkg/ticker"
"github.com/rancher/types/apis/project.cattle.io/v3"
"github.com/sirupsen/logrus"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"time"
)
// This controller is responsible for updating pipeline execution states
// by syncing with the pipeline engine. It also detect executors' status
// and do the actual run pipeline when they are ready
const (
syncStateInterval = 5 * time.Second
)
type ExecutionStateSyncer struct {
clusterName string
pipelineLister v3.PipelineLister
pipelines v3.PipelineInterface
pipelineExecutionLister v3.PipelineExecutionLister
pipelineExecutions v3.PipelineExecutionInterface
pipelineEngine engine.PipelineEngine
}
func (s *ExecutionStateSyncer) sync(ctx context.Context, syncInterval time.Duration) {
for range ticker.Context(ctx, syncInterval) {
s.syncState()
}
}
func (s *ExecutionStateSyncer) syncState() {
set := labels.Set(map[string]string{utils.PipelineFinishLabel: "false"})
allExecutions, err := s.pipelineExecutionLister.List("", set.AsSelector())
if err != nil {
logrus.Errorf("Error listing PipelineExecutions - %v", err)
return
}
executions := []*v3.PipelineExecution{}
for _, e := range allExecutions {
if controller.ObjectInCluster(s.clusterName, e) {
executions = append(executions, e)
}
}
if len(executions) < 1 {
return
}
for _, execution := range executions {
if v3.PipelineExecutionConditionInitialized.IsUnknown(execution) {
s.checkAndRun(execution)
} else if v3.PipelineExecutionConditionInitialized.IsTrue(execution) {
e := execution.DeepCopy()
updated, err := s.pipelineEngine.SyncExecution(e)
if err != nil {
logrus.Errorf("got error in syncExecution: %v", err)
v3.PipelineExecutionConditionBuilt.False(e)
v3.PipelineExecutionConditionBuilt.ReasonAndMessageFromError(e, err)
e.Status.ExecutionState = utils.StateFailed
updated = true
}
if updated {
if err := s.updateExecutionAndLastRunState(e); err != nil {
logrus.Error(err)
continue
}
}
} else {
if err := s.updateExecutionAndLastRunState(execution); err != nil {
logrus.Errorf("Error update pipeline execution - %v", err)
}
}
}
logrus.Debugf("Sync pipeline execution state complete")
}
func (s *ExecutionStateSyncer) checkAndRun(execution *v3.PipelineExecution) {
ready, err := s.pipelineEngine.PreCheck(execution)
if err != nil {
e := execution.DeepCopy()
v3.PipelineExecutionConditionBuilt.False(e)
v3.PipelineExecutionConditionBuilt.ReasonAndMessageFromError(e, err)
e.Status.ExecutionState = utils.StateFailed
if err := s.updateExecutionAndLastRunState(e); err != nil {
logrus.Error(err)
}
}
if ready {
e := execution.DeepCopy()
if err := s.pipelineEngine.RunPipelineExecution(e); err != nil {
v3.PipelineExecutionConditionProvisioned.False(e)
v3.PipelineExecutionConditionProvisioned.ReasonAndMessageFromError(e, err)
e.Status.ExecutionState = utils.StateFailed
if err := s.updateExecutionAndLastRunState(e); err != nil {
logrus.Error(err)
}
return
}
v3.PipelineExecutionConditionInitialized.True(e)
v3.PipelineExecutionConditionProvisioned.CreateUnknownIfNotExists(e)
v3.PipelineExecutionConditionProvisioned.Message(e, "Assigning jobs to pipeline engine")
if err := s.updateExecutionAndLastRunState(e); err != nil {
logrus.Error(err)
}
}
if v3.PipelineExecutionConditionInitialized.GetMessage(execution) == "" {
e := execution.DeepCopy()
v3.PipelineExecutionConditionInitialized.Message(e, "Setting up jenkins. If it is not deployed, this can take a few minutes.")
if err := s.updateExecutionAndLastRunState(e); err != nil {
logrus.Error(err)
}
}
}
func (s *ExecutionStateSyncer) updateExecutionAndLastRunState(execution *v3.PipelineExecution) error {
if v3.PipelineExecutionConditionInitialized.IsFalse(execution) || v3.PipelineExecutionConditionProvisioned.IsFalse(execution) ||
v3.PipelineExecutionConditionBuilt.IsFalse(execution) {
execution.Labels[utils.PipelineFinishLabel] = "true"
if execution.Status.Ended == "" {
execution.Status.Ended = time.Now().Format(time.RFC3339)
}
}
if _, err := s.pipelineExecutions.Update(execution); err != nil {
return err
}
//check and update lastrunstate of the pipeline when necessary
ns, name := ref.Parse(execution.Spec.PipelineName)
p, err := s.pipelineLister.Get(ns, name)
if apierrors.IsNotFound(err) {
logrus.Warningf("pipeline of execution '%s' is not found", execution.Name)
return nil
} else if err != nil {
return err
}
if p.Status.LastExecutionID == execution.Namespace+":"+execution.Name &&
p.Status.LastRunState != execution.Status.ExecutionState {
p.Status.LastRunState = execution.Status.ExecutionState
if _, err := s.pipelines.Update(p); err != nil {
return err
}
}
return nil
}