2021-03-11 22:06:04 +02:00
|
|
|
package common
|
|
|
|
|
2022-01-04 17:36:33 -08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-05-17 13:12:43 +02:00
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2022-01-04 17:36:33 -08:00
|
|
|
"github.com/kyverno/kyverno/pkg/utils"
|
|
|
|
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
|
|
|
|
)
|
2021-09-27 14:28:55 -07:00
|
|
|
|
2021-10-11 21:44:43 +02:00
|
|
|
func GetRawKeyIfWrappedWithAttributes(str string) string {
|
|
|
|
if len(str) < 2 {
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
if str[0] == '(' && str[len(str)-1] == ')' {
|
|
|
|
return str[1 : len(str)-1]
|
|
|
|
} else if (str[0] == '$' || str[0] == '^' || str[0] == '+' || str[0] == '=') && (str[1] == '(' && str[len(str)-1] == ')') {
|
|
|
|
return str[2 : len(str)-1]
|
|
|
|
} else {
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
}
|
2022-01-04 17:36:33 -08:00
|
|
|
|
|
|
|
func TransformConditions(original apiextensions.JSON) (interface{}, error) {
|
|
|
|
// conditions are currently in the form of []interface{}
|
|
|
|
oldConditions, err := utils.ApiextensionsJsonToKyvernoConditions(original)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch typedValue := oldConditions.(type) {
|
2022-05-17 13:12:43 +02:00
|
|
|
case kyvernov1.AnyAllConditions:
|
2022-04-06 21:04:08 +02:00
|
|
|
return *typedValue.DeepCopy(), nil
|
2022-05-17 13:12:43 +02:00
|
|
|
case []kyvernov1.Condition: // backwards compatibility
|
|
|
|
var copies []kyvernov1.Condition
|
2022-04-06 21:04:08 +02:00
|
|
|
for _, condition := range typedValue {
|
|
|
|
copies = append(copies, *condition.DeepCopy())
|
|
|
|
}
|
|
|
|
return copies, nil
|
2022-01-04 17:36:33 -08:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("invalid preconditions")
|
|
|
|
}
|