-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathwebhook.go
81 lines (70 loc) · 2.17 KB
/
webhook.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
// Copyright 2021 Antrea 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 v1alpha2
import (
"fmt"
"reflect"
"k8s.io/apimachinery/pkg/runtime"
)
// WebhookImpl implements webhook validator of a resource.
type WebhookImpl interface {
Default(in *ExternalEntity)
ValidateCreate(in *ExternalEntity) error
ValidateUpdate(in *ExternalEntity, old runtime.Object) error
ValidateDelete(in *ExternalEntity) error
}
var (
externalEntityWebhook WebhookImpl
)
// RegisterWebhook registers webhook implementation of a resource.
func RegisterWebhook(in runtime.Object, webhook WebhookImpl) error {
switch in.(type) {
case *ExternalEntity:
if externalEntityWebhook != nil {
return fmt.Errorf("externalEntityWebhook already registered")
}
externalEntityWebhook = webhook
default:
return fmt.Errorf("unknown type %s to register webhook", reflect.TypeOf(in).Elem().Name())
}
return nil
}
// Default implements webhook Defaulter.
func (in *ExternalEntity) Default() {
if externalEntityWebhook != nil {
externalEntityWebhook.Default(in)
}
return
}
// ValidateCreate implements webhook Validator.
func (in *ExternalEntity) ValidateCreate() error {
if externalEntityWebhook != nil {
return externalEntityWebhook.ValidateCreate(in)
}
return nil
}
// ValidateUpdate implements webhook Validator.
func (in *ExternalEntity) ValidateUpdate(old runtime.Object) error {
if externalEntityWebhook != nil {
return externalEntityWebhook.ValidateUpdate(in, old)
}
return nil
}
// ValidateDelete implements webhook Validator.
func (in *ExternalEntity) ValidateDelete() error {
if externalEntityWebhook != nil {
return externalEntityWebhook.ValidateDelete(in)
}
return nil
}