forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
111 lines (94 loc) · 3.25 KB
/
helpers.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
package graph
import (
"sort"
"github.com/gonum/graph"
osgraph "github.com/openshift/origin/pkg/api/graph"
buildapi "github.com/openshift/origin/pkg/build/api"
buildgraph "github.com/openshift/origin/pkg/build/graph/nodes"
)
// RelevantBuilds returns the lastSuccessful build, lastUnsuccessful build, and a list of active builds
func RelevantBuilds(g osgraph.Graph, bcNode *buildgraph.BuildConfigNode) (*buildgraph.BuildNode, *buildgraph.BuildNode, []*buildgraph.BuildNode) {
var (
lastSuccessfulBuild *buildgraph.BuildNode
lastUnsuccessfulBuild *buildgraph.BuildNode
)
activeBuilds := []*buildgraph.BuildNode{}
allBuilds := []*buildgraph.BuildNode{}
uncastBuilds := g.SuccessorNodesByEdgeKind(bcNode, BuildEdgeKind)
for i := range uncastBuilds {
buildNode := uncastBuilds[i].(*buildgraph.BuildNode)
if belongsToBuildConfig(bcNode.BuildConfig, buildNode.Build) {
allBuilds = append(allBuilds, buildNode)
}
}
if len(allBuilds) == 0 {
return nil, nil, []*buildgraph.BuildNode{}
}
sort.Sort(RecentBuildReferences(allBuilds))
for i := range allBuilds {
switch allBuilds[i].Build.Status.Phase {
case buildapi.BuildPhaseComplete:
if lastSuccessfulBuild == nil {
lastSuccessfulBuild = allBuilds[i]
}
case buildapi.BuildPhaseFailed, buildapi.BuildPhaseCancelled, buildapi.BuildPhaseError:
if lastUnsuccessfulBuild == nil {
lastUnsuccessfulBuild = allBuilds[i]
}
default:
activeBuilds = append(activeBuilds, allBuilds[i])
}
}
return lastSuccessfulBuild, lastUnsuccessfulBuild, activeBuilds
}
func belongsToBuildConfig(config *buildapi.BuildConfig, b *buildapi.Build) bool {
if b.Labels == nil {
return false
}
if b.Annotations != nil && b.Annotations[buildapi.BuildConfigAnnotation] == config.Name {
return true
}
if b.Labels[buildapi.BuildConfigLabel] == config.Name {
return true
}
if b.Labels[buildapi.BuildConfigLabelDeprecated] == config.Name {
return true
}
return false
}
type RecentBuildReferences []*buildgraph.BuildNode
func (m RecentBuildReferences) Len() int { return len(m) }
func (m RecentBuildReferences) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m RecentBuildReferences) Less(i, j int) bool {
return m[i].Build.CreationTimestamp.After(m[j].Build.CreationTimestamp.Time)
}
func defaultNamespace(value, defaultValue string) string {
if len(value) == 0 {
return defaultValue
}
return value
}
// BuildConfigsForTag returns the buildConfig that points to the provided imageStreamTag.
func BuildConfigsForTag(g osgraph.Graph, istag graph.Node) []*buildgraph.BuildConfigNode {
bcs := []*buildgraph.BuildConfigNode{}
for _, bcNode := range g.PredecessorNodesByEdgeKind(istag, BuildOutputEdgeKind) {
bcs = append(bcs, bcNode.(*buildgraph.BuildConfigNode))
}
return bcs
}
// GetLatestBuild returns the latest build for the provided buildConfig.
func GetLatestBuild(g osgraph.Graph, bc graph.Node) *buildgraph.BuildNode {
builds := g.SuccessorNodesByEdgeKind(bc, BuildEdgeKind)
if len(builds) == 0 {
return nil
}
latestBuild := builds[0].(*buildgraph.BuildNode)
for _, buildNode := range builds[1:] {
if build, ok := buildNode.(*buildgraph.BuildNode); ok {
if latestBuild.Build.CreationTimestamp.Before(build.Build.CreationTimestamp) {
latestBuild = build
}
}
}
return latestBuild
}