@@ -16,11 +16,16 @@ import (
16
16
type ImageChangeController struct {
17
17
NextImageRepository func () * imageapi.ImageRepository
18
18
BuildConfigStore cache.Store
19
+ BuildConfigUpdater buildConfigUpdater
19
20
BuildCreator buildCreator
20
21
// Stop is an optional channel that controls when the controller exits
21
22
Stop <- chan struct {}
22
23
}
23
24
25
+ type buildConfigUpdater interface {
26
+ UpdateBuildConfig (buildConfig * buildapi.BuildConfig ) error
27
+ }
28
+
24
29
type buildCreator interface {
25
30
CreateBuild (build * buildapi.BuildConfig , imageSubstitutions map [string ]string ) error
26
31
}
@@ -42,33 +47,47 @@ func (c *ImageChangeController) HandleImageRepo() {
42
47
glog .V (4 ).Infof ("Detecting changed images for buildConfig %s" , config .Name )
43
48
44
49
// Extract relevant triggers for this imageRepo for this config
45
- var triggerForConfig * buildapi. ImageChangeTrigger
50
+ shouldTriggerBuild := false
46
51
for _ , trigger := range config .Triggers {
52
+ if trigger .Type != buildapi .ImageChangeBuildTriggerType {
53
+ continue
54
+ }
47
55
// for every ImageChange trigger, record the image it substitutes for and get the latest
48
56
// image id from the imagerepository. We will substitute all images in the buildconfig
49
57
// with the latest values from the imagerepositories.
50
- if trigger .Type == buildapi .ImageChangeBuildTriggerType {
51
- // TODO: we don't really want to create a build for a buildconfig based the "test" tag if the "prod" tag is what just got
52
- // updated, but ImageRepository doesn't give us that granularity today, so the only way to avoid these spurious builds is
53
- // to check if the new imageid is different from the last time we built this buildcfg. Need to add this check.
54
- // Will be effectively identical the logic needed on startup to spin new builds only if we missed a new image event.
55
- var tag string
56
- if tag = trigger .ImageChange .Tag ; len (tag ) == 0 {
57
- tag = buildapi .DefaultImageTag
58
- }
59
- if repoImageID , repoHasTag := imageRepo .Tags [tag ]; repoHasTag {
60
- imageSubstitutions [trigger .ImageChange .Image ] = imageRepo .DockerImageRepository + ":" + repoImageID
61
- }
62
- if trigger .ImageChange .ImageRepositoryRef .Name == imageRepo .Name {
63
- triggerForConfig = trigger .ImageChange
64
- }
58
+ icTrigger := trigger .ImageChange
59
+
60
+ // TODO: we don't really want to create a build for a buildconfig based the "test" tag if the "prod" tag is what just got
61
+ // updated, but ImageRepository doesn't give us that granularity today, so the only way to avoid these spurious builds is
62
+ // to check if the new imageid is different from the last time we built this buildcfg. Need to add this check.
63
+ // Will be effectively identical the logic needed on startup to spin new builds only if we missed a new image event.
64
+ tag := icTrigger .Tag
65
+ if len (tag ) == 0 {
66
+ tag = buildapi .DefaultImageTag
67
+ }
68
+ imageID , hasTag := imageRepo .Tags [tag ]
69
+ if ! hasTag {
70
+ continue
71
+ }
72
+
73
+ // comparison requires us to match Name of the image and LastTriggeredImageID
74
+ // (must be different) to trigger a build
75
+ if icTrigger .ImageRepositoryRef .Name == imageRepo .Name &&
76
+ icTrigger .LastTriggeredImageID != imageID {
77
+ imageSubstitutions [icTrigger .Image ] = imageRepo .DockerImageRepository + ":" + imageID
78
+ shouldTriggerBuild = true
79
+ icTrigger .LastTriggeredImageID = imageID
65
80
}
66
81
}
67
82
68
- if triggerForConfig != nil {
83
+ if shouldTriggerBuild {
69
84
glog .V (4 ).Infof ("Running build for buildConfig %s" , config .Name )
70
85
if err := c .BuildCreator .CreateBuild (config , imageSubstitutions ); err != nil {
71
86
glog .V (2 ).Infof ("Error starting build for buildConfig %v: %v" , config .Name , err )
87
+ } else {
88
+ if err := c .BuildConfigUpdater .UpdateBuildConfig (config ); err != nil {
89
+ glog .V (2 ).Infof ("Error updating buildConfig %v: %v" , config .Name , err )
90
+ }
72
91
}
73
92
}
74
93
}
0 commit comments