Mateusz Zalega | b838e05 | 2022-08-12 18:08:10 +0200 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "log" |
| 6 | |
| 7 | "github.com/spf13/cobra" |
| 8 | |
| 9 | "source.monogon.dev/metropolis/cli/metroctl/core" |
| 10 | clicontext "source.monogon.dev/metropolis/cli/pkg/context" |
| 11 | "source.monogon.dev/metropolis/node/core/identity" |
| 12 | apb "source.monogon.dev/metropolis/proto/api" |
| 13 | ) |
| 14 | |
| 15 | var describeCmd = &cobra.Command{ |
| 16 | Short: "Describes cluster nodes.", |
| 17 | Use: "describe [node-id] [--filter] [--output] [--format]", |
| 18 | Example: "metroctl node describe metropolis-c556e31c3fa2bf0a36e9ccb9fd5d6056", |
| 19 | Run: doDescribe, |
| 20 | Args: cobra.ArbitraryArgs, |
| 21 | } |
| 22 | |
| 23 | func init() { |
| 24 | nodeCmd.AddCommand(describeCmd) |
| 25 | } |
| 26 | |
| 27 | func printNodes(of func(*encoder, *apb.Node) error, nodes []*apb.Node, args []string) { |
| 28 | // Narrow down the output set to supplied node IDs, if any. |
| 29 | qids := make(map[string]bool) |
| 30 | if len(args) != 0 && args[0] != "all" { |
| 31 | for _, a := range args { |
| 32 | qids[a] = true |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | enc := newOutputEncoder() |
| 37 | defer enc.close() |
| 38 | |
| 39 | for _, n := range nodes { |
| 40 | // Filter the information we want client-side. |
| 41 | if len(qids) != 0 { |
| 42 | nid := identity.NodeID(n.Pubkey) |
| 43 | if _, e := qids[nid]; !e { |
| 44 | continue |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | if err := of(enc, n); err != nil { |
| 49 | log.Fatalf("While listing nodes: %v", err) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func doDescribe(cmd *cobra.Command, args []string) { |
| 55 | ctx := clicontext.WithInterrupt(context.Background()) |
| 56 | cc := dialAuthenticated(ctx) |
| 57 | mgmt := apb.NewManagementClient(cc) |
| 58 | |
| 59 | nodes, err := core.GetNodes(ctx, mgmt, flags.filter) |
| 60 | if err != nil { |
| 61 | log.Fatalf("While calling Management.GetNodes: %v", err) |
| 62 | } |
| 63 | |
| 64 | of := func(enc *encoder, n *apb.Node) error { |
| 65 | return enc.writeNode(n) |
| 66 | } |
| 67 | printNodes(of, nodes, args) |
| 68 | } |