mirror of
https://github.com/kyverno/kyverno.git
synced 2025-01-20 18:52:16 +00:00
44 lines
1.4 KiB
Go
44 lines
1.4 KiB
Go
package webhooks
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/golang/glog"
|
|
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
|
|
policyvalidate "github.com/nirmata/kyverno/pkg/policy"
|
|
v1beta1 "k8s.io/api/admission/v1beta1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
)
|
|
|
|
//HandlePolicyValidation performs the validation check on policy resource
|
|
func (ws *WebhookServer) handlePolicyValidation(request *v1beta1.AdmissionRequest) *v1beta1.AdmissionResponse {
|
|
var policy *kyverno.ClusterPolicy
|
|
admissionResp := &v1beta1.AdmissionResponse{
|
|
Allowed: true,
|
|
}
|
|
|
|
//TODO: can this happen? wont this be picked by OpenAPI spec schema ?
|
|
raw := request.Object.Raw
|
|
if err := json.Unmarshal(raw, &policy); err != nil {
|
|
glog.Errorf("Failed to unmarshal policy admission request, err %v\n", err)
|
|
return &v1beta1.AdmissionResponse{Allowed: false,
|
|
Result: &metav1.Status{
|
|
Message: fmt.Sprintf("Failed to unmarshal policy admission request err %v", err),
|
|
}}
|
|
}
|
|
if err := policyvalidate.Validate(*policy); err != nil {
|
|
admissionResp = &v1beta1.AdmissionResponse{
|
|
Allowed: false,
|
|
Result: &metav1.Status{
|
|
Message: err.Error(),
|
|
},
|
|
}
|
|
}
|
|
if admissionResp.Allowed {
|
|
// if the policy contains mutating & validation rules and it config does not exist we create one
|
|
// queue the request
|
|
ws.resourceWebhookWatcher.RegisterResourceWebhook()
|
|
}
|
|
return admissionResp
|
|
}
|