| Serge Bazanski | 7d1a0de | 2023-07-05 01:17:15 +0200 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| Serge Bazanski | 568c38c | 2024-02-05 14:40:39 +0100 | [diff] [blame] | 4 | "context" |
| Serge Bazanski | 7d1a0de | 2023-07-05 01:17:15 +0200 | [diff] [blame] | 5 | "log" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | |
| 9 | "github.com/spf13/cobra" |
| 10 | |
| 11 | "source.monogon.dev/metropolis/cli/metroctl/core" |
| Serge Bazanski | 568c38c | 2024-02-05 14:40:39 +0100 | [diff] [blame] | 12 | clicontext "source.monogon.dev/metropolis/cli/pkg/context" |
| Serge Bazanski | 7d1a0de | 2023-07-05 01:17:15 +0200 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | var k8sCommand = &cobra.Command{ |
| 16 | Short: "Manages kubernetes-specific functionality in Metropolis.", |
| 17 | Use: "k8s", |
| 18 | } |
| 19 | |
| 20 | var 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) |
| 24 | to 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 | |
| 30 | func doK8sConfigure(cmd *cobra.Command, _ []string) { |
| Serge Bazanski | 568c38c | 2024-02-05 14:40:39 +0100 | [diff] [blame] | 31 | ctx := clicontext.WithInterrupt(context.Background()) |
| Serge Bazanski | 7d1a0de | 2023-07-05 01:17:15 +0200 | [diff] [blame] | 32 | 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 Bazanski | 568c38c | 2024-02-05 14:40:39 +0100 | [diff] [blame] | 49 | if err := core.InstallKubeletConfig(ctx, metroctlPath, connectOptions(), configName, flags.clusterEndpoints[0]); err != nil { |
| Serge Bazanski | 7d1a0de | 2023-07-05 01:17:15 +0200 | [diff] [blame] | 50 | 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 | |
| 55 | func init() { |
| 56 | k8sCommand.AddCommand(k8sConfigureCommand) |
| 57 | rootCmd.AddCommand(k8sCommand) |
| 58 | } |