-
Notifications
You must be signed in to change notification settings - Fork 458
/
show.go
114 lines (88 loc) · 2.73 KB
/
show.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package cluster
import (
"context"
"fmt"
stdnet "net"
"os"
"sort"
"strings"
"text/tabwriter"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
"github.com/talos-systems/net"
"github.com/talos-systems/talos/pkg/cli"
"github.com/talos-systems/talos/pkg/machinery/generic/slices"
"github.com/talos-systems/talos/pkg/provision"
"github.com/talos-systems/talos/pkg/provision/providers"
)
// showCmd represents the cluster show command.
var showCmd = &cobra.Command{
Use: "show",
Short: "Shows info about a local provisioned kubernetes cluster",
Long: ``,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return cli.WithContext(context.Background(), show)
},
}
func show(ctx context.Context) error {
provisioner, err := providers.Factory(ctx, provisionerName)
if err != nil {
return err
}
defer provisioner.Close() //nolint:errcheck
cluster, err := provisioner.Reflect(ctx, clusterName, stateDir)
if err != nil {
return err
}
return showCluster(cluster)
}
func showCluster(cluster provision.Cluster) error {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
fmt.Fprintf(w, "PROVISIONER\t%s\n", cluster.Provisioner())
fmt.Fprintf(w, "NAME\t%s\n", cluster.Info().ClusterName)
fmt.Fprintf(w, "NETWORK NAME\t%s\n", cluster.Info().Network.Name)
cidrs := slices.Map(cluster.Info().Network.CIDRs, func(v stdnet.IPNet) string { return net.FormatCIDR(v.IP, v) })
fmt.Fprintf(w, "NETWORK CIDR\t%s\n", strings.Join(cidrs, ","))
gateways := slices.Map(cluster.Info().Network.GatewayAddrs, stdnet.IP.String)
fmt.Fprintf(w, "NETWORK GATEWAY\t%s\n", strings.Join(gateways, ","))
fmt.Fprintf(w, "NETWORK MTU\t%d\n", cluster.Info().Network.MTU)
if err := w.Flush(); err != nil {
return err
}
fmt.Fprint(os.Stdout, "\nNODES:\n\n")
w = tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
fmt.Fprintf(w, "NAME\tTYPE\tIP\tCPU\tRAM\tDISK\n")
nodes := cluster.Info().Nodes
sort.Slice(nodes, func(i, j int) bool { return nodes[i].Name < nodes[j].Name })
for _, node := range nodes {
cpus := "-"
if node.NanoCPUs > 0 {
cpus = fmt.Sprintf("%.2f", float64(node.NanoCPUs)/1000.0/1000.0/1000.0)
}
mem := "-"
if node.Memory > 0 {
mem = humanize.Bytes(uint64(node.Memory))
}
disk := "-"
if node.DiskSize > 0 {
disk = humanize.Bytes(node.DiskSize)
}
ips := slices.Map(node.IPs, stdnet.IP.String)
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\n",
node.Name,
node.Type,
strings.Join(ips, ","),
cpus,
mem,
disk,
)
}
return w.Flush()
}
func init() {
Cmd.AddCommand(showCmd)
}