Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(build): Better report build progress #1064

Merged
merged 2 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions deploy/crd-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ spec:
description: The build phase
JSONPath: .status.phase
- name: Age
type: date
description: The time at which the build was created
JSONPath: .metadata.creationTimestamp
- name: Started
type: date
description: The time at which the build was last (re-)started
JSONPath: .status.startedAt
Expand Down
18 changes: 14 additions & 4 deletions pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,17 @@ func New(c client.Client) Builder {
}

// Build --
func (b *defaultBuilder) Build(build v1alpha1.BuildSpec) v1alpha1.BuildStatus {
result := v1alpha1.BuildStatus{}
func (b *defaultBuilder) Build(build v1alpha1.BuildSpec) <-chan v1alpha1.BuildStatus {
channel := make(chan v1alpha1.BuildStatus)
go b.build(build, channel)
return channel
}

func (b *defaultBuilder) build(build v1alpha1.BuildSpec, channel chan<- v1alpha1.BuildStatus) {
result := v1alpha1.BuildStatus{}
result.Phase = v1alpha1.BuildPhaseRunning
result.StartedAt = metav1.Now()
channel <- result

// create tmp path
buildDir := build.BuildDir
Expand Down Expand Up @@ -115,7 +122,9 @@ func (b *defaultBuilder) Build(build v1alpha1.BuildSpec) v1alpha1.BuildStatus {

if result.Phase == v1alpha1.BuildPhaseFailed {
result.Duration = metav1.Now().Sub(result.StartedAt.Time).String()
return result
channel <- result
close(channel)
return
}

steps := make([]Step, 0)
Expand Down Expand Up @@ -187,5 +196,6 @@ func (b *defaultBuilder) Build(build v1alpha1.BuildSpec) v1alpha1.BuildStatus {
b.log.Infof("build request %s interrupted after %s", build.Meta.Name, result.Duration)
}

return result
channel <- result
close(channel)
}
12 changes: 9 additions & 3 deletions pkg/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,14 @@ func TestFailure(t *testing.T) {
},
}

result := b.Build(r)
progress := b.Build(r)

assert.NotNil(t, result)
assert.Equal(t, v1alpha1.BuildPhaseFailed, result.Phase)
status := make([]v1alpha1.BuildStatus, 0)
for s := range progress {
status = append(status, s)
}

assert.Len(t, status, 2)
assert.Equal(t, v1alpha1.BuildPhaseRunning, status[0].Phase)
assert.Equal(t, v1alpha1.BuildPhaseFailed, status[1].Phase)
}
2 changes: 1 addition & 1 deletion pkg/builder/builder_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const (

// Builder --
type Builder interface {
Build(build v1alpha1.BuildSpec) v1alpha1.BuildStatus
Build(build v1alpha1.BuildSpec) <-chan v1alpha1.BuildStatus
}

// Step --
Expand Down
13 changes: 3 additions & 10 deletions pkg/cmd/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,10 @@ func Run(namespace string, buildName string) {
c.Get(ctx, types.NamespacedName{Namespace: build.Namespace, Name: build.Name}, build),
)

status := v1alpha1.BuildStatus{
Phase: v1alpha1.BuildPhaseRunning,
progress := builder.New(c).Build(build.Spec)
for status := range progress {
exitOnError(util.UpdateBuildStatus(ctx, build, status, c, log))
}
exitOnError(
util.UpdateBuildStatus(ctx, build, status, c, log),
)

status = builder.New(c).Build(build.Spec)
exitOnError(
util.UpdateBuildStatus(ctx, build, status, c, log),
)

switch build.Status.Phase {
case v1alpha1.BuildPhaseSucceeded:
Expand Down
40 changes: 17 additions & 23 deletions pkg/controller/build/schedule_routine.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,42 +71,36 @@ func (action *scheduleRoutineAction) Handle(ctx context.Context, build *v1alpha1

// Emulate a serialized working queue to only allow one build to run at a given time.
// This is currently necessary for the incremental build to work as expected.
hasScheduledBuild := false
for _, b := range builds.Items {
if b.Status.Phase == v1alpha1.BuildPhasePending || b.Status.Phase == v1alpha1.BuildPhaseRunning {
hasScheduledBuild = true
break
// Let's requeue the build in case one is already running
return nil, nil
}
}

if hasScheduledBuild {
// Let's requeue the build in case one is already running
return nil, nil
}

// Transition the build to running state
// Transition the build to pending state
// This must be done in the critical section rather than delegated to the controller
target := build.DeepCopy()
target.Status.Phase = v1alpha1.BuildPhaseRunning
target.Status.Phase = v1alpha1.BuildPhasePending
action.L.Info("Build state transition", "phase", target.Status.Phase)
err = action.client.Status().Update(ctx, target)
if err != nil {
return nil, err
}

// and run it asynchronously to avoid blocking the reconcile loop
// Start the build
progress := action.builder.Build(build.Spec)
// And follow the build progress asynchronously to avoid blocking the reconcile loop
go func() {
for status := range progress {
err := UpdateBuildStatus(ctx, build, status, action.client, action.L)
if err != nil {
action.L.Errorf(err, "Error while updating build status: %s", build.Name)
}
}
}()

action.routines.Store(build.Name, true)
go action.build(ctx, build)

return nil, nil
}

func (action *scheduleRoutineAction) build(ctx context.Context, build *v1alpha1.Build) {
defer action.routines.Delete(build.Name)

status := action.builder.Build(build.Spec)

err := UpdateBuildStatus(ctx, build, status, action.client, action.L)
if err != nil {
action.L.Errorf(err, "Error while running build: %s", build.Name)
}
}