This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
webhooks.go
247 lines (221 loc) · 8.68 KB
/
webhooks.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
/*
* Copyright 2022 The flomesh.io 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"
"fmt"
flomeshadmission "github.com/flomesh-io/ErieCanal/pkg/admission"
"github.com/flomesh-io/ErieCanal/pkg/certificate"
"github.com/flomesh-io/ErieCanal/pkg/commons"
"github.com/flomesh-io/ErieCanal/pkg/config"
"github.com/flomesh-io/ErieCanal/pkg/kube"
"github.com/flomesh-io/ErieCanal/pkg/webhooks"
clusterwh "github.com/flomesh-io/ErieCanal/pkg/webhooks/cluster"
cmwh "github.com/flomesh-io/ErieCanal/pkg/webhooks/cm"
gatewaywh "github.com/flomesh-io/ErieCanal/pkg/webhooks/gateway"
gatewayclasswh "github.com/flomesh-io/ErieCanal/pkg/webhooks/gatewayclass"
gtpwh "github.com/flomesh-io/ErieCanal/pkg/webhooks/globaltrafficpolicy"
httproutewh "github.com/flomesh-io/ErieCanal/pkg/webhooks/httproute"
ingwh "github.com/flomesh-io/ErieCanal/pkg/webhooks/ingress"
idwh "github.com/flomesh-io/ErieCanal/pkg/webhooks/namespacedingress"
svcexpwh "github.com/flomesh-io/ErieCanal/pkg/webhooks/serviceexport"
svcimpwh "github.com/flomesh-io/ErieCanal/pkg/webhooks/serviceimport"
"io/ioutil"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"os"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
func createWebhookConfigurations(k8sApi *kube.K8sAPI, configStore *config.Store, certMgr certificate.Manager) {
mc := configStore.MeshConfig.GetConfig()
cert, err := issueCertForWebhook(certMgr, mc)
if err != nil {
os.Exit(1)
}
ns := config.GetErieCanalNamespace()
svcName := mc.Webhook.ServiceName
caBundle := cert.CA
webhooks.RegisterWebhooks(ns, svcName, caBundle)
if mc.GatewayApi.Enabled {
webhooks.RegisterGatewayApiWebhooks(ns, svcName, caBundle)
}
// Mutating
mwc := flomeshadmission.NewMutatingWebhookConfiguration()
mutating := k8sApi.Client.
AdmissionregistrationV1().
MutatingWebhookConfigurations()
if _, err = mutating.Create(context.Background(), mwc, metav1.CreateOptions{}); err != nil {
if apierrors.IsAlreadyExists(err) {
existingMwc, err := mutating.Get(context.Background(), mwc.Name, metav1.GetOptions{})
if err != nil {
klog.Errorf("Unable to get MutatingWebhookConfigurations %q, %s", mwc.Name, err.Error())
os.Exit(1)
}
existingMwc.Webhooks = mwc.Webhooks
_, err = mutating.Update(context.Background(), existingMwc, metav1.UpdateOptions{})
if err != nil {
// Should be not conflict for a leader-election manager, error is error
klog.Errorf("Unable to update MutatingWebhookConfigurations %q, %s", mwc.Name, err.Error())
os.Exit(1)
}
} else {
klog.Errorf("Unable to create MutatingWebhookConfigurations %q, %s", mwc.Name, err.Error())
os.Exit(1)
}
}
// Validating
vmc := flomeshadmission.NewValidatingWebhookConfiguration()
validating := k8sApi.Client.
AdmissionregistrationV1().
ValidatingWebhookConfigurations()
if _, err = validating.Create(context.Background(), vmc, metav1.CreateOptions{}); err != nil {
if apierrors.IsAlreadyExists(err) {
existingVmc, err := validating.Get(context.Background(), vmc.Name, metav1.GetOptions{})
if err != nil {
klog.Errorf("Unable to get ValidatingWebhookConfigurations %q, %s", mwc.Name, err.Error())
os.Exit(1)
}
existingVmc.Webhooks = vmc.Webhooks
_, err = validating.Update(context.Background(), existingVmc, metav1.UpdateOptions{})
if err != nil {
klog.Errorf("Unable to update ValidatingWebhookConfigurations %q, %s", vmc.Name, err.Error())
os.Exit(1)
}
} else {
klog.Errorf("Unable to create ValidatingWebhookConfigurations %q, %s", vmc.Name, err.Error())
os.Exit(1)
}
}
}
func issueCertForWebhook(certMgr certificate.Manager, mc *config.MeshConfig) (*certificate.Certificate, error) {
// TODO: refactoring it later, configurable CN and dns names
cert, err := certMgr.IssueCertificate(
mc.Webhook.ServiceName,
commons.DefaultCAValidityPeriod,
[]string{
mc.Webhook.ServiceName,
fmt.Sprintf("%s.%s.svc", mc.Webhook.ServiceName, config.GetErieCanalNamespace()),
fmt.Sprintf("%s.%s.svc.cluster.local", mc.Webhook.ServiceName, config.GetErieCanalNamespace()),
},
)
if err != nil {
klog.Error("Error issuing certificate, ", err)
return nil, err
}
// write ca.crt, tls.crt & tls.key to file
if err := os.MkdirAll(commons.WebhookServerServingCertsPath, 755); err != nil {
klog.Errorf("error creating dir %q, %s", commons.WebhookServerServingCertsPath, err.Error())
return nil, err
}
certFiles := map[string][]byte{
commons.RootCACertName: cert.CA,
commons.TLSCertName: cert.CrtPEM,
commons.TLSPrivateKeyName: cert.KeyPEM,
}
for file, data := range certFiles {
fileName := fmt.Sprintf("%s/%s", commons.WebhookServerServingCertsPath, file)
if err := ioutil.WriteFile(
fileName,
data,
420); err != nil {
klog.Errorf("error writing file %q, %s", fileName, err.Error())
return nil, err
}
}
return cert, nil
}
func registerToWebhookServer(mgr manager.Manager, api *kube.K8sAPI, controlPlaneConfigStore *config.Store) {
hookServer := mgr.GetWebhookServer()
mc := controlPlaneConfigStore.MeshConfig.GetConfig()
// Cluster
hookServer.Register(commons.ClusterMutatingWebhookPath,
webhooks.DefaultingWebhookFor(clusterwh.NewDefaulter(api, controlPlaneConfigStore)),
)
hookServer.Register(commons.ClusterValidatingWebhookPath,
webhooks.ValidatingWebhookFor(clusterwh.NewValidator(api)),
)
// NamespacedIngress
hookServer.Register(commons.NamespacedIngressMutatingWebhookPath,
webhooks.DefaultingWebhookFor(idwh.NewDefaulter(api, controlPlaneConfigStore)),
)
hookServer.Register(commons.NamespacedIngressValidatingWebhookPath,
webhooks.ValidatingWebhookFor(idwh.NewValidator(api)),
)
// ServiceExport
hookServer.Register(commons.ServiceExportMutatingWebhookPath,
webhooks.DefaultingWebhookFor(svcexpwh.NewDefaulter(api, controlPlaneConfigStore)),
)
hookServer.Register(commons.ServiceExportValidatingWebhookPath,
webhooks.ValidatingWebhookFor(svcexpwh.NewValidator(api)),
)
// ServiceImport
hookServer.Register(commons.ServiceImportMutatingWebhookPath,
webhooks.DefaultingWebhookFor(svcimpwh.NewDefaulter(api, controlPlaneConfigStore)),
)
hookServer.Register(commons.ServiceImportValidatingWebhookPath,
webhooks.ValidatingWebhookFor(svcimpwh.NewValidator(api)),
)
// GlobalTrafficPolicy
hookServer.Register(commons.GlobalTrafficPolicyMutatingWebhookPath,
webhooks.DefaultingWebhookFor(gtpwh.NewDefaulter(api, controlPlaneConfigStore)),
)
hookServer.Register(commons.GlobalTrafficPolicyValidatingWebhookPath,
webhooks.ValidatingWebhookFor(gtpwh.NewValidator(api)),
)
// core ConfigMap
hookServer.Register(commons.ConfigMapMutatingWebhookPath,
webhooks.DefaultingWebhookFor(cmwh.NewDefaulter(api)),
)
hookServer.Register(commons.ConfigMapValidatingWebhookPath,
webhooks.ValidatingWebhookFor(cmwh.NewValidator(api)),
)
// networking v1 Ingress
hookServer.Register(commons.IngressMutatingWebhookPath,
webhooks.DefaultingWebhookFor(ingwh.NewDefaulter(api)),
)
hookServer.Register(commons.IngressValidatingWebhookPath,
webhooks.ValidatingWebhookFor(ingwh.NewValidator(api)),
)
// Gateway API
if mc.GatewayApi.Enabled {
registerGatewayApiToWebhookServer(mgr, api, controlPlaneConfigStore)
}
}
func registerGatewayApiToWebhookServer(mgr manager.Manager, api *kube.K8sAPI, controlPlaneConfigStore *config.Store) {
hookServer := mgr.GetWebhookServer()
// Gateway
hookServer.Register(commons.GatewayMutatingWebhookPath,
webhooks.DefaultingWebhookFor(gatewaywh.NewDefaulter(api, controlPlaneConfigStore)),
)
hookServer.Register(commons.GatewayValidatingWebhookPath,
webhooks.ValidatingWebhookFor(gatewaywh.NewValidator(api)),
)
// GatewayClass
hookServer.Register(commons.GatewayClassMutatingWebhookPath,
webhooks.DefaultingWebhookFor(gatewayclasswh.NewDefaulter(api, controlPlaneConfigStore)),
)
hookServer.Register(commons.GatewayClassValidatingWebhookPath,
webhooks.ValidatingWebhookFor(gatewayclasswh.NewValidator(api)),
)
// HTTPRoute
hookServer.Register(commons.HTTPRouteMutatingWebhookPath,
webhooks.DefaultingWebhookFor(httproutewh.NewDefaulter(api, controlPlaneConfigStore)),
)
hookServer.Register(commons.HTTPRouteValidatingWebhookPath,
webhooks.ValidatingWebhookFor(httproutewh.NewValidator(api)),
)
}