-
-
Notifications
You must be signed in to change notification settings - Fork 10
feat(start): wait for cluster readiness on Kind/K3d cluster start #4897
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2d7e30b
feat(start): wait for cluster readiness on Kind/K3d cluster start
devantler 3b4bbad
refactor(k8s): canonicalize kubeconfig path and speed up readiness po…
devantler 30aabb1
Merge branch 'main' into claude/exciting-heyrovsky-6d1681
devantler 6d2a270
fix(k3d): store trimmed kubeconfig path in WithKubeconfig
devantler 9a7f2aa
Merge branch 'main' into claude/exciting-heyrovsky-6d1681
devantler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package k8s | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "k8s.io/client-go/kubernetes" | ||
| ) | ||
|
|
||
| // WaitForAuthorizedReadForTest exposes waitForAuthorizedRead for unit testing | ||
| // with a fake clientset, avoiding the need for a real API server. | ||
| func WaitForAuthorizedReadForTest(ctx context.Context, clientset kubernetes.Interface) error { | ||
| return waitForAuthorizedRead(ctx, clientset) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| package k8s_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/devantler-tech/ksail/v7/pkg/k8s" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| k8sfake "k8s.io/client-go/kubernetes/fake" | ||
| k8stesting "k8s.io/client-go/testing" | ||
| ) | ||
|
|
||
| // TestWaitForAuthorizedRead_Succeeds verifies the authorized read returns nil | ||
| // as soon as listing namespaces succeeds. | ||
| func TestWaitForAuthorizedRead_Succeeds(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| clientset := k8sfake.NewClientset() | ||
|
|
||
| err := k8s.WaitForAuthorizedReadForTest(context.Background(), clientset) | ||
|
|
||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| // TestWaitForAuthorizedRead_RetriesTransientForbidden verifies a transient 403 | ||
| // (authorizer warm-up) is retried rather than treated as fatal, and the wait | ||
| // succeeds once the read is authorized. | ||
| func TestWaitForAuthorizedRead_RetriesTransientForbidden(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| clientset := k8sfake.NewClientset() | ||
|
|
||
|
devantler marked this conversation as resolved.
|
||
| var calls atomic.Int32 | ||
|
|
||
| clientset.PrependReactor( | ||
| "list", "namespaces", | ||
| func(_ k8stesting.Action) (bool, runtime.Object, error) { | ||
| // Fail the first call with Forbidden, then allow the default | ||
| // tracker to serve the (empty) list. | ||
| if calls.Add(1) == 1 { | ||
| return true, nil, apierrors.NewForbidden( | ||
| schema.GroupResource{Resource: "namespaces"}, | ||
| "", | ||
| assert.AnError, | ||
| ) | ||
| } | ||
|
|
||
| return false, nil, nil | ||
| }, | ||
| ) | ||
|
|
||
| err := k8s.WaitForAuthorizedReadForTest(context.Background(), clientset) | ||
|
|
||
| require.NoError(t, err) | ||
| assert.GreaterOrEqual(t, calls.Load(), int32(2), "expected the forbidden read to be retried") | ||
| } | ||
|
|
||
| // TestWaitForAuthorizedRead_TimesOut verifies the wait surfaces | ||
| // ErrClusterNotReady (wrapping the last error) when the read never succeeds | ||
| // before the context deadline. | ||
| func TestWaitForAuthorizedRead_TimesOut(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| clientset := k8sfake.NewClientset() | ||
| clientset.PrependReactor( | ||
| "list", "namespaces", | ||
| func(_ k8stesting.Action) (bool, runtime.Object, error) { | ||
| return true, nil, apierrors.NewForbidden( | ||
| schema.GroupResource{Resource: "namespaces"}, | ||
| "", | ||
| assert.AnError, | ||
| ) | ||
| }, | ||
| ) | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) | ||
| defer cancel() | ||
|
|
||
| err := k8s.WaitForAuthorizedReadForTest(ctx, clientset) | ||
|
|
||
| require.ErrorIs(t, err, k8s.ErrClusterNotReady) | ||
| } | ||
|
|
||
| // TestWaitForClusterReady_InvalidKubeconfig verifies the public entry point | ||
| // surfaces an error for a bogus context with no reachable API server (the | ||
| // API-server wait fails fast against the empty/short deadline). | ||
| func TestWaitForClusterReady_InvalidKubeconfig(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // An empty path resolves to the default kubeconfig; pair it with a | ||
| // context that will not exist so client construction or the API-server | ||
| // wait fails rather than hanging. | ||
| ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) | ||
| defer cancel() | ||
|
|
||
| err := k8s.WaitForClusterReady(ctx, t.TempDir()+"/missing-kubeconfig", "does-not-exist") | ||
|
|
||
| require.Error(t, err) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.