1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-15 17:51:20 +00:00
kyverno/pkg/engine/common/utils.go
Charles-Edouard Brétéché c8275b7c00
refactor: make response type (RuleType) typed (#3556)
* refactor: move common utils

Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>

* refactor: make response type (RuleType) typed

Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>

* fix: merge

Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>

Co-authored-by: shuting <shuting@nirmata.com>
Co-authored-by: Vyankatesh Kudtarkar <vyankateshkd@gmail.com>
2022-04-06 19:04:08 +00:00

41 lines
1.1 KiB
Go

package common
import (
"fmt"
kyverno "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 kyverno.AnyAllConditions:
return *typedValue.DeepCopy(), nil
case []kyverno.Condition: // backwards compatibility
var copies []kyverno.Condition
for _, condition := range typedValue {
copies = append(copies, *condition.DeepCopy())
}
return copies, nil
}
return nil, fmt.Errorf("invalid preconditions")
}