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: add logger to wait #135

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
34 changes: 29 additions & 5 deletions kubernetes/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ package kubernetes

import (
"context"
"fmt"
"io"
"log/slog"
"strings"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic"
Expand Down Expand Up @@ -37,7 +41,10 @@ func WatcherForConfig(cfg *rest.Config) (watcher.StatusWatcher, error) {
}

// WaitForReadyRuntime waits for all of the runtime objects to reach a ready state.
func WaitForReadyRuntime(ctx context.Context, sw watcher.StatusWatcher, robjs []runtime.Object) error {
func WaitForReadyRuntime(ctx context.Context, sw watcher.StatusWatcher, robjs []runtime.Object, logger *slog.Logger) error {
if logger == nil {
logger = slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
}
objs := []object.ObjMetadata{}
for _, robj := range robjs {
obj, err := object.RuntimeToObjMeta(robj)
Expand All @@ -46,11 +53,14 @@ func WaitForReadyRuntime(ctx context.Context, sw watcher.StatusWatcher, robjs []
}
objs = append(objs, obj)
}
return WaitForReady(ctx, sw, objs)
return WaitForReady(ctx, sw, objs, logger)
}

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

Expand All @@ -73,17 +83,31 @@ func WaitForReady(ctx context.Context, sw watcher.StatusWatcher, objs []object.O
}),
)
<-done

for _, id := range objs {
rs := statusCollector.ResourceStatuses[id]
switch rs.Status {
// TODO (@austinabro321) once callers have proper sloggers change logging here to use the slog style
case status.CurrentStatus:
logger.Debug(fmt.Sprintf("%s: %s ready", rs.Identifier.Name, strings.ToLower(rs.Identifier.GroupKind.Kind)))
case status.NotFoundStatus:
logger.Error(fmt.Sprintf("%s: %s not found", rs.Identifier.Name, strings.ToLower(rs.Identifier.GroupKind.Kind)))
default:
logger.Error(fmt.Sprintf("%s: %s not ready", rs.Identifier.Name, strings.ToLower(rs.Identifier.GroupKind.Kind)))
}
}

if statusCollector.Error != nil {
return statusCollector.Error
}
// Only check parent context error, otherwise we would error when desired status is acheived.
// Only check parent context error, otherwise we would error when desired status is achieved.
if ctx.Err() != nil {
return ctx.Err()
}
return nil
}

// ImmediateWatcher should only be used for testing and returns the set status immediatly.
// ImmediateWatcher should only be used for testing and returns the set status immediately.
type ImmediateWatcher struct {
status status.Status
}
Expand Down
14 changes: 12 additions & 2 deletions kubernetes/wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
package kubernetes

import (
"bytes"
"context"
"log/slog"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -15,6 +17,8 @@ import (
)

func TestWaitForReady(t *testing.T) {
var buf bytes.Buffer
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
sw := NewImmediateWatcher(status.CurrentStatus)
objs := []object.ObjMetadata{
{
Expand All @@ -26,11 +30,15 @@ func TestWaitForReady(t *testing.T) {
Name: "bar",
},
}
err := WaitForReady(context.Background(), sw, objs)
err := WaitForReady(context.Background(), sw, objs, logger)
require.NoError(t, err)
logOutput := buf.String()
require.Contains(t, logOutput, "bar: deployment ready")
}

func TestWaitForReadyCanceled(t *testing.T) {
var buf bytes.Buffer
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
ctx, cancel := context.WithCancel(context.Background())
cancel()
sw := watcher.BlindStatusWatcher{}
Expand All @@ -44,6 +52,8 @@ func TestWaitForReadyCanceled(t *testing.T) {
Name: "bar",
},
}
err := WaitForReady(ctx, sw, objs)
err := WaitForReady(ctx, sw, objs, logger)
require.EqualError(t, err, "context canceled")
logOutput := buf.String()
require.Contains(t, logOutput, "bar: deployment not ready")
}
Loading