-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (91 loc) · 3.77 KB
/
main.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 main
import (
"context"
"reflect"
"k8s.io/apimachinery/pkg/runtime/schema"
"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"
"knative.dev/pkg/injection/sharedmain"
"knative.dev/pkg/signals"
"knative.dev/pkg/webhook"
"knative.dev/pkg/webhook/certificates"
"knative.dev/pkg/webhook/resourcesemantics"
"knative.dev/pkg/webhook/resourcesemantics/defaulting"
"knative.dev/pkg/webhook/resourcesemantics/validation"
"github.com/zeiss/typhoon/pkg/apis/common/v1alpha1"
extensionsv1alpha1 "github.com/zeiss/typhoon/pkg/apis/extensions/v1alpha1"
flowv1alpha1 "github.com/zeiss/typhoon/pkg/apis/flow/v1alpha1"
routingv1alpha1 "github.com/zeiss/typhoon/pkg/apis/routing/v1alpha1"
sourcesv1alpha1 "github.com/zeiss/typhoon/pkg/apis/sources/v1alpha1"
targetsv1alpha1 "github.com/zeiss/typhoon/pkg/apis/targets/v1alpha1"
)
var (
validationTypes = map[schema.GroupVersionKind]resourcesemantics.GenericCRD{}
defaultingTypes = map[schema.GroupVersionKind]resourcesemantics.GenericCRD{
sourcesv1alpha1.SchemeGroupVersion.WithKind("CloudEventsSource"): &sourcesv1alpha1.CloudEventsSource{},
routingv1alpha1.SchemeGroupVersion.WithKind("Filter"): &routingv1alpha1.Filter{},
flowv1alpha1.SchemeGroupVersion.WithKind("XSLTTransformation"): &flowv1alpha1.XSLTTransformation{},
}
)
// NewDefaultingAdmissionController returns defaulting webhook controller implementation.
func NewDefaultingAdmissionController(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
return defaulting.NewAdmissionController(ctx,
// Name of the resource webhook.
"defaulting.webhook.typhoon.zeiss.com",
// The path on which to serve the webhook.
"/defaulting",
// The resources to default.
defaultingTypes,
// A function that infuses the context passed to Validate/SetDefaults with custom metadata.
func(ctx context.Context) context.Context {
return ctx
},
// Whether to disallow unknown fields.
true,
)
}
// NewValidationAdmissionController returns validation webhook controller implementation.
func NewValidationAdmissionController(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
return validation.NewAdmissionController(ctx,
// Name of the resource webhook.
"validation.webhook.typhoon.zeiss.com",
// The path on which to serve the webhook.
"/validation",
// The resources to validate.
validationTypes,
// A function that infuses the context passed to Validate/SetDefaults with custom metadata.
func(ctx context.Context) context.Context {
return ctx
},
// Whether to disallow unknown fields.
true,
)
}
func main() {
webhookName := webhook.NameFromEnv()
// Set up a signal context with our webhook options
ctx := webhook.WithOptions(signals.NewContext(), webhook.Options{
ServiceName: webhookName,
Port: webhook.PortFromEnv(8443),
SecretName: webhookName + "-certs",
})
registerValidationType(sourcesv1alpha1.SchemeGroupVersion, sourcesv1alpha1.AllTypes)
registerValidationType(targetsv1alpha1.SchemeGroupVersion, targetsv1alpha1.AllTypes)
registerValidationType(flowv1alpha1.SchemeGroupVersion, flowv1alpha1.AllTypes)
registerValidationType(extensionsv1alpha1.SchemeGroupVersion, extensionsv1alpha1.AllTypes)
registerValidationType(routingv1alpha1.SchemeGroupVersion, routingv1alpha1.AllTypes)
sharedmain.MainWithContext(ctx, webhookName,
certificates.NewController,
NewDefaultingAdmissionController,
NewValidationAdmissionController,
)
}
// registerValidationType registers components in the validation controller.
func registerValidationType(gv schema.GroupVersion, objects []v1alpha1.GroupObject) {
for _, object := range objects {
t := reflect.TypeOf(object.Single)
if admissible, ok := object.Single.(resourcesemantics.GenericCRD); ok {
validationTypes[gv.WithKind(t.Elem().Name())] = admissible
}
}
}