1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-15 17:51:20 +00:00
kyverno/pkg/utils/admission/policy.go
Charles-Edouard Brétéché c9bbf38191
refactor: remove more admission request pointers (#6774)
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
2023-04-04 13:11:18 +08:00

42 lines
1.2 KiB
Go

package admission
import (
"encoding/json"
"fmt"
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
admissionv1 "k8s.io/api/admission/v1"
)
func UnmarshalPolicy(kind string, raw []byte) (kyvernov1.PolicyInterface, error) {
if kind == "ClusterPolicy" {
var policy *kyvernov1.ClusterPolicy
if err := json.Unmarshal(raw, &policy); err != nil {
return nil, err
}
return policy, nil
} else if kind == "Policy" {
var policy *kyvernov1.Policy
if err := json.Unmarshal(raw, &policy); err != nil {
return nil, err
}
return policy, nil
}
return nil, fmt.Errorf("admission request does not contain a policy")
}
func GetPolicy(request admissionv1.AdmissionRequest) (kyvernov1.PolicyInterface, error) {
return UnmarshalPolicy(request.Kind.Kind, request.Object.Raw)
}
func GetPolicies(request admissionv1.AdmissionRequest) (kyvernov1.PolicyInterface, kyvernov1.PolicyInterface, error) {
policy, err := UnmarshalPolicy(request.Kind.Kind, request.Object.Raw)
if err != nil {
return policy, nil, err
}
if request.Operation == admissionv1.Update {
oldPolicy, err := UnmarshalPolicy(request.Kind.Kind, request.OldObject.Raw)
return policy, oldPolicy, err
}
return policy, nil, nil
}