forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
buildconfigs.go
195 lines (179 loc) · 5.23 KB
/
buildconfigs.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
package buildconfigs
import (
"fmt"
"reflect"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
kapi "k8s.io/kubernetes/pkg/api"
"github.com/golang/glog"
buildapi "github.com/openshift/origin/pkg/build/api"
buildutil "github.com/openshift/origin/pkg/build/util"
triggerapi "github.com/openshift/origin/pkg/image/api/v1/trigger"
"github.com/openshift/origin/pkg/image/trigger"
)
// calculateBuildConfigTriggers transforms a build config into a set of image change triggers.
// It uses synthetic field paths since we don't need to generically transform the config.
func calculateBuildConfigTriggers(bc *buildapi.BuildConfig) []triggerapi.ObjectFieldTrigger {
var triggers []triggerapi.ObjectFieldTrigger
for _, t := range bc.Spec.Triggers {
if t.ImageChange == nil {
continue
}
var (
fieldPath string
from *kapi.ObjectReference
)
if t.ImageChange.From != nil {
from = t.ImageChange.From
fieldPath = "spec.triggers"
} else {
from = buildutil.GetInputReference(bc.Spec.Strategy)
fieldPath = "spec.strategy.*.from"
}
if from == nil || from.Kind != "ImageStreamTag" || len(from.Name) == 0 {
continue
}
// add a trigger
triggers = append(triggers, triggerapi.ObjectFieldTrigger{
From: triggerapi.ObjectReference{
Name: from.Name,
Namespace: from.Namespace,
Kind: from.Kind,
APIVersion: from.APIVersion,
},
FieldPath: fieldPath,
})
}
return triggers
}
// buildConfigTriggerIndexer converts build config events into entries for the trigger cache, and
// also calculates the latest state of the changes on the object.
type buildConfigTriggerIndexer struct {
prefix string
}
func NewBuildConfigTriggerIndexer(prefix string) trigger.Indexer {
return buildConfigTriggerIndexer{prefix: prefix}
}
func (i buildConfigTriggerIndexer) Index(obj, old interface{}) (string, *trigger.CacheEntry, cache.DeltaType, error) {
var (
triggers []triggerapi.ObjectFieldTrigger
bc *buildapi.BuildConfig
change cache.DeltaType
)
switch {
case obj != nil && old == nil:
// added
bc = obj.(*buildapi.BuildConfig)
triggers = calculateBuildConfigTriggers(bc)
change = cache.Added
case old != nil && obj == nil:
// deleted
bc = old.(*buildapi.BuildConfig)
triggers = calculateBuildConfigTriggers(bc)
change = cache.Deleted
default:
// updated
bc = obj.(*buildapi.BuildConfig)
triggers = calculateBuildConfigTriggers(bc)
oldTriggers := calculateBuildConfigTriggers(old.(*buildapi.BuildConfig))
switch {
case len(oldTriggers) == 0:
change = cache.Added
case !reflect.DeepEqual(oldTriggers, triggers):
change = cache.Updated
}
}
if len(triggers) > 0 {
key := i.prefix + bc.Namespace + "/" + bc.Name
return key, &trigger.CacheEntry{
Key: key,
Namespace: bc.Namespace,
Triggers: triggers,
}, change, nil
}
return "", nil, change, nil
}
// BuildConfigInstantiator abstracts creating builds from build requests.
type BuildConfigInstantiator interface {
// Instantiate should launch a build from the provided build request.
Instantiate(namespace string, request *buildapi.BuildRequest) (*buildapi.Build, error)
}
// BuildConfigReactor converts trigger changes into new builds. It will request a build if
// at least one image is out of date.
type BuildConfigReactor struct {
Instantiator BuildConfigInstantiator
}
// ImageChanged is passed a build config and a set of changes and updates the object if
// necessary.
func (r *BuildConfigReactor) ImageChanged(obj interface{}, tagRetriever trigger.TagRetriever) error {
bc := obj.(*buildapi.BuildConfig)
var (
changed bool
id string
from *kapi.ObjectReference
ref string
)
for _, t := range bc.Spec.Triggers {
p := t.ImageChange
if p == nil || (p.From != nil && p.From.Kind != "ImageStreamTag") {
continue
}
if p.From != nil {
from = p.From
} else {
from = buildutil.GetInputReference(bc.Spec.Strategy)
}
namespace := from.Namespace
if len(namespace) == 0 {
namespace = bc.Namespace
}
latest, _, found := tagRetriever.ImageStreamTag(namespace, from.Name)
if !found {
continue
}
if latest != p.LastTriggeredImageID {
changed = true
ref = latest
break
}
}
if !changed {
return nil
}
// instantiate new build
glog.V(4).Infof("Running build for BuildConfig %s/%s", bc.Namespace, bc.Name)
request := &buildapi.BuildRequest{
ObjectMeta: metav1.ObjectMeta{
Name: bc.Name,
Namespace: bc.Namespace,
},
TriggeredBy: []buildapi.BuildTriggerCause{
{
Message: buildapi.BuildTriggerCauseImageMsg,
ImageChangeBuild: &buildapi.ImageChangeCause{
ImageID: id,
FromRef: from,
},
},
},
TriggeredByImage: &kapi.ObjectReference{
Kind: "DockerImage",
Name: ref,
},
From: from,
}
_, err := r.Instantiator.Instantiate(bc.Namespace, request)
return err
}
func printTriggers(triggers []buildapi.BuildTriggerPolicy) string {
var values []string
for _, t := range triggers {
if t.ImageChange.From != nil {
values = append(values, fmt.Sprintf("[from=%s last=%s]", t.ImageChange.From.Name, t.ImageChange.LastTriggeredImageID))
} else {
values = append(values, fmt.Sprintf("[from=* last=%s]", t.ImageChange.LastTriggeredImageID))
}
}
return strings.Join(values, ", ")
}