/
ops.go
76 lines (67 loc) · 1.68 KB
/
ops.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
package plural
import (
"fmt"
"github.com/pluralsh/plural-cli/pkg/provider"
"github.com/pluralsh/plural-cli/pkg/utils"
"github.com/urfave/cli"
v1 "k8s.io/api/core/v1"
)
func (p *Plural) opsCommands() []cli.Command {
return []cli.Command{
{
Name: "terminate",
Usage: "terminates a worker node in your cluster",
ArgsUsage: "NAME",
Action: latestVersion(initKubeconfig(p.handleTerminateNode)),
},
{
Name: "cluster",
Usage: "list the nodes in your cluster",
Action: latestVersion(initKubeconfig(p.handleListNodes)),
},
}
}
func (p *Plural) handleTerminateNode(c *cli.Context) error {
name := c.Args().Get(0)
provider, err := getProvider()
if err != nil {
return err
}
if err := p.InitKube(); err != nil {
return err
}
node, err := p.Node(name)
if err != nil {
return err
}
return provider.Decommision(node)
}
func (p *Plural) handleListNodes(cli *cli.Context) error {
if err := p.InitKube(); err != nil {
return err
}
nodes, err := p.Nodes()
if err != nil {
return err
}
headers := []string{"Name", "CPU", "Memory", "Region", "Zone"}
return utils.PrintTable(nodes.Items, headers, func(node v1.Node) ([]string, error) {
status := node.Status
labels := node.ObjectMeta.Labels
cpu, mem := status.Capacity["cpu"], status.Capacity["memory"]
return []string{
node.Name,
cpu.String(),
mem.String(),
labels["topology.kubernetes.io/region"],
labels["topology.kubernetes.io/zone"],
}, nil
})
}
func getProvider() (provider.Provider, error) {
_, found := utils.ProjectRoot()
if !found {
return nil, fmt.Errorf("project not initialized, run `plural init` to set up a workspace")
}
return provider.GetProvider()
}