forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
206 lines (185 loc) · 6.62 KB
/
util.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
package util
import (
"fmt"
"strconv"
"strings"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/labels"
"github.com/golang/glog"
buildapi "github.com/openshift/origin/pkg/build/api"
buildclient "github.com/openshift/origin/pkg/build/client"
)
const (
// NoBuildLogsMessage reports that no build logs are available
NoBuildLogsMessage = "No logs are available."
)
// GetBuildName returns name of the build pod.
func GetBuildName(pod *kapi.Pod) string {
if pod == nil {
return ""
}
return pod.Annotations[buildapi.BuildAnnotation]
}
// GetInputReference returns the From ObjectReference associated with the
// BuildStrategy.
func GetInputReference(strategy buildapi.BuildStrategy) *kapi.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
}
}
// IsBuildComplete returns whether the provided build is complete or not
func IsBuildComplete(build *buildapi.Build) bool {
return build.Status.Phase != buildapi.BuildPhaseRunning && build.Status.Phase != buildapi.BuildPhasePending && build.Status.Phase != buildapi.BuildPhaseNew
}
// IsPaused returns true if the provided BuildConfig is paused and cannot be used to create a new Build
func IsPaused(bc *buildapi.BuildConfig) bool {
return strings.ToLower(bc.Annotations[buildapi.BuildConfigPausedAnnotation]) == "true"
}
// BuildNumber returns the given build number.
func BuildNumber(build *buildapi.Build) (int64, error) {
annotations := build.GetAnnotations()
if stringNumber, ok := annotations[buildapi.BuildNumberAnnotation]; ok {
return strconv.ParseInt(stringNumber, 10, 64)
}
return 0, fmt.Errorf("build %s/%s does not have %s annotation", build.Namespace, build.Name, buildapi.BuildNumberAnnotation)
}
// BuildRunPolicy returns the scheduling policy for the build based on the
// "queued" label.
func BuildRunPolicy(build *buildapi.Build) buildapi.BuildRunPolicy {
labels := build.GetLabels()
if value, found := labels[buildapi.BuildRunPolicyLabel]; found {
switch value {
case "Parallel":
return buildapi.BuildRunPolicyParallel
case "Serial":
return buildapi.BuildRunPolicySerial
case "SerialLatestOnly":
return buildapi.BuildRunPolicySerialLatestOnly
}
}
glog.V(5).Infof("Build %s/%s does not have start policy label set, using default (Serial)", build.Namespace, build.Name)
return buildapi.BuildRunPolicySerial
}
// BuildNameForConfigVersion returns the name of the version-th build
// for the config that has the provided name.
func BuildNameForConfigVersion(name string, version int) string {
return fmt.Sprintf("%s-%d", name, version)
}
// BuildConfigSelector returns a label Selector which can be used to find all
// builds for a BuildConfig.
func BuildConfigSelector(name string) labels.Selector {
return labels.Set{buildapi.BuildConfigLabel: buildapi.LabelValue(name)}.AsSelector()
}
// BuildConfigSelectorDeprecated returns a label Selector which can be used to find
// all builds for a BuildConfig that use the deprecated labels.
func BuildConfigSelectorDeprecated(name string) labels.Selector {
return labels.Set{buildapi.BuildConfigLabelDeprecated: name}.AsSelector()
}
type buildFilter func(buildapi.Build) bool
// BuildConfigBuilds return a list of builds for the given build config.
// Optionally you can specify a filter function to select only builds that
// matches your criteria.
func BuildConfigBuilds(c buildclient.BuildLister, namespace, name string, filterFunc buildFilter) (*buildapi.BuildList, error) {
result, err := c.List(namespace, kapi.ListOptions{
LabelSelector: BuildConfigSelector(name),
})
if err != nil {
return nil, err
}
if filterFunc == nil {
return result, nil
}
filteredList := &buildapi.BuildList{TypeMeta: result.TypeMeta, ListMeta: result.ListMeta}
for _, b := range result.Items {
if filterFunc(b) {
filteredList.Items = append(filteredList.Items, b)
}
}
return filteredList, nil
}
// ConfigNameForBuild returns the name of the build config from a
// build name.
func ConfigNameForBuild(build *buildapi.Build) string {
if build == nil {
return ""
}
if build.Annotations != nil {
if _, exists := build.Annotations[buildapi.BuildConfigAnnotation]; exists {
return build.Annotations[buildapi.BuildConfigAnnotation]
}
}
if _, exists := build.Labels[buildapi.BuildConfigLabel]; exists {
return build.Labels[buildapi.BuildConfigLabel]
}
return build.Labels[buildapi.BuildConfigLabelDeprecated]
}
// VersionForBuild returns the version from the provided build name.
// If no version can be found, 0 is returned to indicate no version.
func VersionForBuild(build *buildapi.Build) int {
if build == nil {
return 0
}
versionString := build.Annotations[buildapi.BuildNumberAnnotation]
version, err := strconv.Atoi(versionString)
if err != nil {
return 0
}
return version
}
func BuildDeepCopy(build *buildapi.Build) (*buildapi.Build, error) {
objCopy, err := kapi.Scheme.DeepCopy(build)
if err != nil {
return nil, err
}
copied, ok := objCopy.(*buildapi.Build)
if !ok {
return nil, fmt.Errorf("expected Build, got %#v", objCopy)
}
return copied, nil
}
// MergeTrustedEnvWithoutDuplicates merges two environment lists without having
// duplicate items in the output list. The source list will be filtered
// such that only whitelisted environment variables are merged into the
// output list. If sourcePrecedence is true, keys in the source list
// will override keys in the output list.
func MergeTrustedEnvWithoutDuplicates(source []kapi.EnvVar, output *[]kapi.EnvVar, sourcePrecedence bool) {
// filter out all environment variables except trusted/well known
// values, because we do not want random environment variables being
// fed into the privileged STI container via the BuildConfig definition.
type sourceMapItem struct {
index int
value string
}
index := 0
filteredSourceMap := make(map[string]sourceMapItem)
filteredSource := []kapi.EnvVar{}
for _, env := range source {
for _, acceptable := range buildapi.WhitelistEnvVarNames {
if env.Name == acceptable {
filteredSource = append(filteredSource, env)
filteredSourceMap[env.Name] = sourceMapItem{index, env.Value}
index++
break
}
}
}
result := *output
for i, env := range result {
// If the value exists in output, override it and remove it
// from the source list
if v, found := filteredSourceMap[env.Name]; found {
if sourcePrecedence {
result[i].Value = v.value
}
filteredSource = append(filteredSource[:v.index], filteredSource[v.index+1:]...)
}
}
*output = append(result, filteredSource...)
}