forked from openshift/library-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildutil.go
85 lines (77 loc) · 2.62 KB
/
buildutil.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
package buildutil
import (
corev1 "k8s.io/api/core/v1"
buildv1 "github.com/openshift/api/build/v1"
)
// GetInputReference returns the From ObjectReference associated with the
// BuildStrategy.
func GetInputReference(strategy buildv1.BuildStrategy) *corev1.ObjectReference {
switch {
case strategy.SourceStrategy != nil:
return &strategy.SourceStrategy.From
case strategy.DockerStrategy != nil:
return strategy.DockerStrategy.From
case strategy.CustomStrategy != nil:
return &strategy.CustomStrategy.From
default:
return nil
}
}
// GetBuildEnv gets the build strategy environment
func GetBuildEnv(build *buildv1.Build) []corev1.EnvVar {
switch {
case build.Spec.Strategy.SourceStrategy != nil:
return build.Spec.Strategy.SourceStrategy.Env
case build.Spec.Strategy.DockerStrategy != nil:
return build.Spec.Strategy.DockerStrategy.Env
case build.Spec.Strategy.CustomStrategy != nil:
return build.Spec.Strategy.CustomStrategy.Env
case build.Spec.Strategy.JenkinsPipelineStrategy != nil:
return build.Spec.Strategy.JenkinsPipelineStrategy.Env
default:
return nil
}
}
// SetBuildEnv replaces the current build environment
func SetBuildEnv(build *buildv1.Build, env []corev1.EnvVar) {
var oldEnv *[]corev1.EnvVar
switch {
case build.Spec.Strategy.SourceStrategy != nil:
oldEnv = &build.Spec.Strategy.SourceStrategy.Env
case build.Spec.Strategy.DockerStrategy != nil:
oldEnv = &build.Spec.Strategy.DockerStrategy.Env
case build.Spec.Strategy.CustomStrategy != nil:
oldEnv = &build.Spec.Strategy.CustomStrategy.Env
case build.Spec.Strategy.JenkinsPipelineStrategy != nil:
oldEnv = &build.Spec.Strategy.JenkinsPipelineStrategy.Env
default:
return
}
*oldEnv = env
}
// FindTriggerPolicy retrieves the BuildTrigger(s) of a given type from a build configuration.
// Returns nil if no matches are found.
func FindTriggerPolicy(triggerType buildv1.BuildTriggerType, config *buildv1.BuildConfig) (buildTriggers []buildv1.BuildTriggerPolicy) {
for _, specTrigger := range config.Spec.Triggers {
if specTrigger.Type == triggerType {
buildTriggers = append(buildTriggers, specTrigger)
}
}
return buildTriggers
}
// ConfigNameForBuild returns the name of the build config from a
// build name.
func ConfigNameForBuild(build *buildv1.Build) string {
if build == nil {
return ""
}
if build.Annotations != nil {
if _, exists := build.Annotations[buildv1.BuildConfigAnnotation]; exists {
return build.Annotations[buildv1.BuildConfigAnnotation]
}
}
if _, exists := build.Labels[buildv1.BuildConfigLabel]; exists {
return build.Labels[buildv1.BuildConfigLabel]
}
return build.Labels[buildv1.BuildConfigLabelDeprecated]
}