blob: 61d6c4ecf9adeadb0f0a287f97847f8fa545ba47 [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) {
31 if len(flags.clusterEndpoints) != 1 {
32 log.Fatalf("takeownership requires a single cluster endpoint to be provided with the --endpoints parameter.")
33 }
Mateusz Zalegab1e7ee42022-07-08 12:19:02 +020034
Mateusz Zalega18464502022-07-14 16:18:26 +020035 // Retrieve the cluster owner's private key, and use it to construct
36 // ephemeral credentials. Then, dial the cluster.
Serge Bazanskicf23ebc2023-03-14 17:02:04 +010037 opk, err := core.GetOwnerKey(flags.configPath)
38 if err == core.NoCredentialsError {
Lorenz Bruna9b455f2021-12-07 03:53:22 +010039 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 +010040 }
Mateusz Zalega18464502022-07-14 16:18:26 +020041 if err != nil {
42 log.Fatalf("Couldn't get owner's key: %v", err)
Lorenz Bruna9b455f2021-12-07 03:53:22 +010043 }
Mateusz Zalega18464502022-07-14 16:18:26 +020044 ctx := clicontext.WithInterrupt(context.Background())
Serge Bazanski925ec3d2024-02-05 14:38:20 +010045 opts, err := core.DialOpts(ctx, connectOptions())
46 if err != nil {
47 log.Fatalf("While configuring cluster dial opts: %v", err)
48 }
49 creds, err := rpc.NewEphemeralCredentials(opk, rpc.WantInsecure())
50 if err != nil {
51 log.Fatalf("While generating ephemeral credentials: %v", err)
52 }
53 opts = append(opts, grpc.WithTransportCredentials(creds))
54
55 cc, err := grpc.Dial(resolver.MetropolisControlAddress, opts...)
Mateusz Zalega18464502022-07-14 16:18:26 +020056 if err != nil {
57 log.Fatalf("While dialing the cluster: %v", err)
Lorenz Bruna9b455f2021-12-07 03:53:22 +010058 }
Mateusz Zalega18464502022-07-14 16:18:26 +020059 aaa := apb.NewAAAClient(cc)
Lorenz Bruna9b455f2021-12-07 03:53:22 +010060
Mateusz Zalega18464502022-07-14 16:18:26 +020061 ownerCert, err := rpc.RetrieveOwnerCertificate(ctx, aaa, opk)
Lorenz Bruna9b455f2021-12-07 03:53:22 +010062 if err != nil {
63 log.Fatalf("Failed to retrive owner certificate from cluster: %v", err)
64 }
Serge Bazanski925ec3d2024-02-05 14:38:20 +010065
Serge Bazanskicf23ebc2023-03-14 17:02:04 +010066 if err := core.WriteOwnerCertificate(flags.configPath, ownerCert.Certificate[0]); err != nil {
Lorenz Bruna9b455f2021-12-07 03:53:22 +010067 log.Printf("Failed to store retrieved owner certificate: %v", err)
68 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.")
69 }
Lorenz Brun20d1dd12022-07-01 12:21:42 +000070 log.Print("Successfully retrieved owner credentials! You now own this cluster. Setting up kubeconfig now...")
71
Lorenz Brun20d1dd12022-07-01 12:21:42 +000072 // If the user has metroctl in their path, use the metroctl from path as
73 // a credential plugin. Otherwise use the path to the currently-running
74 // metroctl.
75 metroctlPath := "metroctl"
76 if _, err := exec.LookPath("metroctl"); err != nil {
77 metroctlPath, err = os.Executable()
78 if err != nil {
79 log.Fatalf("Failed to create kubectl entry as metroctl is neither in PATH nor can its absolute path be determined: %v", err)
80 }
81 }
Serge Bazanski1f8cad72023-03-20 16:58:10 +010082 // TODO(q3k, issues/144): this only works as long as all nodes are kubernetes controller
83 // nodes. This won't be the case for too long. Figure this out.
Tim Windelschmidtb37d7d82023-06-14 19:06:44 +020084 configName := "metroctl"
85 if err := core.InstallKubeletConfig(metroctlPath, connectOptions(), configName, flags.clusterEndpoints[0]); err != nil {
Serge Bazanskicf23ebc2023-03-14 17:02:04 +010086 log.Fatalf("Failed to install metroctl/k8s integration: %v", err)
Lorenz Brun20d1dd12022-07-01 12:21:42 +000087 }
Tim Windelschmidtb37d7d82023-06-14 19:06:44 +020088 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 +010089}
90
91func init() {
92 rootCmd.AddCommand(takeownershipCommand)
93}