Skip to content

Commit 67e6ed2

Browse files
committed
✨ Add support to clusterctl for inserting workload kubeconfig into an existing kubeconfig file
1 parent 3a8728f commit 67e6ed2

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

cmd/clusterctl/cmd/get_kubeconfig.go

+33
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ package cmd
1919
import (
2020
"context"
2121
"fmt"
22+
"os"
2223

2324
"github.com/pkg/errors"
2425
"github.com/spf13/cobra"
26+
"k8s.io/client-go/tools/clientcmd"
2527

2628
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2729
"sigs.k8s.io/cluster-api/cmd/clusterctl/client"
@@ -32,6 +34,7 @@ type getKubeconfigOptions struct {
3234
kubeconfig string
3335
kubeconfigContext string
3436
namespace string
37+
intoKubeconfig bool
3538
}
3639

3740
var gk = &getKubeconfigOptions{}
@@ -67,6 +70,8 @@ func init() {
6770
"Path to the kubeconfig file to use for accessing the management cluster. If unspecified, default discovery rules apply.")
6871
getKubeconfigCmd.Flags().StringVar(&gk.kubeconfigContext, "kubeconfig-context", "",
6972
"Context to be used within the kubeconfig file. If empty, current context will be used.")
73+
getKubeconfigCmd.Flags().BoolVar(&gk.intoKubeconfig, "into-kubeconfig", false,
74+
"Inserts the workload cluster kubeconfig into the kubeconfig file.")
7075

7176
// completions
7277
getKubeconfigCmd.ValidArgsFunction = resourceNameCompletionFunc(
@@ -98,6 +103,34 @@ func runGetKubeconfig(workloadClusterName string) error {
98103
if err != nil {
99104
return err
100105
}
106+
if gk.intoKubeconfig {
107+
return intoKubeconfig(out)
108+
}
109+
101110
fmt.Println(out)
102111
return nil
103112
}
113+
114+
func intoKubeconfig(kubeconfig string) error {
115+
kubeconfigFile, err := os.CreateTemp("", "kubeconfig")
116+
if err != nil {
117+
return err
118+
}
119+
defer os.Remove(kubeconfigFile.Name())
120+
121+
if _, err = kubeconfigFile.WriteString(kubeconfig); err != nil {
122+
return err
123+
}
124+
if err = kubeconfigFile.Close(); err != nil {
125+
return err
126+
}
127+
128+
rules := clientcmd.NewDefaultClientConfigLoadingRules()
129+
rules.Precedence = append(rules.Precedence, kubeconfigFile.Name())
130+
config, err := rules.Load()
131+
if err != nil {
132+
return err
133+
}
134+
135+
return clientcmd.WriteToFile(*config, rules.Precedence[0])
136+
}

docs/book/src/clusterctl/commands/get-kubeconfig.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# clusterctl get kubeconfig
22

3-
This command prints the kubeconfig of an existing workload cluster into stdout.
3+
This command prints the kubeconfig of an existing workload cluster into stdout or inserts it into the default or defined kubeconfig file.
44
This functionality is available in clusterctl v0.3.9 or newer.
55

66
## Examples
@@ -11,6 +11,12 @@ Get the kubeconfig of a workload cluster named foo.
1111
clusterctl get kubeconfig foo
1212
```
1313

14+
Get the kubeconfig of a workload cluster named foo and insert it into the default kubeconfig file.
15+
16+
```bash
17+
clusterctl get kubeconfig foo --into-kubeconfig
18+
```
19+
1420
Get the kubeconfig of a workload cluster named foo in the namespace bar
1521

1622
```bash

0 commit comments

Comments
 (0)