blob: dd4bfb464a2e4c56da8125316bed7bc0c49d14d8 [file] [log] [blame]
Lorenz Bruna9b455f2021-12-07 03:53:22 +01001package main
2
3import (
4 "context"
Lorenz Bruna9b455f2021-12-07 03:53:22 +01005 "log"
Lorenz Bruna9b455f2021-12-07 03:53:22 +01006 "os"
Lorenz Brun20d1dd12022-07-01 12:21:42 +00007 "os/exec"
Lorenz Bruna9b455f2021-12-07 03:53:22 +01008
Lorenz Bruna9b455f2021-12-07 03:53:22 +01009 "github.com/spf13/cobra"
Serge Bazanski925ec3d2024-02-05 14:38:20 +010010 "google.golang.org/grpc"
Lorenz Bruna9b455f2021-12-07 03:53:22 +010011
Mateusz Zalega18a67b02022-08-02 13:37:50 +020012 "source.monogon.dev/metropolis/cli/metroctl/core"
Lorenz Bruna9b455f2021-12-07 03:53:22 +010013 clicontext "source.monogon.dev/metropolis/cli/pkg/context"
Lorenz Bruna9b455f2021-12-07 03:53:22 +010014 "source.monogon.dev/metropolis/node/core/rpc"
Serge Bazanski925ec3d2024-02-05 14:38:20 +010015 "source.monogon.dev/metropolis/node/core/rpc/resolver"
Serge Bazanskidc1bec42021-12-16 17:38:53 +010016 apb "source.monogon.dev/metropolis/proto/api"
Lorenz Bruna9b455f2021-12-07 03:53:22 +010017)
18
19var takeownershipCommand = &cobra.Command{
Mateusz Zalegab1e7ee42022-07-08 12:19:02 +020020 Use: "takeownership",
Lorenz Bruna9b455f2021-12-07 03:53:22 +010021 Short: "Takes ownership of a new Metropolis cluster",
22 Long: `This takes ownership of a new Metropolis cluster by asking the new
23cluster to issue an owner certificate to for the owner key generated by a
Mateusz Zalegab1e7ee42022-07-08 12:19:02 +020024previous invocation of metroctl install on this machine. A single cluster
25endpoint must be provided with the --endpoints parameter.`,
26 Args: cobra.ExactArgs(0),
27 Run: doTakeOwnership,
Lorenz Bruna9b455f2021-12-07 03:53:22 +010028}
29
Mateusz Zalegab1e7ee42022-07-08 12:19:02 +020030func doTakeOwnership(cmd *cobra.Command, _ []string) {
Tim Windelschmidt0a8797d2024-03-04 18:58:47 +010031 ctx := clicontext.WithInterrupt(context.Background())
Mateusz Zalegab1e7ee42022-07-08 12:19:02 +020032 if len(flags.clusterEndpoints) != 1 {
33 log.Fatalf("takeownership requires a single cluster endpoint to be provided with the --endpoints parameter.")
34 }
Tim Windelschmidt0a8797d2024-03-04 18:58:47 +010035
36 contextName, err := cmd.Flags().GetString("context")
37 if err != nil || contextName == "" {
38 log.Fatalf("takeownership requires a valid context name to be provided with the --context parameter.")
39 }
Serge Bazanski7eeef0f2024-02-05 14:40:15 +010040
41 ca, err := core.GetClusterCAWithTOFU(ctx, connectOptions())
42 if err != nil {
43 log.Fatalf("Could not retrieve cluster CA: %v", err)
44 }
Mateusz Zalegab1e7ee42022-07-08 12:19:02 +020045
Mateusz Zalega18464502022-07-14 16:18:26 +020046 // Retrieve the cluster owner's private key, and use it to construct
47 // ephemeral credentials. Then, dial the cluster.
Serge Bazanskicf23ebc2023-03-14 17:02:04 +010048 opk, err := core.GetOwnerKey(flags.configPath)
49 if err == core.NoCredentialsError {
Lorenz Bruna9b455f2021-12-07 03:53:22 +010050 log.Fatalf("Owner key does not exist. takeownership needs to be executed on the same system that has previously installed the cluster using metroctl install.")
Lorenz Bruna9b455f2021-12-07 03:53:22 +010051 }
Mateusz Zalega18464502022-07-14 16:18:26 +020052 if err != nil {
53 log.Fatalf("Couldn't get owner's key: %v", err)
Lorenz Bruna9b455f2021-12-07 03:53:22 +010054 }
Serge Bazanski925ec3d2024-02-05 14:38:20 +010055 opts, err := core.DialOpts(ctx, connectOptions())
56 if err != nil {
57 log.Fatalf("While configuring cluster dial opts: %v", err)
58 }
Serge Bazanski7eeef0f2024-02-05 14:40:15 +010059 creds, err := rpc.NewEphemeralCredentials(opk, rpc.WantRemoteCluster(ca))
Serge Bazanski925ec3d2024-02-05 14:38:20 +010060 if err != nil {
61 log.Fatalf("While generating ephemeral credentials: %v", err)
62 }
63 opts = append(opts, grpc.WithTransportCredentials(creds))
64
65 cc, err := grpc.Dial(resolver.MetropolisControlAddress, opts...)
Mateusz Zalega18464502022-07-14 16:18:26 +020066 if err != nil {
67 log.Fatalf("While dialing the cluster: %v", err)
Lorenz Bruna9b455f2021-12-07 03:53:22 +010068 }
Mateusz Zalega18464502022-07-14 16:18:26 +020069 aaa := apb.NewAAAClient(cc)
Lorenz Bruna9b455f2021-12-07 03:53:22 +010070
Mateusz Zalega18464502022-07-14 16:18:26 +020071 ownerCert, err := rpc.RetrieveOwnerCertificate(ctx, aaa, opk)
Lorenz Bruna9b455f2021-12-07 03:53:22 +010072 if err != nil {
73 log.Fatalf("Failed to retrive owner certificate from cluster: %v", err)
74 }
Serge Bazanski925ec3d2024-02-05 14:38:20 +010075
Serge Bazanskicf23ebc2023-03-14 17:02:04 +010076 if err := core.WriteOwnerCertificate(flags.configPath, ownerCert.Certificate[0]); err != nil {
Lorenz Bruna9b455f2021-12-07 03:53:22 +010077 log.Printf("Failed to store retrieved owner certificate: %v", err)
78 log.Fatalln("Sorry, the cluster has been lost as taking ownership cannot be repeated. Fix the reason the file couldn't be written and reinstall the node.")
79 }
Lorenz Brun20d1dd12022-07-01 12:21:42 +000080 log.Print("Successfully retrieved owner credentials! You now own this cluster. Setting up kubeconfig now...")
81
Lorenz Brun20d1dd12022-07-01 12:21:42 +000082 // If the user has metroctl in their path, use the metroctl from path as
83 // a credential plugin. Otherwise use the path to the currently-running
84 // metroctl.
85 metroctlPath := "metroctl"
86 if _, err := exec.LookPath("metroctl"); err != nil {
87 metroctlPath, err = os.Executable()
88 if err != nil {
89 log.Fatalf("Failed to create kubectl entry as metroctl is neither in PATH nor can its absolute path be determined: %v", err)
90 }
91 }
Serge Bazanski1f8cad72023-03-20 16:58:10 +010092 // TODO(q3k, issues/144): this only works as long as all nodes are kubernetes controller
93 // nodes. This won't be the case for too long. Figure this out.
Tim Windelschmidtb37d7d82023-06-14 19:06:44 +020094 configName := "metroctl"
Serge Bazanski568c38c2024-02-05 14:40:39 +010095 if err := core.InstallKubeletConfig(ctx, metroctlPath, connectOptions(), configName, flags.clusterEndpoints[0]); err != nil {
Serge Bazanskicf23ebc2023-03-14 17:02:04 +010096 log.Fatalf("Failed to install metroctl/k8s integration: %v", err)
Lorenz Brun20d1dd12022-07-01 12:21:42 +000097 }
Tim Windelschmidtb37d7d82023-06-14 19:06:44 +020098 log.Printf("Success! kubeconfig is set up. You can now run kubectl --context=%s ... to access the Kubernetes cluster.", configName)
Lorenz Bruna9b455f2021-12-07 03:53:22 +010099}
100
101func init() {
Tim Windelschmidt0a8797d2024-03-04 18:58:47 +0100102 takeownershipCommand.Flags().String("context", "metroctl", "The name for the kubernetes context to configure")
Lorenz Bruna9b455f2021-12-07 03:53:22 +0100103 rootCmd.AddCommand(takeownershipCommand)
104}