Skip to content

Commit a7484fe

Browse files
committed
generate build state events
1 parent d521c48 commit a7484fe

File tree

16 files changed

+318
-93
lines changed

16 files changed

+318
-93
lines changed

pkg/build/api/types.go

+25
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,31 @@ const (
6363
// forces the build to be processed by the build controller queue without waiting
6464
// for a resync.
6565
BuildAcceptedAnnotation = "build.openshift.io/accepted"
66+
67+
// BuildCreatedEvent is the reason associated with the event registered when a build is created.
68+
BuildCreatedEventReason = "BuildCreated"
69+
// BuildCreatedEventMessage is the message associatd with the event registered when a build is created.
70+
BuildCreatedEventMessage = "Build %s/%s has been created"
71+
// BuildStartedEvent is the reason associated with the event registered when a build is started (pod is created).
72+
BuildStartedEventReason = "BuildStarted"
73+
// BuildStartedEvent is the message associated with the event registered when a build is started (pod is created).
74+
BuildStartedEventMessage = "Pod has been created to run build %s/%s"
75+
// BuildRunningEvent is the reason associated with the event registered when the build pod starts running.
76+
BuildRunningEventReason = "BuildRunning"
77+
// BuildRunningEvent is the message associated with the event registered when the build pod starts running.
78+
BuildRunningEventMessage = "Pod for build %s/%s started running"
79+
// BuildCompletedEvent is the reason associated with the event registered when build completes successfully.
80+
BuildCompletedEventReason = "BuildCompleted"
81+
// BuildCompletedEvent is the message associated with the event registered when build completes successfully.
82+
BuildCompletedEventMessage = "Build %s/%s completed successfully"
83+
// BuildFailedEvent is the reason associated with the event registered when build fails.
84+
BuildFailedEventReason = "BuildFailed"
85+
// BuildFailedEvent is the message associated with the event registered when build fails.
86+
BuildFailedEventMessage = "Build %s/%s failed"
87+
// BuildCancelledEvent is the reason associated with the event registered when build is cancelled.
88+
BuildCancelledEventReason = "BuildCancelled"
89+
// BuildCancelledEvent is the message associated with the event registered when build is cancelled.
90+
BuildCancelledEventMessage = "Build %s/%s has been cancelled"
6691
)
6792

6893
// +genclient=true

pkg/build/client/clients.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ func (c OSClientBuildConfigClient) Update(buildConfig *buildapi.BuildConfig) err
3737
return err
3838
}
3939

40+
// BuildGetter provides methods for getting existing Builds.
41+
type BuildGetter interface {
42+
Get(namespace, name string) (*buildapi.Build, error)
43+
}
44+
4045
// BuildUpdater provides methods for updating existing Builds.
4146
type BuildUpdater interface {
4247
Update(namespace string, build *buildapi.Build) error
@@ -47,7 +52,7 @@ type BuildLister interface {
4752
List(namespace string, opts kapi.ListOptions) (*buildapi.BuildList, error)
4853
}
4954

50-
// OSClientBuildClient deletes build create and update operations to the OpenShift client interface
55+
// OSClientBuildClient delegates build create and update operations to the OpenShift client interface
5156
type OSClientBuildClient struct {
5257
Client osclient.Interface
5358
}
@@ -57,6 +62,11 @@ func NewOSClientBuildClient(client osclient.Interface) *OSClientBuildClient {
5762
return &OSClientBuildClient{Client: client}
5863
}
5964

65+
// Get returns a Build using the OpenShift client.
66+
func (c OSClientBuildClient) Get(namespace, name string) (*buildapi.Build, error) {
67+
return c.Client.Builds(namespace).Get(name)
68+
}
69+
6070
// Update updates builds using the OpenShift client.
6171
func (c OSClientBuildClient) Update(namespace string, build *buildapi.Build) error {
6272
_, e := c.Client.Builds(namespace).Update(build)

pkg/build/controller/buildpod/controller.go

+16
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"k8s.io/kubernetes/pkg/client/cache"
1212
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
1313
kcoreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
14+
"k8s.io/kubernetes/pkg/client/record"
1415
kcontroller "k8s.io/kubernetes/pkg/controller"
1516
utilruntime "k8s.io/kubernetes/pkg/util/runtime"
1617
"k8s.io/kubernetes/pkg/util/wait"
@@ -49,16 +50,22 @@ type BuildPodController struct {
4950
podStoreSynced func() bool
5051

5152
runPolicies []policy.RunPolicy
53+
54+
recorder record.EventRecorder
5255
}
5356

5457
// NewBuildPodController creates a new BuildPodController.
5558
func NewBuildPodController(buildInformer, podInformer cache.SharedIndexInformer, kc kclientset.Interface, oc osclient.Interface) *BuildPodController {
59+
eventBroadcaster := record.NewBroadcaster()
60+
eventBroadcaster.StartRecordingToSink(&kcoreclient.EventSinkImpl{Interface: kc.Core().Events("")})
61+
5662
buildListerUpdater := buildclient.NewOSClientBuildClient(oc)
5763
c := &BuildPodController{
5864
buildUpdater: buildListerUpdater,
5965
secretClient: kc.Core(), // TODO: Replace with cache client
6066
podClient: kc.Core(),
6167
queue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()),
68+
recorder: eventBroadcaster.NewRecorder(kapi.EventSource{Component: "build-pod-controller"}),
6269
}
6370

6471
c.runPolicies = policy.GetAllRunPolicies(buildListerUpdater, buildListerUpdater)
@@ -197,6 +204,7 @@ func (bc *BuildPodController) HandlePod(pod *kapi.Pod) error {
197204
if build.Status.Phase == buildapi.BuildPhaseRunning {
198205
now := unversioned.Now()
199206
build.Status.StartTimestamp = &now
207+
bc.recorder.Eventf(build, kapi.EventTypeNormal, buildapi.BuildRunningEventReason, fmt.Sprintf(buildapi.BuildRunningEventMessage, build.Namespace, build.Name))
200208
}
201209
}
202210

@@ -218,6 +226,12 @@ func (bc *BuildPodController) HandlePod(pod *kapi.Pod) error {
218226
// handle completion for it. otherwise ignore it because we've already
219227
// handled its completion previously.
220228
if !buildWasComplete && buildutil.IsBuildComplete(build) {
229+
switch build.Status.Phase {
230+
case buildapi.BuildPhaseComplete:
231+
bc.recorder.Eventf(build, kapi.EventTypeNormal, buildapi.BuildCompletedEventReason, fmt.Sprintf(buildapi.BuildCompletedEventMessage, build.Namespace, build.Name))
232+
case buildapi.BuildPhaseError, buildapi.BuildPhaseFailed:
233+
bc.recorder.Eventf(build, kapi.EventTypeNormal, buildapi.BuildFailedEventReason, fmt.Sprintf(buildapi.BuildFailedEventMessage, build.Namespace, build.Name))
234+
}
221235
common.HandleBuildCompletion(build, bc.runPolicies)
222236
}
223237

@@ -259,6 +273,8 @@ func (bc *BuildPodController) HandleBuildPodDeletion(pod *kapi.Pod) error {
259273
build.Status.Reason = buildapi.StatusReasonBuildPodDeleted
260274
build.Status.Message = buildapi.StatusMessageBuildPodDeleted
261275
common.SetBuildCompletionTimeAndDuration(build)
276+
bc.recorder.Eventf(build, kapi.EventTypeNormal, buildapi.BuildFailedEventReason, fmt.Sprintf(buildapi.BuildFailedEventMessage, build.Namespace, build.Name))
277+
262278
if err := bc.buildUpdater.Update(build.Namespace, build); err != nil {
263279
return fmt.Errorf("Failed to update build %s/%s: %v", build.Namespace, build.Name, err)
264280
}

pkg/build/controller/controller.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func (bc *BuildController) CancelBuild(build *buildapi.Build) error {
7272

7373
build.Status.Phase = buildapi.BuildPhaseCancelled
7474
common.SetBuildCompletionTimeAndDuration(build)
75+
bc.Recorder.Eventf(build, kapi.EventTypeNormal, buildapi.BuildCancelledEventReason, fmt.Sprintf(buildapi.BuildCancelledEventMessage, build.Namespace, build.Name))
7576
// set the status details for the cancelled build before updating the build
7677
// object.
7778
build.Status.Reason = buildapi.StatusReasonCancelledBuild
@@ -219,8 +220,9 @@ func (bc *BuildController) nextBuildPhase(build *buildapi.Build) error {
219220
build.Status.Message = buildapi.StatusMessageCannotCreateBuildPod
220221
return fmt.Errorf("failed to create build pod: %v", err)
221222
}
222-
common.SetBuildPodNameAnnotation(build, podSpec.Name)
223223
glog.V(4).Infof("Created pod for build: %#v", podSpec)
224+
bc.Recorder.Eventf(build, kapi.EventTypeNormal, buildapi.BuildStartedEventReason, fmt.Sprintf(buildapi.BuildStartedEventMessage, build.Namespace, build.Name))
225+
common.SetBuildPodNameAnnotation(build, podSpec.Name)
224226

225227
// Set the build phase, which will be persisted.
226228
build.Status.Phase = buildapi.BuildPhasePending

pkg/build/controller/image_change_controller_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
buildapi "github.com/openshift/origin/pkg/build/api"
1414
buildtest "github.com/openshift/origin/pkg/build/controller/test"
1515
buildgenerator "github.com/openshift/origin/pkg/build/generator"
16+
mocks "github.com/openshift/origin/pkg/build/generator/test"
1617
"github.com/openshift/origin/pkg/cmd/server/bootstrappolicy"
1718
imageapi "github.com/openshift/origin/pkg/image/api"
1819
)
@@ -386,7 +387,11 @@ func mockBuildConfigInstantiator(buildcfg *buildapi.BuildConfig, imageStream *im
386387
}
387388
instantiator := &buildConfigInstantiator{}
388389
instantiator.buildConfigUpdater = &mockBuildConfigUpdater{}
390+
recorder := &mocks.MockEventRecorder{}
391+
buildGetter := &mocks.MockBuildGetter{}
389392
generator := buildgenerator.BuildGenerator{
393+
Builds: buildGetter,
394+
Recorder: recorder,
390395
Secrets: fake.NewSimpleClientset().Core(),
391396
ServiceAccounts: fake.NewSimpleClientset(&builderAccount).Core(),
392397
Client: buildgenerator.Client{

pkg/build/generator/generator.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ import (
1313
"k8s.io/kubernetes/pkg/api/errors"
1414
"k8s.io/kubernetes/pkg/api/unversioned"
1515
kcoreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
16+
"k8s.io/kubernetes/pkg/client/record"
1617
"k8s.io/kubernetes/pkg/credentialprovider"
1718
kvalidation "k8s.io/kubernetes/pkg/util/validation"
1819

1920
buildapi "github.com/openshift/origin/pkg/build/api"
21+
buildclient "github.com/openshift/origin/pkg/build/client"
2022
buildutil "github.com/openshift/origin/pkg/build/util"
2123
"github.com/openshift/origin/pkg/cmd/admin/policy"
2224
"github.com/openshift/origin/pkg/cmd/server/bootstrappolicy"
@@ -49,6 +51,8 @@ type BuildGenerator struct {
4951
DefaultServiceAccountName string
5052
ServiceAccounts kcoreclient.ServiceAccountsGetter
5153
Secrets kcoreclient.SecretsGetter
54+
Recorder record.EventRecorder
55+
Builds buildclient.BuildGetter
5256
}
5357

5458
// GeneratorClient is the API client used by the generator
@@ -430,7 +434,12 @@ func (g *BuildGenerator) createBuild(ctx kapi.Context, build *buildapi.Build) (*
430434
if err != nil {
431435
return nil, err
432436
}
433-
return g.Client.GetBuild(ctx, build.Name)
437+
b, err := g.Builds.Get(build.Namespace, build.Name)
438+
//b, err := g.Client.GetBuild(ctx, build.Name)
439+
if err == nil {
440+
g.Recorder.Eventf(b, kapi.EventTypeNormal, buildapi.BuildCreatedEventReason, fmt.Sprintf(buildapi.BuildCreatedEventMessage, build.Namespace, build.Name))
441+
}
442+
return build, err
434443
}
435444

436445
// generateBuildFromConfig generates a build definition based on the current imageid

0 commit comments

Comments
 (0)