-
Notifications
You must be signed in to change notification settings - Fork 15
/
helpers.go
179 lines (150 loc) · 5.17 KB
/
helpers.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
/*
Copyright 2021 Adobe. All rights reserved.
This file is licensed to you 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 REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
package helpers
import (
"encoding/json"
"fmt"
"io"
"math/rand"
"net/http"
"os"
"reflect"
"runtime"
"strconv"
"time"
"github.com/labstack/echo/v4"
cr "github.com/adobe/cluster-registry/pkg/api/registry/v1"
"github.com/adobe/cluster-registry/test/slt/metrics"
)
// ClusterList ...
type ClusterList struct {
Items []*cr.ClusterSpec `json:"items"`
ItemsCount int `json:"itemsCount"`
}
// GetRandomTime returns a random time interval as a string
func GetRandomTime(maxTime string, logger echo.Logger) string {
interval, err := time.ParseDuration(maxTime)
if err != nil {
logger.Fatalf("error parsing time interval: %s", err.Error())
}
maxSeconds := int(interval.Seconds())
radomSeconds := rand.Intn(maxSeconds)
return (time.Second * time.Duration(radomSeconds)).String()
}
// RunFuncInLoop runs a function in a loop.
// f is the function to run,
// timeInterval is the time it waits to run again,
// offSetStart is the time to wait before the loop starts.
func RunFuncInLoop(f func(interface{}), config interface{}, timeInterval string, offSetStart string, logger echo.Logger) {
interval, err := time.ParseDuration(timeInterval)
if err != nil {
logger.Fatalf("error parsing time interval to run %s function in loop: %s", GetFunctionName(f), err)
}
if offSetStart != "" {
offSet, err := time.ParseDuration(offSetStart)
if err != nil {
logger.Fatalf("error parsing time interval to run %s function in loop: %s", GetFunctionName(f), err)
}
time.Sleep(offSet)
}
for {
f(config)
time.Sleep(interval)
}
}
// GetEnv gets env variable with an fallback value, if fallback is empty then env variable
// is mandatory and if missing exit the program
func GetEnv(key, fallback string, logger echo.Logger) string {
if value, ok := os.LookupEnv(key); ok {
logger.Debugf("got env variable %s:%s", key, value)
return value
}
if fallback == "" {
logger.Fatalf("missing environment variable %s", key)
}
logger.Debugf("could not find env variable %s, set to fallback value %s", key, fallback)
return fallback
}
// GetFunctionName returns the name ofa function
func GetFunctionName(i interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}
func reqGet(endpoint, bearer string) (*[]byte, int, error) {
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
return nil, 0, fmt.Errorf("cannot build http request: %s", err.Error())
}
req.Header.Add("Authorization", bearer)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, 0, fmt.Errorf("cannot make http request: %s", err.Error())
}
if resp.StatusCode != 200 {
message, err := io.ReadAll(resp.Body)
if err != nil {
return nil, resp.StatusCode, fmt.Errorf("status code %d: could "+
"not read response body: %s", resp.StatusCode, err.Error())
}
return nil, resp.StatusCode, fmt.Errorf("cannot get cluster: Status "+
"code %d, body:%s", resp.StatusCode, string(message))
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, resp.StatusCode, fmt.Errorf("cannot read response body: %s", err.Error())
}
return &body, resp.StatusCode, nil
}
// GetCluster gets cluster from CR
func GetCluster(url, clusterName, jwtToken string) (*cr.ClusterSpec, error) {
var cluster cr.ClusterSpec
endpoint := fmt.Sprintf("%s/api/v1/clusters/%s", url, clusterName)
bearer := "Bearer " + jwtToken
start := time.Now()
body, respCode, err := reqGet(endpoint, bearer)
timeTook := float64(time.Since(start).Seconds())
metrics.EgressReqDuration.WithLabelValues(
"/api/v1/clusters/[cluster]",
"GET",
strconv.Itoa(respCode)).Observe(timeTook)
if err != nil {
if respCode == 404 {
return nil, fmt.Errorf("cluster %s was not found: %s", clusterName, err.Error())
}
return nil, err
}
err = json.Unmarshal(*body, &cluster)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %s", err.Error())
}
return &cluster, nil
}
// GetClusters gets cluster from CR
func GetClusters(url, perPageLimit, pageNr, jwtToken string) (*ClusterList, error) {
var clusters ClusterList
endpoint := fmt.Sprintf("%s/api/v1/clusters?offset=%s&limit=%s", url, pageNr, perPageLimit)
bearer := "Bearer " + jwtToken
start := time.Now()
body, respCode, err := reqGet(endpoint, bearer)
timeTook := float64(time.Since(start).Seconds())
metrics.EgressReqDuration.WithLabelValues(
"/api/v1/clusters",
"GET",
strconv.Itoa(respCode)).Observe(timeTook)
if err != nil {
return nil, err
}
err = json.Unmarshal(*body, &clusters)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %s", err.Error())
}
return &clusters, nil
}