-
Notifications
You must be signed in to change notification settings - Fork 19
/
sks_nodepool_evict.go
103 lines (84 loc) · 2.52 KB
/
sks_nodepool_evict.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
package cmd
import (
"errors"
"fmt"
"github.com/exoscale/egoscale"
apiv2 "github.com/exoscale/egoscale/api/v2"
"github.com/spf13/cobra"
)
var sksNodepoolEvictCmd = &cobra.Command{
Use: "evict <cluster name | ID> <Nodepool name | ID> <Node name | ID> [Node name | ID ...]",
Short: "Evict SKS cluster Nodepool members",
Long: `This command evicts specific members from a SKS cluster Nodepool, effectively
shrinking down the Nodepool similar to the "exo sks nodepool scale" command.
Note: Kubernetes Nodes should be drained from their workload prior to being
evicted from their Nodepool, e.g. using "kubectl drain".`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 3 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
cmdSetZoneFlagFromDefault(cmd)
return cmdCheckRequiredFlags(cmd, []string{"zone"})
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
c = args[0]
np = args[1]
nodepool *egoscale.SKSNodepool
)
z, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
zone, err := getZoneByNameOrID(z)
if err != nil {
return fmt.Errorf("error retrieving zone: %s", err)
}
force, err := cmd.Flags().GetBool("force")
if err != nil {
return err
}
if !force {
if !askQuestion(fmt.Sprintf("Do you really want to evict %v from Nodepool %q?", args[2:], np)) {
return nil
}
}
ctx := apiv2.WithEndpoint(gContext, apiv2.NewReqEndpoint(gCurrentAccount.Environment, zone.Name))
cluster, err := lookupSKSCluster(ctx, zone.Name, c)
if err != nil {
return err
}
for _, n := range cluster.Nodepools {
if n.ID == np || n.Name == np {
nodepool = n
break
}
}
if nodepool == nil {
return errors.New("Nodepool not found") // nolint:golint
}
nodes := make([]string, len(args[2:]))
for i, n := range args[2:] {
instance, err := getVirtualMachineByNameOrID(n)
if err != nil {
return fmt.Errorf("invalid Node %q: %s", n, err)
}
nodes[i] = instance.ID.String()
}
decorateAsyncOperation(fmt.Sprintf("Evicting Nodes from Nodepool %q...", np), func() {
err = cluster.EvictNodepoolMembers(ctx, nodepool, nodes)
})
if err != nil {
return err
}
if !gQuiet {
return output(showSKSNodepool(zone, cluster.ID, nodepool.ID))
}
return nil
},
}
func init() {
sksNodepoolEvictCmd.Flags().BoolP("force", "f", false, "Attempt to evict without prompting for confirmation")
sksNodepoolEvictCmd.Flags().StringP("zone", "z", "", "SKS cluster zone")
sksNodepoolCmd.AddCommand(sksNodepoolEvictCmd)
}