-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
215 lines (194 loc) · 5.38 KB
/
utils.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
package artifacts
import (
"bytes"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"github.com/fsnotify/fsnotify"
"github.com/getsentry/sentry-go"
)
var analysisEventsLogFmt = "%s,%s,%s,%s,%s,%s,%s,%s,%s,%d\n"
// Returns bearer token that is used to authenticate while
// interacting with the k8s REST API
// Utilized by janus and atlas.
func GetNewBearerToken(tokenFilePath string) (string, error) {
authToken, err := ioutil.ReadFile(tokenFilePath)
if err != nil {
return "", err
}
bearer := "Bearer " + string(authToken)
return bearer, nil
}
// Utility function to retry any routine
func Retry(attempts int, sleep time.Duration, f func() error) (err error) {
for i := 0; ; i++ {
err = f()
if err == nil {
return
}
if i >= (attempts - 1) {
break
}
time.Sleep(sleep)
log.Println("Retrying job after error:", err)
}
sentry.CaptureException(err)
return fmt.Errorf("Failed to trigger the job aftert %d attempts, last error: %s", attempts, err)
}
// Get a HTTP client for interacting with k8s REST API
func GetNewHTTPClient(certFilePath string) (*http.Client, error) {
var httpClient *http.Client
// if auth is true, send request with the certificate
caCert, err := ioutil.ReadFile(certFilePath)
if err != nil {
return httpClient, err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
httpClient = &http.Client{
Timeout: time.Second * 10,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: false,
RootCAs: caCertPool,
MinVersion: tls.VersionTLS12,
MaxVersion: 0,
},
},
}
return httpClient, nil
}
// Fibonacci returns successive Fibonacci numbers starting from 1
func fibonacci() func() int {
a, b := 0, 1
return func() int {
a, b = b, a+b
return a
}
}
// FibonacciNext returns next number in Fibonacci sequence greater than start
func fibonacciNextNum(start int) int {
fib := fibonacci()
num := fib()
for num <= start {
num = fib()
}
return num
}
// Gets the duration interval of retries based on fibonacci series
func GetRetryTimeout(currentTimeout int) (int, time.Duration) {
retryTimeout := fibonacciNextNum(currentTimeout)
durationString := fmt.Sprintf("%vs", retryTimeout)
duration, _ := time.ParseDuration(durationString)
return retryTimeout, duration
}
// Watches the broker config for any "WRITE" events
// Exits with status code 1 after any changes to config
func WatchBrokerConfigForChanges(filePath string, reloadFunc func() error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Println(err)
sentry.CaptureException(err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Println(event)
if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Remove == fsnotify.Remove {
log.Println("Received event:", event)
log.Println("modified file:", event.Name)
if strings.HasSuffix(event.Name, filePath) {
watcher.Close()
if reloadErr := reloadFunc(); reloadErr != nil {
log.Fatalln("Failed to reload janus. Exiting.") // skipcq
}
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("Watcher error:", err)
}
}
}()
err = watcher.Add(filePath)
if err != nil {
log.Println(err)
sentry.CaptureException(err)
}
<-done
}
// Utility to do a rolling restart deployment of a kubernetes pod
func TriggerDeploymentRestart(auth bool, podName, patchData, baseURL, tokenPath string, httpClient *http.Client) error {
// Configuring the request
restartDeploymentRequest, _ := http.NewRequest("PATCH", baseURL+"/apis/apps/v1/namespaces/default/deployments/"+podName, bytes.NewBuffer([]byte(patchData)))
restartDeploymentRequest.Header.Set("Content-Type", "application/strategic-merge-patch+json")
// If auth is true, set Authorization header
if auth {
bearer, err := GetNewBearerToken(tokenPath)
if err != nil {
log.Println(err)
return err
}
restartDeploymentRequest.Header.Add("Authorization", bearer)
}
// If not, try anyways without the Bearer Token in the Header
err := Retry(5, 2*time.Second, func() (err error) {
restartDeploymentResponse, err := httpClient.Do(restartDeploymentRequest)
if err != nil {
log.Println(err)
return err
}
if restartDeploymentResponse.StatusCode != 200 && restartDeploymentResponse.StatusCode != 201 {
responseBody, _ := ioutil.ReadAll(restartDeploymentResponse.Body)
log.Println(string(responseBody))
return errors.New("Unable to create a kubernetes job")
}
defer restartDeploymentResponse.Body.Close()
return nil
})
if err != nil {
log.Print(err.Error())
return err
}
return nil
}
// AnalysisEventsLog represents the struct that contains the fields
// that need to be logged for tracking analysis pipeline's performance.
type AnalysisEventsLog struct {
RunID string
RunSerial string
CheckSequence string
Repository string
Shortcode string
CommitSHA string
IsFullRun string
}
// logAnalysisEventTimestamp logs the timestamp at various analysis stages.
func (a *AnalysisEventsLog) LogAnalysisEventTimestamp(runType, stage string) {
fmt.Printf(analysisEventsLogFmt,
runType,
a.RunID,
a.RunSerial,
a.CheckSequence,
a.Shortcode,
a.Repository,
a.CommitSHA,
a.IsFullRun,
stage,
time.Now().Unix(),
)
}