blob: 97198d8b591eb94a94786ad1b4380c165d8f5756 [file] [log] [blame]
Serge Bazanskicfbbbdb2023-03-22 17:48:08 +01001package main
2
3import (
4 "context"
5 "io"
6 "log"
7 "os"
8
9 "github.com/spf13/cobra"
10
11 "source.monogon.dev/metropolis/cli/metroctl/core"
12 clicontext "source.monogon.dev/metropolis/cli/pkg/context"
13 "source.monogon.dev/metropolis/node/core/identity"
14 apb "source.monogon.dev/metropolis/proto/api"
15)
16
17var nodeCmd = &cobra.Command{
18 Short: "Updates and queries node information.",
19 Use: "node",
20}
21
22var nodeDescribeCmd = &cobra.Command{
23 Short: "Describes cluster nodes.",
24 Use: "describe [node-id] [--filter] [--output] [--format]",
25 Example: "metroctl node describe metropolis-c556e31c3fa2bf0a36e9ccb9fd5d6056",
26 Run: func(cmd *cobra.Command, args []string) {
27 ctx := clicontext.WithInterrupt(context.Background())
28 cc := dialAuthenticated(ctx)
29 mgmt := apb.NewManagementClient(cc)
30
31 nodes, err := core.GetNodes(ctx, mgmt, flags.filter)
32 if err != nil {
33 log.Fatalf("While calling Management.GetNodes: %v", err)
34 }
35
36 printNodes(nodes, args, nil)
37 },
38 Args: cobra.ArbitraryArgs,
39}
40
41var nodeListCmd = &cobra.Command{
42 Short: "Lists cluster nodes.",
43 Use: "list [node-id] [--filter] [--output] [--format]",
44 Example: "metroctl node list --filter node.status.external_address==\"10.8.0.2\"",
45 Run: func(cmd *cobra.Command, args []string) {
46 ctx := clicontext.WithInterrupt(context.Background())
47 cc := dialAuthenticated(ctx)
48 mgmt := apb.NewManagementClient(cc)
49
50 nodes, err := core.GetNodes(ctx, mgmt, flags.filter)
51 if err != nil {
52 log.Fatalf("While calling Management.GetNodes: %v", err)
53 }
54
55 printNodes(nodes, args, map[string]bool{"node id": true})
56 },
57 Args: cobra.ArbitraryArgs,
58}
59
60func init() {
61 nodeCmd.AddCommand(nodeDescribeCmd)
62 nodeCmd.AddCommand(nodeListCmd)
63 rootCmd.AddCommand(nodeCmd)
64}
65
66func printNodes(nodes []*apb.Node, args []string, onlyColumns map[string]bool) {
67 o := io.WriteCloser(os.Stdout)
68 if flags.output != "" {
69 of, err := os.Create(flags.output)
70 if err != nil {
71 log.Fatalf("Couldn't create the output file at %s: %v", flags.output, err)
72 }
73 o = of
74 }
75
76 // Narrow down the output set to supplied node IDs, if any.
77 qids := make(map[string]bool)
78 if len(args) != 0 && args[0] != "all" {
79 for _, a := range args {
80 qids[a] = true
81 }
82 }
83
84 var t table
85 for _, n := range nodes {
86 // Filter the information we want client-side.
87 if len(qids) != 0 {
88 nid := identity.NodeID(n.Pubkey)
89 if _, e := qids[nid]; !e {
90 continue
91 }
92 }
93 t.add(nodeEntry(n))
94 }
95
96 t.print(o, onlyColumns)
97}