forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.go
346 lines (286 loc) · 8.18 KB
/
check.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package teststep
import (
"context"
"encoding/json"
goerrors "errors"
"fmt"
"io"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
"net/http"
"net/url"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/retry"
"github.com/kyma-project/kyma/tests/function-controller/pkg/function"
"github.com/kyma-project/kyma/tests/function-controller/pkg/poller"
"github.com/kyma-project/kyma/tests/function-controller/pkg/step"
)
type HTTPCheck struct {
name string
log *logrus.Entry
endpoint string
expectedMsg string
poll poller.Poller
}
var _ step.Step = HTTPCheck{}
const (
publisherURL = "http://eventing-event-publisher-proxy.kyma-system/publish"
)
func NewHTTPCheck(log *logrus.Entry, name string, url *url.URL, poller poller.Poller, expectedMsg string) *HTTPCheck {
return &HTTPCheck{
name: name,
log: log.WithField(step.LogStepKey, name),
endpoint: url.String(),
expectedMsg: expectedMsg,
poll: poller.WithLogger(log),
}
}
func (h HTTPCheck) Name() string {
return h.name
}
func (h HTTPCheck) Run() error {
// backoff is needed because even tough the deployment may be ready
// the language specific server may not start yet
// there may also be some problems with istio sidecars etc
backoff := wait.Backoff{
Steps: 10,
Duration: 500 * time.Millisecond,
Factor: 2.0,
Jitter: 0.1,
}
return retry.OnError(backoff, func(err error) bool {
return true
}, func() error {
err := errors.Wrap(h.poll.PollForAnswer(h.endpoint, "", h.expectedMsg), "while checking connection to function")
if err != nil {
h.log.Warn(err)
}
return err
})
}
func (h HTTPCheck) Cleanup() error {
return nil
}
func (h HTTPCheck) OnError() error {
return nil
}
var _ step.Step = DefaultedFunctionCheck{}
type DefaultedFunctionCheck struct {
name string
fn *function.Function
}
func NewDefaultedFunctionCheck(name string, fn *function.Function) step.Step {
return &DefaultedFunctionCheck{
name: name,
fn: fn,
}
}
func (d DefaultedFunctionCheck) Name() string {
return d.name
}
func (d DefaultedFunctionCheck) Run() error {
fn, err := d.fn.Get()
if err != nil {
return err
}
if fn == nil {
return errors.New("function can't be nil")
}
spec := fn.Spec
if spec.Replicas == nil {
return errors.New("replicas equal nil")
} else if spec.ResourceConfiguration.Function.Resources.Requests.Memory().IsZero() || spec.ResourceConfiguration.Function.Resources.Requests.Cpu().IsZero() {
return errors.New("requests equal zero")
} else if spec.ResourceConfiguration.Function.Resources.Limits.Memory().IsZero() || spec.ResourceConfiguration.Function.Resources.Limits.Cpu().IsZero() {
return errors.New("limits equal zero")
}
return nil
}
func (d DefaultedFunctionCheck) Cleanup() error {
return nil
}
func (d DefaultedFunctionCheck) OnError() error {
return nil
}
var _ step.Step = &TracingHTTPCheck{}
type TracingHTTPCheck struct {
name string
log *logrus.Entry
endpoint string
poll poller.Poller
}
type tracingResponse struct {
TraceParent string `json:"traceparent"`
TraceID string `json:"x-b3-traceid"`
SpanID string `json:"x-b3-spanid"`
}
func NewTracingHTTPCheck(log *logrus.Entry, name string, url *url.URL, poller poller.Poller) *TracingHTTPCheck {
return &TracingHTTPCheck{
name: name,
log: log.WithField(step.LogStepKey, name),
endpoint: url.String(),
poll: poller.WithLogger(log),
}
}
func (t TracingHTTPCheck) Name() string {
return t.name
}
func (t TracingHTTPCheck) Run() error {
req, err := http.NewRequest(http.MethodGet, t.endpoint, nil)
if err != nil {
return err
}
req.Header.Add("X-B3-Sampled", "1")
resp, err := t.doRetrievableHttpCall(req, 5)
if err != nil {
return errors.Wrap(err, "while doing http call")
}
out, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, "while reading response body")
}
trResponse := tracingResponse{}
err = json.Unmarshal(out, &trResponse)
if err != nil {
return errors.Wrapf(err, "while unmarshalling response to json")
}
err = t.assertTracingResponse(trResponse)
if err != nil {
return errors.Wrapf(err, "Got following headers: %s", out)
}
t.log.Info("headers are okay")
return nil
}
func (t TracingHTTPCheck) doRetrievableHttpCall(req *http.Request, retries int) (*http.Response, error) {
client := &http.Client{Timeout: 5 * time.Second}
var finalResp *http.Response = nil
var backoff = wait.Backoff{
Steps: retries,
Duration: 2 * time.Second,
Factor: 1.0,
}
err := retry.OnError(backoff, func(err error) bool {
t.log.Warnf("Got error: %s", err.Error())
return true
}, func() error {
resp, err := client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return errors.Errorf(" expected status code: %d, got: %d", http.StatusOK, resp.StatusCode)
}
finalResp = resp
return nil
})
return finalResp, errors.Wrap(err, "while trying to call function")
}
func (t TracingHTTPCheck) Cleanup() error {
return nil
}
func (t TracingHTTPCheck) OnError() error {
return nil
}
func (t TracingHTTPCheck) assertTracingResponse(response tracingResponse) error {
if response.TraceID == "" {
return errors.New("No trace ID")
}
if response.TraceParent == "" {
return errors.New("No TraceParent")
}
if response.SpanID == "" {
return errors.New("No span ID")
}
return nil
}
const (
resourceLabel = "serverless.kyma-project.io/resource"
functionNameLabel = "serverless.kyma-project.io/function-name"
manageByLabel = "serverless.kyma-project.io/managed-by"
uuidLabel = "serverless.kyma-project.io/uuid"
)
type APIGatewayFunctionCheck struct {
name string
fn *function.Function
client *v1.CoreV1Client
namespace string
runtime string
}
func NewAPIGatewayFunctionCheck(name string, fn *function.Function, coreV1 *v1.CoreV1Client, ns string, rt string) *APIGatewayFunctionCheck {
return &APIGatewayFunctionCheck{
name: name,
fn: fn,
client: coreV1,
namespace: ns,
runtime: rt,
}
}
func (d APIGatewayFunctionCheck) Name() string {
return d.name
}
func (d APIGatewayFunctionCheck) Run() error {
svc, err := d.client.Services(d.namespace).Get(context.Background(), d.name, metav1.GetOptions{})
if err != nil {
return errors.Wrap(err, "while trying to get service")
}
pod, err := d.client.Pods(d.namespace).List(context.Background(), metav1.ListOptions{LabelSelector: fmt.Sprintf("%s=deployment,%s=%s", resourceLabel, functionNameLabel, d.name)})
if err != nil {
return errors.Wrap(err, "while trying to get pod")
}
err = checkIfRequiredLabelsExists(svc.Spec.Selector, true)
if err != nil {
return errors.Wrap(err, " while checking the service labels")
}
err = checkIfRequiredLabelsExists(pod.Items[0].ObjectMeta.Labels, false)
if err != nil {
return errors.Wrap(err, " while checking the pod labels")
}
err = checkIfContractIsFulfilled(pod.Items[0], *svc)
if err != nil {
return errors.Wrap(err, " while checking labels")
}
if len(svc.Spec.Selector) != 0 {
return errors.New("The labels are not matching")
}
return nil
}
func (d APIGatewayFunctionCheck) Cleanup() error {
return nil
}
func (d APIGatewayFunctionCheck) OnError() error {
return nil
}
func checkIfContractIsFulfilled(pod corev1.Pod, service corev1.Service) error {
var errJoined error
for k, v := range pod.Labels {
if val, exists := service.Spec.Selector[k]; exists {
if val == v {
delete(service.Spec.Selector, k)
} else {
err := errors.Errorf("Expected %s but got %s", v, val)
errJoined = goerrors.Join(err)
}
}
}
return errJoined
}
func checkIfRequiredLabelsExists(labels map[string]string, isService bool) error {
requiredLabels := []string{resourceLabel, functionNameLabel, manageByLabel, uuidLabel}
if isService {
if len(labels) != 4 {
return errors.New(fmt.Sprintf("Service has got %s istead of 4 labels", len(labels)))
}
}
var errJoined error
for _, label := range requiredLabels {
if _, exists := labels[label]; !exists {
err := errors.New(fmt.Sprintf("Label %s is missing", label))
errJoined = goerrors.Join(err)
}
}
return errJoined
}