forked from feedhenry/fh-system-dump-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.go
223 lines (191 loc) · 7.1 KB
/
analysis.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
package main
import (
"encoding/json"
"os"
"path/filepath"
)
type Info struct {
Name string
Namespace string
Kind string
Count int
Message string
}
type Result struct {
CheckName string `json:"checkName" yaml:"checkName"`
Status int `json:"status" yaml:"status"`
StatusMessage string `json:"statusMessage" yaml:"statusMessage"`
Info []Info `json:"info" yaml:"info"`
Events []Event `json:"events" yaml:"events"`
}
type Event struct {
Kind string `json:"kind"`
InvolvedObject struct {
Namespace string `json:"namespace"`
Name string `json:"name"`
} `json:"involvedObject"`
Reason string `json:"reason"`
Message string `json:"message"`
Count int `json:"count"`
Type string `json:"type"`
}
type Events struct {
Items []Event `json:"items"`
}
type DeploymentConfigs struct {
Items []struct {
Kind string `json:"kind"`
Metadata struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
} `json:"metadata"`
Spec struct {
Replicas int `json:"replicas"`
} `json:"spec"`
} `json:"items"`
}
type ContainerWaiting struct {
Reason string `json:"reason"`
Message string `json:"message"`
}
type Pods struct {
Items []struct {
Metadata struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
} `json:"metadata"`
Status struct {
ContainerStatuses []struct {
Name string `json:"name"`
State struct {
Waiting *ContainerWaiting `json:"waiting,omitempty"`
} `json:"state"`
} `json:"containerStatuses"`
} `json:"status"`
} `json:"items"`
}
type CheckTask func(DumpedJSONResourceFactory) (Result, error)
func GetAnalysisTasks(tasks chan<- Task, basepath string, projects []string, results chan<- CheckResults) {
// Platform-wide analysis goes here
// project specific analysis in here
for _, p := range projects {
JSONResourceFactory := getDumpedJSONResourceFactory([]string{basepath, "projects", p})
tasks <- CheckProjectTask(p, results, JSONResourceFactory)
}
}
// CheckTasks is a task factory for tasks that diagnose system conditions.
func CheckProjectTask(project string, results chan<- CheckResults, JSONResourceFactory DumpedJSONResourceFactory) Task {
return checkProjectTask(func() []CheckTask {
return []CheckTask{CheckEventLogForErrors, CheckDeployConfigsReplicasNotZero, CheckForWaitingPods}
}, JSONResourceFactory, project, results)
}
// A getProjectCheckFactory generates tasks to diagnose system conditions.
type getProjectCheckFactory func() []CheckTask
type CheckResults struct {
Scope string `json:"scope"`
Results []Result
}
// checkTasks executes all the CheckTasks returned from the supplied
// checkFactory against the specified project. The results of the checks are
// combined into a single JSON object and returned
func checkProjectTask(checkFactory getProjectCheckFactory, JSONResourceFactory DumpedJSONResourceFactory, project string, results chan<- CheckResults) Task {
return func() error {
result := CheckResults{Scope: project, Results: []Result{}}
checks := checkFactory()
var errors errorList
for _, check := range checks {
res, err := check(JSONResourceFactory)
if err != nil {
errors = append(errors, err)
}
result.Results = append(result.Results, res)
}
results <- result
if len(errors) > 0 {
return errors
}
return nil
}
}
// Takes an array of strings describing the path to a JSON file
// which will be parsed and loaded into the supplied interface
type DumpedJSONResourceFactory func([]string, interface{}) error
// Returns a factory which will load resource from a given basepath. The factory parses the file
// contents as JSON and loads it into the provided dest interface
func getDumpedJSONResourceFactory(basepath []string) DumpedJSONResourceFactory {
return func(path []string, dest interface{}) error {
file := filepath.Join(append(basepath, path...)...)
contents, err := os.Open(file)
if err != nil {
return err
}
decoder := json.NewDecoder(contents)
if err := decoder.Decode(&dest); err != nil {
return err
}
return nil
}
}
// CheckForWaitingPods checks all pods for any containers in waiting status
func CheckForWaitingPods(JSONResourceFactory DumpedJSONResourceFactory) (Result, error) {
result := Result{Status: 0, StatusMessage: "this issue was not detected", CheckName: "check pods for 'waiting' containers", Info: []Info{}, Events: []Event{}}
pods := Pods{}
if err := JSONResourceFactory([]string{"definitions", "pods.json"}, &pods); err != nil {
result.Status = 2
result.StatusMessage = "Error executing task: " + err.Error()
return result, err
}
for _, pod := range pods.Items {
for _, container := range pod.Status.ContainerStatuses {
if container.State.Waiting != nil {
result.Status = 1
result.StatusMessage = "Waiting containers have been detected"
msg := "container " + container.Name + " in pod " + pod.Metadata.Name + " is in waiting state"
info := Info{Name: container.Name, Count: 1, Namespace: pod.Metadata.Namespace, Kind: "container", Message: msg}
result.Info = append(result.Info, info)
}
}
}
return result, nil
}
// CheckEventLogForErrors checks all events in the supplied project and if any
// are not type 'Normal' (i.e. Warning or Error), it will add them to the returned results.
func CheckEventLogForErrors(JSONResourceFactory DumpedJSONResourceFactory) (Result, error) {
result := Result{Status: 0, StatusMessage: "this issue was not detected", CheckName: "check eventlog for any errors", Info: []Info{}, Events: []Event{}}
events := Events{}
if err := JSONResourceFactory([]string{"definitions", "events.json"}, &events); err != nil {
result.Status = 2
result.StatusMessage = "Error executing task"
return result, err
}
for _, event := range events.Items {
if event.Type != "Normal" {
result.Status = 1
result.StatusMessage = "Errors detected in event log"
result.Events = append(result.Events, event)
}
}
return result, nil
}
// CheckDeployConfigsReplicasNotZero checks all deployment configs in the supplied
// JSON Resource Factory, and if any are found with a replica of 0, it will add a
// note about it to the returned result
func CheckDeployConfigsReplicasNotZero(ResourceFactory DumpedJSONResourceFactory) (Result, error) {
result := Result{Status: 0, StatusMessage: "this issue was not detected", CheckName: "check deployconfig replicas not 0", Info: []Info{}, Events: []Event{}}
deploymentConfigs := DeploymentConfigs{}
err := ResourceFactory([]string{"definitions", "deploymentconfigs.json"}, &deploymentConfigs)
if err != nil {
result.Status = 2
result.StatusMessage = "Error executing task"
return result, err
}
for _, deploymentConfig := range deploymentConfigs.Items {
if deploymentConfig.Spec.Replicas == 0 {
info := Info{Name: deploymentConfig.Metadata.Name, Namespace: deploymentConfig.Metadata.Namespace, Kind: deploymentConfig.Kind, Count: 1, Message: "the replica parameter is set to 0, this should be greater than 0"}
result.Status = 1
result.StatusMessage = "one or more deployConfig replicas are set to 0"
result.Info = append(result.Info, info)
}
}
return result, nil
}