Skip to content

Commit

Permalink
Analyze TCP connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Reed committed Feb 12, 2021
1 parent 529fe8c commit 40af0f8
Show file tree
Hide file tree
Showing 9 changed files with 336 additions and 1 deletion.
25 changes: 25 additions & 0 deletions examples/preflight/host-tcp-connect.yaml
@@ -0,0 +1,25 @@
apiVersion: troubleshoot.sh/v1beta2
kind: HostPreflight
metadata:
name: connect
spec:
collectors:
- tcpConnect:
collectorName: weave host 1
address: 10.128.0.2:6783
analyzers:
- tcpConnect:
collectorName: weave host 1
outcomes:
- fail:
when: "connection-refused"
message: Connection to weave on host 1 was refused
- fail:
when: "connection-timeout"
message: Timed out connecting to weave on host 1
- fail:
when: "error"
message: Unexpected error connecting to weave on host 1
- pass:
when: "connected"
message: Successfully connected to weave on host 1
8 changes: 7 additions & 1 deletion pkg/analyze/analyzer.go
Expand Up @@ -97,7 +97,13 @@ func HostAnalyze(hostAnalyzer *troubleshootv1beta2.HostAnalyze, getFile getColle
}
return []*AnalyzeResult{result}, nil
}

if hostAnalyzer.TCPConnect != nil {
result, err := analyzeHostTCPConnect(hostAnalyzer.TCPConnect, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
return nil, errors.New("invalid analyzer")
}

Expand Down
86 changes: 86 additions & 0 deletions pkg/analyze/host_tcp_connect.go
@@ -0,0 +1,86 @@
package analyzer

import (
"encoding/json"
"fmt"
"path"

"github.com/pkg/errors"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
"github.com/replicatedhq/troubleshoot/pkg/collect"
)

func analyzeHostTCPConnect(hostAnalyzer *troubleshootv1beta2.TCPConnectAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
fullPath := path.Join("connect", fmt.Sprintf("%s.json", hostAnalyzer.CollectorName))

collected, err := getCollectedFileContents(fullPath)
if err != nil {
return nil, errors.Wrapf(err, "failed to read collected file name: %s", fullPath)
}
actual := collect.NetworkStatusResult{}
if err := json.Unmarshal(collected, &actual); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal collected")
}

result := AnalyzeResult{}

title := hostAnalyzer.CheckName
if title == "" {
title = "TCP Connection Attempt"
}
result.Title = title

for _, outcome := range hostAnalyzer.Outcomes {
if outcome.Fail != nil {
if outcome.Fail.When == "" {
result.IsFail = true
result.Message = outcome.Fail.Message
result.URI = outcome.Fail.URI

return &result, nil
}

if string(actual.Status) == outcome.Fail.When {
result.IsFail = true
result.Message = outcome.Fail.Message
result.URI = outcome.Fail.URI

return &result, nil
}
} else if outcome.Warn != nil {
if outcome.Warn.When == "" {
result.IsWarn = true
result.Message = outcome.Warn.Message
result.URI = outcome.Warn.URI

return &result, nil
}

if string(actual.Status) == outcome.Warn.When {
result.IsWarn = true
result.Message = outcome.Warn.Message
result.URI = outcome.Warn.URI

return &result, nil
}
} else if outcome.Pass != nil {
if outcome.Pass.When == "" {
result.IsPass = true
result.Message = outcome.Pass.Message
result.URI = outcome.Pass.URI

return &result, nil
}

if string(actual.Status) == outcome.Pass.When {
result.IsPass = true
result.Message = outcome.Pass.Message
result.URI = outcome.Pass.URI

return &result, nil
}
}
}

return &result, nil
}
92 changes: 92 additions & 0 deletions pkg/analyze/host_tcp_connect_test.go
@@ -0,0 +1,92 @@
package analyzer

import (
"encoding/json"
"testing"

troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
"github.com/replicatedhq/troubleshoot/pkg/collect"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAnalyzeTCPConnect(t *testing.T) {
tests := []struct {
name string
info *collect.NetworkStatusResult
hostAnalyzer *troubleshootv1beta2.TCPConnectAnalyze
result *AnalyzeResult
expectErr bool
}{
{
name: "connection refused, fail",
info: &collect.NetworkStatusResult{
Status: collect.NetworkStatusConnectionRefused,
},
hostAnalyzer: &troubleshootv1beta2.TCPConnectAnalyze{
Outcomes: []*troubleshootv1beta2.Outcome{
{
Fail: &troubleshootv1beta2.SingleOutcome{
When: "connection-refused",
Message: "Connection was refused",
},
},
},
},
result: &AnalyzeResult{
Title: "TCP Connection Attempt",
IsFail: true,
Message: "Connection was refused",
},
},
{
name: "connected, fail",
info: &collect.NetworkStatusResult{
Status: collect.NetworkStatusConnected,
},
hostAnalyzer: &troubleshootv1beta2.TCPConnectAnalyze{
Outcomes: []*troubleshootv1beta2.Outcome{
{
Fail: &troubleshootv1beta2.SingleOutcome{
When: "connection-refused",
Message: "Connection was refused",
},
},
{
Pass: &troubleshootv1beta2.SingleOutcome{
When: "connected",
Message: "Connection was successful",
},
},
},
},
result: &AnalyzeResult{
Title: "TCP Connection Attempt",
IsPass: true,
Message: "Connection was successful",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
req := require.New(t)
b, err := json.Marshal(test.info)
if err != nil {
t.Fatal(err)
}

getCollectedFileContents := func(filename string) ([]byte, error) {
return b, nil
}

result, err := analyzeHostTCPConnect(test.hostAnalyzer, getCollectedFileContents)
if test.expectErr {
req.Error(err)
} else {
req.NoError(err)
}

assert.Equal(t, test.result, result)
})
}
}
8 changes: 8 additions & 0 deletions pkg/apis/troubleshoot/v1beta2/hostanalyzer_shared.go
Expand Up @@ -44,6 +44,12 @@ type BlockDevicesAnalyze struct {
Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"`
}

type TCPConnectAnalyze struct {
AnalyzeMeta `json:",inline" yaml:",inline"`
CollectorName string `json:"collectorName,omitempty" yaml:"collectorName,omitempty"`
Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"`
}

type HostAnalyze struct {
CPU *CPUAnalyze `json:"cpu,omitempty" yaml:"cpu,omitempty"`
//
Expand All @@ -60,4 +66,6 @@ type HostAnalyze struct {
Time *TimeAnalyze `json:"time" yaml:"time"`

BlockDevices *BlockDevicesAnalyze `json:"blockDevices" yaml:"blockDevices"`

TCPConnect *TCPConnectAnalyze `json:"tcpConnect" yaml:"tcpConnect"`
}
7 changes: 7 additions & 0 deletions pkg/apis/troubleshoot/v1beta2/hostcollector_shared.go
Expand Up @@ -67,6 +67,12 @@ type HostBlockDevices struct {
HostCollectorMeta `json:",inline" yaml:",inline"`
}

type TCPConnect struct {
HostCollectorMeta `json:",inline" yaml:",inline"`
Address string `json:"address"`
Timeout string `json:"timeout,omitempty"`
}

type HostCollect struct {
CPU *CPU `json:"cpu,omitempty" yaml:"cpu,omitempty"`
Memory *Memory `json:"memory,omitempty" yaml:"memory,omitempty"`
Expand All @@ -79,6 +85,7 @@ type HostCollect struct {
HTTP *HostHTTP `json:"http,omitempty" yaml:"http,omitempty"`
Time *HostTime `json:"time,omitempty" yaml:"time,omitempty"`
BlockDevices *HostBlockDevices `json:"blockDevices,omitempty" yaml:"blockDevices,omitempty"`
TCPConnect *TCPConnect `json:"tcpConnect,omitempty" yaml:"tcpConnect,omitempty"`
}

func (c *HostCollect) GetName() string {
Expand Down
53 changes: 53 additions & 0 deletions pkg/apis/troubleshoot/v1beta2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/collect/host_collector.go
Expand Up @@ -34,6 +34,8 @@ func (c *HostCollector) RunCollectorSync() (result map[string][]byte, err error)
result, err = HostTime(c)
} else if c.Collect.BlockDevices != nil {
result, err = HostBlockDevices(c)
} else if c.Collect.TCPConnect != nil {
result, err = HostTCPConnect(c)
} else {
err = errors.New("no spec found to run")
return
Expand Down

0 comments on commit 40af0f8

Please sign in to comment.