forked from knative/serving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
168 lines (133 loc) · 5.87 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
Copyright 2018 The Knative 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 main
import (
"context"
"k8s.io/apimachinery/pkg/runtime/schema"
"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"
"knative.dev/pkg/injection/sharedmain"
"knative.dev/pkg/leaderelection"
"knative.dev/pkg/logging"
"knative.dev/pkg/metrics"
"knative.dev/pkg/signals"
"knative.dev/pkg/webhook"
"knative.dev/pkg/webhook/certificates"
"knative.dev/pkg/webhook/configmaps"
"knative.dev/pkg/webhook/resourcesemantics"
"knative.dev/pkg/webhook/resourcesemantics/defaulting"
"knative.dev/pkg/webhook/resourcesemantics/validation"
// resource validation types
net "knative.dev/networking/pkg/apis/networking/v1alpha1"
autoscalingv1alpha1 "knative.dev/serving/pkg/apis/autoscaling/v1alpha1"
servingv1 "knative.dev/serving/pkg/apis/serving/v1"
extravalidation "knative.dev/serving/pkg/webhook"
// config validation constructors
network "knative.dev/networking/pkg"
tracingconfig "knative.dev/pkg/tracing/config"
defaultconfig "knative.dev/serving/pkg/apis/config"
autoscalerconfig "knative.dev/serving/pkg/autoscaler/config"
"knative.dev/serving/pkg/deployment"
"knative.dev/serving/pkg/gc"
domainconfig "knative.dev/serving/pkg/reconciler/route/config"
)
var types = map[schema.GroupVersionKind]resourcesemantics.GenericCRD{
servingv1.SchemeGroupVersion.WithKind("Revision"): &servingv1.Revision{},
servingv1.SchemeGroupVersion.WithKind("Configuration"): &servingv1.Configuration{},
servingv1.SchemeGroupVersion.WithKind("Route"): &servingv1.Route{},
servingv1.SchemeGroupVersion.WithKind("Service"): &servingv1.Service{},
autoscalingv1alpha1.SchemeGroupVersion.WithKind("PodAutoscaler"): &autoscalingv1alpha1.PodAutoscaler{},
autoscalingv1alpha1.SchemeGroupVersion.WithKind("Metric"): &autoscalingv1alpha1.Metric{},
net.SchemeGroupVersion.WithKind("Certificate"): &net.Certificate{},
net.SchemeGroupVersion.WithKind("Ingress"): &net.Ingress{},
net.SchemeGroupVersion.WithKind("ServerlessService"): &net.ServerlessService{},
}
var serviceValidation = validation.NewCallback(
extravalidation.ValidateService, webhook.Create, webhook.Update)
var configValidation = validation.NewCallback(
extravalidation.ValidateConfiguration, webhook.Create, webhook.Update)
var callbacks = map[schema.GroupVersionKind]validation.Callback{
servingv1.SchemeGroupVersion.WithKind("Service"): serviceValidation,
servingv1.SchemeGroupVersion.WithKind("Configuration"): configValidation,
}
func newDefaultingAdmissionController(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
// Decorate contexts with the current state of the config.
store := defaultconfig.NewStore(logging.FromContext(ctx).Named("config-store"))
store.WatchConfigs(cmw)
return defaulting.NewAdmissionController(ctx,
// Name of the resource webhook.
"webhook.serving.knative.dev",
// The path on which to serve the webhook.
"/defaulting",
// The resources to default.
types,
// A function that infuses the context passed to Validate/SetDefaults with custom metadata.
store.ToContext,
// Whether to disallow unknown fields.
true,
)
}
func newValidationAdmissionController(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
// Decorate contexts with the current state of the config.
store := defaultconfig.NewStore(logging.FromContext(ctx).Named("config-store"))
store.WatchConfigs(cmw)
return validation.NewAdmissionController(ctx,
// Name of the resource webhook.
"validation.webhook.serving.knative.dev",
// The path on which to serve the webhook.
"/resource-validation",
// The resources to validate.
types,
// A function that infuses the context passed to Validate/SetDefaults with custom metadata.
store.ToContext,
// Whether to disallow unknown fields.
true,
// Extra validating callbacks to be applied to resources.
callbacks,
)
}
func newConfigValidationController(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
return configmaps.NewAdmissionController(ctx,
// Name of the configmap webhook.
"config.webhook.serving.knative.dev",
// The path on which to serve the webhook.
"/config-validation",
// The configmaps to validate.
configmap.Constructors{
tracingconfig.ConfigName: tracingconfig.NewTracingConfigFromConfigMap,
autoscalerconfig.ConfigName: autoscalerconfig.NewConfigFromConfigMap,
gc.ConfigName: gc.NewConfigFromConfigMapFunc(ctx),
network.ConfigName: network.NewConfigFromConfigMap,
deployment.ConfigName: deployment.NewConfigFromConfigMap,
metrics.ConfigMapName(): metrics.NewObservabilityConfigFromConfigMap,
logging.ConfigMapName(): logging.NewConfigFromConfigMap,
leaderelection.ConfigMapName(): leaderelection.NewConfigFromConfigMap,
domainconfig.DomainConfigName: domainconfig.NewDomainFromConfigMap,
defaultconfig.DefaultsConfigName: defaultconfig.NewDefaultsConfigFromConfigMap,
},
)
}
func main() {
// Set up a signal context with our webhook options
ctx := webhook.WithOptions(signals.NewContext(), webhook.Options{
ServiceName: webhook.NameFromEnv(),
Port: webhook.PortFromEnv(8443),
SecretName: "webhook-certs",
})
sharedmain.WebhookMainWithContext(ctx, "webhook",
certificates.NewController,
newDefaultingAdmissionController,
newValidationAdmissionController,
newConfigValidationController,
)
}