2021-06-21 16:56:16 +05:30
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2021-06-21 20:12:14 +05:30
|
|
|
"bytes"
|
2021-07-09 18:01:46 -07:00
|
|
|
"fmt"
|
2022-02-23 15:52:08 +00:00
|
|
|
"os"
|
2021-06-21 16:56:16 +05:30
|
|
|
"strings"
|
|
|
|
"time"
|
2021-06-21 20:12:14 +05:30
|
|
|
|
2022-02-23 15:52:08 +00:00
|
|
|
"github.com/blang/semver/v4"
|
2021-06-21 20:12:14 +05:30
|
|
|
"github.com/kyverno/kyverno/test/e2e"
|
2021-06-21 16:56:16 +05:30
|
|
|
)
|
|
|
|
|
2022-02-23 15:52:08 +00:00
|
|
|
const defaultTestK8sVersion = "1.21.0"
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-02-23 15:52:08 +00:00
|
|
|
func GetKubernetesVersion() semver.Version {
|
|
|
|
ver, err := semver.Parse(os.Getenv("K8S_VERSION"))
|
|
|
|
if err != nil {
|
|
|
|
return semver.MustParse(defaultTestK8sVersion)
|
|
|
|
}
|
|
|
|
return ver
|
|
|
|
}
|
|
|
|
|
2021-06-29 11:52:24 +05:30
|
|
|
// ProcessMetrics checks the metrics log and identify if the policy is added in cache or not
|
2021-07-09 18:01:46 -07:00
|
|
|
func ProcessMetrics(newStr, e2ePolicyName string) error {
|
2021-06-21 16:56:16 +05:30
|
|
|
splitByNewLine := strings.Split(newStr, "\n")
|
2021-07-09 18:01:46 -07:00
|
|
|
for _, lineSplitByNewLine := range splitByNewLine {
|
2021-10-05 00:15:09 -07:00
|
|
|
// kyverno_policy_rule_info_total{policy_background_mode=\"false\",policy_name=\"gen-cluster-policy\",policy_namespace=\"-\",policy_type=\"cluster\",policy_validation_mode=\"audit\",rule_name=\"gen-cluster-role\",rule_type=\"generate\",status_ready="false"} 1
|
2021-07-09 18:01:46 -07:00
|
|
|
if !strings.HasPrefix(lineSplitByNewLine, "kyverno_policy_rule_info_total{") {
|
|
|
|
continue
|
|
|
|
}
|
2021-06-21 16:56:16 +05:30
|
|
|
|
2021-07-09 18:01:46 -07:00
|
|
|
if !strings.HasSuffix(lineSplitByNewLine, "} 1") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
splitByComma := strings.Split(lineSplitByNewLine, ",")
|
|
|
|
for _, lineSplitByComma := range splitByComma {
|
|
|
|
if strings.HasPrefix(lineSplitByComma, "policy_name=") {
|
|
|
|
splitByQuote := strings.Split(lineSplitByComma, "\"")
|
|
|
|
policyName := splitByQuote[1]
|
2021-10-05 00:15:09 -07:00
|
|
|
if policyName != e2ePolicyName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(lineSplitByComma, "status_ready=") {
|
|
|
|
splitByQuote := strings.Split(lineSplitByComma, "\"")
|
|
|
|
status := splitByQuote[1]
|
|
|
|
if status == "true" {
|
2021-07-09 18:01:46 -07:00
|
|
|
return nil
|
2021-06-21 16:56:16 +05:30
|
|
|
}
|
|
|
|
}
|
2021-10-05 00:15:09 -07:00
|
|
|
|
2021-06-21 16:56:16 +05:30
|
|
|
}
|
|
|
|
}
|
2021-07-09 18:01:46 -07:00
|
|
|
|
|
|
|
return fmt.Errorf("policy %s not found in metrics %s", e2ePolicyName, newStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PolicyCreated(policyName string) error {
|
|
|
|
return e2e.GetWithRetry(1*time.Second, 60, checkPolicyCreated(policyName))
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkPolicyCreated(policyName string) func() error {
|
|
|
|
return func() error {
|
|
|
|
var metricsString string
|
|
|
|
metricsString, err := CallMetrics()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get metrics: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ProcessMetrics(metricsString, policyName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("policy not created: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-04-25 18:19:39 +03:00
|
|
|
// Wait to make sure that the Policy is ready.
|
|
|
|
time.Sleep(2 * time.Second)
|
2021-07-09 18:01:46 -07:00
|
|
|
return nil
|
|
|
|
}
|
2021-06-21 16:56:16 +05:30
|
|
|
}
|