/
rollout_webhook.go
131 lines (117 loc) · 3.36 KB
/
rollout_webhook.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
/*
Copyright 2021 The KubeVela 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
http://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 rollout
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
"github.com/oam-dev/kubevela/apis/standard.oam.dev/v1alpha1"
)
// issue an http call to the an end ponit
func makeHTTPRequest(ctx context.Context, webhookEndPoint, method string, payload interface{}) ([]byte, int, error) {
payloadBin, err := json.Marshal(payload)
if err != nil {
return nil, http.StatusInternalServerError, err
}
hook, err := url.Parse(webhookEndPoint)
if err != nil {
return nil, http.StatusInternalServerError, err
}
req, err := http.NewRequestWithContext(context.Background(), method, hook.String(), bytes.NewBuffer(payloadBin))
if err != nil {
return nil, http.StatusInternalServerError, err
}
req.Header.Set("Content-Type", "application/json")
// issue request with retry
var r *http.Response
var body []byte
err = retry.OnError(retry.DefaultBackoff,
func(error) bool {
// not sure what not to retry on
return true
}, func() error {
var requestErr error
r, requestErr = http.DefaultClient.Do(req.WithContext(ctx))
defer func() {
if r != nil {
_ = r.Body.Close()
}
}()
if requestErr != nil {
return requestErr
}
body, requestErr = io.ReadAll(r.Body)
if requestErr != nil {
return requestErr
}
if r.StatusCode >= http.StatusInternalServerError {
requestErr = fmt.Errorf("internal server error, status code = %d", r.StatusCode)
}
return requestErr
})
// failed even with retry
if err != nil {
if r != nil {
return nil, r.StatusCode, err
}
return nil, -1, err
}
return body, r.StatusCode, nil
}
// callWebhook does a HTTP POST to an external service and
// returns an error if the response status code is non-2xx
func callWebhook(ctx context.Context, resource klog.KMetadata, phase string, rw v1alpha1.RolloutWebhook) error {
payload := v1alpha1.RolloutWebhookPayload{
Name: resource.GetName(),
Namespace: resource.GetNamespace(),
Phase: phase,
}
if rw.Metadata != nil {
payload.Metadata = *rw.Metadata
}
// make the http request
if len(rw.Method) == 0 {
rw.Method = http.MethodPost
}
_, status, err := makeHTTPRequest(ctx, rw.URL, rw.Method, payload)
if err != nil {
return err
}
if len(rw.ExpectedStatus) == 0 {
if status > http.StatusAccepted {
err := fmt.Errorf("we fail the webhook request based on status, http status = %d", status)
return err
}
return nil
}
// check if the returned status is expected
accepted := false
for _, es := range rw.ExpectedStatus {
if es == status {
accepted = true
break
}
}
if !accepted {
err := fmt.Errorf("http request to the webhook not accepeted, http status = %d", status)
klog.ErrorS(err, "The status is not expected", "expected status", rw.ExpectedStatus)
return err
}
return nil
}