-
Notifications
You must be signed in to change notification settings - Fork 345
/
build_types_support.go
323 lines (276 loc) · 8.99 KB
/
build_types_support.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
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1
import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func NewBuild(namespace string, name string) *Build {
return &Build{
TypeMeta: metav1.TypeMeta{
APIVersion: SchemeGroupVersion.String(),
Kind: BuildKind,
},
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
},
}
}
func NewBuildList() BuildList {
return BuildList{
TypeMeta: metav1.TypeMeta{
APIVersion: SchemeGroupVersion.String(),
Kind: BuildKind,
},
}
}
// BuilderPodNamespace returns the namespace of the operator in charge to reconcile this Build.
func (build *Build) BuilderPodNamespace() string {
for _, t := range build.Spec.Tasks {
if t.Builder != nil {
return t.Builder.Configuration.BuilderPodNamespace
}
}
return ""
}
// BuilderConfiguration returns the builder configuration for this Build.
func (build *Build) BuilderConfiguration() *BuildConfiguration {
return build.TaskConfiguration("builder")
}
// TaskConfiguration returns the task configuration of this Build.
func (build *Build) TaskConfiguration(name string) *BuildConfiguration {
return ConfigurationTasksByName(build.Spec.Tasks, name)
}
// BuilderDependencies returns the list of dependencies configured on by the builder task for this Build.
func (build *Build) BuilderDependencies() []string {
if builder, ok := FindBuilderTask(build.Spec.Tasks); ok {
return builder.Dependencies
}
return []string{}
}
// FindBuilderTask returns the 1st builder task from the task list.
func FindBuilderTask(tasks []Task) (*BuilderTask, bool) {
for _, t := range tasks {
if t.Builder != nil {
return t.Builder, true
}
}
return nil, false
}
// ConfigurationTasksByName returns the container configuration from the task list.
func ConfigurationTasksByName(tasks []Task, name string) *BuildConfiguration {
for _, t := range tasks {
if t.Builder != nil && t.Builder.Name == name {
return &t.Builder.Configuration
}
if t.Custom != nil && t.Custom.Name == name {
return &t.Custom.Configuration
}
if t.Package != nil && t.Package.Name == name {
return &t.Package.Configuration
}
if t.Spectrum != nil && t.Spectrum.Name == name {
return &t.Spectrum.Configuration
}
if t.S2i != nil && t.S2i.Name == name {
return &t.S2i.Configuration
}
if t.Jib != nil && t.Jib.Name == name {
return &t.Jib.Configuration
}
}
return &BuildConfiguration{}
}
// SetBuilderConfigurationTasks set the configuration required for the builder in the list of tasks.
func SetBuilderConfigurationTasks(tasks []Task, conf *BuildConfiguration) {
for _, t := range tasks {
if t.Builder != nil {
t.Builder.Configuration = *conf
return
}
}
}
func (buildPhase *BuildPhase) String() string {
return string(*buildPhase)
}
// GetCondition returns the condition with the provided type.
func (in *BuildStatus) GetCondition(condType BuildConditionType) *BuildCondition {
for i := range in.Conditions {
c := in.Conditions[i]
if c.Type == condType {
return &c
}
}
return nil
}
func (in *BuildStatus) Failed(err error) BuildStatus {
in.Error = err.Error()
in.Phase = BuildPhaseFailed
return *in
}
func (in *BuildStatus) IsFinished() bool {
return in.Phase == BuildPhaseSucceeded || in.Phase == BuildPhaseFailed ||
in.Phase == BuildPhaseInterrupted || in.Phase == BuildPhaseError
}
func (in *BuildStatus) SetCondition(condType BuildConditionType, status corev1.ConditionStatus, reason string, message string) {
in.SetConditions(BuildCondition{
Type: condType,
Status: status,
LastUpdateTime: metav1.Now(),
LastTransitionTime: metav1.Now(),
Reason: reason,
Message: message,
})
}
func (in *BuildStatus) SetErrorCondition(condType BuildConditionType, reason string, err error) {
in.SetConditions(BuildCondition{
Type: condType,
Status: corev1.ConditionFalse,
LastUpdateTime: metav1.Now(),
LastTransitionTime: metav1.Now(),
Reason: reason,
Message: err.Error(),
})
}
// SetConditions updates the resource to include the provided conditions.
//
// If a condition that we are about to add already exists and has the same status and
// reason then we are not going to update.
func (in *BuildStatus) SetConditions(conditions ...BuildCondition) {
for _, condition := range conditions {
if condition.LastUpdateTime.IsZero() {
condition.LastUpdateTime = metav1.Now()
}
if condition.LastTransitionTime.IsZero() {
condition.LastTransitionTime = metav1.Now()
}
currentCond := in.GetCondition(condition.Type)
if currentCond != nil && currentCond.Status == condition.Status && currentCond.Reason == condition.Reason {
return
}
// Do not update lastTransitionTime if the status of the condition doesn't change.
if currentCond != nil && currentCond.Status == condition.Status {
condition.LastTransitionTime = currentCond.LastTransitionTime
}
in.RemoveCondition(condition.Type)
in.Conditions = append(in.Conditions, condition)
}
}
// RemoveCondition removes the resource condition with the provided type.
func (in *BuildStatus) RemoveCondition(condType BuildConditionType) {
newConditions := in.Conditions[:0]
for _, c := range in.Conditions {
if c.Type != condType {
newConditions = append(newConditions, c)
}
}
in.Conditions = newConditions
}
var _ ResourceCondition = BuildCondition{}
func (in *BuildStatus) GetConditions() []ResourceCondition {
res := make([]ResourceCondition, 0, len(in.Conditions))
for _, c := range in.Conditions {
res = append(res, c)
}
return res
}
func (c BuildCondition) GetType() string {
return string(c.Type)
}
func (c BuildCondition) GetStatus() corev1.ConditionStatus {
return c.Status
}
func (c BuildCondition) GetLastUpdateTime() metav1.Time {
return c.LastUpdateTime
}
func (c BuildCondition) GetLastTransitionTime() metav1.Time {
return c.LastTransitionTime
}
func (c BuildCondition) GetReason() string {
return c.Reason
}
func (c BuildCondition) GetMessage() string {
return c.Message
}
func (bl BuildList) HasRunningBuilds() bool {
for _, b := range bl.Items {
if b.Status.Phase == BuildPhasePending || b.Status.Phase == BuildPhaseRunning {
return true
}
}
return false
}
func (bl BuildList) HasScheduledBuildsBefore(build *Build) (bool, *Build) {
for _, b := range bl.Items {
if b.Name == build.Name {
continue
}
if (b.Status.Phase == BuildPhaseInitialization || b.Status.Phase == BuildPhaseScheduling) &&
b.CreationTimestamp.Before(&build.CreationTimestamp) {
return true, &b
}
}
return false, nil
}
// HasMatchingBuild visit all items in the list of builds and search for a scheduled build that matches the given build's dependencies.
// It returns the first matching build found regardless it may have any one more appropriate.
func (bl BuildList) HasMatchingBuild(build *Build) (bool, *Build) {
required := build.BuilderDependencies()
if len(required) == 0 {
return false, nil
}
for _, b := range bl.Items {
if b.Name == build.Name || b.Status.IsFinished() {
continue
}
dependencies := b.BuilderDependencies()
dependencyMap := make(map[string]int, len(dependencies))
for i, item := range dependencies {
dependencyMap[item] = i
}
allMatching := true
missing := 0
for _, item := range required {
if _, ok := dependencyMap[item]; !ok {
allMatching = false
missing++
}
}
// Heuristic approach: if there are too many unrelated libraries then this image is
// not suitable to be used as base image
if !allMatching && missing > len(required)/2 {
continue
}
// handle suitable build that has started already
if b.Status.Phase == BuildPhasePending || b.Status.Phase == BuildPhaseRunning {
return true, &b
}
// handle suitable scheduled build
if b.Status.Phase == BuildPhaseInitialization || b.Status.Phase == BuildPhaseScheduling {
if allMatching && len(required) == len(dependencies) {
// seems like both builds require exactly the same list of dependencies
// additionally check for the creation timestamp
if b.CreationTimestamp.Before(&build.CreationTimestamp) {
return true, &b
}
} else if missing > 0 {
// found another suitable scheduled build with fewer dependencies that should build first in order to reuse the produced image
return true, &b
}
}
}
return false, nil
}