-
Notifications
You must be signed in to change notification settings - Fork 93
/
weave.go
197 lines (160 loc) · 4.87 KB
/
weave.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
package analyzer
import (
"encoding/json"
"fmt"
"regexp"
"strings"
"github.com/pkg/errors"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
)
// relevant fields from https://github.com/weaveworks/weave/blob/e3712152d2a0fe3bc998964c948e45bdf8ff6144/prog/weaver/http.go#L295
type WeaveReport struct {
Router WeaveRouter
IPAM WeaveIPAM
}
type WeaveRouter struct {
NickName string // this is the hostname
Connections []WeaveConnection
}
type WeaveIPAM struct {
RangeNumIPs int
ActiveIPs int
PendingAllocates []string
}
type WeaveConnection struct {
State string
Info string
Attrs WeaveAttributes
}
type WeaveAttributes struct {
Encrypted bool `json:"encrypted"`
MTU int `json:"mtu"`
Name string `json:"name"`
}
type AnalyzeWeaveReport struct {
analyzer *troubleshootv1beta2.WeaveReportAnalyze
}
func (a *AnalyzeWeaveReport) Title() string {
return "Weave CNI"
}
func (a *AnalyzeWeaveReport) IsExcluded() (bool, error) {
return isExcluded(a.analyzer.Exclude)
}
func (a *AnalyzeWeaveReport) Analyze(getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
results, err := analyzeWeaveReport(a.analyzer, findFiles)
if err != nil {
return nil, err
}
for i := range results {
results[i].Strict = a.analyzer.Strict.BoolOrDefaultFalse()
}
return results, nil
}
func analyzeWeaveReport(analyzer *troubleshootv1beta2.WeaveReportAnalyze, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
excludeFiles := []string{}
files, err := findFiles(analyzer.ReportFileGlob, excludeFiles)
if err != nil {
return nil, errors.Wrapf(err, "failed to find weave report files in %q", analyzer.ReportFileGlob)
}
if len(files) == 0 {
return nil, nil
}
reports := map[string]WeaveReport{}
for name, file := range files {
report := WeaveReport{}
if err := json.Unmarshal(file, &report); err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal weave report json from %s", name)
}
reports[report.Router.NickName] = report
}
results := []*AnalyzeResult{}
if result := analyzeWeaveIPAMPools(reports); result != nil {
results = append(results, result)
}
if result := analyzeWeavePendingAllocation(reports); result != nil {
results = append(results, result)
}
if result := analyzeWeaveConnections(reports); result != nil {
results = append(results, result)
}
if len(results) == 0 {
results = append(results, &AnalyzeResult{
Title: "Weave Report",
IsPass: true,
Message: "No issues detected in weave report",
})
}
return results, nil
}
func analyzeWeaveIPAMPools(reports map[string]WeaveReport) *AnalyzeResult {
for _, report := range reports {
if result := analyzeWeaveIPAMPool(&report); result != nil {
return result
}
}
return nil
}
func analyzeWeaveIPAMPool(report *WeaveReport) *AnalyzeResult {
if report.IPAM.RangeNumIPs == 0 {
return nil
}
ipsUsed := float64(report.IPAM.ActiveIPs) / float64(report.IPAM.RangeNumIPs)
if ipsUsed < 0.85 {
return nil
}
return &AnalyzeResult{
Title: "Available Pod IPs",
IsWarn: true,
Message: fmt.Sprintf("%d of %d total available IPs have been assigned", report.IPAM.ActiveIPs, report.IPAM.RangeNumIPs),
}
}
func analyzeWeavePendingAllocation(reports map[string]WeaveReport) *AnalyzeResult {
for _, report := range reports {
if len(report.IPAM.PendingAllocates) > 0 {
return &AnalyzeResult{
Title: "Pending IP Allocation",
IsWarn: true,
Message: "Waiting for IPs to become available",
}
}
}
return nil
}
// Get the peer hostname for logging purposes from a string like: "encrypted fastdp 1a:5b:a9:53:2b:11(areed-aka-kkz0)"
var weaveConnectionInfoPeerRegex = regexp.MustCompile(`\(([^)]+)\)$`)
func parseWeaveConnectionInfoHostname(info string) string {
matches := weaveConnectionInfoPeerRegex.FindStringSubmatch(info)
if len(matches) == 2 {
return matches[1]
}
return ""
}
func analyzeWeaveConnections(reports map[string]WeaveReport) *AnalyzeResult {
for host, report := range reports {
for _, connection := range report.Router.Connections {
// older versions of weave show connection to self as failed
if strings.HasPrefix(connection.Info, "cannot connect to ourself") {
continue
}
peer := parseWeaveConnectionInfoHostname(connection.Info)
if peer == "" {
peer = "peer"
}
if connection.State != "established" {
return &AnalyzeResult{
Title: "Weave Inter-Node Connections",
IsWarn: true,
Message: fmt.Sprintf("Connection from %s to %s is %s", host, peer, connection.State),
}
}
if connection.Attrs.Name != "" && connection.Attrs.Name != "fastdp" {
return &AnalyzeResult{
Title: "Weave Inter-Node Connections",
IsWarn: true,
Message: fmt.Sprintf("Connection from %s to %s protocol is %q, not fastdp", host, peer, connection.Attrs.Name),
}
}
}
}
return nil
}