Skip to content

Commit 4b78c43

Browse files
committed
Host preflight ux improvements
1 parent 09d16ff commit 4b78c43

23 files changed

+358
-186
lines changed

cmd/preflight/cli/run.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func runPreflights(v *viper.Viper, arg string) error {
9393

9494
s := spin.New()
9595
go func() {
96+
lastMsg := ""
9697
for {
9798
select {
9899
case msg, ok := <-progressCh:
@@ -104,6 +105,10 @@ func runPreflights(v *viper.Viper, arg string) error {
104105
c := color.New(color.FgHiRed)
105106
c.Println(fmt.Sprintf("%s\r * %v", cursor.ClearEntireLine(), msg))
106107
case string:
108+
if lastMsg == msg {
109+
break
110+
}
111+
lastMsg = msg
107112
c := color.New(color.FgCyan)
108113
c.Println(fmt.Sprintf("%s\r * %s", cursor.ClearEntireLine(), msg))
109114
}

pkg/analyze/analyzer.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,8 @@ func HostAnalyze(hostAnalyzer *troubleshootv1beta2.HostAnalyze, getFile getColle
4747
return NewAnalyzeResultError(analyzer, errors.New("invalid analyzer"))
4848
}
4949

50-
isExcluded, err := analyzer.IsExcluded()
51-
if err != nil {
52-
return NewAnalyzeResultError(analyzer, errors.Wrap(err, "is excluded"))
53-
} else if isExcluded {
50+
isExcluded, _ := analyzer.IsExcluded()
51+
if isExcluded {
5452
return nil
5553
}
5654

pkg/analyze/host_analyzer.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@ func GetHostAnalyzer(analyzer *troubleshootv1beta2.HostAnalyze) (HostAnalyzer, b
1212
switch {
1313
case analyzer.CPU != nil:
1414
return &AnalyzeHostCPU{analyzer.CPU}, true
15+
case analyzer.Memory != nil:
16+
return &AnalyzeHostMemory{analyzer.Memory}, true
1517
case analyzer.TCPLoadBalancer != nil:
1618
return &AnalyzeHostTCPLoadBalancer{analyzer.TCPLoadBalancer}, true
1719
case analyzer.HTTPLoadBalancer != nil:
1820
return &AnalyzeHostHTTPLoadBalancer{analyzer.HTTPLoadBalancer}, true
1921
case analyzer.DiskUsage != nil:
2022
return &AnalyzeHostDiskUsage{analyzer.DiskUsage}, true
21-
case analyzer.Memory != nil:
22-
return &AnalyzeHostMemory{analyzer.Memory}, true
2323
case analyzer.TCPPortStatus != nil:
2424
return &AnalyzeHostTCPPortStatus{analyzer.TCPPortStatus}, true
2525
case analyzer.HTTP != nil:
2626
return &AnalyzeHostHTTP{analyzer.HTTP}, true
27+
case analyzer.Time != nil:
28+
return &AnalyzeHostTime{analyzer.Time}, true
2729
case analyzer.BlockDevices != nil:
2830
return &AnalyzeHostBlockDevices{analyzer.BlockDevices}, true
2931
case analyzer.TCPConnect != nil:

pkg/collect/host_block_device.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os/exec"
99

1010
"github.com/pkg/errors"
11+
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
1112
)
1213

1314
type BlockDeviceInfo struct {
@@ -28,7 +29,19 @@ type BlockDeviceInfo struct {
2829
const lsblkColumns = "NAME,KNAME,PKNAME,TYPE,MAJ:MIN,SIZE,FSTYPE,MOUNTPOINT,SERIAL,RO,RM"
2930
const lsblkFormat = `NAME=%q KNAME=%q PKNAME=%q TYPE=%q MAJ:MIN="%d:%d" SIZE="%d" FSTYPE=%q MOUNTPOINT=%q SERIAL=%q RO="%d" RM="%d0"`
3031

31-
func HostBlockDevices(c *HostCollector) (map[string][]byte, error) {
32+
type CollectHostBlockDevices struct {
33+
hostCollector *troubleshootv1beta2.HostBlockDevices
34+
}
35+
36+
func (c *CollectHostBlockDevices) Title() string {
37+
return hostCollectorTitleOrDefault(c.hostCollector.HostCollectorMeta, "Block Devices")
38+
}
39+
40+
func (c *CollectHostBlockDevices) IsExcluded() (bool, error) {
41+
return isExcluded(c.hostCollector.Exclude)
42+
}
43+
44+
func (c *CollectHostBlockDevices) Collect(progressChan chan<- interface{}) (map[string][]byte, error) {
3245
var devices []BlockDeviceInfo
3346

3447
cmd := exec.Command("lsblk", "--noheadings", "--bytes", "--pairs", "-o", lsblkColumns)

pkg/collect/host_certificate.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"io/ioutil"
77
"path/filepath"
88
"strings"
9+
10+
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
911
)
1012

1113
const KeyPairMissing = "key-pair-missing"
@@ -15,10 +17,22 @@ const KeyPairMismatch = "key-pair-mismatch"
1517
const KeyPairInvalid = "key-pair-invalid"
1618
const KeyPairValid = "key-pair-valid"
1719

18-
func HostCertificate(c *HostCollector) (map[string][]byte, error) {
20+
type CollectHostCertificate struct {
21+
hostCollector *troubleshootv1beta2.Certificate
22+
}
23+
24+
func (c *CollectHostCertificate) Title() string {
25+
return hostCollectorTitleOrDefault(c.hostCollector.HostCollectorMeta, "Certificate Key Pair")
26+
}
27+
28+
func (c *CollectHostCertificate) IsExcluded() (bool, error) {
29+
return isExcluded(c.hostCollector.Exclude)
30+
}
31+
32+
func (c *CollectHostCertificate) Collect(progressChan chan<- interface{}) (map[string][]byte, error) {
1933
var result = KeyPairValid
2034

21-
_, err := tls.LoadX509KeyPair(c.Collect.Certificate.CertificatePath, c.Collect.Certificate.KeyPath)
35+
_, err := tls.LoadX509KeyPair(c.hostCollector.CertificatePath, c.hostCollector.KeyPath)
2236
if err != nil {
2337
if strings.Contains(err.Error(), "no such file") {
2438
result = KeyPairMissing
@@ -29,7 +43,7 @@ func HostCertificate(c *HostCollector) (map[string][]byte, error) {
2943
} else if strings.Contains(err.Error(), "private key does not match public key") {
3044
result = KeyPairMismatch
3145
} else if strings.Contains(err.Error(), "failed to parse private key") {
32-
if encrypted, _ := isEncryptedKey(c.Collect.Certificate.KeyPath); encrypted {
46+
if encrypted, _ := isEncryptedKey(c.hostCollector.KeyPath); encrypted {
3347
result = KeyPairEncrypted
3448
} else {
3549
result = KeyPairInvalid
@@ -39,7 +53,7 @@ func HostCertificate(c *HostCollector) (map[string][]byte, error) {
3953
}
4054
}
4155

42-
collectorName := c.Collect.Certificate.CollectorName
56+
collectorName := c.hostCollector.CollectorName
4357
if collectorName == "" {
4458
collectorName = "certificate"
4559
}

pkg/collect/host_collector.go

Lines changed: 38 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,51 @@
11
package collect
22

33
import (
4-
"github.com/pkg/errors"
54
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
6-
"github.com/replicatedhq/troubleshoot/pkg/multitype"
75
)
86

9-
type HostCollector struct {
10-
Collect *troubleshootv1beta2.HostCollect
7+
type HostCollector interface {
8+
Title() string
9+
IsExcluded() (bool, error)
10+
Collect(progressChan chan<- interface{}) (map[string][]byte, error)
1111
}
1212

13-
type HostCollectors []*HostCollector
14-
15-
func (c *HostCollector) RunCollectorSync() (result map[string][]byte, err error) {
16-
defer func() {
17-
if r := recover(); r != nil {
18-
err = errors.Errorf("recovered rom panic: %v", r)
19-
}
20-
}()
21-
22-
if c.IsExcluded() {
23-
return
24-
}
25-
26-
if c.Collect.CPU != nil {
27-
result, err = HostCPU(c)
28-
} else if c.Collect.Memory != nil {
29-
result, err = HostMemory(c)
30-
} else if c.Collect.TCPLoadBalancer != nil {
31-
result, err = HostTCPLoadBalancer(c)
32-
} else if c.Collect.HTTPLoadBalancer != nil {
33-
result, err = HostHTTPLoadBalancer(c)
34-
} else if c.Collect.DiskUsage != nil {
35-
result, err = HostDiskUsage(c)
36-
} else if c.Collect.TCPPortStatus != nil {
37-
result, err = HostTCPPortStatus(c)
38-
} else if c.Collect.HTTP != nil {
39-
result, err = HostHTTP(c)
40-
} else if c.Collect.Time != nil {
41-
result, err = HostTime(c)
42-
} else if c.Collect.BlockDevices != nil {
43-
result, err = HostBlockDevices(c)
44-
} else if c.Collect.TCPConnect != nil {
45-
result, err = HostTCPConnect(c)
46-
} else if c.Collect.IPV4Interfaces != nil {
47-
result, err = HostIPV4Interfaces(c)
48-
} else if c.Collect.FilesystemPerformance != nil {
49-
result, err = HostFilesystemPerformance(c)
50-
} else if c.Collect.Certificate != nil {
51-
result, err = HostCertificate(c)
52-
} else {
53-
err = errors.New("no spec found to run")
54-
return
55-
}
56-
if err != nil {
57-
return
13+
func GetHostCollector(collector *troubleshootv1beta2.HostCollect) (HostCollector, bool) {
14+
switch {
15+
case collector.CPU != nil:
16+
return &CollectHostCPU{collector.CPU}, true
17+
case collector.Memory != nil:
18+
return &CollectHostMemory{collector.Memory}, true
19+
case collector.TCPLoadBalancer != nil:
20+
return &CollectHostTCPLoadBalancer{collector.TCPLoadBalancer}, true
21+
case collector.HTTPLoadBalancer != nil:
22+
return &CollectHostHTTPLoadBalancer{collector.HTTPLoadBalancer}, true
23+
case collector.DiskUsage != nil:
24+
return &CollectHostDiskUsage{collector.DiskUsage}, true
25+
case collector.TCPPortStatus != nil:
26+
return &CollectHostTCPPortStatus{collector.TCPPortStatus}, true
27+
case collector.HTTP != nil:
28+
return &CollectHostHTTP{collector.HTTP}, true
29+
case collector.Time != nil:
30+
return &CollectHostTime{collector.Time}, true
31+
case collector.BlockDevices != nil:
32+
return &CollectHostBlockDevices{collector.BlockDevices}, true
33+
case collector.TCPConnect != nil:
34+
return &CollectHostTCPConnect{collector.TCPConnect}, true
35+
case collector.IPV4Interfaces != nil:
36+
return &CollectHostIPV4Interfaces{collector.IPV4Interfaces}, true
37+
case collector.FilesystemPerformance != nil:
38+
return &CollectHostFilesystemPerformance{collector.FilesystemPerformance}, true
39+
case collector.Certificate != nil:
40+
return &CollectHostCertificate{collector.Certificate}, true
41+
default:
42+
return nil, false
5843
}
59-
60-
return
6144
}
6245

63-
func (c *HostCollector) IsExcluded() bool {
64-
exclude := multitype.BoolOrString{}
65-
if c.Collect.CPU != nil {
66-
exclude = c.Collect.CPU.Exclude
67-
} else if c.Collect.Memory != nil {
68-
exclude = c.Collect.Memory.Exclude
69-
} else if c.Collect.TCPLoadBalancer != nil {
70-
exclude = c.Collect.TCPLoadBalancer.Exclude
71-
} else if c.Collect.HTTPLoadBalancer != nil {
72-
exclude = c.Collect.HTTPLoadBalancer.Exclude
73-
} else if c.Collect.DiskUsage != nil {
74-
exclude = c.Collect.DiskUsage.Exclude
75-
} else if c.Collect.TCPPortStatus != nil {
76-
exclude = c.Collect.TCPPortStatus.Exclude
77-
} else if c.Collect.HTTP != nil {
78-
exclude = c.Collect.HTTP.Exclude
79-
} else if c.Collect.Time != nil {
80-
exclude = c.Collect.Time.Exclude
81-
} else if c.Collect.BlockDevices != nil {
82-
exclude = c.Collect.BlockDevices.Exclude
83-
} else if c.Collect.TCPConnect != nil {
84-
exclude = c.Collect.TCPConnect.Exclude
85-
} else if c.Collect.IPV4Interfaces != nil {
86-
exclude = c.Collect.IPV4Interfaces.Exclude
87-
} else if c.Collect.FilesystemPerformance != nil {
88-
exclude = c.Collect.FilesystemPerformance.Exclude
89-
} else if c.Collect.Certificate != nil {
90-
exclude = c.Collect.Certificate.Exclude
91-
} else {
92-
return true
93-
}
94-
95-
isExcludedResult, err := isExcluded(exclude)
96-
if err != nil {
97-
return true
46+
func hostCollectorTitleOrDefault(meta troubleshootv1beta2.HostCollectorMeta, defaultTitle string) string {
47+
if meta.CollectorName != "" {
48+
return meta.CollectorName
9849
}
99-
return isExcludedResult
100-
}
101-
102-
func (c *HostCollector) GetDisplayName() string {
103-
return c.Collect.GetName()
50+
return defaultTitle
10451
}

pkg/collect/host_cpu.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55

66
"github.com/pkg/errors"
7+
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
78
"github.com/shirou/gopsutil/cpu"
89
)
910

@@ -12,7 +13,19 @@ type CPUInfo struct {
1213
PhysicalCount int `json:"physicalCount"`
1314
}
1415

15-
func HostCPU(c *HostCollector) (map[string][]byte, error) {
16+
type CollectHostCPU struct {
17+
hostCollector *troubleshootv1beta2.CPU
18+
}
19+
20+
func (c *CollectHostCPU) Title() string {
21+
return hostCollectorTitleOrDefault(c.hostCollector.HostCollectorMeta, "CPU Info")
22+
}
23+
24+
func (c *CollectHostCPU) IsExcluded() (bool, error) {
25+
return isExcluded(c.hostCollector.Exclude)
26+
}
27+
28+
func (c *CollectHostCPU) Collect(progressChan chan<- interface{}) (map[string][]byte, error) {
1629
cpuInfo := CPUInfo{}
1730

1831
logicalCount, err := cpu.Counts(true)

pkg/collect/host_disk_usage.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88

99
"github.com/pkg/errors"
10+
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
1011
"github.com/shirou/gopsutil/disk"
1112
)
1213

@@ -15,14 +16,26 @@ type DiskUsageInfo struct {
1516
UsedBytes uint64 `json:"used_bytes"`
1617
}
1718

18-
func HostDiskUsage(c *HostCollector) (map[string][]byte, error) {
19+
type CollectHostDiskUsage struct {
20+
hostCollector *troubleshootv1beta2.DiskUsage
21+
}
22+
23+
func (c *CollectHostDiskUsage) Title() string {
24+
return hostCollectorTitleOrDefault(c.hostCollector.HostCollectorMeta, fmt.Sprintf("Disk Usage %s", c.hostCollector.CollectorName))
25+
}
26+
27+
func (c *CollectHostDiskUsage) IsExcluded() (bool, error) {
28+
return isExcluded(c.hostCollector.Exclude)
29+
}
30+
31+
func (c *CollectHostDiskUsage) Collect(progressChan chan<- interface{}) (map[string][]byte, error) {
1932
result := map[string][]byte{}
2033

21-
if c.Collect.DiskUsage == nil {
34+
if c.hostCollector == nil {
2235
return result, nil
2336
}
2437

25-
pathExists, err := traverseFiletreeDirExists(c.Collect.DiskUsage.Path)
38+
pathExists, err := traverseFiletreeDirExists(c.hostCollector.Path)
2639
if err != nil {
2740
return result, errors.Wrap(err, "traverse file tree")
2841
}
@@ -39,7 +52,7 @@ func HostDiskUsage(c *HostCollector) (map[string][]byte, error) {
3952
if err != nil {
4053
return nil, errors.Wrap(err, "failed to marshal disk space info")
4154
}
42-
key := HostDiskUsageKey(c.Collect.DiskUsage.CollectorName)
55+
key := HostDiskUsageKey(c.hostCollector.CollectorName)
4356
result[key] = b
4457

4558
return result, nil

pkg/collect/host_filesystem_performance.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,30 @@ import (
44
"math"
55
"math/rand"
66
"time"
7+
8+
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
79
)
810

911
func init() {
1012
rand.Seed(time.Now().UnixNano())
1113
}
1214

15+
type CollectHostFilesystemPerformance struct {
16+
hostCollector *troubleshootv1beta2.FilesystemPerformance
17+
}
18+
19+
func (c *CollectHostFilesystemPerformance) Title() string {
20+
return hostCollectorTitleOrDefault(c.hostCollector.HostCollectorMeta, "Filesystem Performance")
21+
}
22+
23+
func (c *CollectHostFilesystemPerformance) IsExcluded() (bool, error) {
24+
return isExcluded(c.hostCollector.Exclude)
25+
}
26+
27+
func (c *CollectHostFilesystemPerformance) Collect(progressChan chan<- interface{}) (map[string][]byte, error) {
28+
return collectHostFilesystemPerformance(c.hostCollector)
29+
}
30+
1331
type FSPerfResults struct {
1432
Min time.Duration
1533
Max time.Duration
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package collect
22

3-
import "github.com/pkg/errors"
3+
import (
4+
"github.com/pkg/errors"
5+
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
6+
)
47

5-
func HostFilesystemPerformance(c *HostCollector) (map[string][]byte, error) {
8+
func collectHostFilesystemPerformance(hostCollector *troubleshootv1beta2.FilesystemPerformance) (map[string][]byte, error) {
69
return nil, errors.New("Filesystem performance collector is only implemented for Linux")
710
}

0 commit comments

Comments
 (0)