/
check.go
298 lines (248 loc) · 7.11 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
package assertion
import (
"context"
"encoding/json"
goerrors "errors"
"fmt"
"github.com/kyma-project/kyma/tests/function-controller/internal/executor"
"github.com/kyma-project/kyma/tests/function-controller/internal/resources/function"
"github.com/kyma-project/kyma/tests/function-controller/internal/utils"
"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"
)
type HTTPCheck struct {
name string
log *logrus.Entry
endpoint string
expectedMsg string
poll utils.Poller
}
var _ executor.Step = HTTPCheck{}
const (
publisherURL = "http://eventing-event-publisher-proxy.kyma-system/publish"
)
func NewHTTPCheck(log *logrus.Entry, name string, url *url.URL, poller utils.Poller, expectedMsg string) *HTTPCheck {
return &HTTPCheck{
name: name,
log: log.WithField(executor.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 _ executor.Step = &tracingHTTPCheck{}
type tracingHTTPCheck struct {
name string
log *logrus.Entry
endpoint string
poll utils.Poller
}
type tracingResponse struct {
TraceParent string `json:"traceparent"`
TraceID string `json:"x-b3-traceid"`
SpanID string `json:"x-b3-spanid"`
}
func TracingHTTPCheck(log *logrus.Entry, name string, url *url.URL, poller utils.Poller) *tracingHTTPCheck {
return &tracingHTTPCheck{
name: name,
log: log.WithField(executor.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 APIGatewayFunctionCheck(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 %d instead 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
}