Skip to content

Commit 32abf95

Browse files
authored
Allow overriding API address in kubeconfig subcommand (#249)
1 parent 780e776 commit 32abf95

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

cmd/kubeconfig.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ var kubeconfigCommand = &cli.Command{
1313
Name: "kubeconfig",
1414
Usage: "Output the admin kubeconfig of the cluster",
1515
Flags: []cli.Flag{
16+
&cli.StringFlag{
17+
Name: "address",
18+
Usage: "Set kubernetes API address (default: auto-detect)",
19+
Value: "",
20+
},
1621
configFlag,
1722
debugFlag,
1823
traceFlag,
@@ -42,7 +47,7 @@ var kubeconfigCommand = &cli.Command{
4247
manager.AddPhase(
4348
&phase.Connect{},
4449
&phase.DetectOS{},
45-
&phase.GetKubeconfig{},
50+
&phase.GetKubeconfig{APIAddress: ctx.String("address")},
4651
&phase.Disconnect{},
4752
)
4853

phase/get_kubeconfig.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
// GetKubeconfig is a phase to get and dump the admin kubeconfig
1010
type GetKubeconfig struct {
1111
GenericPhase
12+
APIAddress string
1213
}
1314

1415
// Title for the phase
@@ -24,19 +25,23 @@ func (p *GetKubeconfig) Run() error {
2425
return err
2526
}
2627

27-
// the controller admin.conf is aways pointing to localhost, thus we need to change the address
28-
// something usable from outside
29-
address := h.Address()
30-
if a, ok := p.Config.Spec.K0s.Config.Dig("spec", "api", "externalAddress").(string); ok {
31-
address = a
32-
}
28+
if p.APIAddress == "" {
29+
// the controller admin.conf is aways pointing to localhost, thus we need to change the address
30+
// something usable from outside
31+
address := h.Address()
32+
if a, ok := p.Config.Spec.K0s.Config.Dig("spec", "api", "externalAddress").(string); ok {
33+
address = a
34+
}
35+
36+
port := 6443
37+
if p, ok := p.Config.Spec.K0s.Config.Dig("spec", "api", "port").(int); ok {
38+
port = p
39+
}
3340

34-
port := 6443
35-
if p, ok := p.Config.Spec.K0s.Config.Dig("spec", "api", "port").(int); ok {
36-
port = p
41+
p.APIAddress = fmt.Sprintf("https://%s:%d", address, port)
3742
}
3843

39-
cfgString, err := kubeConfig(output, p.Config.Metadata.Name, address, port)
44+
cfgString, err := kubeConfig(output, p.Config.Metadata.Name, p.APIAddress)
4045
if err != nil {
4146
return err
4247
}
@@ -46,15 +51,15 @@ func (p *GetKubeconfig) Run() error {
4651

4752
// kubeConfig reads in the raw kubeconfig and changes the given address
4853
// and cluster name into it
49-
func kubeConfig(raw string, name string, address string, port int) (string, error) {
54+
func kubeConfig(raw string, name string, address string) (string, error) {
5055
cfg, err := clientcmd.Load([]byte(raw))
5156
if err != nil {
5257
return "", err
5358
}
5459

5560
cfg.Clusters[name] = cfg.Clusters["local"]
5661
delete(cfg.Clusters, "local")
57-
cfg.Clusters[name].Server = fmt.Sprintf("https://%s:%d", address, port)
62+
cfg.Clusters[name].Server = address
5863

5964
cfg.Contexts[name] = cfg.Contexts["Default"]
6065
delete(cfg.Contexts, "Default")

0 commit comments

Comments
 (0)