1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00
kyverno/pkg/utils/admission/metadata.go
Vishal Choudhary 3142af64a0
feat: add global context entry validation webhook (#9619)
* feat: add global context entry validation webhook

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* fix: use `k8s.io/apimachinery/pkg/util/json` instead of `encoding/json`

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* fix: lint

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

---------

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
2024-02-02 18:04:50 +01:00

30 lines
858 B
Go

package admission
import (
admissionv1 "k8s.io/api/admission/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/json"
)
func UnmarshalPartialObjectMetadata(raw []byte) (*metav1.PartialObjectMetadata, error) {
var object *metav1.PartialObjectMetadata
if err := json.Unmarshal(raw, &object); err != nil {
return nil, err
}
return object, nil
}
func GetPartialObjectMetadatas(request admissionv1.AdmissionRequest) (*metav1.PartialObjectMetadata, *metav1.PartialObjectMetadata, error) {
object, err := UnmarshalPartialObjectMetadata(request.Object.Raw)
if err != nil {
return nil, nil, err
}
if request.Operation != admissionv1.Update {
return object, nil, nil
}
oldObject, err := UnmarshalPartialObjectMetadata(request.OldObject.Raw)
if err != nil {
return nil, nil, err
}
return object, oldObject, nil
}