This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
/
main.go
288 lines (247 loc) · 11.6 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
Copyright 2019 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"
"log"
brokerv1 "github.com/google/knative-gcp/pkg/apis/broker/v1"
"github.com/google/knative-gcp/pkg/apis/configs/brokerdelivery"
"github.com/google/knative-gcp/pkg/apis/configs/dataresidency"
"github.com/google/knative-gcp/pkg/apis/configs/gcpauth"
"github.com/google/knative-gcp/pkg/apis/events"
eventsv1 "github.com/google/knative-gcp/pkg/apis/events/v1"
eventsv1beta1 "github.com/google/knative-gcp/pkg/apis/events/v1beta1"
"github.com/google/knative-gcp/pkg/apis/intevents"
inteventsv1 "github.com/google/knative-gcp/pkg/apis/intevents/v1"
inteventsv1alpha1 "github.com/google/knative-gcp/pkg/apis/intevents/v1alpha1"
inteventsv1beta1 "github.com/google/knative-gcp/pkg/apis/intevents/v1beta1"
messagingv1beta1 "github.com/google/knative-gcp/pkg/apis/messaging/v1beta1"
"k8s.io/apimachinery/pkg/runtime/schema"
"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"
"knative.dev/pkg/injection"
"knative.dev/pkg/injection/sharedmain"
"knative.dev/pkg/leaderelection"
"knative.dev/pkg/logging"
"knative.dev/pkg/signals"
tracingconfig "knative.dev/pkg/tracing/config"
"knative.dev/pkg/webhook"
"knative.dev/pkg/webhook/certificates"
"knative.dev/pkg/webhook/configmaps"
"knative.dev/pkg/webhook/resourcesemantics"
"knative.dev/pkg/webhook/resourcesemantics/conversion"
"knative.dev/pkg/webhook/resourcesemantics/defaulting"
"knative.dev/pkg/webhook/resourcesemantics/validation"
)
var types = map[schema.GroupVersionKind]resourcesemantics.GenericCRD{
// For group eventing.knative.dev.
brokerv1.SchemeGroupVersion.WithKind("Broker"): &brokerv1.Broker{},
brokerv1.SchemeGroupVersion.WithKind("Trigger"): &brokerv1.Trigger{},
// For group messaging.cloud.google.com.
messagingv1beta1.SchemeGroupVersion.WithKind("Channel"): &messagingv1beta1.Channel{},
// For group events.cloud.google.com.
eventsv1beta1.SchemeGroupVersion.WithKind("CloudStorageSource"): &eventsv1beta1.CloudStorageSource{},
eventsv1beta1.SchemeGroupVersion.WithKind("CloudSchedulerSource"): &eventsv1beta1.CloudSchedulerSource{},
eventsv1beta1.SchemeGroupVersion.WithKind("CloudPubSubSource"): &eventsv1beta1.CloudPubSubSource{},
eventsv1beta1.SchemeGroupVersion.WithKind("CloudAuditLogsSource"): &eventsv1beta1.CloudAuditLogsSource{},
eventsv1beta1.SchemeGroupVersion.WithKind("CloudBuildSource"): &eventsv1beta1.CloudBuildSource{},
eventsv1.SchemeGroupVersion.WithKind("CloudStorageSource"): &eventsv1.CloudStorageSource{},
eventsv1.SchemeGroupVersion.WithKind("CloudSchedulerSource"): &eventsv1.CloudSchedulerSource{},
eventsv1.SchemeGroupVersion.WithKind("CloudPubSubSource"): &eventsv1.CloudPubSubSource{},
eventsv1.SchemeGroupVersion.WithKind("CloudAuditLogsSource"): &eventsv1.CloudAuditLogsSource{},
eventsv1.SchemeGroupVersion.WithKind("CloudBuildSource"): &eventsv1.CloudBuildSource{},
// For group internal.events.cloud.google.com.
inteventsv1beta1.SchemeGroupVersion.WithKind("PullSubscription"): &inteventsv1beta1.PullSubscription{},
inteventsv1beta1.SchemeGroupVersion.WithKind("Topic"): &inteventsv1beta1.Topic{},
inteventsv1.SchemeGroupVersion.WithKind("PullSubscription"): &inteventsv1.PullSubscription{},
inteventsv1.SchemeGroupVersion.WithKind("Topic"): &inteventsv1.Topic{},
inteventsv1alpha1.SchemeGroupVersion.WithKind("BrokerCell"): &inteventsv1alpha1.BrokerCell{},
}
type defaultingAdmissionController func(context.Context, configmap.Watcher) *controller.Impl
func newDefaultingAdmissionConstructor(brokerdeliverys *brokerdelivery.StoreSingleton, gcpas *gcpauth.StoreSingleton) defaultingAdmissionController {
return func(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
return newDefaultingAdmissionController(ctx, cmw, brokerdeliverys.Store(ctx, cmw), gcpas.Store(ctx, cmw))
}
}
func newDefaultingAdmissionController(ctx context.Context, cmw configmap.Watcher, brokerdeliverys *brokerdelivery.Store, gcpas *gcpauth.Store) *controller.Impl {
// Decorate contexts with the current state of the config.
ctxFunc := func(ctx context.Context) context.Context {
return brokerdeliverys.ToContext(gcpas.ToContext(ctx))
}
return defaulting.NewAdmissionController(ctx,
// Name of the default webhook.
"webhook.events.cloud.google.com",
// The path on which to serve the webhook.
"/defaulting",
// The resources to validate and default.
types,
// A function that infuses the context passed to Validate/SetDefaults with custom metadata.
ctxFunc,
// Whether to disallow unknown fields.
true,
)
}
type validationController func(context.Context, configmap.Watcher) *controller.Impl
func newValidationConstructor(brokerdeliverys *brokerdelivery.StoreSingleton, gcpas *gcpauth.StoreSingleton) validationController {
return func(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
return newValidationAdmissionController(ctx, cmw, brokerdeliverys.Store(ctx, cmw), gcpas.Store(ctx, cmw))
}
}
func newValidationAdmissionController(ctx context.Context, cmw configmap.Watcher, brokerdeliverys *brokerdelivery.Store, gcpas *gcpauth.Store) *controller.Impl {
// A function that infuses the context passed to Validate/SetDefaults with custom metadata.
ctxFunc := func(ctx context.Context) context.Context {
return brokerdeliverys.ToContext(gcpas.ToContext(ctx))
}
return validation.NewAdmissionController(ctx,
// Name of the validation webhook.
"validation.webhook.events.cloud.google.com",
// The path on which to serve the webhook.
"/validation",
// The resources to validate and default.
types,
ctxFunc,
// Whether to disallow unknown fields.
true,
)
}
// NewConfigValidationController creates a new admission controller to validate configuration
// maps, which will be used when applying new configmap or editing existing configmap.
func NewConfigValidationController(ctx context.Context, _ configmap.Watcher) *controller.Impl {
return configmaps.NewAdmissionController(ctx,
// Name of the configmap webhook.
"config.webhook.events.cloud.google.com",
// The path on which to serve the webhook.
"/config-validation",
// The configmaps to validate.
configmap.Constructors{
tracingconfig.ConfigName: tracingconfig.NewTracingConfigFromConfigMap,
// metrics.ConfigMapName(): metricsconfig.NewObservabilityConfigFromConfigMap,
logging.ConfigMapName(): logging.NewConfigFromConfigMap,
leaderelection.ConfigMapName(): leaderelection.NewConfigFromConfigMap,
gcpauth.ConfigMapName(): gcpauth.NewDefaultsConfigFromConfigMap,
brokerdelivery.ConfigMapName(): brokerdelivery.NewDefaultsConfigFromConfigMap,
dataresidency.ConfigMapName(): dataresidency.NewDefaultsConfigFromConfigMap,
},
)
}
type conversionController func(context.Context, configmap.Watcher) *controller.Impl
func newConversionConstructor(brokerdeliverys *brokerdelivery.StoreSingleton, gcpas *gcpauth.StoreSingleton) conversionController {
return func(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
return newConversionController(ctx, cmw, brokerdeliverys.Store(ctx, cmw), gcpas.Store(ctx, cmw))
}
}
func newConversionController(ctx context.Context, cmw configmap.Watcher, brokerdeliverys *brokerdelivery.Store, gcpas *gcpauth.Store) *controller.Impl {
var (
eventsv1beta1_ = eventsv1beta1.SchemeGroupVersion.Version
eventsv1_ = eventsv1.SchemeGroupVersion.Version
inteventsv1beta1_ = inteventsv1beta1.SchemeGroupVersion.Version
inteventsv1_ = inteventsv1.SchemeGroupVersion.Version
)
// Decorate contexts with the current state of the config.
ctxFunc := func(ctx context.Context) context.Context {
return brokerdeliverys.ToContext(gcpas.ToContext(ctx))
}
return conversion.NewConversionController(ctx,
// The path on which to serve the webhook
"/resource-conversion",
// Specify the types of custom resource definitions that should be converted
map[schema.GroupKind]conversion.GroupKindConversion{
// events
eventsv1.Kind("CloudAuditLogsSource"): {
DefinitionName: events.CloudAuditLogsSourcesResource.String(),
HubVersion: eventsv1beta1_,
Zygotes: map[string]conversion.ConvertibleObject{
eventsv1beta1_: &eventsv1beta1.CloudAuditLogsSource{},
eventsv1_: &eventsv1.CloudAuditLogsSource{},
},
},
eventsv1.Kind("CloudPubSubSource"): {
DefinitionName: events.CloudPubSubSourcesResource.String(),
HubVersion: eventsv1beta1_,
Zygotes: map[string]conversion.ConvertibleObject{
eventsv1beta1_: &eventsv1beta1.CloudPubSubSource{},
eventsv1_: &eventsv1.CloudPubSubSource{},
},
},
eventsv1.Kind("CloudSchedulerSource"): {
DefinitionName: events.CloudSchedulerSourcesResource.String(),
HubVersion: eventsv1beta1_,
Zygotes: map[string]conversion.ConvertibleObject{
eventsv1beta1_: &eventsv1beta1.CloudSchedulerSource{},
eventsv1_: &eventsv1.CloudSchedulerSource{},
},
},
eventsv1.Kind("CloudStorageSource"): {
DefinitionName: events.CloudStorageSourcesResource.String(),
HubVersion: eventsv1beta1_,
Zygotes: map[string]conversion.ConvertibleObject{
eventsv1beta1_: &eventsv1beta1.CloudStorageSource{},
eventsv1_: &eventsv1.CloudStorageSource{},
},
},
eventsv1.Kind("CloudBuildSource"): {
DefinitionName: events.CloudBuildSourcesResource.String(),
HubVersion: eventsv1beta1_,
Zygotes: map[string]conversion.ConvertibleObject{
eventsv1beta1_: &eventsv1beta1.CloudBuildSource{},
eventsv1_: &eventsv1.CloudBuildSource{},
},
},
// intevents
inteventsv1.Kind("PullSubscription"): {
DefinitionName: intevents.PullSubscriptionsResource.String(),
HubVersion: inteventsv1beta1_,
Zygotes: map[string]conversion.ConvertibleObject{
inteventsv1beta1_: &inteventsv1beta1.PullSubscription{},
inteventsv1_: &inteventsv1.PullSubscription{},
},
},
inteventsv1.Kind("Topic"): {
DefinitionName: intevents.TopicsResource.String(),
HubVersion: inteventsv1beta1_,
Zygotes: map[string]conversion.ConvertibleObject{
inteventsv1beta1_: &inteventsv1beta1.Topic{},
inteventsv1_: &inteventsv1.Topic{},
},
},
},
ctxFunc,
)
}
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 must match the name of the Secret created in the configuration.
SecretName: "webhook-certs",
})
controllers, err := InitializeControllers(ctx)
if err != nil {
log.Fatal(err)
}
sharedmain.WebhookMainWithContext(ctx, webhook.NameFromEnv(), controllers...)
}
func Controllers(
conversionController conversionController,
defaultingAdmissionController defaultingAdmissionController,
validationController validationController,
) []injection.ControllerConstructor {
return []injection.ControllerConstructor{
certificates.NewController,
NewConfigValidationController,
injection.ControllerConstructor(validationController),
injection.ControllerConstructor(defaultingAdmissionController),
injection.ControllerConstructor(conversionController),
}
}