-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
249 lines (191 loc) · 6.18 KB
/
utils.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
import (
"fmt"
"os"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/kubicorn/kubicorn/pkg/logger"
"github.com/weaveworks/eksctl/pkg/eks"
"github.com/weaveworks/eksctl/pkg/eks/api"
"github.com/weaveworks/eksctl/pkg/utils/kubeconfig"
)
var (
utilsKubeconfigInputPath string
utilsKubeconfigOutputPath string
utilsSetContext bool
utilsAutoKubeconfigPath bool
utilsDescribeStackAll bool
utilsDescribeStackEvents bool
)
func utilsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "utils",
Short: "Various utils",
Run: func(c *cobra.Command, _ []string) {
c.Help()
},
}
cmd.AddCommand(waitNodesCmd())
cmd.AddCommand(writeKubeconfigCmd())
cmd.AddCommand(describeStacksCmd())
return cmd
}
func waitNodesCmd() *cobra.Command {
cfg := &api.ClusterConfig{}
cmd := &cobra.Command{
Use: "wait-nodes",
Short: "Wait for nodes",
Run: func(_ *cobra.Command, _ []string) {
if err := doWaitNodes(cfg); err != nil {
logger.Critical("%s\n", err.Error())
os.Exit(1)
}
},
}
fs := cmd.Flags()
fs.StringVar(&utilsKubeconfigInputPath, "kubeconfig", "kubeconfig", "path to read kubeconfig")
fs.IntVarP(&cfg.MinNodes, "nodes-min", "m", DEFAULT_NODE_COUNT, "minimum number of nodes to wait for")
fs.DurationVar(&cfg.WaitTimeout, "timeout", api.DefaultWaitTimeout, "how long to wait")
return cmd
}
func doWaitNodes(cfg *api.ClusterConfig) error {
ctl := eks.New(cfg)
if utilsKubeconfigInputPath == "" {
return fmt.Errorf("--kubeconfig must be set")
}
clientConfig, err := clientcmd.BuildConfigFromFlags("", utilsKubeconfigInputPath)
if err != nil {
return err
}
clientset, err := kubernetes.NewForConfig(clientConfig)
if err != nil {
return err
}
if err := ctl.WaitForNodes(clientset); err != nil {
return err
}
return nil
}
func writeKubeconfigCmd() *cobra.Command {
cfg := &api.ClusterConfig{}
cmd := &cobra.Command{
Use: "write-kubeconfig",
Short: "Write kubeconfig file for a given cluster",
Run: func(_ *cobra.Command, args []string) {
if err := doWriteKubeconfigCmd(cfg, getNameArg(args)); err != nil {
logger.Critical("%s\n", err.Error())
os.Exit(1)
}
},
}
fs := cmd.Flags()
fs.StringVarP(&cfg.ClusterName, "name", "n", "", "EKS cluster name (required)")
fs.StringVarP(&cfg.Region, "region", "r", api.DEFAULT_EKS_REGION, "AWS region")
fs.StringVarP(&cfg.Profile, "profile", "p", "", "AWS credentials profile to use (overrides the AWS_PROFILE environment variable)")
fs.BoolVar(&utilsAutoKubeconfigPath, "auto-kubeconfig", false, fmt.Sprintf("save kubconfig file by cluster name – %q", kubeconfig.AutoPath("<name>")))
fs.StringVar(&utilsKubeconfigOutputPath, "kubeconfig", kubeconfig.DefaultPath, "path to write kubeconfig")
fs.BoolVar(&utilsSetContext, "set-kubeconfig-context", true, "if true then current-context will be set in kubeconfig; if a context is already set then it will be overwritten")
return cmd
}
func doWriteKubeconfigCmd(cfg *api.ClusterConfig, name string) error {
ctl := eks.New(cfg)
if err := ctl.CheckAuth(); err != nil {
return err
}
if cfg.ClusterName != "" && name != "" {
return fmt.Errorf("--name=%s and argument %s cannot be used at the same time", cfg.ClusterName, name)
}
if name != "" {
cfg.ClusterName = name
}
if cfg.ClusterName == "" {
return fmt.Errorf("--name must be set")
}
if utilsAutoKubeconfigPath {
if utilsKubeconfigOutputPath != kubeconfig.DefaultPath {
return fmt.Errorf("--kubeconfig and --auto-kubeconfig cannot be used at the same time")
}
utilsKubeconfigOutputPath = kubeconfig.AutoPath(cfg.ClusterName)
}
cluster, err := ctl.DescribeControlPlane()
if err != nil {
return err
}
logger.Debug("cluster = %#v", cluster)
if err := ctl.GetCredentials(*cluster); err != nil {
return err
}
clientConfigBase, err := ctl.NewClientConfig()
if err != nil {
return err
}
config := clientConfigBase.WithExecAuthenticator()
filename, err := kubeconfig.Write(utilsKubeconfigOutputPath, config.Client, utilsSetContext)
if err != nil {
return errors.Wrap(err, "writing kubeconfig")
}
logger.Success("saved kubeconfig as %q", filename)
return nil
}
func describeStacksCmd() *cobra.Command {
cfg := &api.ClusterConfig{}
cmd := &cobra.Command{
Use: "describe-stacks",
Short: "Describe CloudFormation stack for a given cluster",
Run: func(_ *cobra.Command, args []string) {
if err := doDescribeStacksCmd(cfg, getNameArg(args)); err != nil {
logger.Critical("%s\n", err.Error())
os.Exit(1)
}
},
}
fs := cmd.Flags()
fs.StringVarP(&cfg.ClusterName, "name", "n", "", "EKS cluster name (required)")
fs.StringVarP(&cfg.Region, "region", "r", api.DEFAULT_EKS_REGION, "AWS region")
fs.StringVarP(&cfg.Profile, "profile", "p", "", "AWS credentials profile to use (overrides the AWS_PROFILE environment variable)")
fs.BoolVar(&utilsDescribeStackAll, "all", false, "include deleted stacks")
fs.BoolVar(&utilsDescribeStackEvents, "events", true, "include stack events")
return cmd
}
func doDescribeStacksCmd(cfg *api.ClusterConfig, name string) error {
ctl := eks.New(cfg)
if err := ctl.CheckAuth(); err != nil {
return err
}
if cfg.ClusterName != "" && name != "" {
return fmt.Errorf("--name=%s and argument %s cannot be used at the same time", cfg.ClusterName, name)
}
if name != "" {
cfg.ClusterName = name
}
if cfg.ClusterName == "" {
return fmt.Errorf("--name must be set")
}
stackManager := ctl.NewStackManager()
stacks, err := stackManager.DescribeStacks(cfg.ClusterName)
if err != nil {
return err
}
if len(stacks) < 2 {
logger.Warning("only %d stacks found, for a ready-to-use cluster there should be at least 2", len(stacks))
}
for _, s := range stacks {
if !utilsDescribeStackAll && *s.StackStatus == cloudformation.StackStatusDeleteComplete {
continue
}
logger.Info("stack/%s = %#v", *s.StackName, s)
if utilsDescribeStackEvents {
events, err := stackManager.DescribeStackEvents(s)
if err != nil {
logger.Critical(err.Error())
}
for i, e := range events {
logger.Info("events/%s[%d] = %#v", *s.StackName, i, e)
}
}
}
return nil
}