1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 07:57:07 +00:00
kyverno/pkg/engine/common/utils.go
Charles-Edouard Brétéché 5aaf2d8770
chore: make kyverno api import aliases consistent (#3939)
Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
2022-05-17 13:12:43 +02:00

41 lines
1.1 KiB
Go

package common
import (
"fmt"
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
"github.com/kyverno/kyverno/pkg/utils"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
)
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
}
}
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) {
case kyvernov1.AnyAllConditions:
return *typedValue.DeepCopy(), nil
case []kyvernov1.Condition: // backwards compatibility
var copies []kyvernov1.Condition
for _, condition := range typedValue {
copies = append(copies, *condition.DeepCopy())
}
return copies, nil
}
return nil, fmt.Errorf("invalid preconditions")
}