github.com/pluralsh/plural-cli@v0.9.5/cmd/plural/ops.go (about)

     1  package plural
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/pluralsh/plural-cli/pkg/provider"
     7  	"github.com/pluralsh/plural-cli/pkg/utils"
     8  	"github.com/urfave/cli"
     9  	v1 "k8s.io/api/core/v1"
    10  )
    11  
    12  func (p *Plural) opsCommands() []cli.Command {
    13  	return []cli.Command{
    14  		{
    15  			Name:      "terminate",
    16  			Usage:     "terminates a worker node in your cluster",
    17  			ArgsUsage: "NAME",
    18  			Action:    latestVersion(initKubeconfig(p.handleTerminateNode)),
    19  		},
    20  		{
    21  			Name:   "cluster",
    22  			Usage:  "list the nodes in your cluster",
    23  			Action: latestVersion(initKubeconfig(p.handleListNodes)),
    24  		},
    25  	}
    26  }
    27  
    28  func (p *Plural) handleTerminateNode(c *cli.Context) error {
    29  	name := c.Args().Get(0)
    30  	provider, err := getProvider()
    31  	if err != nil {
    32  		return err
    33  	}
    34  	if err := p.InitKube(); err != nil {
    35  		return err
    36  	}
    37  	node, err := p.Node(name)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	return provider.Decommision(node)
    43  }
    44  
    45  func (p *Plural) handleListNodes(cli *cli.Context) error {
    46  	if err := p.InitKube(); err != nil {
    47  		return err
    48  	}
    49  	nodes, err := p.Nodes()
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	headers := []string{"Name", "CPU", "Memory", "Region", "Zone"}
    55  	return utils.PrintTable(nodes.Items, headers, func(node v1.Node) ([]string, error) {
    56  		status := node.Status
    57  		labels := node.ObjectMeta.Labels
    58  		cpu, mem := status.Capacity["cpu"], status.Capacity["memory"]
    59  		return []string{
    60  			node.Name,
    61  			cpu.String(),
    62  			mem.String(),
    63  			labels["topology.kubernetes.io/region"],
    64  			labels["topology.kubernetes.io/zone"],
    65  		}, nil
    66  	})
    67  }
    68  
    69  func getProvider() (provider.Provider, error) {
    70  	_, found := utils.ProjectRoot()
    71  	if !found {
    72  		return nil, fmt.Errorf("project not initialized, run `plural init` to set up a workspace")
    73  	}
    74  
    75  	return provider.GetProvider()
    76  }