...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package webhook
16
17 import (
18 "context"
19 "net/http"
20
21 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/resourceoverrides"
22
23 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
24 "k8s.io/klog/v2"
25 "sigs.k8s.io/controller-runtime/pkg/client"
26 "sigs.k8s.io/controller-runtime/pkg/runtime/inject"
27 "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
28 )
29
30 type resourceValidatorHandler struct {
31 client client.Client
32 }
33
34
35 var _ inject.Client = &resourceValidatorHandler{}
36
37 func NewResourceValidatorHandler() *resourceValidatorHandler {
38 return &resourceValidatorHandler{}
39 }
40
41
42 func (a *resourceValidatorHandler) InjectClient(c client.Client) error {
43 a.client = c
44 return nil
45 }
46
47 func (a *resourceValidatorHandler) Handle(ctx context.Context, req admission.Request) admission.Response {
48 deserializer := codecs.UniversalDeserializer()
49 obj := &unstructured.Unstructured{}
50 if _, _, err := deserializer.Decode(req.AdmissionRequest.Object.Raw, nil, obj); err != nil {
51 klog.Error(err)
52 return admission.Errored(http.StatusBadRequest, err)
53 }
54 if err := resourceoverrides.Handler.ConfigValidate(obj); err != nil {
55 return admission.Errored(http.StatusForbidden, err)
56 }
57 return admission.ValidationResponse(true, "admission controller passed")
58 }
59
View as plain text