@@ -19,9 +19,11 @@ package cmd
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "os"
22
23
23
24
"github.com/pkg/errors"
24
25
"github.com/spf13/cobra"
26
+ "k8s.io/client-go/tools/clientcmd"
25
27
26
28
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
27
29
"sigs.k8s.io/cluster-api/cmd/clusterctl/client"
@@ -32,6 +34,7 @@ type getKubeconfigOptions struct {
32
34
kubeconfig string
33
35
kubeconfigContext string
34
36
namespace string
37
+ intoKubeconfig bool
35
38
}
36
39
37
40
var gk = & getKubeconfigOptions {}
@@ -67,6 +70,8 @@ func init() {
67
70
"Path to the kubeconfig file to use for accessing the management cluster. If unspecified, default discovery rules apply." )
68
71
getKubeconfigCmd .Flags ().StringVar (& gk .kubeconfigContext , "kubeconfig-context" , "" ,
69
72
"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." )
70
75
71
76
// completions
72
77
getKubeconfigCmd .ValidArgsFunction = resourceNameCompletionFunc (
@@ -98,6 +103,34 @@ func runGetKubeconfig(workloadClusterName string) error {
98
103
if err != nil {
99
104
return err
100
105
}
106
+ if gk .intoKubeconfig {
107
+ return intoKubeconfig (out )
108
+ }
109
+
101
110
fmt .Println (out )
102
111
return nil
103
112
}
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
+ }
0 commit comments