blob: 4b29fb052295d1799f842b2028ae95906d30a53a [file] [log] [blame]
Serge Bazanski7d1a0de2023-07-05 01:17:15 +02001package main
2
3import (
Serge Bazanski568c38c2024-02-05 14:40:39 +01004 "context"
Serge Bazanski7d1a0de2023-07-05 01:17:15 +02005 "log"
6 "os"
7 "os/exec"
8
9 "github.com/spf13/cobra"
10
11 "source.monogon.dev/metropolis/cli/metroctl/core"
Serge Bazanski568c38c2024-02-05 14:40:39 +010012 clicontext "source.monogon.dev/metropolis/cli/pkg/context"
Serge Bazanski7d1a0de2023-07-05 01:17:15 +020013)
14
15var k8sCommand = &cobra.Command{
16 Short: "Manages kubernetes-specific functionality in Metropolis.",
17 Use: "k8s",
18}
19
20var k8sConfigureCommand = &cobra.Command{
21 Use: "configure",
22 Short: "Configures local `kubectl` for use with a Metropolis cluster.",
23 Long: `Configures a local kubectl instance (or any other Kubernetes application)
24to connect to a Metropolis cluster. A cluster endpoint must be provided with the
25--endpoints parameter.`,
26 Args: cobra.ExactArgs(0),
27 Run: doK8sConfigure,
28}
29
30func doK8sConfigure(cmd *cobra.Command, _ []string) {
Serge Bazanski568c38c2024-02-05 14:40:39 +010031 ctx := clicontext.WithInterrupt(context.Background())
Serge Bazanski7d1a0de2023-07-05 01:17:15 +020032 if len(flags.clusterEndpoints) < 1 {
33 log.Fatalf("k8s configure requires at least one cluster endpoint to be provided with the --endpoints parameter.")
34 }
35
36 // If the user has metroctl in their path, use the metroctl from path as
37 // a credential plugin. Otherwise use the path to the currently-running
38 // metroctl.
39 metroctlPath := "metroctl"
40 if _, err := exec.LookPath("metroctl"); err != nil {
41 metroctlPath, err = os.Executable()
42 if err != nil {
43 log.Fatalf("Failed to create kubectl entry as metroctl is neither in PATH nor can its absolute path be determined: %v", err)
44 }
45 }
46 // TODO(q3k, issues/144): this only works as long as all nodes are kubernetes controller
47 // nodes. This won't be the case for too long. Figure this out.
48 configName := "metroctl"
Serge Bazanski568c38c2024-02-05 14:40:39 +010049 if err := core.InstallKubeletConfig(ctx, metroctlPath, connectOptions(), configName, flags.clusterEndpoints[0]); err != nil {
Serge Bazanski7d1a0de2023-07-05 01:17:15 +020050 log.Fatalf("Failed to install metroctl/k8s integration: %v", err)
51 }
52 log.Printf("Success! kubeconfig is set up. You can now run kubectl --context=%s ... to access the Kubernetes cluster.", configName)
53}
54
55func init() {
56 k8sCommand.AddCommand(k8sConfigureCommand)
57 rootCmd.AddCommand(k8sCommand)
58}