/
refresh_nodes.go
74 lines (62 loc) · 2.19 KB
/
refresh_nodes.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
package cmd
import (
"log"
"strings"
"github.com/Songmu/prompter"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/get-bridge/truss-cli/truss"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type asgFilterFunc func(*autoscaling.Group) bool
var refreshNodesCmd = &cobra.Command{
Use: "refresh-nodes [-a|--all] [-n|--nodegroup]",
Short: "Trigger an instance refresh on a Truss Nodegroup's ASG",
RunE: func(cmd *cobra.Command, args []string) error {
var ff truss.ASGFilterFunc
if viper.GetBool("refresh_all") {
ff = clusterFilterFunc(must(envClusterName()))
} else {
ff = nodeGroupFilterFunc(must(envClusterName()), viper.GetString("refresh_node_group"))
}
rnc := truss.NewRefreshNodesCmd(must(envClusterRegion()), must(envClusterRoleArn()))
asgs, err := rnc.GetFilteredAutoscalingGroups(ff)
if err != nil {
return err
}
for _, asg := range asgs {
if viper.GetBool("refresh_yes") || prompter.YN("Trigger Instance Refresh on "+*asg.AutoScalingGroupName+"?", false) {
log.Printf("Triggering instance refresh on ASG %s", *asg.AutoScalingGroupName)
if err := rnc.RefreshNodes(asg); err != nil {
return err
}
}
}
return nil
},
}
func clusterFilterFunc(clusterName string) truss.ASGFilterFunc {
return func(g *autoscaling.Group) bool {
for _, t := range g.Tags {
if *t.Key == "kubernetes.io/cluster/"+clusterName && *t.Value == "owned" {
return true
}
}
return false
}
}
func nodeGroupFilterFunc(clusterName, groupName string) truss.ASGFilterFunc {
return func(g *autoscaling.Group) bool {
prefix := strings.Replace(clusterName, "cluster", "", 1) + groupName
return strings.HasPrefix(*g.AutoScalingGroupName, prefix)
}
}
func init() {
rootCmd.AddCommand(refreshNodesCmd)
refreshNodesCmd.Flags().BoolP("all", "a", false, "Refresh all node groups")
viper.BindPFlag("refresh_all", refreshNodesCmd.Flags().Lookup("all"))
refreshNodesCmd.Flags().StringP("nodegroup", "n", "default", "Node group to refresh")
viper.BindPFlag("refresh_node_group", refreshNodesCmd.Flags().Lookup("nodegroup"))
refreshNodesCmd.Flags().BoolP("yes", "y", false, "Say yes to prompts")
viper.BindPFlag("refresh_yes", refreshNodesCmd.Flags().Lookup("yes"))
}