2021-06-21 16:56:16 +05:30
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2021-06-21 20:12:14 +05:30
|
|
|
"bytes"
|
2021-06-21 16:56:16 +05:30
|
|
|
"strings"
|
|
|
|
"time"
|
2021-06-21 20:12:14 +05:30
|
|
|
|
|
|
|
"github.com/kyverno/kyverno/test/e2e"
|
2021-06-21 16:56:16 +05:30
|
|
|
)
|
|
|
|
|
2021-06-21 20:12:14 +05:30
|
|
|
func CallMetrics() (string, error) {
|
|
|
|
requestObj := e2e.APIRequest{
|
|
|
|
URL: "http://localhost:8000/metrics",
|
|
|
|
Type: "GET",
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := e2e.CallAPI(requestObj)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
2021-06-21 21:50:49 +05:30
|
|
|
_, err = buf.ReadFrom(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-06-21 20:12:14 +05:30
|
|
|
newStr := buf.String()
|
|
|
|
return newStr, nil
|
|
|
|
}
|
|
|
|
|
2021-06-22 00:29:04 +05:30
|
|
|
func ProcessMetrics(newStr, e2ePolicyName string, e2eTime time.Time) (bool, error) {
|
2021-06-21 16:56:16 +05:30
|
|
|
var action, policyName string
|
|
|
|
var timeInTimeFormat time.Time
|
|
|
|
var err error
|
|
|
|
splitByNewLine := strings.Split(newStr, "\n")
|
|
|
|
for _, lineSplitedByNewLine := range splitByNewLine {
|
|
|
|
if strings.HasPrefix(lineSplitedByNewLine, "kyverno_policy_changes_info{") {
|
|
|
|
splitByComma := strings.Split(lineSplitedByNewLine, ",")
|
|
|
|
for _, lineSplitedByComma := range splitByComma {
|
|
|
|
if strings.HasPrefix(lineSplitedByComma, "policy_change_type=") {
|
|
|
|
splitByQuote := strings.Split(lineSplitedByComma, "\"")
|
|
|
|
action = splitByQuote[1]
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(lineSplitedByComma, "policy_name=") {
|
|
|
|
splitByQuote := strings.Split(lineSplitedByComma, "\"")
|
|
|
|
policyName = splitByQuote[1]
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(lineSplitedByComma, "timestamp=") {
|
|
|
|
splitByQuote := strings.Split(lineSplitedByComma, "\"")
|
|
|
|
layout := "2006-01-02 15:04:05 -0700 MST"
|
|
|
|
timeInTimeFormat, err = time.Parse(layout, splitByQuote[1])
|
|
|
|
if err != nil {
|
2021-06-22 00:29:04 +05:30
|
|
|
return false, err
|
2021-06-21 16:56:16 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if policyName == e2ePolicyName {
|
|
|
|
diff := e2eTime.Sub(timeInTimeFormat)
|
2021-06-21 18:14:47 +05:30
|
|
|
if diff < time.Second {
|
2021-06-21 16:56:16 +05:30
|
|
|
if action == "created" {
|
2021-06-22 00:29:04 +05:30
|
|
|
return true, nil
|
2021-06-21 16:56:16 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-22 00:29:04 +05:30
|
|
|
return false, nil
|
2021-06-21 16:56:16 +05:30
|
|
|
}
|