blob: c238cdf4663b7500ef898cbcee7a80e8303b7e71 [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"
Tim Windelschmidtb765f242024-05-08 01:40:02 +02008 "os/signal"
Serge Bazanski7d1a0de2023-07-05 01:17:15 +02009
10 "github.com/spf13/cobra"
11
12 "source.monogon.dev/metropolis/cli/metroctl/core"
13)
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) {
Tim Windelschmidtb765f242024-05-08 01:40:02 +020031 ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt)
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
Tim Windelschmidt0a8797d2024-03-04 18:58:47 +010036 contextName, err := cmd.Flags().GetString("context")
37 if err != nil || contextName == "" {
38 log.Fatalf("k8s configure requires a valid context name to be provided with the --context parameter.")
39 }
40
Serge Bazanski7d1a0de2023-07-05 01:17:15 +020041 // If the user has metroctl in their path, use the metroctl from path as
42 // a credential plugin. Otherwise use the path to the currently-running
43 // metroctl.
44 metroctlPath := "metroctl"
45 if _, err := exec.LookPath("metroctl"); err != nil {
46 metroctlPath, err = os.Executable()
47 if err != nil {
48 log.Fatalf("Failed to create kubectl entry as metroctl is neither in PATH nor can its absolute path be determined: %v", err)
49 }
50 }
51 // TODO(q3k, issues/144): this only works as long as all nodes are kubernetes controller
52 // nodes. This won't be the case for too long. Figure this out.
Tim Windelschmidt0a8797d2024-03-04 18:58:47 +010053 if err := core.InstallKubeletConfig(ctx, metroctlPath, connectOptions(), contextName, flags.clusterEndpoints[0]); err != nil {
Serge Bazanski7d1a0de2023-07-05 01:17:15 +020054 log.Fatalf("Failed to install metroctl/k8s integration: %v", err)
55 }
Tim Windelschmidt0a8797d2024-03-04 18:58:47 +010056 log.Printf("Success! kubeconfig is set up. You can now run kubectl --context=%s ... to access the Kubernetes cluster.", contextName)
Serge Bazanski7d1a0de2023-07-05 01:17:15 +020057}
58
59func init() {
Tim Windelschmidt2f842372024-05-07 00:07:11 +020060 k8sConfigureCommand.Flags().String("context", "metroctl", "The name for the kubernetes context to configure")
Serge Bazanski7d1a0de2023-07-05 01:17:15 +020061 k8sCommand.AddCommand(k8sConfigureCommand)
Serge Bazanski7d1a0de2023-07-05 01:17:15 +020062 rootCmd.AddCommand(k8sCommand)
63}