forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
77 lines (66 loc) · 2.43 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
package api
import (
kapi "k8s.io/kubernetes/pkg/api"
)
// BuildToPodLogOptions builds a PodLogOptions object out of a BuildLogOptions.
// Currently BuildLogOptions.Container and BuildLogOptions.Previous aren't used
// so they won't be copied to PodLogOptions.
func BuildToPodLogOptions(opts *BuildLogOptions) *kapi.PodLogOptions {
return &kapi.PodLogOptions{
Follow: opts.Follow,
SinceSeconds: opts.SinceSeconds,
SinceTime: opts.SinceTime,
Timestamps: opts.Timestamps,
TailLines: opts.TailLines,
LimitBytes: opts.LimitBytes,
}
}
// PredicateFunc is testing an argument and decides does it meet some criteria or not.
// It can be used for filtering elements based on some conditions.
type PredicateFunc func(interface{}) bool
// FilterBuilds returns array of builds that satisfies predicate function.
func FilterBuilds(builds []Build, predicate PredicateFunc) []Build {
if len(builds) == 0 {
return builds
}
result := make([]Build, 0)
for _, build := range builds {
if predicate(build) {
result = append(result, build)
}
}
return result
}
// ByBuildConfigPredicate matches all builds that have build config annotation or label with specified value.
func ByBuildConfigPredicate(labelValue string) PredicateFunc {
return func(arg interface{}) bool {
return (hasBuildConfigAnnotation(arg.(Build), BuildConfigAnnotation, labelValue) ||
hasBuildConfigLabel(arg.(Build), BuildConfigLabel, labelValue) ||
hasBuildConfigLabel(arg.(Build), BuildConfigLabelDeprecated, labelValue))
}
}
func hasBuildConfigLabel(build Build, labelName, labelValue string) bool {
value, ok := build.Labels[labelName]
return ok && value == labelValue
}
func hasBuildConfigAnnotation(build Build, annotationName, annotationValue string) bool {
if build.Annotations == nil {
return false
}
value, ok := build.Annotations[annotationName]
return ok && value == annotationValue
}
// FindTriggerPolicy retrieves the BuildTrigger(s) of a given type from a build configuration.
// Returns nil if no matches are found.
func FindTriggerPolicy(triggerType BuildTriggerType, config *BuildConfig) (buildTriggers []BuildTriggerPolicy) {
for _, specTrigger := range config.Spec.Triggers {
if specTrigger.Type == triggerType {
buildTriggers = append(buildTriggers, specTrigger)
}
}
return buildTriggers
}
func HasTriggerType(triggerType BuildTriggerType, bc *BuildConfig) bool {
matches := FindTriggerPolicy(triggerType, bc)
return len(matches) > 0
}