This repository has been archived by the owner on May 6, 2022. It is now read-only.
/
handler.go
146 lines (121 loc) · 4.18 KB
/
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
/*
Copyright 2019 The Kubernetes 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 validation
import (
"context"
"net/http"
sc "github.com/kubernetes-sigs/service-catalog/pkg/apis/servicecatalog/v1beta1"
"github.com/kubernetes-sigs/service-catalog/pkg/webhookutil"
admissionTypes "k8s.io/api/admission/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
// Validator is used to implement new validation logic
type Validator interface {
Validate(context.Context, admission.Request, *sc.ServicePlan, *webhookutil.TracedLogger) *webhookutil.WebhookError
}
// SpecValidationHandler handles ServiceInstance validation
type SpecValidationHandler struct {
decoder *admission.Decoder
CreateValidators []Validator
UpdateValidators []Validator
}
var _ admission.Handler = &SpecValidationHandler{}
var _ admission.DecoderInjector = &SpecValidationHandler{}
var _ inject.Client = &SpecValidationHandler{}
// NewSpecValidationHandler creates new SpecValidationHandler and initializes validators list
func NewSpecValidationHandler() *SpecValidationHandler {
return &SpecValidationHandler{
CreateValidators: []Validator{&StaticCreate{}},
UpdateValidators: []Validator{&StaticUpdate{}},
}
}
// Handle handles admission requests.
func (h *SpecValidationHandler) Handle(ctx context.Context, req admission.Request) admission.Response {
traced := webhookutil.NewTracedLogger(req.UID)
traced.Infof("Start handling validation operation: %s for %s: %q", req.Operation, req.Kind.Kind, req.Name)
servicePlan := &sc.ServicePlan{}
if err := webhookutil.MatchKinds(servicePlan, req.Kind); err != nil {
traced.Errorf("Error matching kinds: %v", err)
return admission.Errored(http.StatusBadRequest, err)
}
if err := h.decoder.Decode(req, servicePlan); err != nil {
traced.Errorf("Could not decode request object: %v", err)
return admission.Errored(http.StatusBadRequest, err)
}
traced.Infof("start validation process for %s: %s/%s", servicePlan.Kind, servicePlan.Namespace, servicePlan.Name)
var err *webhookutil.WebhookError
switch req.Operation {
case admissionTypes.Create:
for _, v := range h.CreateValidators {
err = v.Validate(ctx, req, servicePlan, traced)
if err != nil {
break
}
}
case admissionTypes.Update:
for _, v := range h.UpdateValidators {
err = v.Validate(ctx, req, servicePlan, traced)
if err != nil {
break
}
}
default:
traced.Infof("ServicePlan 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)
}
}
traced.Infof("Completed successfully validation operation: %s for %s: %q", req.Operation, req.Kind.Kind, req.Name)
return admission.Allowed("ServiceInstance validation successful")
}
// InjectDecoder injects the decoder into the handlers
func (h *SpecValidationHandler) InjectDecoder(d *admission.Decoder) error {
h.decoder = d
for _, v := range h.CreateValidators {
_, err := admission.InjectDecoderInto(d, v)
if err != nil {
return err
}
}
for _, v := range h.UpdateValidators {
_, err := admission.InjectDecoderInto(d, v)
if err != nil {
return err
}
}
return nil
}
// InjectClient injects the client into the handlers
func (h *SpecValidationHandler) 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
}