forked from knative/eventing
/
requests.go
272 lines (238 loc) · 8.47 KB
/
requests.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package lib
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"time"
cloudevents "github.com/cloudevents/sdk-go/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"knative.dev/pkg/test/monitoring"
"knative.dev/pkg/test/logging"
)
// Port for the recordevents pod REST listener
const RecordEventsPort = 8392
// HTTP path for the GetMinMax REST call
const GetMinMaxPath = "/minmax"
// HTTP path for the GetEntry REST call
const GetEntryPath = "/entry/"
// HTTP path for the TrimThrough REST call
const TrimThroughPath = "/trimthrough/"
// On-wire json rest api format for recordevents GetMinMax calls
// sennt to the recordevents pod.
type MinMaxResponse struct {
MinAvail int
MaxSeen int
}
// Structure to hold information about an event seen by recordevents pod.
type EventInfo struct {
// Set if the cloudevent received by the pod didn't pass validation
ValidationError string
// Event received if the cloudevent received by the pod passed validation
Event *cloudevents.Event
// HTTPHeaders of the connection that delivered the event
HTTPHeaders map[string][]string
}
// Pretty print the event. Meant for debugging. This formats the validation error
// or the full event as appropriate. This does NOT format the headers.
func (ei *EventInfo) String() string {
if ei.Event != nil {
return ei.Event.String()
} else {
return fmt.Sprintf("invalid event \"%s\"", ei.ValidationError)
}
}
// This is mainly used for providing better failure messages
type SearchedInfo struct {
TotalEvent int
LastNEvent []EventInfo
}
// Pretty print the SearchedInfor for error messages
func (s *SearchedInfo) String() string {
return fmt.Sprintf("%d events seen, last N = %s", s.TotalEvent, s.LastNEvent)
}
// Connection state for a REST connection to a pod
type eventGetter struct {
podName string
podNamespace string
podPort int
kubeClientset kubernetes.Interface
logf logging.FormatLogger
host string
port int
forwardPID int
}
// Creates a forwarded port to the specified recordevents pod and waits until
// it can successfully talk to the REST API. Times out after timeoutEvRetry
func newEventGetter(podName string, client *Client, logf logging.FormatLogger) (eventGetterInterface, error) {
egi := &eventGetter{podName: podName, podNamespace: client.Namespace,
kubeClientset: client.Kube.Kube, podPort: RecordEventsPort, logf: logf}
err := egi.forwardPort()
if err != nil {
return nil, err
}
err = egi.waitTillUp()
if err != nil {
return nil, err
}
return egi, nil
}
// Get information about the provided podName. Uses list (rather than get) and
// returns a pod list for compatibility with the monitoring.PortForward
// interface
func (eg *eventGetter) getRunningPodInfo(podName, namespace string) (*v1.PodList, error) {
pods, err := eg.kubeClientset.CoreV1().Pods(namespace).List(
metav1.ListOptions{FieldSelector: fmt.Sprintf("metadata.name=%s", podName)})
if err == nil && len(pods.Items) != 1 {
err = fmt.Errorf("no %s Pod found on the cluster", podName)
} else if pods.Items[0].Status.Phase != corev1.PodRunning {
err = fmt.Errorf("pod %s in state %s, wanted Running", podName,
pods.Items[0].Status.Phase)
}
return pods, err
}
// Try to forward the pod port to a local port somewhere in the range 30000-60000.
// keeps retrying with random ports in that range, timing out after timeoutEvRetry
func (eg *eventGetter) forwardPort() error {
portRand := rand.New(rand.NewSource(time.Now().UnixNano()))
portMin := 30000
portMax := 60000
var internalErr error
wait.PollImmediate(minEvRetryInterval, timeoutEvRetry, func() (bool, error) {
localPort := portMin + portRand.Intn(portMax-portMin)
if err := monitoring.CheckPortAvailability(localPort); err != nil {
internalErr = err
return false, nil
}
pods, err := eg.getRunningPodInfo(eg.podName, eg.podNamespace)
if err != nil {
internalErr = err
return false, nil
}
pid, err := monitoring.PortForward(eg.logf, pods, localPort, eg.podPort, eg.podNamespace)
if err != nil {
internalErr = err
return false, nil
}
internalErr = nil
eg.forwardPID = pid
eg.port = localPort
eg.host = "localhost"
return true, nil
})
if internalErr != nil {
return fmt.Errorf("timeout forwarding port: %v", internalErr)
}
return nil
}
// Return the min available, max seen by the recordevents pod.
// maxRet is the largest event that has ever been seen (whether it's been trimmed
// or not). minRet is the smallest event still available via Get, or 1+maxRet if
// no events are available. maxRet starts at 0 when no events have been seen.
func (eg *eventGetter) getMinMax() (minRet int, maxRet int, errRet error) {
resp, err := http.Get(fmt.Sprintf("http://%s:%d%s", eg.host, eg.port, GetMinMaxPath))
if err != nil {
return -1, -1, fmt.Errorf("http get error: %v", err)
}
defer resp.Body.Close()
bodyContents, err := ioutil.ReadAll(resp.Body)
if err != nil {
return -1, -1, fmt.Errorf("error reading response body %w", err)
}
if resp.StatusCode != http.StatusOK {
return -1, -1, fmt.Errorf("error %d reading GetMinMax response", resp.StatusCode)
}
minMaxResponse := MinMaxResponse{}
err = json.Unmarshal(bodyContents, &minMaxResponse)
if err != nil {
return -1, -1, fmt.Errorf("error unmarshalling response %w", err)
}
if minMaxResponse.MinAvail == 0 || minMaxResponse.MaxSeen == 0 {
return -1, -1, fmt.Errorf("invalid decoded json: %+v", minMaxResponse)
}
return minMaxResponse.MinAvail, minMaxResponse.MaxSeen, nil
}
// Return the event with the provided sequence number. Returns the appropriate
// EventInfo or an error if no such event is known, or the event has already
// been trimmed.
func (eg *eventGetter) getEntry(seqno int) (EventInfo, error) {
resp, err := http.Get(fmt.Sprintf("http://%s:%d%s/%d", eg.host, eg.port, GetEntryPath, seqno))
if err != nil {
return EventInfo{}, fmt.Errorf("http get err %v", err)
}
defer resp.Body.Close()
bodyContents, err := ioutil.ReadAll(resp.Body)
if err != nil {
return EventInfo{}, fmt.Errorf("error reading response body %w", err)
}
if resp.StatusCode != http.StatusOK {
return EventInfo{}, fmt.Errorf("error %d reading GetEntry response", resp.StatusCode)
}
entryResponse := EventInfo{}
err = json.Unmarshal(bodyContents, &entryResponse)
if err != nil {
return EventInfo{}, fmt.Errorf("error unmarshalling response %w", err)
}
if len(entryResponse.ValidationError) == 0 && entryResponse.Event == nil {
return EventInfo{}, fmt.Errorf("invalid decoded json: %+v", entryResponse)
}
return entryResponse, nil
}
// Trim the events up to and including seqno from the recordevents pod.
// Returns an error if a nonsensical seqno is passed in, but does not return
// error for trimming already trimmed regions.
func (eg *eventGetter) trimThrough(seqno int) error {
resp, err := http.Post(fmt.Sprintf("http://%s:%d%s/%d", eg.host, eg.port, TrimThroughPath, seqno), "", nil)
if err != nil {
return fmt.Errorf("http post err %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error reading response body %w", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("error %d reading TrimThrough response: %s", resp.StatusCode, string(body))
}
return nil
}
// Clean up the getter by tearing down the port forward.
func (eg *eventGetter) cleanup() {
pid := eg.forwardPID
eg.forwardPID = 0
if pid != 0 {
monitoring.Cleanup(pid)
}
}
// Wait (up to timeoutEvRetry) for the pod to RestAPI to answer request.
func (eg *eventGetter) waitTillUp() error {
var internalErr error
wait.PollImmediate(minEvRetryInterval, timeoutEvRetry, func() (bool, error) {
_, _, internalErr = eg.getMinMax()
if internalErr != nil {
return false, nil
}
return true, nil
})
if internalErr != nil {
return fmt.Errorf("timeout waiting for recordevents pod to come up: %v", internalErr)
}
return nil
}