/
host_preflight.go
469 lines (411 loc) · 15.2 KB
/
host_preflight.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package cli
import (
"context"
"fmt"
"io"
"os"
"regexp"
"time"
"github.com/briandowns/spinner"
"github.com/mattn/go-isatty"
"github.com/pkg/errors"
"github.com/replicatedhq/kurl/pkg/installer"
"github.com/replicatedhq/kurl/pkg/preflight"
analyze "github.com/replicatedhq/troubleshoot/pkg/analyze"
"github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
"github.com/replicatedhq/troubleshoot/pkg/collect"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)
const hostPreflightCmdExample = `
# Installer spec from file
$ kurl host preflight spec.yaml
# Installer spec from STDIN
$ kubectl get installer 6abe39c -oyaml | kurl host preflight -`
const preflightCmdExample = `
# Installer spec from file
$ kurl cluster preflight spec.yaml
# Installer spec from STDIN
$ kubectl get installer 6abe39c -oyaml | kurl cluster preflight -`
const (
preflightsWarningCode = 3
preflightsIgnoreWarningCode = 2
preflightsErrorCode = 1
)
func newHostPreflightCmd(cli CLI) *cobra.Command {
cmd := &cobra.Command{
Use: "preflight [INSTALLER SPEC FILE|-]",
Short: "Runs kURL host preflight checks",
Example: hostPreflightCmdExample,
SilenceUsage: true,
Args: cobra.ExactArgs(1),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return cli.GetViper().BindPFlags(cmd.PersistentFlags())
},
PreRunE: func(cmd *cobra.Command, args []string) error {
return cli.GetViper().BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
v := cli.GetViper()
installerSpecData, err := retrieveInstallerSpecDataFromArg(cli.GetFS(), cmd.InOrStdin(), args[0])
if err != nil {
return errors.Wrap(err, "retrieve installer spec from arg")
}
installerSpec, err := installer.DecodeSpec(installerSpecData)
if err != nil {
return errors.Wrap(err, "decode installer spec")
}
remotes := append([]string{}, v.GetStringSlice("primary-host")...)
remotes = append(remotes, v.GetStringSlice("secondary-host")...)
data := installer.TemplateData{
Installer: *installerSpec,
IsPrimary: v.GetBool("is-primary"),
IsJoin: v.GetBool("is-join"),
IsUpgrade: v.GetBool("is-upgrade"),
PrimaryHosts: v.GetStringSlice("primary-host"),
SecondaryHosts: v.GetStringSlice("secondary-host"),
RemoteHosts: remotes,
}
preflightSpec := &troubleshootv1beta2.HostPreflight{}
if !v.GetBool("exclude-builtin") {
builtin := preflight.Builtin()
s, err := decodeHostPreflightSpec(builtin, data)
if err != nil {
return errors.Wrap(err, "builtin")
}
preflightSpec = s
}
for _, filename := range v.GetStringSlice("spec") {
spec, err := os.ReadFile(filename)
if err != nil {
return errors.Wrapf(err, "read spec file %s", filename)
}
decoded, err := decodeHostPreflightSpec(string(spec), data)
if err != nil {
return errors.Wrap(err, filename)
}
preflightSpec.Spec.Collectors = append(preflightSpec.Spec.Collectors, decoded.Spec.Collectors...)
preflightSpec.Spec.Analyzers = append(preflightSpec.Spec.Analyzers, decoded.Spec.Analyzers...)
}
if data.IsJoin && data.IsPrimary && !data.IsUpgrade {
// Check connection to kubelet on all remotes
for _, remote := range remotes {
remote = formatAddress(remote)
preflightSpec.Spec.Collectors = append(preflightSpec.Spec.Collectors, tcpHostCollector("kubelet", remote, "10250"))
preflightSpec.Spec.Analyzers = append(preflightSpec.Spec.Analyzers, tcpHostAnalyzer("kubelet", remote, "10250"))
}
// Check connection to etcd on all primaries
for _, primary := range data.PrimaryHosts {
primary = formatAddress(primary)
preflightSpec.Spec.Collectors = append(preflightSpec.Spec.Collectors, tcpHostCollector("etcd peer", primary, "2379"))
preflightSpec.Spec.Collectors = append(preflightSpec.Spec.Collectors, tcpHostCollector("etcd peer", primary, "2380"))
preflightSpec.Spec.Analyzers = append(preflightSpec.Spec.Analyzers, tcpHostAnalyzer("etcd peer", primary, "2379"))
preflightSpec.Spec.Analyzers = append(preflightSpec.Spec.Analyzers, tcpHostAnalyzer("etcd peer", primary, "2380"))
}
}
if data.IsJoin && !data.IsUpgrade {
// Check connection to api-server on all primaries
for _, primary := range data.PrimaryHosts {
primary = formatAddress(primary)
preflightSpec.Spec.Collectors = append(preflightSpec.Spec.Collectors, tcpHostCollector("api-server", primary, "6443"))
preflightSpec.Spec.Analyzers = append(preflightSpec.Spec.Analyzers, tcpHostAnalyzer("api-server", primary, "6443"))
}
}
progressChan := make(chan interface{})
progressContext, progressCancel := context.WithCancel(cmd.Context())
isTerminal := isatty.IsTerminal(os.Stderr.Fd())
go writeProgress(cmd.ErrOrStderr(), progressChan, progressCancel, isTerminal)
results, err := cli.GetHostPreflightRunner().RunHostPreflights(cmd.Context(), preflightSpec, progressChan)
close(progressChan)
<-progressContext.Done()
if err != nil {
return errors.Wrap(err, "run host preflight")
}
printPreflightResults(cmd.OutOrStdout(), results)
if v.GetBool("use-exit-codes") {
switch {
case preflightIsFail(results):
os.Exit(preflightsErrorCode)
case preflightIsWarn(results):
if v.GetBool("ignore-warnings") {
os.Exit(preflightsIgnoreWarningCode)
}
os.Exit(preflightsWarningCode)
}
return nil
}
switch {
case preflightIsFail(results):
return errors.New("host preflights have failures")
case preflightIsWarn(results):
if v.GetBool("ignore-warnings") {
fmt.Fprintln(cmd.ErrOrStderr(), "Warnings ignored by CLI flag \"ignore-warnings\"")
} else {
return ErrWarn
}
}
return nil
},
}
cmd.Flags().Bool("ignore-warnings", false, "ignore host preflight warnings")
cmd.Flags().Bool("is-join", false, "set to true if this node is joining an existing cluster (non-primary implies join)")
cmd.Flags().Bool("is-primary", true, "set to true if this node is a primary")
cmd.Flags().Bool("is-upgrade", false, "set to true if this is an upgrade")
cmd.Flags().Bool("exclude-builtin", false, "set to true to exclude builtin host preflights")
cmd.Flags().Bool("use-exit-codes", true, "set to false to return an error instead of an exit code")
cmd.Flags().StringSlice("primary-host", nil, "host or IP of a control plane node running a Kubernetes API server and etcd peer")
cmd.Flags().StringSlice("secondary-host", nil, "host or IP of a secondary node running kubelet")
cmd.Flags().StringSlice("spec", nil, "host preflight specs")
_ = cmd.MarkFlagFilename("spec", "yaml", "yml")
return cmd
}
func newPreflightCmd(cli CLI) *cobra.Command {
cmd := &cobra.Command{
Use: "preflight [INSTALLER SPEC FILE|-]",
Short: "Runs kURL preflight checks",
Example: preflightCmdExample,
SilenceUsage: true,
Args: cobra.ExactArgs(1),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return cli.GetViper().BindPFlags(cmd.PersistentFlags())
},
PreRunE: func(cmd *cobra.Command, args []string) error {
return cli.GetViper().BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
v := cli.GetViper()
installerSpecData, err := retrieveInstallerSpecDataFromArg(cli.GetFS(), cmd.InOrStdin(), args[0])
if err != nil {
return errors.Wrap(err, "retrieve installer spec from arg")
}
installerSpec, err := installer.DecodeSpec(installerSpecData)
if err != nil {
return errors.Wrap(err, "decode installer spec")
}
remotes := append([]string{}, v.GetStringSlice("primary-host")...)
remotes = append(remotes, v.GetStringSlice("secondary-host")...)
data := installer.TemplateData{
Installer: *installerSpec,
IsPrimary: v.GetBool("is-primary"),
IsJoin: v.GetBool("is-join"),
IsUpgrade: v.GetBool("is-upgrade"),
PrimaryHosts: v.GetStringSlice("primary-host"),
SecondaryHosts: v.GetStringSlice("secondary-host"),
RemoteHosts: remotes,
}
preflightSpec := &troubleshootv1beta2.Preflight{}
if !v.GetBool("exclude-builtin") {
builtin := preflight.BuiltinCluster()
s, err := decodeClusterPreflightSpec(builtin, data)
if err != nil {
return errors.Wrap(err, "builtin")
}
preflightSpec = s
}
for _, filename := range v.GetStringSlice("spec") {
spec, err := os.ReadFile(filename)
if err != nil {
return errors.Wrapf(err, "read spec file %s", filename)
}
decoded, err := decodeClusterPreflightSpec(string(spec), data)
if err != nil {
return errors.Wrap(err, filename)
}
preflightSpec.Spec.Collectors = append(preflightSpec.Spec.Collectors, decoded.Spec.Collectors...)
preflightSpec.Spec.Analyzers = append(preflightSpec.Spec.Analyzers, decoded.Spec.Analyzers...)
}
progressChan := make(chan interface{})
progressContext, progressCancel := context.WithCancel(cmd.Context())
isTerminal := isatty.IsTerminal(os.Stderr.Fd())
go writeProgress(cmd.ErrOrStderr(), progressChan, progressCancel, isTerminal)
results, err := cli.GetClusterPreflightRunner().RunClusterPreflight(preflightSpec, progressChan)
close(progressChan)
<-progressContext.Done()
if err != nil {
return errors.Wrap(err, "run preflight")
}
printPreflightResults(cmd.OutOrStdout(), results)
if v.GetBool("use-exit-codes") {
switch {
case preflightIsFail(results):
os.Exit(preflightsErrorCode)
case preflightIsWarn(results):
if v.GetBool("ignore-warnings") {
os.Exit(preflightsIgnoreWarningCode)
}
os.Exit(preflightsWarningCode)
}
return nil
}
switch {
case preflightIsFail(results):
return errors.New("preflights have failures")
case preflightIsWarn(results):
if v.GetBool("ignore-warnings") {
fmt.Fprintln(cmd.ErrOrStderr(), "Warnings ignored by CLI flag \"ignore-warnings\"")
} else {
return ErrWarn
}
}
return nil
},
}
cmd.Flags().Bool("ignore-warnings", false, "ignore preflight warnings")
cmd.Flags().Bool("is-join", false, "set to true if this node is joining an existing cluster (non-primary implies join)")
cmd.Flags().Bool("is-primary", true, "set to true if this node is a primary")
cmd.Flags().Bool("is-upgrade", false, "set to true if this is an upgrade")
cmd.Flags().Bool("exclude-builtin", false, "set to true to exclude builtin preflights")
cmd.Flags().Bool("use-exit-codes", true, "set to false to return an error instead of an exit code")
cmd.Flags().StringSlice("primary-host", nil, "host or IP of a control plane node running a Kubernetes API server and etcd peer")
cmd.Flags().StringSlice("secondary-host", nil, "host or IP of a secondary node running kubelet")
cmd.Flags().StringSlice("spec", nil, "preflight specs")
_ = cmd.MarkFlagFilename("spec", "yaml", "yml")
return cmd
}
func tcpHostCollector(service, address, port string) *v1beta2.HostCollect {
name := fmt.Sprintf("%s %s:%s", service, address, port)
return &v1beta2.HostCollect{
TCPConnect: &v1beta2.TCPConnect{
HostCollectorMeta: v1beta2.HostCollectorMeta{
CollectorName: name,
},
Address: fmt.Sprintf("%s:%s", address, port),
Timeout: "5s",
},
}
}
func tcpHostAnalyzer(service, address, port string) *v1beta2.HostAnalyze {
name := fmt.Sprintf("%s %s:%s", service, address, port)
return &v1beta2.HostAnalyze{
TCPConnect: &v1beta2.TCPConnectAnalyze{
AnalyzeMeta: v1beta2.AnalyzeMeta{
CheckName: fmt.Sprintf("%s %s:%s TCP connection status", service, address, port),
},
CollectorName: name,
Outcomes: []*v1beta2.Outcome{
{
Warn: &v1beta2.SingleOutcome{
When: collect.NetworkStatusConnectionRefused,
Message: fmt.Sprintf("Connection to %s %s:%s was refused", service, address, port),
},
},
{
Warn: &v1beta2.SingleOutcome{
When: collect.NetworkStatusConnectionTimeout,
Message: fmt.Sprintf("Timed out connecting to %s %s:%s", service, address, port),
},
},
{
Warn: &v1beta2.SingleOutcome{
When: collect.NetworkStatusErrorOther,
Message: fmt.Sprintf("Unexpected error connecting to %s %s:%s", service, address, port),
},
},
{
Pass: &v1beta2.SingleOutcome{
When: collect.NetworkStatusConnected,
Message: fmt.Sprintf("Successfully connected to %s %s:%s", service, address, port),
},
},
},
},
}
}
func decodeHostPreflightSpec(raw string, data installer.TemplateData) (*troubleshootv1beta2.HostPreflight, error) {
spec, err := installer.ExecuteTemplate("installerSpec", raw, data)
if err != nil {
return nil, errors.Wrapf(err, "execute installer template")
}
decoded, err := preflight.HostDecode(spec)
return decoded, errors.Wrap(err, "decode HostPreflight spec")
}
func decodeClusterPreflightSpec(raw string, data installer.TemplateData) (*troubleshootv1beta2.Preflight, error) {
spec, err := installer.ExecuteTemplate("installerSpec", raw, data)
if err != nil {
return nil, errors.Wrapf(err, "execute installer template")
}
decoded, err := preflight.Decode(spec)
return decoded, errors.Wrap(err, "decode Preflight spec")
}
var collectorStartRegexp = regexp.MustCompile(`^\[.+\] Running collector\.\.\.$`)
func writeProgress(w io.Writer, ch <-chan interface{}, cancel func(), isTerminal bool) {
var sp *spinner.Spinner
for line := range ch {
s := fmt.Sprintf("%s", line)
if collectorStartRegexp.MatchString(s) {
if sp != nil {
sp.Stop()
}
fmt.Fprintln(w, s)
if isTerminal {
sp = spinner.New(
spinner.CharSets[9],
100*time.Millisecond,
spinner.WithWriter(w),
spinner.WithColor("reset"),
spinner.WithFinalMSG("✔ complete\n"),
)
sp.Start()
}
} else {
if sp != nil {
sp.Suffix = fmt.Sprintf(" %s", line)
}
}
}
if sp != nil {
sp.Stop()
}
cancel()
}
func retrieveInstallerSpecDataFromArg(fs afero.Fs, stdin io.Reader, arg string) ([]byte, error) {
if arg == "-" {
data, err := io.ReadAll(stdin)
if err != nil {
return nil, errors.Wrap(err, "read from stdin")
}
if len(data) == 0 {
return nil, errors.New("no data read from stdin")
}
return data, nil
}
data, err := afero.ReadFile(fs, arg)
return data, errors.Wrapf(err, "read from file %s", arg)
}
func printPreflightResults(w io.Writer, results []*analyze.AnalyzeResult) {
for _, result := range results {
printPreflightResult(w, result)
}
}
func printPreflightResult(w io.Writer, result *analyze.AnalyzeResult) {
switch {
case result.IsPass:
fmt.Fprintln(w, OutputPassGreen(), fmt.Sprintf("%s: %s", result.Title, result.Message))
case result.IsWarn:
fmt.Fprintln(w, OutputWarnYellow(), fmt.Sprintf("%s: %s", result.Title, result.Message))
case result.IsFail:
fmt.Fprintln(w, OutputFailRed(), fmt.Sprintf("%s: %s", result.Title, result.Message))
}
}
func preflightIsFail(results []*analyze.AnalyzeResult) bool {
for _, result := range results {
switch {
case result.IsFail:
return true
}
}
return false
}
func preflightIsWarn(results []*analyze.AnalyzeResult) bool {
hasWarn := false
for _, result := range results {
switch {
case result.IsFail:
return false
case result.IsWarn:
hasWarn = true
}
}
return hasWarn
}