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 immediate watcher and test wait ready #104

Merged
merged 1 commit into from
Jun 20, 2024
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
28 changes: 28 additions & 0 deletions kubernetes/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,31 @@ func WaitForReady(ctx context.Context, sw watcher.StatusWatcher, objs []object.O
}
return nil
}

// ImmediateWatcher should only be used for testing and returns the set status immediatly.
type ImmediateWatcher struct {
status status.Status
}

// NewImmediateWatcher returns a ImmediateWatcher.
func NewImmediateWatcher(status status.Status) *ImmediateWatcher {
return &ImmediateWatcher{
status: status,
}
}

// Watch watches the given objects and immediatly returns the configured status.
func (w *ImmediateWatcher) Watch(_ context.Context, objs object.ObjMetadataSet, _ watcher.Options) <-chan event.Event {
eventCh := make(chan event.Event, len(objs))
for _, obj := range objs {
eventCh <- event.Event{
Type: event.ResourceUpdateEvent,
Resource: &event.ResourceStatus{
Identifier: obj,
Status: w.status,
},
}
}
close(eventCh)
return eventCh
}
17 changes: 17 additions & 0 deletions kubernetes/wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,27 @@ import (

"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/cli-utils/pkg/kstatus/status"
"sigs.k8s.io/cli-utils/pkg/kstatus/watcher"
"sigs.k8s.io/cli-utils/pkg/object"
)

func TestWaitForReady(t *testing.T) {
sw := NewImmediateWatcher(status.CurrentStatus)
objs := []object.ObjMetadata{
{
GroupKind: schema.GroupKind{
Group: "apps",
Kind: "Deployment",
},
Namespace: "foo",
Name: "bar",
},
}
err := WaitForReady(context.Background(), sw, objs)
require.NoError(t, err)
}

func TestWaitForReadyCanceled(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
Expand Down
Loading