forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipelineexecution.go
123 lines (106 loc) · 3.9 KB
/
pipelineexecution.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
package pipelineexecution
import (
"context"
"fmt"
"github.com/rancher/rancher/pkg/controllers/user/pipeline/engine"
"github.com/rancher/rancher/pkg/controllers/user/pipeline/utils"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
//Lifecycle is responsible for initializing logs for pipeline execution
//and calling the run for the execution.
type Lifecycle struct {
pipelineExecutionLogs v3.PipelineExecutionLogInterface
pipelineEngine engine.PipelineEngine
}
func Register(ctx context.Context, cluster *config.UserContext) {
clusterName := cluster.ClusterName
clusterPipelineLister := cluster.Management.Management.ClusterPipelines("").Controller().Lister()
pipelines := cluster.Management.Management.Pipelines("")
pipelineLister := pipelines.Controller().Lister()
pipelineExecutions := cluster.Management.Management.PipelineExecutions("")
pipelineExecutionLister := pipelineExecutions.Controller().Lister()
pipelineExecutionLogs := cluster.Management.Management.PipelineExecutionLogs("")
pipelineExecutionLogLister := pipelineExecutionLogs.Controller().Lister()
pipelineEngine := engine.New(cluster)
pipelineExecutionLifecycle := &Lifecycle{
pipelineExecutionLogs: pipelineExecutionLogs,
pipelineEngine: pipelineEngine,
}
stateSyncer := &ExecutionStateSyncer{
clusterName: clusterName,
clusterPipelineLister: clusterPipelineLister,
pipelineLister: pipelineLister,
pipelines: pipelines,
pipelineExecutionLister: pipelineExecutionLister,
pipelineExecutions: pipelineExecutions,
pipelineEngine: pipelineEngine,
}
logSyncer := &ExecutionLogSyncer{
clusterName: clusterName,
clusterPipelineLister: clusterPipelineLister,
pipelineExecutionLister: pipelineExecutionLister,
pipelineExecutionLogLister: pipelineExecutionLogLister,
pipelineExecutionLogs: pipelineExecutionLogs,
pipelineEngine: pipelineEngine,
}
pipelineExecutions.AddClusterScopedLifecycle(pipelineExecutionLifecycle.GetName(), cluster.ClusterName, pipelineExecutionLifecycle)
go stateSyncer.sync(ctx, syncStateInterval)
go logSyncer.sync(ctx, syncLogInterval)
}
func (l *Lifecycle) Create(obj *v3.PipelineExecution) (*v3.PipelineExecution, error) {
if obj.Status.ExecutionState != utils.StateWaiting {
return obj, nil
}
if err := l.initLogs(obj); err != nil {
return obj, err
}
obj.Status.ExecutionState = utils.StateBuilding
if err := l.pipelineEngine.PreCheck(); err != nil {
logrus.Errorf("Error get Jenkins engine - %v", err)
obj.Status.ExecutionState = utils.StateFail
return obj, nil
}
if err := l.pipelineEngine.RunPipeline(&obj.Spec.Pipeline, obj.Spec.TriggeredBy); err != nil {
logrus.Errorf("Error run pipeline - %v", err)
obj.Status.ExecutionState = utils.StateFail
return obj, nil
}
return obj, nil
}
func (l *Lifecycle) Updated(obj *v3.PipelineExecution) (*v3.PipelineExecution, error) {
return obj, nil
}
func (l *Lifecycle) Remove(obj *v3.PipelineExecution) (*v3.PipelineExecution, error) {
return obj, nil
}
func (l *Lifecycle) GetName() string {
return "pipeline-execution-controller"
}
func (l *Lifecycle) initLogs(obj *v3.PipelineExecution) error {
pipeline := obj.Spec.Pipeline
//create log entries
for j, stage := range pipeline.Spec.Stages {
for k := range stage.Steps {
log := &v3.PipelineExecutionLog{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-%d-%d", obj.Name, j, k),
Namespace: obj.Namespace,
Labels: map[string]string{utils.PipelineFinishLabel: "false"},
},
ProjectName: pipeline.ProjectName,
Spec: v3.PipelineExecutionLogSpec{
PipelineExecutionName: obj.Namespace + ":" + obj.Name,
Stage: j,
Step: k,
},
}
if _, err := l.pipelineExecutionLogs.Create(log); err != nil {
return err
}
}
}
return nil
}