Skip to content
Open
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
50 changes: 46 additions & 4 deletions internal/cmd/create/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,17 +431,22 @@ func (cmd *applicationCmd) warnDeprecatedBuildFlags() {
}

type buildError struct {
build *apps.Build
build *apps.Build
timedOut bool
}

func (b buildError) Error() string {
if b.timedOut {
return fmt.Sprintf("timeout waiting for build, last status was %s", b.build.Status.AtProvider.BuildStatus)
}
return fmt.Sprintf("build failed with status %s", b.build.Status.AtProvider.BuildStatus)
}

// Build returns the build that caused the error.
func (b buildError) Build() *apps.Build { return b.build }

func waitForBuildStart(app *apps.Application) waiter.Stage {
var lastBuild *apps.Build
return waiter.Stage{
Kind: strings.ToLower(apps.BuildKind),
ObjectList: &apps.BuildList{},
Expand All @@ -461,6 +466,7 @@ func waitForBuildStart(app *apps.Application) waiter.Stage {
if !ok {
return false, nil
}
lastBuild = build

switch build.Status.AtProvider.BuildStatus {
case buildStatusRunning:
Expand All @@ -473,6 +479,12 @@ func waitForBuildStart(app *apps.Application) waiter.Stage {

return false, nil
},
OnTimeout: func() error {
if lastBuild == nil {
return nil
}
return buildError{build: lastBuild, timedOut: true}
},
}
}

Expand All @@ -491,6 +503,7 @@ func waitForBuildFinish(
tea.WithInput(nil),
tea.WithContext(ctx),
)
var lastBuild *apps.Build
return waiter.Stage{
DisableSpinner: true,
Kind: strings.ToLower(apps.BuildKind),
Expand Down Expand Up @@ -540,6 +553,7 @@ func waitForBuildFinish(
if !ok {
return false, nil
}
lastBuild = build

switch build.Status.AtProvider.BuildStatus {
case buildStatusSuccess:
Expand All @@ -554,11 +568,18 @@ func waitForBuildFinish(

return false, nil
},
OnTimeout: func() error {
if lastBuild == nil {
return nil
}
return buildError{build: lastBuild, timedOut: true}
},
}
}

type releaseError struct {
release *apps.Release
release *apps.Release
timedOut bool
}

func (r releaseError) Error() string {
Expand All @@ -571,13 +592,17 @@ func (r releaseError) Error() string {
return "deploy job of release timed out. Increase the timeout or ensure it finishes earlier."
}
}
if r.timedOut {
return fmt.Sprintf("timeout waiting for release, last status was %s", r.release.Status.AtProvider.ReleaseStatus)
}
return fmt.Sprintf("release failed with status %s", r.release.Status.AtProvider.ReleaseStatus)
}

// Release returns the release that caused the error.
func (r releaseError) Release() *apps.Release { return r.release }

func waitForRelease(app *apps.Application) waiter.Stage {
var lastRelease *apps.Release
return waiter.Stage{
Kind: strings.ToLower(apps.ReleaseKind),
ObjectList: &apps.ReleaseList{},
Expand All @@ -598,6 +623,7 @@ func waitForRelease(app *apps.Application) waiter.Stage {
if !ok {
return false, nil
}
lastRelease = release

switch release.Status.AtProvider.ReleaseStatus {
case releaseStatusAvailable:
Expand All @@ -608,6 +634,12 @@ func waitForRelease(app *apps.Application) waiter.Stage {

return false, nil
},
OnTimeout: func() error {
if lastRelease == nil {
return nil
}
return releaseError{release: lastRelease, timedOut: true}
},
}
}

Expand Down Expand Up @@ -687,17 +719,27 @@ func (cmd *applicationCmd) printErrorDetails(
err error,
) error {
if buildErr, ok := errors.AsType[buildError](err); ok {
verb := "has failed"
if buildErr.timedOut {
verb = "did not finish in time"
}
cmd.Infof(
"❌", "Your build has failed with status %q. Here are the last %v lines of the log:",
"❌", "Your build %s with status %q. Here are the last %v lines of the log:",
verb,
buildErr.Build().Status.AtProvider.BuildStatus,
errorLogLines,
)
return printBuildLogs(ctx, client, buildErr.Build())
}

if releaseErr, ok := errors.AsType[releaseError](err); ok {
verb := "has failed"
if releaseErr.timedOut {
verb = "did not finish in time"
}
cmd.Infof(
"❌", "Your release has failed with status %q. Here are the last %v lines of the log:",
"❌", "Your release %s with status %q. Here are the last %v lines of the log:",
verb,
releaseErr.Release().Status.AtProvider.ReleaseStatus,
errorLogLines,
)
Expand Down
91 changes: 91 additions & 0 deletions internal/cmd/create/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package create
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -900,6 +902,95 @@ func TestApplicationBuildFail(t *testing.T) {
is.Equal(testutil.CountLines(buf.String()), errorLogLines)
}

// TestApplicationBuildTimeout covers a build that never reaches a terminal
// status before the wait times out: it must still print logs, the same as an
// outright build failure, instead of the bare timeout message it used to get.
func TestApplicationBuildTimeout(t *testing.T) {
t.Parallel()

is := require.New(t)
cmd := applicationCmd{
Wait: true,
WaitTimeout: 300 * time.Millisecond,
Name: "some-name",
SkipRepoAccessCheck: true,
}
project := testutil.DefaultProject

build := &apps.Build{
Name: "any-name",
Namespace: project,
Labels: map[string]string{
application.ApplicationNameLabel: cmd.Name,
},
}

client := testutil.SetupClient(t, testutil.WithObjects(build))

var buf bytes.Buffer
out, err := output.NewLogOutput(&buf, log.Mode("default"), &output.LogOutputOptions{
NoLabels: true, ColoredOutput: false, Timezone: time.Local,
})
is.NoError(err)

// fill our logs so we have more than errorLogLines
logString := "still building"
buildLog := []string{}
for range errorLogLines + 30 {
buildLog = append(buildLog, logString)
}
client.Log = &log.Client{Client: log.NewFake(t, time.Now(), buildLog...), StdOut: out}

ctx := t.Context()

// the build is repeatedly touched, but never reaches a terminal status,
// so the wait for it to start has to time out while a build is known.
ticker := time.NewTicker(20 * time.Millisecond)
done := make(chan bool)
errCh := make(chan error, 1)

go func() {
for {
select {
case <-done:
close(errCh)
return
case <-ticker.C:
app := &apps.Application{
Name: cmd.Name,
Namespace: project}
if err := setResourceCondition(ctx, client, app, runtimev1.ReconcileSuccess()); err != nil {
errCh <- err
}

if err := client.Get(ctx, api.ObjectName(build), build); err != nil {
errCh <- err
}
build.Status.AtProvider.Stack = fmt.Sprintf("touch-%d", time.Now().UnixNano())
if err := client.Update(ctx, build); err != nil {
errCh <- err
}
}
}
}()

runErr := cmd.Run(ctx, client)
ticker.Stop()
done <- true
for e := range errCh {
t.Fatal(e)
}

is.Error(runErr)
buildErr, ok := errors.AsType[buildError](runErr)
is.True(ok, "expected a buildError, got %T: %v", runErr, runErr)
is.True(buildErr.timedOut)
is.Equal(build.Name, buildErr.Build().Name)

is.Contains(buf.String(), logString)
is.Equal(testutil.CountLines(buf.String()), errorLogLines)
}

func setResourceCondition(ctx context.Context, apiClient *api.Client, mg resource.Managed, condition runtimev1.Condition) error {
if err := apiClient.Get(ctx, api.ObjectName(mg), mg); err != nil {
return err
Expand Down
22 changes: 16 additions & 6 deletions internal/waiter/waiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ type Waiter struct {
type Stage struct {
writer format.Writer

Kind string
WaitMessage *Message
DoneMessage *Message
ObjectList runtimeclient.ObjectList
ListOpts []runtimeclient.ListOption
OnResult ResultFunc
Kind string
WaitMessage *Message
DoneMessage *Message
ObjectList runtimeclient.ObjectList
ListOpts []runtimeclient.ListOption
OnResult ResultFunc
// OnTimeout, if set, is called when the wait times out. If it returns a
// non-nil error, that error is returned instead of the generic timeout
// error, e.g. to report the last known state of the watched object.
OnTimeout func() error
spinner *yacspin.Spinner
DisableSpinner bool
startTime time.Time
Expand Down Expand Up @@ -219,6 +223,12 @@ func (w *Stage) watch(ctx context.Context, client *api.Client) error {
w.spinner.StopFailMessage(format.Progressf("", msg, w.Kind))
_ = w.spinner.StopFail()

if w.OnTimeout != nil {
if err := w.OnTimeout(); err != nil {
return err
}
}

return fmt.Errorf(msg, w.Kind)
case context.Canceled:
_ = w.spinner.StopFail()
Expand Down
Loading