Skip to content

Commit 504dc2c

Browse files
author
Jim Minter
committed
add event when build image trigger fails
1 parent 6755b0d commit 504dc2c

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

pkg/cmd/server/origin/controller/image.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (c *ImageTriggerControllerConfig) RunController(ctx ControllerContext) (boo
6868
Informer: ctx.BuildInformers.Build().InternalVersion().BuildConfigs().Informer(),
6969
Store: ctx.BuildInformers.Build().InternalVersion().BuildConfigs().Informer().GetIndexer(),
7070
TriggerFn: triggerbuildconfigs.NewBuildConfigTriggerIndexer,
71-
Reactor: &triggerbuildconfigs.BuildConfigReactor{Instantiator: bcInstantiator},
71+
Reactor: triggerbuildconfigs.NewBuildConfigReactor(bcInstantiator, kclient.Core().RESTClient()),
7272
})
7373
}
7474
if !c.HasDeploymentsEnabled {

pkg/image/controller/trigger/image_trigger_controller_test.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,7 @@ func TestTriggerControllerSyncBuildConfigResource(t *testing.T) {
297297
},
298298
}
299299
inst := fakeBuildConfigInstantiator(test.bc, test.is)
300-
reaction := &buildconfigs.BuildConfigReactor{
301-
Instantiator: inst,
302-
}
300+
reaction := buildconfigs.NewBuildConfigReactor(inst, nil)
303301
controller := TriggerController{
304302
triggerCache: NewTriggerCache(),
305303
lister: lister,
@@ -398,9 +396,7 @@ func TestTriggerControllerSyncBuildConfigResourceErrorHandling(t *testing.T) {
398396
if test.err != nil {
399397
inst.err = test.err
400398
}
401-
reaction := &buildconfigs.BuildConfigReactor{
402-
Instantiator: inst,
403-
}
399+
reaction := buildconfigs.NewBuildConfigReactor(inst, nil)
404400
controller := TriggerController{
405401
triggerCache: NewTriggerCache(),
406402
lister: lister,

pkg/image/trigger/buildconfigs/buildconfigs.go

+26-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package buildconfigs
22

33
import (
4+
"fmt"
45
"reflect"
56

67
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
9+
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
10+
clientv1 "k8s.io/client-go/pkg/api/v1"
11+
"k8s.io/client-go/rest"
712
"k8s.io/client-go/tools/cache"
13+
"k8s.io/client-go/tools/record"
814
kapi "k8s.io/kubernetes/pkg/api"
915

1016
"github.com/golang/glog"
@@ -107,15 +113,25 @@ type BuildConfigInstantiator interface {
107113
Instantiate(namespace string, request *buildapi.BuildRequest) (*buildapi.Build, error)
108114
}
109115

110-
// BuildConfigReactor converts trigger changes into new builds. It will request a build if
116+
// buildConfigReactor converts trigger changes into new builds. It will request a build if
111117
// at least one image is out of date.
112-
type BuildConfigReactor struct {
113-
Instantiator BuildConfigInstantiator
118+
type buildConfigReactor struct {
119+
instantiator BuildConfigInstantiator
120+
eventRecorder record.EventRecorder
121+
}
122+
123+
// NewBuildConfigReactor creates a new buildConfigReactor
124+
func NewBuildConfigReactor(instantiator BuildConfigInstantiator, restclient rest.Interface) trigger.ImageReactor {
125+
eventBroadcaster := record.NewBroadcaster()
126+
eventBroadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: v1core.New(restclient).Events("")})
127+
eventRecorder := eventBroadcaster.NewRecorder(kapi.Scheme, clientv1.EventSource{Component: "buildconfig-controller"})
128+
129+
return &buildConfigReactor{instantiator: instantiator, eventRecorder: eventRecorder}
114130
}
115131

116132
// ImageChanged is passed a build config and a set of changes and updates the object if
117133
// necessary.
118-
func (r *BuildConfigReactor) ImageChanged(obj interface{}, tagRetriever trigger.TagRetriever) error {
134+
func (r *buildConfigReactor) ImageChanged(obj interface{}, tagRetriever trigger.TagRetriever) error {
119135
bc := obj.(*buildapi.BuildConfig)
120136

121137
var request *buildapi.BuildRequest
@@ -193,6 +209,11 @@ func (r *BuildConfigReactor) ImageChanged(obj interface{}, tagRetriever trigger.
193209

194210
// instantiate new build
195211
glog.V(4).Infof("Requesting build for BuildConfig based on image triggers %s/%s: %#v", bc.Namespace, bc.Name, request)
196-
_, err := r.Instantiator.Instantiate(bc.Namespace, request)
212+
_, err := r.instantiator.Instantiate(bc.Namespace, request)
213+
if err != nil {
214+
instantiateErr := fmt.Errorf("error triggering Build for BuildConfig %s/%s: %v", bc.Namespace, bc.Name, err)
215+
utilruntime.HandleError(instantiateErr)
216+
r.eventRecorder.Event(bc, kapi.EventTypeWarning, "BuildConfigTriggerFailed", instantiateErr.Error())
217+
}
197218
return err
198219
}

pkg/image/trigger/buildconfigs/buildconfigs_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ func TestBuildConfigReactor(t *testing.T) {
246246

247247
for i, test := range testCases {
248248
instantiator := &instantiator{build: test.response}
249-
r := BuildConfigReactor{Instantiator: instantiator}
249+
r := buildConfigReactor{instantiator: instantiator}
250250
initial, err := kapi.Scheme.DeepCopy(test.obj)
251251
if err != nil {
252252
t.Fatal(err)

0 commit comments

Comments
 (0)