Skip to content

feat(kubeconfig): fall back to default kubeconfig loading rules - #688

Merged
derekbit merged 1 commit into
longhorn:masterfrom
derekbit:issue-14594
Nov 19, 2026
Merged

derekbit merged 1 commit into
longhorn:masterfrom
derekbit:issue-14594

Conversation

@derekbit

Copy link
Copy Markdown
Member

Which issue(s) this PR fixes:

Issue longhorn/longhorn#14594

What this PR does / why we need it:

Make --kubeconfig optional across longhornctl commands by aligning kubeconfig resolution with kubectl via client-go's NewDefaultClientConfigLoadingRules and
NewNonInteractiveDeferredLoadingClientConfig:

  1. Use the explicit --kubeconfig file when provided (failing immediately if the specified file does not exist).
  2. Fall back to the KUBECONFIG environment variable (supporting multi-file path lists).
  3. Fall back to ~/.kube/config, and finally to in-cluster configuration when available.

Special notes for your reviewer:

Additional documentation or context

Make `--kubeconfig` optional across `longhornctl` commands by aligning
kubeconfig resolution with `kubectl` via `client-go`'s
`NewDefaultClientConfigLoadingRules` and
`NewNonInteractiveDeferredLoadingClientConfig`:

1. Use the explicit `--kubeconfig` file when provided (failing
   immediately if the specified file does not exist).
2. Fall back to the `KUBECONFIG` environment variable (supporting
   multi-file path lists).
3. Fall back to `~/.kube/config`, and finally to in-cluster
   configuration when available.

Longhorn 14594

Signed-off-by: Derek Su <derek.su@suse.com>
@mergify

mergify Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

This pull request is merged.

@derekbit derekbit self-assigned this Nov 19, 2026
@derekbit
derekbit requested a balanced review from Copilot November 19, 2026 03:54

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.

🟢 Approval recommended

No active review issues remain for the change to default kubeconfig discovery.

No open issues 🎉

What changed in this PR

Makes --kubeconfig optional across longhornctl commands by using client-go’s default loading rules, aligning cluster configuration discovery with kubectl.

Changes:

  • Resolves configuration from an explicit file, KUBECONFIG path lists, the default home file, or in-cluster credentials.
  • Adds tests for loading precedence and missing-file errors.
  • Updates flag defaults, help text, and command documentation.
File Description
pkg/​utils/​kubernetes/​client.go Implements default configuration loading and actionable errors.
pkg/​utils/​kubernetes/​client_test.go Tests configuration precedence and missing-file handling.
pkg/​utils/​cmd.go Updates shared kubeconfig flag help.
pkg/​consts/​environment.go Removes the redundant kubeconfig environment constant.
docs/​longhornctl.md Documents default kubeconfig discovery.
docs/​longhornctl_version.md Updates inherited kubeconfig help.
docs/​longhornctl_trim.md Updates trimming command help.
docs/​longhornctl_trim_volume.md Updates volume trimming help.
docs/​longhornctl_install.md Updates installation command help.
docs/​longhornctl_install_preflight.md Updates preflight installation help.
docs/​longhornctl_install_preflight_stop.md Updates preflight stop help.
docs/​longhornctl_global-options.md Documents kubeconfig fallback in global options.
docs/​longhornctl_get.md Updates information-gathering command help.
docs/​longhornctl_get_replica.md Updates replica retrieval help.
docs/​longhornctl_export.md Updates export command help.
docs/​longhornctl_export_replica.md Updates replica export help.
docs/​longhornctl_export_replica_stop.md Updates replica export stop help.
docs/​longhornctl_doc.md Updates inherited kubeconfig help.
docs/​longhornctl_diagnose.md Updates diagnostic command help.
docs/​longhornctl_checksum.md Updates checksum command help.
docs/​longhornctl_checksum_volume.md Updates volume checksum help.
docs/​longhornctl_check.md Updates checking command help.
docs/​longhornctl_check_preflight.md Updates preflight checking help.
cmd/​remote/​subcmd/​diagnose.go Updates the diagnostic kubeconfig flag help.
cmd/​remote/​longhornctl.go Leaves environment resolution to client-go and updates help.

Review effort: Balanced

@davidcheng0922 davidcheng0922 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@derekbit
derekbit merged commit d8485e6 into longhorn:master Nov 19, 2026
8 checks passed
@codecov

codecov Bot commented Nov 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 71.42857% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 7.45%. Comparing base (16566da) to head (a2fb8f2).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
pkg/utils/kubernetes/client.go 83.33% 3 Missing ⚠️
cmd/remote/longhornctl.go 0.00% 1 Missing ⚠️
cmd/remote/subcmd/diagnose.go 0.00% 1 Missing ⚠️
pkg/utils/cmd.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##           master    #688      +/-   ##
=========================================
+ Coverage    6.91%   7.45%   +0.54%     
=========================================
  Files          57      57              
  Lines        4440    4453      +13     
=========================================
+ Hits          307     332      +25     
+ Misses       4133    4121      -12     
Flag Coverage Δ
unittests 7.45% <71.42%> (+0.54%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants