Skip to content

Commit 1f16a96

Browse files
sebrandon1praveenkumar
authored andcommitted
Issue #4752 Add generate-kubeconfig command
Add a crc generate-kubeconfig command that outputs the kubeconfig for the CRC instance to stdout. Checks that the VM exists and is running before attempting to read the kubeconfig.
1 parent c460204 commit 1f16a96

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

cmd/crc/cmd/generate_kubeconfig.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/crc-org/crc/v2/pkg/crc/constants"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
func init() {
12+
rootCmd.AddCommand(generateKubeconfigCmd)
13+
}
14+
15+
var generateKubeconfigCmd = &cobra.Command{
16+
Use: "generate-kubeconfig",
17+
Short: "Generate a kubeconfig file for the CRC instance",
18+
Long: "Output the kubeconfig file for the CRC instance to stdout",
19+
RunE: func(_ *cobra.Command, _ []string) error {
20+
return runGenerateKubeconfig()
21+
},
22+
}
23+
24+
func runGenerateKubeconfig() error {
25+
client := newMachine()
26+
if err := checkIfMachineMissing(client); err != nil {
27+
return err
28+
}
29+
30+
running, err := client.IsRunning()
31+
if err != nil {
32+
return err
33+
}
34+
if !running {
35+
return fmt.Errorf("the CRC instance is not running, cannot retrieve kubeconfig")
36+
}
37+
38+
data, err := os.ReadFile(constants.KubeconfigFilePath)
39+
if err != nil {
40+
return fmt.Errorf("Error reading kubeconfig: %v", err)
41+
}
42+
fmt.Print(string(data))
43+
return nil
44+
}

cmd/crc/cmd/root_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func TestCrcManPageGenerator_WhenInvoked_GeneratesManPagesForAllCrcSubCommands(t
3333
"crc-config.1",
3434
"crc-console.1",
3535
"crc-delete.1",
36+
"crc-generate-kubeconfig.1",
3637
"crc-ip.1",
3738
"crc-oc-env.1",
3839
"crc-podman-env.1",

0 commit comments

Comments
 (0)