github.com/oam-dev/kubevela@v1.9.11/pkg/webhook/core.oam.dev/v1beta1/policydefinition/validating_handler.go (about) 1 /* 2 Copyright 2021. The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package policydefinition 18 19 import ( 20 "context" 21 "fmt" 22 "net/http" 23 24 admissionv1 "k8s.io/api/admission/v1" 25 "sigs.k8s.io/controller-runtime/pkg/client" 26 "sigs.k8s.io/controller-runtime/pkg/manager" 27 "sigs.k8s.io/controller-runtime/pkg/runtime/inject" 28 "sigs.k8s.io/controller-runtime/pkg/webhook" 29 "sigs.k8s.io/controller-runtime/pkg/webhook/admission" 30 31 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" 32 "github.com/oam-dev/kubevela/pkg/oam" 33 webhookutils "github.com/oam-dev/kubevela/pkg/webhook/utils" 34 ) 35 36 var policyDefGVR = v1beta1.SchemeGroupVersion.WithResource("policydefinitions") 37 38 // ValidatingHandler handles validation of policy definition 39 type ValidatingHandler struct { 40 // Decoder decodes object 41 Decoder *admission.Decoder 42 Client client.Client 43 } 44 45 var _ inject.Client = &ValidatingHandler{} 46 47 // InjectClient injects the client into the ApplicationValidateHandler 48 func (h *ValidatingHandler) InjectClient(c client.Client) error { 49 if h.Client != nil { 50 return nil 51 } 52 h.Client = c 53 return nil 54 } 55 56 var _ admission.Handler = &ValidatingHandler{} 57 58 // Handle validate component definition 59 func (h *ValidatingHandler) Handle(ctx context.Context, req admission.Request) admission.Response { 60 obj := &v1beta1.PolicyDefinition{} 61 if req.Resource.String() != policyDefGVR.String() { 62 return admission.Errored(http.StatusBadRequest, fmt.Errorf("expect resource to be %s", policyDefGVR)) 63 } 64 65 if req.Operation == admissionv1.Create || req.Operation == admissionv1.Update { 66 err := h.Decoder.Decode(req, obj) 67 if err != nil { 68 return admission.Errored(http.StatusBadRequest, err) 69 } 70 71 // validate cueTemplate 72 if obj.Spec.Schematic != nil && obj.Spec.Schematic.CUE != nil { 73 err = webhookutils.ValidateCueTemplate(obj.Spec.Schematic.CUE.Template) 74 if err != nil { 75 return admission.Denied(err.Error()) 76 } 77 } 78 79 revisionName := obj.GetAnnotations()[oam.AnnotationDefinitionRevisionName] 80 if len(revisionName) != 0 { 81 defRevName := fmt.Sprintf("%s-v%s", obj.Name, revisionName) 82 err = webhookutils.ValidateDefinitionRevision(ctx, h.Client, obj, client.ObjectKey{Namespace: obj.Namespace, Name: defRevName}) 83 if err != nil { 84 return admission.Denied(err.Error()) 85 } 86 } 87 } 88 return admission.ValidationResponse(true, "") 89 } 90 91 var _ admission.DecoderInjector = &ValidatingHandler{} 92 93 // InjectDecoder injects the decoder into the ValidatingHandler 94 func (h *ValidatingHandler) InjectDecoder(d *admission.Decoder) error { 95 h.Decoder = d 96 return nil 97 } 98 99 // RegisterValidatingHandler will register ComponentDefinition validation to webhook 100 func RegisterValidatingHandler(mgr manager.Manager) { 101 server := mgr.GetWebhookServer() 102 server.Register("/validating-core-oam-dev-v1beta1-policydefinitions", &webhook.Admission{Handler: &ValidatingHandler{}}) 103 }