-
Notifications
You must be signed in to change notification settings - Fork 890
/
validating_handler.go
156 lines (131 loc) · 5.32 KB
/
validating_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Copyright 2021 The KubeVela Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package traitdefinition
import (
"context"
"fmt"
"net/http"
"github.com/pkg/errors"
admissionv1 "k8s.io/api/admission/v1"
"k8s.io/klog"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
"github.com/oam-dev/kubevela/pkg/appfile"
controller "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev"
"github.com/oam-dev/kubevela/pkg/oam"
"github.com/oam-dev/kubevela/pkg/oam/discoverymapper"
webhookutils "github.com/oam-dev/kubevela/pkg/webhook/utils"
)
const (
errValidateDefRef = "error occurs when validating definition reference"
failInfoDefRefOmitted = "if definition reference is omitted, patch or output with GVK is required"
)
var traitDefGVR = v1beta1.SchemeGroupVersion.WithResource("traitdefinitions")
// ValidatingHandler handles validation of trait definition
type ValidatingHandler struct {
Client client.Client
Mapper discoverymapper.DiscoveryMapper
// Decoder decodes object
Decoder *admission.Decoder
// Validators validate objects
Validators []TraitDefValidator
}
// TraitDefValidator validate trait definition
type TraitDefValidator interface {
Validate(context.Context, v1beta1.TraitDefinition) error
}
// TraitDefValidatorFn implements TraitDefValidator
type TraitDefValidatorFn func(context.Context, v1beta1.TraitDefinition) error
// Validate implements TraitDefValidator method
func (fn TraitDefValidatorFn) Validate(ctx context.Context, td v1beta1.TraitDefinition) error {
return fn(ctx, td)
}
var _ admission.Handler = &ValidatingHandler{}
// Handle validate trait definition
func (h *ValidatingHandler) Handle(ctx context.Context, req admission.Request) admission.Response {
obj := &v1beta1.TraitDefinition{}
if req.Resource.String() != traitDefGVR.String() {
return admission.Errored(http.StatusBadRequest, fmt.Errorf("expect resource to be %s", traitDefGVR))
}
if req.Operation == admissionv1.Create || req.Operation == admissionv1.Update {
err := h.Decoder.Decode(req, obj)
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
klog.Info("validating ", " name: ", obj.Name, " operation: ", string(req.Operation))
for _, validator := range h.Validators {
if err := validator.Validate(ctx, *obj); err != nil {
klog.Info("validation failed ", " name: ", obj.Name, " errMsgi: ", err.Error())
return admission.Denied(err.Error())
}
}
revisionName := obj.GetAnnotations()[oam.AnnotationDefinitionRevisionName]
if len(revisionName) != 0 {
defRevName := fmt.Sprintf("%s-v%s", obj.Name, revisionName)
err = webhookutils.ValidateDefinitionRevision(ctx, h.Client, obj, client.ObjectKey{Namespace: obj.Namespace, Name: defRevName})
if err != nil {
return admission.Denied(err.Error())
}
}
klog.Info("validation passed ", " name: ", obj.Name, " operation: ", string(req.Operation))
}
return admission.ValidationResponse(true, "")
}
var _ inject.Client = &ValidatingHandler{}
// InjectClient injects the client into the ValidatingHandler
func (h *ValidatingHandler) InjectClient(c client.Client) error {
h.Client = c
return nil
}
var _ admission.DecoderInjector = &ValidatingHandler{}
// InjectDecoder injects the decoder into the ValidatingHandler
func (h *ValidatingHandler) InjectDecoder(d *admission.Decoder) error {
h.Decoder = d
return nil
}
// RegisterValidatingHandler will register TraitDefinition validation to webhook
func RegisterValidatingHandler(mgr manager.Manager, args controller.Args) {
server := mgr.GetWebhookServer()
server.Register("/validating-core-oam-dev-v1alpha2-traitdefinitions", &webhook.Admission{Handler: &ValidatingHandler{
Mapper: args.DiscoveryMapper,
Validators: []TraitDefValidator{
TraitDefValidatorFn(ValidateDefinitionReference),
// add more validators here
},
}})
}
// ValidateDefinitionReference validates whether the trait definition is valid if
// its `.spec.reference` field is unset.
// It's valid if
// it has at least one output, and all outputs must have GVK
// or it has no output but has a patch
// or it has a patch and outputs, and all outputs must have GVK
// TODO(roywang) currently we only validate whether it contains CUE template.
// Further validation, e.g., output with GVK, valid patch, etc, remains to be done.
func ValidateDefinitionReference(_ context.Context, td v1beta1.TraitDefinition) error {
if len(td.Spec.Reference.Name) > 0 {
return nil
}
cap, err := appfile.ConvertTemplateJSON2Object(td.Name, td.Spec.Extension, td.Spec.Schematic)
if err != nil {
return errors.WithMessage(err, errValidateDefRef)
}
if cap.CueTemplate == "" {
return errors.New(failInfoDefRefOmitted)
}
return nil
}