Skip to content

fix(kubeadm): preserve kubelet.conf server line indent via backrefs - #13429

Open
mehrdadbn9 wants to merge 1 commit into
kubernetes-sigs:masterfrom
mehrdadbn9:fix/kubelet-conf-server-indent
Open

fix(kubeadm): preserve kubelet.conf server line indent via backrefs#13429
mehrdadbn9 wants to merge 1 commit into
kubernetes-sigs:masterfrom
mehrdadbn9:fix/kubelet-conf-server-indent

Conversation

@mehrdadbn9

@mehrdadbn9 mehrdadbn9 commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Description

The "Update server field in kubelet kubeconfig" tasks rewrote the server: line with a fixed indent. When the on-disk kubelet.conf uses a different indent than assumed, server: lands outside the cluster: mapping and kubelet fails after upgrade with:

invalid configuration: no server found for cluster "default-cluster"

and worker nodes crash-loop (NotReady, tainted unreachable).

This switches both variants (default and external-lb) to backrefs: true with regexp: '^(\s*)server: .*$' and line: '\1server: {{ kube_apiserver_endpoint }}', so the existing indent is captured and preserved while only the endpoint value is replaced.

Refs #13277

Verification

Reproduced with real ansible-playbook 2.16 lineinfile before pushing: without backrefs: true the literal text \1server: is written (invalid YAML, parse error); with backrefs, both 4-space and 8-space indents are preserved, the endpoint is substituted, and the CA line is untouched.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
Fix kubelet.conf `server:` handling to preserve the existing indentation when updating the endpoint, so worker nodes do not crash-loop after upgrade (kubernetes-sigs/kubespray#13277).

@kubernetes-prow kubernetes-prow Bot added the do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. label Aug 28, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: mehrdadbn9
Once this PR has been reviewed and has the lgtm label, please assign mzaian for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubernetes-prow
kubernetes-prow Bot requested review from VannTen and mzaian August 28, 2026 10:18
@kubernetes-prow kubernetes-prow Bot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Aug 28, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Hi @mehrdadbn9. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@mehrdadbn9 mehrdadbn9 changed the title x Fix kubelet.conf server: indentation for kubeadm v1.35+ Aug 28, 2026
@kubernetes-prow kubernetes-prow Bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Aug 28, 2026
@mehrdadbn9
mehrdadbn9 force-pushed the fix/kubelet-conf-server-indent branch from 45aefae to 0d0b694 Compare August 28, 2026 13:14
@yankay

yankay commented Aug 30, 2026

Copy link
Copy Markdown
Member

/ok-to-test

@kubernetes-prow kubernetes-prow Bot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Aug 30, 2026
@guoard

guoard commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

I don't think kubeadm output actually changed. v1.35/v1.36 still write kubelet.conf via clientcmd.WriteToFile (sigs.k8s.io/yaml v1.6.0, goyaml.v2 encoder), so server: is still at 4 spaces. The samples in #13277 don't look like real kubeadm output either.

Against a real kubelet.conf this patch breaks things: the first task replaces the 4-space line with an 8-space one (YAML parse error next to certificate-authority-data:), and the new external lb regexp never matches, so lineinfile appends the line at EOF.

If we want to make this more robust, preserving the existing indent would be better than hardcoding it:

regexp: '^(\s*)server: .*$'
line: '\1server: {{ kube_apiserver_endpoint }}'
backrefs: true

Would be good to get an unedited kubelet.conf from the reporter of #13277 first.

@mehrdadbn9
mehrdadbn9 force-pushed the fix/kubelet-conf-server-indent branch from 0d0b694 to 34d3fe5 Compare September 1, 2026 18:23
@kubernetes-prow kubernetes-prow Bot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Sep 1, 2026
The lineinfile tasks rewrote the server: line with a hardcoded 4-space
indent. The first task's regexp 'server:' could also match the wrong
server: key. Use a captured indent group to preserve the real indent.

Both tasks need backrefs: true because \1 in line is only expanded
when backrefs is enabled; without it the literal string \1server: is
written and kubelet.conf becomes invalid YAML (seen as 16 CI job
failures across cluster jobs on the previous attempt).

Verified with ansible-playbook 2.16 lineinfile on a sample kubelet.conf
for 4-space and 8-space indents: indent preserved, endpoint substituted,
certificate-authority-data untouched, valid YAML after rewrite.

Relates to PR 13429 CI and issue 13277.

Signed-off-by: Mehrdad Biukian Naeini <mehrdadbiukian@gmail.com>
@mehrdadbn9
mehrdadbn9 force-pushed the fix/kubelet-conf-server-indent branch from 34d3fe5 to ed7bd07 Compare September 1, 2026 18:58
@kubernetes-prow kubernetes-prow Bot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Sep 1, 2026
@mehrdadbn9

Copy link
Copy Markdown
Contributor Author

You were right about the previous attempt. Two real bugs, both reproduced locally with ansible-playbook 2.16 lineinfile before this push:

  1. The rewrite on 34d3fe5 kept '\1' in line but missed backrefs: true on both tasks. lineinfile then writes the literal string '\1server: ...' and kubelet.conf becomes invalid YAML (same failure mode you described).

Repro:

  • without backrefs: literal '\1server: https://LB-VIP:6443' written, yaml parse error line 6
  • with backrefs: indent preserved (4-space and 8-space tested), certificate-authority-data untouched
  1. New head ed7bd07 adds backrefs: true to both tasks and tightens the regexp to '^(\s*)server: .*$' so it can only match a server: key with a value, not a bare key.

The kubeadm-emitted indent question you raised is now moot for correctness: the capture group preserves whatever indent exists (4 or 8 spaces both tested). Happy to test against a real kubelet.conf from the reporter if they can provide one.

@guoard

guoard commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

LGTM.
Please update the title/description/release note though, they still describe the 8-space approach and the kubeadm 1.35 claim.

@guoard

guoard commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

/lgtm

@kubernetes-prow kubernetes-prow Bot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 3, 2026
@mehrdadbn9

Copy link
Copy Markdown
Contributor Author

@guoard thanks for the lgtm.

I'm preparing a Kubernetes GitHub org membership request (kubernetes/org, area/github-membership). Per the requirements I need two sponsors who are reviewers/approvers in an OWNERS file and have had close interactions with my work — your review here (kubespray-reviewers, per OWNERS_ALIASES) fits that.

Would you be willing to sponsor? If yes, a brief +1 on my membership issue once I open it is all that's needed. No pressure either way.

@guoard

guoard commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Would you be willing to sponsor?

Thanks, but I'll pass. I'm only a reviewer on kubespray and we've just crossed paths on a couple of PRs, not enough for me to vouch for someone.
The kubespray approvers know your work much better.

@VannTen

VannTen commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

@mehrdadbn9 I suggest you tone it down with the sponsoring requests. It might not be your intention (written communication is not always as easy as in person), but it comes across as a bit pushy, when you had few interactions with the people before.

@mehrdadbn9

Copy link
Copy Markdown
Contributor Author

@guoard @VannTen Thank you both, genuinely — for the reviews and for telling me straight.

@guoard, understood completely, no hard feelings at all. Your lgtm and the backrefs catch already taught me a lot.

@VannTen, you're right and I appreciate you saying it. I don't know the community flow well yet and I pushed too hard on the sponsorship asks with too few interactions behind them. I'll stop the asks entirely and just keep doing the code work — starting with fixing the #13429 title/description you flagged.

If there's a better way I should be learning the process, I'm glad to hear it.

@mehrdadbn9 mehrdadbn9 changed the title Fix kubelet.conf server: indentation for kubeadm v1.35+ fix(kubeadm): preserve kubelet.conf server line indent via backrefs Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants