This repository has been archived by the owner on Apr 25, 2024. It is now read-only.
/
validation.go
104 lines (89 loc) 路 3.01 KB
/
validation.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
/*
Copyright 2020 DevSpace Technologies Inc.
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 webhooks
import (
"context"
"fmt"
"github.com/kiosk-sh/kiosk/pkg/apis/config/validation"
"github.com/go-logr/logr"
configv1alpha1 "github.com/kiosk-sh/kiosk/pkg/apis/config/v1alpha1"
"github.com/kiosk-sh/kiosk/pkg/util/encoding"
"k8s.io/api/admission/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
type Validator struct {
Log logr.Logger
StrictDecoder encoding.Decoder
NormalDecoder encoding.Decoder
}
func (v *Validator) Handle(ctx context.Context, req admission.Request) admission.Response {
var (
obj runtime.Object
oldObj runtime.Object
err error
kind = schema.GroupVersionKind{
Group: req.Kind.Group,
Version: req.Kind.Version,
Kind: req.Kind.Kind,
}
)
// We allow other api groups
if kind.GroupVersion().String() != configv1alpha1.SchemeGroupVersion.String() {
return admission.Allowed("")
}
if req.Operation == v1.Create {
obj, err = v.StrictDecoder.Decode(req.Object.Raw)
if err != nil {
return admission.Denied(err.Error())
}
} else if req.Operation == v1.Update {
obj, err = v.StrictDecoder.Decode(req.Object.Raw)
if err != nil {
return admission.Denied(err.Error())
}
oldObj, err = v.NormalDecoder.Decode(req.OldObject.Raw)
if err != nil {
return admission.Errored(1, err)
}
} else {
return admission.Errored(1, fmt.Errorf("operation %s not supported", string(req.Operation)))
}
var errs field.ErrorList
switch kind.Kind {
case "Account":
if req.Operation == v1.Create {
errs = validation.ValidateAccount(obj.(*configv1alpha1.Account))
} else {
errs = validation.ValidateAccountUpdate(obj.(*configv1alpha1.Account), oldObj.(*configv1alpha1.Account))
}
case "AccountQuota":
if req.Operation == v1.Create {
errs = validation.ValidateAccountQuota(obj.(*configv1alpha1.AccountQuota))
} else {
errs = validation.ValidateAccountQuotaUpdate(obj.(*configv1alpha1.AccountQuota), oldObj.(*configv1alpha1.AccountQuota))
}
case "TemplateInstance":
if req.Operation == v1.Create {
errs = validation.ValidateTemplateInstance(obj.(*configv1alpha1.TemplateInstance))
} else {
errs = validation.ValidateTemplateInstanceUpdate(obj.(*configv1alpha1.TemplateInstance), oldObj.(*configv1alpha1.TemplateInstance))
}
}
if len(errs) > 0 {
return admission.Denied(errs.ToAggregate().Error())
}
return admission.Allowed("")
}