-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipelineexecution.go
190 lines (163 loc) · 6.1 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
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
package pipelineexecution
import (
"context"
"fmt"
"github.com/rancher/rancher/pkg/controllers/user/pipeline/engine"
"github.com/rancher/rancher/pkg/controllers/user/pipeline/remote"
"github.com/rancher/rancher/pkg/controllers/user/pipeline/utils"
"github.com/rancher/rancher/pkg/ref"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
apierrors "k8s.io/apimachinery/pkg/api/errors"
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
clusterPipelineLister v3.ClusterPipelineLister
sourceCodeCredentialLister v3.SourceCodeCredentialLister
}
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()
sourceCodeCredentialLister := cluster.Management.Management.SourceCodeCredentials("").Controller().Lister()
pipelineEngine := engine.New(cluster)
pipelineExecutionLifecycle := &Lifecycle{
pipelineExecutionLogs: pipelineExecutionLogs,
pipelineEngine: pipelineEngine,
clusterPipelineLister: clusterPipelineLister,
sourceCodeCredentialLister: sourceCodeCredentialLister,
}
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 {
if apierrors.IsAlreadyExists(err) {
return obj, 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
v3.PipelineExecutionConditonProvisioned.False(obj)
v3.PipelineExecutionConditonProvisioned.ReasonAndMessageFromError(obj, err)
return obj, nil
}
//fetch commit info before running the pipeline execution
if obj.Status.Commit == "" {
commit, err := l.getHeadCommit(obj)
if err != nil {
return obj, err
}
obj.Status.Commit = commit
}
if err := l.pipelineEngine.RunPipelineExecution(obj, obj.Spec.TriggeredBy); err != nil {
logrus.Errorf("Error run pipeline - %v", err)
obj.Status.ExecutionState = utils.StateFail
v3.PipelineExecutionConditonProvisioned.False(obj)
v3.PipelineExecutionConditonProvisioned.ReasonAndMessageFromError(obj, err)
return obj, nil
}
v3.PipelineExecutionConditonProvisioned.CreateUnknownIfNotExists(obj)
v3.PipelineExecutionConditonProvisioned.Message(obj, "Assigning jobs to pipeline engine")
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
}
func (l *Lifecycle) getHeadCommit(execution *v3.PipelineExecution) (string, error) {
pipeline := execution.Spec.Pipeline
if err := utils.ValidPipelineSpec(pipeline.Spec); err != nil {
return "", err
}
sourceCodeConfig := pipeline.Spec.Stages[0].Steps[0].SourceCodeConfig
clusterName, _ := ref.Parse(pipeline.ProjectName)
clusterPipeline, err := l.clusterPipelineLister.Get(clusterName, clusterName)
if err != nil {
return "", err
}
sourceCodeType := ""
if clusterPipeline.Spec.GithubConfig != nil {
sourceCodeType = "github"
}
sourceCodeCredentialID := sourceCodeConfig.SourceCodeCredentialName
url := sourceCodeConfig.URL
branch := sourceCodeConfig.Branch
ns, name := ref.Parse(sourceCodeCredentialID)
var credential *v3.SourceCodeCredential
if sourceCodeCredentialID != "" {
credential, err = l.sourceCodeCredentialLister.Get(ns, name)
if err != nil {
return "", err
}
}
client, err := remote.New(*clusterPipeline, sourceCodeType)
if err != nil {
return "", err
}
return client.GetHeadCommit(url, branch, credential)
}