1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00
kyverno/pkg/webhooks/policymutation.go

81 lines
2.3 KiB
Go
Raw Normal View History

package webhooks
import (
"encoding/json"
"fmt"
"reflect"
"strings"
kyverno "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
"github.com/kyverno/kyverno/pkg/policymutation"
v1beta1 "k8s.io/api/admission/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
2020-04-10 23:24:54 +05:30
func (ws *WebhookServer) policyMutation(request *v1beta1.AdmissionRequest) *v1beta1.AdmissionResponse {
2020-03-17 11:05:20 -07:00
logger := ws.log.WithValues("action", "policymutation", "uid", request.UID, "kind", request.Kind, "namespace", request.Namespace, "name", request.Name, "operation", request.Operation)
var policy, oldPolicy *kyverno.ClusterPolicy
raw := request.Object.Raw
//TODO: can this happen? wont this be picked by OpenAPI spec schema ?
if err := json.Unmarshal(raw, &policy); err != nil {
2020-08-05 23:53:27 +05:30
logger.Error(err, "failed to unmarshall policy admission request")
return &v1beta1.AdmissionResponse{
Allowed: true,
Result: &metav1.Status{
2019-08-27 23:48:13 -07:00
Message: fmt.Sprintf("failed to default value, check kyverno controller logs for details: %v", err),
},
}
}
if request.Operation == v1beta1.Update {
if err := json.Unmarshal(request.OldObject.Raw, &oldPolicy); err != nil {
logger.Error(err, "failed to unmarshall old policy admission request")
return &v1beta1.AdmissionResponse{
Allowed: true,
Result: &metav1.Status{
Message: fmt.Sprintf("failed to default value, check kyverno controller logs for details: %v", err),
},
}
}
if isStatusUpdate(oldPolicy, policy) {
logger.V(3).Info("skip policy mutation on status update")
return &v1beta1.AdmissionResponse{Allowed: true}
}
}
// Generate JSON Patches for defaults
2020-07-10 18:12:19 +05:30
patches, updateMsgs := policymutation.GenerateJSONPatchesForDefaults(policy, logger)
if patches != nil {
patchType := v1beta1.PatchTypeJSONPatch
return &v1beta1.AdmissionResponse{
Allowed: true,
Result: &metav1.Status{
Message: strings.Join(updateMsgs, "'"),
},
Patch: patches,
PatchType: &patchType,
}
}
return &v1beta1.AdmissionResponse{
Allowed: true,
}
}
func isStatusUpdate(old, new *kyverno.ClusterPolicy) bool {
if !reflect.DeepEqual(old.GetAnnotations(), new.GetAnnotations()) {
return false
}
if !reflect.DeepEqual(old.GetLabels(), new.GetLabels()) {
return false
}
if !reflect.DeepEqual(old.Spec, new.Spec) {
return false
}
return true
}