Skip to content

Commit 4d48796

Browse files
committed
improve error messages
Signed-off-by: Austin Abro <[email protected]>
1 parent d62ed80 commit 4d48796

File tree

2 files changed

+23
-39
lines changed

2 files changed

+23
-39
lines changed

kubernetes/wait.go

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ package kubernetes
55

66
import (
77
"context"
8+
"errors"
89
"fmt"
9-
"io"
10-
"log/slog"
11-
"strings"
1210

1311
"k8s.io/apimachinery/pkg/runtime"
1412
"k8s.io/client-go/dynamic"
@@ -41,10 +39,7 @@ func WatcherForConfig(cfg *rest.Config) (watcher.StatusWatcher, error) {
4139
}
4240

4341
// WaitForReadyRuntime waits for all of the runtime objects to reach a ready state.
44-
func WaitForReadyRuntime(ctx context.Context, sw watcher.StatusWatcher, robjs []runtime.Object, logger *slog.Logger) error {
45-
if logger == nil {
46-
logger = slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
47-
}
42+
func WaitForReadyRuntime(ctx context.Context, sw watcher.StatusWatcher, robjs []runtime.Object) error {
4843
objs := []object.ObjMetadata{}
4944
for _, robj := range robjs {
5045
obj, err := object.RuntimeToObjMeta(robj)
@@ -53,14 +48,11 @@ func WaitForReadyRuntime(ctx context.Context, sw watcher.StatusWatcher, robjs []
5348
}
5449
objs = append(objs, obj)
5550
}
56-
return WaitForReady(ctx, sw, objs, logger)
51+
return WaitForReady(ctx, sw, objs)
5752
}
5853

5954
// WaitForReady waits for all of the objects to reach a ready state.
60-
func WaitForReady(ctx context.Context, sw watcher.StatusWatcher, objs []object.ObjMetadata, logger *slog.Logger) error {
61-
if logger == nil {
62-
logger = slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
63-
}
55+
func WaitForReady(ctx context.Context, sw watcher.StatusWatcher, objs []object.ObjMetadata) error {
6456
cancelCtx, cancel := context.WithCancel(ctx)
6557
defer cancel()
6658

@@ -84,26 +76,27 @@ func WaitForReady(ctx context.Context, sw watcher.StatusWatcher, objs []object.O
8476
)
8577
<-done
8678

87-
for _, id := range objs {
88-
rs := statusCollector.ResourceStatuses[id]
89-
switch rs.Status {
90-
// TODO (@austinabro321) once callers have proper sloggers change logging here to use the slog style
91-
case status.CurrentStatus:
92-
logger.Debug(fmt.Sprintf("%s: %s ready", rs.Identifier.Name, strings.ToLower(rs.Identifier.GroupKind.Kind)))
93-
case status.NotFoundStatus:
94-
logger.Error(fmt.Sprintf("%s: %s not found", rs.Identifier.Name, strings.ToLower(rs.Identifier.GroupKind.Kind)))
95-
default:
96-
logger.Error(fmt.Sprintf("%s: %s not ready", rs.Identifier.Name, strings.ToLower(rs.Identifier.GroupKind.Kind)))
97-
}
98-
}
99-
10079
if statusCollector.Error != nil {
10180
return statusCollector.Error
10281
}
82+
10383
// Only check parent context error, otherwise we would error when desired status is achieved.
10484
if ctx.Err() != nil {
105-
return ctx.Err()
85+
errs := []error{}
86+
for _, id := range objs {
87+
rs := statusCollector.ResourceStatuses[id]
88+
switch rs.Status {
89+
case status.CurrentStatus:
90+
case status.NotFoundStatus:
91+
errs = append(errs, fmt.Errorf("%s: %s not found", rs.Identifier.Name, rs.Identifier.GroupKind.Kind))
92+
default:
93+
errs = append(errs, fmt.Errorf("%s: %s not ready", rs.Identifier.Name, rs.Identifier.GroupKind.Kind))
94+
}
95+
}
96+
errs = append(errs, ctx.Err())
97+
return errors.Join(errs...)
10698
}
99+
107100
return nil
108101
}
109102

kubernetes/wait_test.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
package kubernetes
55

66
import (
7-
"bytes"
87
"context"
9-
"log/slog"
108
"testing"
119

1210
"github.com/stretchr/testify/require"
@@ -17,8 +15,6 @@ import (
1715
)
1816

1917
func TestWaitForReady(t *testing.T) {
20-
var buf bytes.Buffer
21-
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
2218
sw := NewImmediateWatcher(status.CurrentStatus)
2319
objs := []object.ObjMetadata{
2420
{
@@ -30,15 +26,11 @@ func TestWaitForReady(t *testing.T) {
3026
Name: "bar",
3127
},
3228
}
33-
err := WaitForReady(context.Background(), sw, objs, logger)
29+
err := WaitForReady(context.Background(), sw, objs)
3430
require.NoError(t, err)
35-
logOutput := buf.String()
36-
require.Contains(t, logOutput, "bar: deployment ready")
3731
}
3832

3933
func TestWaitForReadyCanceled(t *testing.T) {
40-
var buf bytes.Buffer
41-
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
4234
ctx, cancel := context.WithCancel(context.Background())
4335
cancel()
4436
sw := watcher.BlindStatusWatcher{}
@@ -52,8 +44,7 @@ func TestWaitForReadyCanceled(t *testing.T) {
5244
Name: "bar",
5345
},
5446
}
55-
err := WaitForReady(ctx, sw, objs, logger)
56-
require.EqualError(t, err, "context canceled")
57-
logOutput := buf.String()
58-
require.Contains(t, logOutput, "bar: deployment not ready")
47+
err := WaitForReady(ctx, sw, objs)
48+
require.ErrorIs(t, err, context.Canceled)
49+
require.ErrorContains(t, err, "bar: Deployment not ready")
5950
}

0 commit comments

Comments
 (0)