forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller_build.go
332 lines (305 loc) · 8.06 KB
/
controller_build.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package cmd
import (
"io"
"reflect"
"strings"
"time"
"unicode"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/builds"
"github.com/jenkins-x/jx/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/tools/cache"
"github.com/jenkins-x/jx/pkg/kube"
)
// ControllerBuildOptions are the flags for the commands
type ControllerBuildOptions struct {
ControllerOptions
Namespace string
}
// NewCmdControllerBuild creates a command object for the generic "get" action, which
// retrieves one or more resources from a server.
func NewCmdControllerBuild(f Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &ControllerBuildOptions{
ControllerOptions: ControllerOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "build",
Short: "Runs the build controller",
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
Aliases: []string{"builds"},
}
cmd.Flags().StringVarP(&options.Namespace, "namespace", "n", "", "The namespace to watch or defaults to the current namespace")
return cmd
}
// Run implements this command
func (o *ControllerBuildOptions) Run() error {
apisClient, err := o.CreateApiExtensionsClient()
if err != nil {
return err
}
err = kube.RegisterPipelineActivityCRD(apisClient)
if err != nil {
return err
}
jxClient, devNs, err := o.JXClientAndDevNamespace()
if err != nil {
return err
}
client, _, err := o.KubeClient()
if err != nil {
return err
}
ns := o.Namespace
if ns == "" {
ns = devNs
}
pod := &corev1.Pod{}
log.Infof("Watching for knative build pods in namespace %s\n", util.ColorInfo(ns))
listWatch := cache.NewListWatchFromClient(client.CoreV1().RESTClient(), "pods", ns, fields.Everything())
kube.SortListWatchByName(listWatch)
_, controller := cache.NewInformer(
listWatch,
pod,
time.Minute*10,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
o.onPod(obj, jxClient, ns)
},
UpdateFunc: func(oldObj, newObj interface{}) {
o.onPod(newObj, jxClient, ns)
},
DeleteFunc: func(obj interface{}) {
},
},
)
stop := make(chan struct{})
go controller.Run(stop)
// Wait forever
select {}
}
func (o *ControllerBuildOptions) onPod(obj interface{}, jxClient versioned.Interface, ns string) {
pod, ok := obj.(*corev1.Pod)
if !ok {
log.Infof("Object is not a Pod %#v\n", obj)
return
}
if pod != nil {
labels := pod.Labels
if labels != nil {
buildName := labels[builds.LabelBuildName]
if buildName != "" {
log.Infof("Found build pod %s\n", pod.Name)
activities := jxClient.JenkinsV1().PipelineActivities(ns)
key := o.createPromoteStepActivityKey(buildName, pod)
if key != nil {
a, created, err := key.GetOrCreate(activities)
if err != nil {
operation := "update"
if created {
operation = "create"
}
log.Warnf("Failed to %s PipelineActivities for build %s: %s\n", operation, buildName, err)
}
if o.updatePipelineActivity(a, buildName, pod) {
_, err := activities.Update(a)
if err != nil {
log.Warnf("Failed to update PipelineActivities%s: %s\n", a.Name, err)
}
}
}
}
}
}
}
// createPromoteStepActivityKey deduces the pipeline metadata from the knative build pod
func (o *ControllerBuildOptions) createPromoteStepActivityKey(buildName string, pod *corev1.Pod) *kube.PromoteStepActivityKey {
branch := ""
lastCommitSha := ""
lastCommitMessage := ""
lastCommitURL := ""
build := DigitSuffix(buildName)
if build == "" {
build = "1"
}
gitURL := ""
for _, initContainer := range pod.Spec.InitContainers {
if initContainer.Name == "build-step-git-source" {
args := initContainer.Args
for i := 0; i <= len(args)-2; i += 2 {
key := args[i]
value := args[i+1]
switch key {
case "-url":
gitURL = value
case "-revision":
branch = value
}
}
break
}
}
if gitURL == "" {
return nil
}
if branch == "" {
branch = "master"
}
gitInfo, err := gits.ParseGitURL(gitURL)
if err != nil {
log.Warnf("Failed to parse git URL %s: %s", gitURL, err)
return nil
}
org := gitInfo.Organisation
repo := gitInfo.Name
name := org + "-" + repo + "-" + branch + "-" + build
pipeline := org + "/" + repo + "/" + branch
return &kube.PromoteStepActivityKey{
PipelineActivityKey: kube.PipelineActivityKey{
Name: name,
Pipeline: pipeline,
Build: build,
LastCommitSHA: lastCommitSha,
LastCommitMessage: lastCommitMessage,
LastCommitURL: lastCommitURL,
GitInfo: gitInfo,
},
}
}
func (o *ControllerBuildOptions) updatePipelineActivity(activity *v1.PipelineActivity, s string, pod *corev1.Pod) bool {
copy := *activity
// TODO update the steps based on the knative build pod's init containers
for _, c := range pod.Status.InitContainerStatuses {
name := strings.Replace(strings.TrimPrefix(c.Name, "build-step-"), "-", " ", -1)
title := strings.Title(name)
_, stage, _ := kube.GetOrCreateStage(activity, title)
running := c.State.Running
terminated := c.State.Terminated
var startedAt metav1.Time
var finishedAt metav1.Time
if running != nil {
startedAt = running.StartedAt
} else if terminated != nil {
startedAt = terminated.StartedAt
finishedAt = terminated.FinishedAt
if !finishedAt.IsZero() {
stage.CompletedTimestamp = &finishedAt
}
}
if !startedAt.IsZero() {
stage.StartedTimestamp = &startedAt
}
stage.Description = createStepDescription(c.Name, pod)
if terminated != nil {
if terminated.ExitCode == 0 {
stage.Status = v1.ActivityStatusTypeSucceeded
} else {
stage.Status = v1.ActivityStatusTypeFailed
}
} else {
if running != nil {
stage.Status = v1.ActivityStatusTypeRunning
} else {
stage.Status = v1.ActivityStatusTypePending
}
}
}
spec := &activity.Spec
var biggestFinishedAt metav1.Time
allCompleted := true
failed := false
running := true
for _, step := range spec.Steps {
stage := step.Stage
if stage != nil {
stageFinished := false
if stage.StartedTimestamp != nil && spec.StartedTimestamp == nil {
spec.StartedTimestamp = stage.StartedTimestamp
}
if stage.CompletedTimestamp != nil {
t := stage.CompletedTimestamp
if !t.IsZero() {
stageFinished = true
if biggestFinishedAt.IsZero() || t.After(biggestFinishedAt.Time) {
biggestFinishedAt = *t
}
}
}
if stageFinished {
if stage.Status != v1.ActivityStatusTypeSucceeded {
failed = true
}
} else {
allCompleted = false
}
if stage.Status == v1.ActivityStatusTypeRunning {
running = true
}
if stage.Status == v1.ActivityStatusTypeRunning || stage.Status == v1.ActivityStatusTypePending {
allCompleted = false
}
}
}
if allCompleted {
if failed {
spec.Status = v1.ActivityStatusTypeFailed
} else {
spec.Status = v1.ActivityStatusTypeSucceeded
}
if !biggestFinishedAt.IsZero() {
spec.CompletedTimestamp = &biggestFinishedAt
}
} else {
if running {
spec.Status = v1.ActivityStatusTypeRunning
} else {
spec.Status = v1.ActivityStatusTypePending
}
}
return !reflect.DeepEqual(©, activity)
}
// createStepDescription uses the spec of the init container to return a description
func createStepDescription(initContainerName string, pod *corev1.Pod) string {
for _, c := range pod.Spec.InitContainers {
if c.Name == initContainerName {
return strings.Join(c.Args, " ")
}
}
return ""
}
// DigitSuffix outputs digital suffix
func DigitSuffix(text string) string {
answer := ""
for {
l := len(text)
if l == 0 {
return answer
}
lastChar := text[l-1:]
for _, rune := range lastChar {
if !unicode.IsDigit(rune) {
return answer
}
break
}
answer = lastChar + answer
text = text[0 : l-1]
}
}