blob: afdf6d9cc4c0bbfab12ee5b838ddbc8aa3ef8c49 [file] [log] [blame]
Mateusz Zalegadb75e212022-08-04 17:31:34 +02001package main
2
3import (
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 "source.monogon.dev/metropolis/proto/api"
13)
14
15var listCmd = &cobra.Command{
16 Short: "Lists cluster nodes.",
17 Use: "list [node-id] [--filter] [--output] [--format]",
18 Example: "metroctl node list --filter node.status.external_address==\"10.8.0.2\"",
19 Run: doList,
20 Args: cobra.ArbitraryArgs,
21}
22
23func init() {
24 nodeCmd.AddCommand(listCmd)
25}
26
27func doList(cmd *cobra.Command, args []string) {
28 ctx := clicontext.WithInterrupt(context.Background())
29 cc := dialAuthenticated(ctx)
30 mgmt := api.NewManagementClient(cc)
31
32 // Narrow down the output set to supplied node IDs, if any.
33 qids := make(map[string]bool)
34 if len(args) != 0 && args[0] != "all" {
35 for _, a := range args {
36 qids[a] = true
37 }
38 }
39
40 nodes, err := core.GetNodes(ctx, mgmt, flags.filter)
41 if err != nil {
42 log.Fatalf("While calling Management.GetNodes: %v", err)
43 }
44
45 enc := newOutputEncoder()
46 defer enc.close()
47
48 for _, n := range nodes {
49 // Filter the information we want client-side.
50 if len(qids) != 0 {
51 nid := identity.NodeID(n.Pubkey)
52 if _, e := qids[nid]; !e {
53 continue
54 }
55 }
56
57 if err := enc.writeNodeID(n); err != nil {
58 log.Fatalf("While listing nodes: %v", err)
59 }
60 }
61}