2022-11-16 18:41:33 +05:30
|
|
|
package admission
|
2022-11-14 15:13:32 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2024-06-26 10:48:32 +02:00
|
|
|
kyvernov2 "github.com/kyverno/kyverno/api/kyverno/v2"
|
2022-11-14 15:13:32 +05:30
|
|
|
admissionv1 "k8s.io/api/admission/v1"
|
2024-02-02 22:34:50 +05:30
|
|
|
"k8s.io/apimachinery/pkg/util/json"
|
2022-11-14 15:13:32 +05:30
|
|
|
)
|
|
|
|
|
2024-06-26 10:48:32 +02:00
|
|
|
func UnmarshalCleanupPolicy(kind string, raw []byte) (kyvernov2.CleanupPolicyInterface, error) {
|
2022-11-14 15:13:32 +05:30
|
|
|
if kind == "CleanupPolicy" {
|
2024-06-26 10:48:32 +02:00
|
|
|
var policy *kyvernov2.CleanupPolicy
|
2022-11-14 15:13:32 +05:30
|
|
|
if err := json.Unmarshal(raw, &policy); err != nil {
|
2022-11-16 18:41:33 +05:30
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return policy, nil
|
|
|
|
} else if kind == "ClusterCleanupPolicy" {
|
2024-06-26 10:48:32 +02:00
|
|
|
var policy *kyvernov2.ClusterCleanupPolicy
|
2022-11-16 18:41:33 +05:30
|
|
|
if err := json.Unmarshal(raw, &policy); err != nil {
|
|
|
|
return nil, err
|
2022-11-14 15:13:32 +05:30
|
|
|
}
|
|
|
|
return policy, nil
|
|
|
|
}
|
2022-11-16 18:41:33 +05:30
|
|
|
return nil, fmt.Errorf("admission request does not contain a cleanuppolicy")
|
2022-11-14 15:13:32 +05:30
|
|
|
}
|
|
|
|
|
2024-06-26 10:48:32 +02:00
|
|
|
func GetCleanupPolicies(request admissionv1.AdmissionRequest) (kyvernov2.CleanupPolicyInterface, kyvernov2.CleanupPolicyInterface, error) {
|
|
|
|
var emptypolicy kyvernov2.CleanupPolicyInterface
|
2022-11-14 15:13:32 +05:30
|
|
|
policy, err := UnmarshalCleanupPolicy(request.Kind.Kind, request.Object.Raw)
|
|
|
|
if err != nil {
|
|
|
|
return policy, emptypolicy, err
|
|
|
|
}
|
|
|
|
if request.Operation == admissionv1.Update {
|
|
|
|
oldPolicy, err := UnmarshalCleanupPolicy(request.Kind.Kind, request.OldObject.Raw)
|
|
|
|
return policy, oldPolicy, err
|
|
|
|
}
|
|
|
|
return policy, emptypolicy, nil
|
|
|
|
}
|