/
validate.go
110 lines (92 loc) · 2.73 KB
/
validate.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
package validate
import (
"context"
"github.com/kyma-project/kyma/components/binding/internal/webhook"
"github.com/kyma-project/kyma/components/binding/pkg/apis/v1alpha1"
admissionTypes "k8s.io/api/admission/v1beta1"
"net/http"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
log "github.com/sirupsen/logrus"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
var _ admission.Handler = &ValidationHandler{}
var _ admission.DecoderInjector = &ValidationHandler{}
type Validator interface {
Validate(*v1alpha1.TargetKind) *webhook.Error
}
type ValidationHandler struct {
decoder *admission.Decoder
CreateValidators []Validator
UpdateValidators []Validator
log log.FieldLogger
}
func NewValidationHandler(log log.FieldLogger) *ValidationHandler {
return &ValidationHandler{
CreateValidators: []Validator{&StaticCreate{}},
UpdateValidators: []Validator{},
log: log,
}
}
func (h *ValidationHandler) Handle(ctx context.Context, req admission.Request) admission.Response {
h.log.Infof("start handling TargetKind: %s", req.UID)
targetKind := &v1alpha1.TargetKind{}
if err := webhook.MatchKinds(targetKind, req.Kind); err != nil {
h.log.Errorf("kind does not match: %s", err)
return admission.Errored(http.StatusBadRequest, err)
}
if err := h.decoder.Decode(req, targetKind); err != nil {
h.log.Errorf("cannot decode TargetKind: %s", err)
return admission.Errored(http.StatusBadRequest, err)
}
var err *webhook.Error
switch req.Operation {
case admissionTypes.Create:
for _, v := range h.CreateValidators {
err = v.Validate(targetKind)
if err != nil {
break
}
}
case admissionTypes.Update:
for _, v := range h.UpdateValidators {
err = v.Validate(targetKind)
if err != nil {
break
}
}
default:
h.log.Infof("TargetKind validation wehbook does not support action %q", req.Operation)
return admission.Allowed("action not taken")
}
if err != nil {
switch err.Code() {
case http.StatusForbidden:
return admission.Denied(err.Error())
default:
return admission.Errored(err.Code(), err)
}
}
h.log.Infof("Completed successfully validation operation: %s for %s: %q", req.Operation, req.Kind.Kind, req.Name)
return admission.Allowed("TargetKind validation successful")
}
func (h *ValidationHandler) InjectDecoder(d *admission.Decoder) error {
h.decoder = d
return nil
}
// InjectClient injects the client into the handlers
func (h *ValidationHandler) InjectClient(c client.Client) error {
for _, v := range h.CreateValidators {
_, err := inject.ClientInto(c, v)
if err != nil {
return err
}
}
for _, v := range h.UpdateValidators {
_, err := inject.ClientInto(c, v)
if err != nil {
return err
}
}
return nil
}