1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 07:57:07 +00:00
kyverno/pkg/webhooks/policyvalidation.go

46 lines
1.4 KiB
Go
Raw Normal View History

2019-07-15 16:07:56 -07:00
package webhooks
import (
"encoding/json"
"fmt"
2020-03-06 03:00:18 +05:30
policyvalidate "github.com/nirmata/kyverno/pkg/policy"
2019-07-15 16:07:56 -07:00
"github.com/golang/glog"
2019-11-13 13:41:08 -08:00
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
2019-07-15 16:07:56 -07:00
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
2019-08-08 13:09:40 -07:00
admissionResp := &v1beta1.AdmissionResponse{
Allowed: true,
}
2019-08-21 16:42:42 -07:00
//TODO: can this happen? wont this be picked by OpenAPI spec schema ?
2019-08-21 16:42:42 -07:00
raw := request.Object.Raw
2019-08-08 13:09:40 -07:00
if err := json.Unmarshal(raw, &policy); err != nil {
glog.Errorf("Failed to unmarshal policy admission request, err %v\n", err)
2019-08-21 12:38:15 -07:00
return &v1beta1.AdmissionResponse{Allowed: false,
Result: &metav1.Status{
Message: fmt.Sprintf("Failed to unmarshal policy admission request err %v", err),
}}
2019-08-08 13:09:40 -07:00
}
if err := policyvalidate.Validate(*policy); err != nil {
admissionResp = &v1beta1.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{
Message: err.Error(),
},
}
}
2019-09-04 13:43:12 -07:00
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()
2019-09-04 13:43:12 -07:00
}
return admissionResp
2019-07-15 16:07:56 -07:00
}