/
jobrunner.go
229 lines (196 loc) · 6.49 KB
/
jobrunner.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
package probe
import (
"strings"
"github.com/mattfenwick/collections/pkg/json"
"github.com/mattfenwick/cyclonus/pkg/generator"
"github.com/mattfenwick/cyclonus/pkg/kube"
"github.com/mattfenwick/cyclonus/pkg/matcher"
"github.com/mattfenwick/cyclonus/pkg/worker"
"github.com/sirupsen/logrus"
)
type Runner struct {
JobRunner JobRunner
JobBuilder *JobBuilder
}
func NewSimulatedRunner(policies *matcher.Policy, jobBuilder *JobBuilder) *Runner {
return &Runner{JobRunner: &SimulatedJobRunner{Policies: policies}, JobBuilder: jobBuilder}
}
func NewKubeRunner(kubernetes kube.IKubernetes, workers int, jobBuilder *JobBuilder) *Runner {
return &Runner{JobRunner: &KubeJobRunner{Kubernetes: kubernetes, Workers: workers}, JobBuilder: jobBuilder}
}
func NewKubeBatchRunner(kubernetes kube.IKubernetes, workers int, jobBuilder *JobBuilder) *Runner {
return &Runner{JobRunner: NewKubeBatchJobRunner(kubernetes, workers), JobBuilder: jobBuilder}
}
func (p *Runner) RunProbeForConfig(probeConfig *generator.ProbeConfig, resources *Resources) *Table {
return NewTableFromJobResults(resources, p.runProbe(p.JobBuilder.GetJobsForProbeConfig(resources, probeConfig)))
}
func (p *Runner) runProbe(jobs *Jobs) []*JobResult {
resultSlice := p.JobRunner.RunJobs(jobs.Valid)
invalidPP := ConnectivityInvalidPortProtocol
unknown := ConnectivityUnknown
for _, j := range jobs.BadPortProtocol {
resultSlice = append(resultSlice, &JobResult{
Job: j,
Ingress: &invalidPP,
Egress: &unknown,
Combined: ConnectivityInvalidPortProtocol,
})
}
invalidNamedPort := ConnectivityInvalidNamedPort
for _, j := range jobs.BadNamedPort {
resultSlice = append(resultSlice, &JobResult{
Job: j,
Ingress: &invalidNamedPort,
Egress: &unknown,
Combined: ConnectivityInvalidNamedPort,
})
}
return resultSlice
}
type JobRunner interface {
RunJobs(job []*Job) []*JobResult
}
type SimulatedJobRunner struct {
Policies *matcher.Policy
}
func (s *SimulatedJobRunner) RunJobs(jobs []*Job) []*JobResult {
results := make([]*JobResult, len(jobs))
for i, job := range jobs {
results[i] = s.RunJob(job)
}
return results
}
func (s *SimulatedJobRunner) RunJob(job *Job) *JobResult {
allowed := s.Policies.IsTrafficAllowed(job.Traffic())
// TODO could also keep the whole `allowed` struct somewhere
logrus.Tracef("to %s\n%s\n", json.MustMarshalToString(job), allowed.Table())
var combined, ingress, egress = ConnectivityBlocked, ConnectivityBlocked, ConnectivityBlocked
if allowed.Ingress.IsAllowed() {
ingress = ConnectivityAllowed
}
if allowed.Egress.IsAllowed() {
egress = ConnectivityAllowed
}
if allowed.IsAllowed() {
combined = ConnectivityAllowed
}
return &JobResult{Job: job, Ingress: &ingress, Egress: &egress, Combined: combined}
}
type KubeJobRunner struct {
Kubernetes kube.IKubernetes
Workers int
}
func (k *KubeJobRunner) RunJobs(jobs []*Job) []*JobResult {
size := len(jobs)
jobsChan := make(chan *Job, size)
resultsChan := make(chan *JobResult, size)
for i := 0; i < k.Workers; i++ {
go k.worker(jobsChan, resultsChan)
}
for _, job := range jobs {
jobsChan <- job
}
close(jobsChan)
var resultSlice []*JobResult
for i := 0; i < size; i++ {
result := <-resultsChan
resultSlice = append(resultSlice, result)
}
return resultSlice
}
// probeWorker continues polling a pod connectivity status, until the incoming "jobs" channel is closed, and writes results back out to the "results" channel.
// it only writes pass/fail status to a channel and has no failure side effects, this is by design since we do not want to fail inside a goroutine.
func (k *KubeJobRunner) worker(jobs <-chan *Job, results chan<- *JobResult) {
for job := range jobs {
connectivity, _ := probeConnectivity(k.Kubernetes, job)
results <- &JobResult{
Job: job,
Combined: connectivity,
}
}
}
func probeConnectivity(k8s kube.IKubernetes, job *Job) (Connectivity, string) {
commandDebugString := strings.Join(job.KubeExecCommand(), " ")
stdout, stderr, commandErr, err := k8s.ExecuteRemoteCommand(job.FromNamespace, job.FromPod, job.FromContainer, job.ClientCommand())
logrus.Debugf("stdout, stderr from %s: \n%s\n%s", commandDebugString, stdout, stderr)
if err != nil {
logrus.Errorf("unable to set up command %s: %+v", commandDebugString, err)
return ConnectivityCheckFailed, commandDebugString
}
if commandErr != nil {
logrus.Debugf("unable to run command %s: %+v", commandDebugString, commandErr)
return ConnectivityBlocked, commandDebugString
}
return ConnectivityAllowed, commandDebugString
}
type KubeBatchJobRunner struct {
Client *worker.Client
Workers int
}
func NewKubeBatchJobRunner(k8s kube.IKubernetes, workers int) *KubeBatchJobRunner {
return &KubeBatchJobRunner{Client: &worker.Client{Kubernetes: k8s}, Workers: workers}
}
func (k *KubeBatchJobRunner) RunJobs(jobs []*Job) []*JobResult {
jobMap := map[string]*Job{}
// 1. batch up jobs
batches := map[string]*worker.Batch{}
for _, job := range jobs {
ns, pod := job.FromNamespace, job.FromPod
if _, ok := batches[job.FromKey]; !ok {
batches[job.FromKey] = &worker.Batch{Namespace: ns, Pod: pod, Container: job.FromContainer}
}
batch := batches[job.FromKey]
batch.Requests = append(batch.Requests, &worker.Request{
Key: job.Key(),
Protocol: job.Protocol,
Host: job.ToHost,
Port: job.ResolvedPort,
})
jobMap[job.Key()] = job
}
// 2. send them out and get the results
size := len(jobs)
batchChan := make(chan *worker.Batch, size)
resultsChan := make(chan *JobResult, size)
for i := 0; i < k.Workers; i++ {
go k.worker(jobMap, batchChan, resultsChan)
}
for _, b := range batches {
batchChan <- b
}
close(batchChan)
var jobResults []*JobResult
for i := 0; i < size; i++ {
result := <-resultsChan
jobResults = append(jobResults, result)
}
return jobResults
}
func (k *KubeBatchJobRunner) worker(jobMap map[string]*Job, batches <-chan *worker.Batch, jobResults chan<- *JobResult) {
for b := range batches {
results, err := k.Client.Batch(b)
if err != nil {
logrus.Errorf("unable to issue batch request: %+v", err)
for _, r := range b.Requests {
jobResults <- &JobResult{
Job: jobMap[r.Key],
Combined: ConnectivityCheckFailed,
}
}
} else {
for _, r := range results {
var c Connectivity
if r.IsSuccess() {
c = ConnectivityAllowed
} else {
logrus.Debugf("request to %s failed: %s", r.Request.Key, r.Error)
c = ConnectivityBlocked
}
jobResults <- &JobResult{
Job: jobMap[r.Request.Key],
Combined: c,
}
}
}
}
}