2019-08-27 16:44:10 -07:00
|
|
|
package webhooks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2019-11-13 13:41:08 -08:00
|
|
|
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
|
2020-07-10 18:12:19 +05:30
|
|
|
"github.com/nirmata/kyverno/pkg/policymutation"
|
2019-08-27 16:44:10 -07:00
|
|
|
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)
|
2019-09-03 14:51:51 -07:00
|
|
|
var policy *kyverno.ClusterPolicy
|
2019-08-27 16:44:10 -07:00
|
|
|
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")
|
2019-08-27 16:44:10 -07:00
|
|
|
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),
|
2019-08-27 16:44:10 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Generate JSON Patches for defaults
|
2020-07-10 18:12:19 +05:30
|
|
|
patches, updateMsgs := policymutation.GenerateJSONPatchesForDefaults(policy, logger)
|
2019-08-27 16:44:10 -07:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|