Skip to content

Fall back to leader address when joining controllers (#1103) - #1106

Draft
kke wants to merge 1 commit into
mainfrom
fix/join-via-leader-address
Draft

Fall back to leader address when joining controllers (#1103)#1106
kke wants to merge 1 commit into
mainfrom
fix/join-via-leader-address

Conversation

@kke

@kke kke commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Fixes #1103 (or tries to)

When spec.api.externalAddress is an externally managed VIP the join token's embedded server URL points at the VIP. During bootstrap the VIP may be assigned to a controller that is not yet running, so both k0sctl'spreflight API check and the actual join fail non-deterministically depending on which node currently holds the VIP.

If the token-embedded URL is unreachable, rewrite the token's server host to the freshly initialized leader's address (which should be up at this point) and retry.

When spec.api.externalAddress is an externally managed VIP the join token's
embedded server URL points at the VIP. During bootstrap the VIP may be
assigned to a controller that is not yet running, so both k0sctl'spreflight
API check and the actual join fail non-deterministically depending on which
node currently holds the VIP.

If the token-embedded URL is unreachable, rewrite the token's server host to
the freshly initialized leader's address (which is guaranteed to be up) and
retry. The token itself is rewritten, so the real join targets the leader too,
not just the preflight validation.

Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
@kke kke added the bug Something isn't working label Jun 22, 2026
@kke
kke requested a review from Copilot June 22, 2026 11:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses non-deterministic controller joins when spec.api.externalAddress is an externally managed VIP that may not be assigned to the freshly bootstrapped leader yet. It adds logic to retry join/preflight API connectivity using the leader’s address by rewriting the token-embedded kubeconfig server host.

Changes:

  • Added TokenData.WithJoinHost() (plus encodeToken()) to rewrite the token’s embedded kubeconfig server host while preserving scheme/port and re-encoding the token.
  • Updated the controller install phase to retry API validation via the leader’s address when the token URL is unreachable, and to use the rewritten token for the actual join.
  • Added unit tests covering token parsing and host-rewrite behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/k0s.go Adds token kubeconfig host-rewrite + token re-encoding helper to support join retries via leader address.
pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/k0s_test.go Adds tests for token host rewriting and reduces reliance on a single hardcoded token fixture.
phase/install_controllers.go Implements “try token URL, then fallback to leader address” logic and rewrites the join token to ensure the join itself uses the fallback target.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +155 to 158
rewritten, rerr := h.Metadata.K0sTokenData.WithJoinHost(leaderJoinHost)
if rerr != nil {
return fmt.Errorf("failed to connect from controller to kubernetes api - check networking: %w", err)
}
Comment on lines +160 to +162
if rerr := retry.WithDefaultTimeout(ctx, node.HTTPStatusFunc(h, rewritten.URL, 200, 401, 404)); rerr != nil {
return fmt.Errorf("failed to connect from controller to kubernetes api - check networking: %w", err)
}
@jnummelin

Copy link
Copy Markdown
Member

Would this fallback also mean that kubelet would also fallback to this address? So in the VIP use case in #1103 , kubelet would only connect to the first node direct IP?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

k0sctl apply fails when externalAddress is a keepalived VIP and VIP is not assigned to the first controller

3 participants