blob: 695fcc7ed7d63b6b038aa88db9010781c2f94214 [file] [log] [blame]
Lorenz Brun52f7f292020-06-24 16:42:02 +02001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package main
18
19import (
Serge Bazanski075465c2021-11-16 15:38:49 +010020 "context"
Lorenz Brun52f7f292020-06-24 16:42:02 +020021 "log"
Tim Windelschmidtb765f242024-05-08 01:40:02 +020022 "os"
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000023 "os/exec"
Tim Windelschmidtb765f242024-05-08 01:40:02 +020024 "os/signal"
Lorenz Brun52f7f292020-06-24 16:42:02 +020025
Serge Bazanski1f8cad72023-03-20 16:58:10 +010026 metroctl "source.monogon.dev/metropolis/cli/metroctl/core"
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020027 mlaunch "source.monogon.dev/metropolis/test/launch"
Lorenz Brun52f7f292020-06-24 16:42:02 +020028)
29
30func main() {
Tim Windelschmidtb765f242024-05-08 01:40:02 +020031 ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt)
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020032 cl, err := mlaunch.LaunchCluster(ctx, mlaunch.ClusterOptions{
Serge Bazanski6a328e62024-02-12 17:37:50 +010033 NumNodes: 3,
Serge Bazanskid09c58f2023-03-17 00:25:08 +010034 NodeLogsToFiles: true,
Serge Bazanski075465c2021-11-16 15:38:49 +010035 })
36 if err != nil {
37 log.Fatalf("LaunchCluster: %v", err)
38 }
Serge Bazanski1f8cad72023-03-20 16:58:10 +010039
Serge Bazanski1f8cad72023-03-20 16:58:10 +010040 wpath, err := cl.MakeMetroctlWrapper()
41 if err != nil {
42 log.Fatalf("MakeWrapper: %v", err)
43 }
44
45 apiservers, err := cl.KubernetesControllerNodeAddresses(ctx)
46 if err != nil {
47 log.Fatalf("Could not get Kubernetes controller nodes: %v", err)
48 }
49 if len(apiservers) < 1 {
50 log.Fatalf("Cluster has no Kubernetes controller nodes")
51 }
52
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000053 // If the user has metroctl in their path, use the metroctl from path as
54 // a credential plugin. Otherwise use the path to the currently-running
55 // metroctl.
56 metroctlPath := "metroctl"
57 if _, err := exec.LookPath("metroctl"); err != nil {
58 metroctlPath, err = os.Executable()
59 if err != nil {
60 log.Fatalf("Failed to create kubectl entry as metroctl is neither in PATH nor can its absolute path be determined: %v", err)
61 }
62 }
63
Serge Bazanski6a328e62024-02-12 17:37:50 +010064 configName := "launch-cluster"
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000065 if err := metroctl.InstallKubeletConfig(ctx, metroctlPath, cl.ConnectOptions(), configName, apiservers[0]); err != nil {
Serge Bazanski1f8cad72023-03-20 16:58:10 +010066 log.Fatalf("InstallKubeletConfig: %v", err)
67 }
68
Serge Bazanski075465c2021-11-16 15:38:49 +010069 log.Printf("Launch: Cluster running!")
Serge Bazanski1f8cad72023-03-20 16:58:10 +010070 log.Printf(" To access cluster use: metroctl %s", cl.MetroctlFlags())
71 log.Printf(" Or use this handy wrapper: %s", wpath)
72 log.Printf(" To access Kubernetes, use kubectl --context=%s", configName)
Serge Bazanski075465c2021-11-16 15:38:49 +010073
74 <-ctx.Done()
75 cl.Close()
Lorenz Brun52f7f292020-06-24 16:42:02 +020076}