/
patches.go
315 lines (296 loc) · 12.1 KB
/
patches.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package main
import (
"github.com/giantswarm/to"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"github.com/giantswarm/apiextensions/v4/pkg/crd"
)
func patchCAPICoreWebhook(crd *v1.CustomResourceDefinition) {
port := int32(9443)
if _, ok := crd.Annotations["cert-manager.io/inject-ca-from"]; ok {
crd.Annotations["cert-manager.io/inject-ca-from"] = "giantswarm/cluster-api-core-cert"
}
crd.Spec.Conversion = &v1.CustomResourceConversion{
Strategy: v1.WebhookConverter,
Webhook: &v1.WebhookConversion{
ClientConfig: &v1.WebhookClientConfig{
Service: &v1.ServiceReference{
Namespace: "giantswarm",
Name: "cluster-api-core",
Path: to.StringP("/convert"),
Port: &port,
},
CABundle: []byte("\n"),
},
ConversionReviewVersions: []string{
"v1",
"v1beta1",
},
},
}
}
func patchCAPIKubeadmBootstrapWebhook(crd *v1.CustomResourceDefinition) {
var isV1alpha4 bool
for _, v := range crd.Spec.Versions {
if v.Name == "v1alpha4" {
isV1alpha4 = true
break
}
}
// The name of the certificate and conversion webhook service changed between v1alpha3 and v1alpha4 (the app also
// changed from cluster-api-bootstrap-provider-kubeadm-app to cluster-api-app) so we check which versions are present and apply the correct
// patch.
if isV1alpha4 {
patchCAPIKubeadmBootstrapWebhookV1Alpha4(crd)
} else {
patchCAPIKubeadmBootstrapWebhookV1Alpha3(crd)
}
}
// Keep in sync with https://github.com/giantswarm/cluster-api-bootstrap-provider-kubeadm-app/tree/main/helm/cluster-api-bootstrap-provider-kubeadm/templates
func patchCAPIKubeadmBootstrapWebhookV1Alpha3(crd *v1.CustomResourceDefinition) {
port := int32(9443)
if _, ok := crd.Annotations["cert-manager.io/inject-ca-from"]; ok {
crd.Annotations["cert-manager.io/inject-ca-from"] = "giantswarm/cluster-api-bootstrap-cert"
}
crd.Spec.Conversion = &v1.CustomResourceConversion{
Strategy: v1.WebhookConverter,
Webhook: &v1.WebhookConversion{
ClientConfig: &v1.WebhookClientConfig{
Service: &v1.ServiceReference{
Namespace: "giantswarm",
Name: "cluster-api-bootstrap",
Path: to.StringP("/convert"),
Port: &port,
},
CABundle: []byte("\n"),
},
ConversionReviewVersions: []string{
"v1",
"v1beta1",
},
},
}
}
// Keep in sync with https://github.com/giantswarm/cluster-api-core-app/tree/main/helm/cluster-api-core/templates
func patchCAPIKubeadmBootstrapWebhookV1Alpha4(crd *v1.CustomResourceDefinition) {
// placeholder
}
func patchCAPIControlPlaneWebhook(crd *v1.CustomResourceDefinition) {
var isV1alpha4 bool
for _, v := range crd.Spec.Versions {
if v.Name == "v1alpha4" {
isV1alpha4 = true
break
}
}
// The name of the certificate and conversion webhook service changed between v1alpha3 and v1alpha4 (the app also
// changed from cluster-api-control-plane-app to cluster-api-app) so we check which versions are present and apply the correct
// patch.
if isV1alpha4 {
patchCAPIControlPlaneWebhookV1Alpha4(crd)
} else {
patchCAPIControlPlaneWebhookV1Alpha3(crd)
}
}
// Keep in sync with https://github.com/giantswarm/cluster-api-control-plane-app/tree/main/helm/cluster-api-control-plane/templates
func patchCAPIControlPlaneWebhookV1Alpha3(crd *v1.CustomResourceDefinition) {
port := int32(9443)
if _, ok := crd.Annotations["cert-manager.io/inject-ca-from"]; ok {
crd.Annotations["cert-manager.io/inject-ca-from"] = "giantswarm/cluster-api-controlplane-cert"
}
crd.Spec.Conversion = &v1.CustomResourceConversion{
Strategy: v1.WebhookConverter,
Webhook: &v1.WebhookConversion{
ClientConfig: &v1.WebhookClientConfig{
Service: &v1.ServiceReference{
Namespace: "giantswarm",
Name: "cluster-api-controlplane",
Path: to.StringP("/convert"),
Port: &port,
},
CABundle: []byte("\n"),
},
ConversionReviewVersions: []string{
"v1",
"v1beta1",
},
},
}
}
// Keep in sync with https://github.com/giantswarm/cluster-api-core-app/tree/main/helm/cluster-api-core/templates
func patchCAPIControlPlaneWebhookV1Alpha4(crd *v1.CustomResourceDefinition) {
// placeholder
}
// Keep in sync with https://github.com/giantswarm/cluster-api-provider-aws-app/tree/master/helm/cluster-api-provider-aws/templates
func patchCAPAWebhook(crd *v1.CustomResourceDefinition) {
port := int32(9443)
if _, ok := crd.Annotations["cert-manager.io/inject-ca-from"]; ok {
crd.Annotations["cert-manager.io/inject-ca-from"] = "giantswarm/cluster-api-provider-aws-webhook"
}
crd.Spec.Conversion = &v1.CustomResourceConversion{
Strategy: v1.WebhookConverter,
Webhook: &v1.WebhookConversion{
ClientConfig: &v1.WebhookClientConfig{
Service: &v1.ServiceReference{
Namespace: "giantswarm",
Name: "cluster-api-provider-aws-webhook",
Path: to.StringP("/convert"),
Port: &port,
},
CABundle: []byte("\n"),
},
ConversionReviewVersions: []string{
"v1",
"v1beta1",
},
},
}
}
// Keep in sync with https://github.com/giantswarm/cluster-api-provider-vsphere-app/tree/master/helm/cluster-api-provider-vsphere/templates
func patchCAPVWebhook(crd *v1.CustomResourceDefinition) {
port := int32(9443)
if _, ok := crd.Annotations["cert-manager.io/inject-ca-from"]; ok {
crd.Annotations["cert-manager.io/inject-ca-from"] = "giantswarm/cluster-api-provider-vsphere-webhook"
}
crd.Spec.Conversion = &v1.CustomResourceConversion{
Strategy: v1.WebhookConverter,
Webhook: &v1.WebhookConversion{
ClientConfig: &v1.WebhookClientConfig{
Service: &v1.ServiceReference{
Namespace: "giantswarm",
Name: "cluster-api-provider-vsphere-webhook",
Path: to.StringP("/convert"),
Port: &port,
},
CABundle: []byte("\n"),
},
ConversionReviewVersions: []string{
"v1",
"v1beta1",
},
},
}
}
// Keep in sync with https://github.com/giantswarm/cluster-api-provider-azure-app/tree/master/helm/cluster-api-provider-azure/templates
func patchCAPZWebhook(crd *v1.CustomResourceDefinition) {
delete(crd.Annotations, "cert-manager.io/inject-ca-from")
crd.Spec.Conversion = nil
}
// Keep in sync with https://github.com/giantswarm/cluster-api-provider-aws-app/tree/master/helm/cluster-api-provider-aws/templates/eks/control-plane
func patchEKSControlPlaneWebhook(crd *v1.CustomResourceDefinition) {
port := int32(9443)
if _, ok := crd.Annotations["cert-manager.io/inject-ca-from"]; ok {
crd.Annotations["cert-manager.io/inject-ca-from"] = "giantswarm/cluster-api-provider-aws-eks-control-plane-webhook"
}
crd.Spec.Conversion = &v1.CustomResourceConversion{
Strategy: v1.WebhookConverter,
Webhook: &v1.WebhookConversion{
ClientConfig: &v1.WebhookClientConfig{
Service: &v1.ServiceReference{
Namespace: "giantswarm",
Name: "cluster-api-provider-aws-eks-control-plane-webhook",
Path: to.StringP("/convert"),
Port: &port,
},
CABundle: []byte("\n"),
},
ConversionReviewVersions: []string{
"v1",
"v1beta1",
},
},
}
}
// Keep in sync with https://github.com/giantswarm/cluster-api-provider-aws-app/tree/master/helm/cluster-api-provider-aws/templates/eks/bootstrap
func patchEKSConfigWebhook(crd *v1.CustomResourceDefinition) {
port := int32(9443)
if _, ok := crd.Annotations["cert-manager.io/inject-ca-from"]; ok {
crd.Annotations["cert-manager.io/inject-ca-from"] = "giantswarm/cluster-api-provider-aws-eks-bootstrap-webhook"
}
crd.Spec.Conversion = &v1.CustomResourceConversion{
Strategy: v1.WebhookConverter,
Webhook: &v1.WebhookConversion{
ClientConfig: &v1.WebhookClientConfig{
Service: &v1.ServiceReference{
Namespace: "giantswarm",
Name: "cluster-api-provider-aws-eks-bootstrap-webhook",
Path: to.StringP("/convert"),
Port: &port,
},
CABundle: []byte("\n"),
},
ConversionReviewVersions: []string{
"v1",
"v1beta1",
},
},
}
}
// Kubebuilder comments can't add validation to metadata properties, so we manually specify the validation for release names here.
func patchReleaseValidation(crd *v1.CustomResourceDefinition) {
for i := range crd.Spec.Versions {
crd.Spec.Versions[i].Schema.OpenAPIV3Schema.Properties["metadata"] = v1.JSONSchemaProps{
Type: "object",
Properties: map[string]v1.JSONSchemaProps{
"name": {
Pattern: "^v(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(-[\\.0-9a-zA-Z]*)?$",
Type: "string",
},
},
}
}
}
// Upstream CRD contains a string which gitleaks detects as a leak. Applies the normal patchCAPAWebhook patch and then
// edits the description to avoid the false positive.
func patchAWSClusterStaticIdentities(crd *v1.CustomResourceDefinition) {
patchCAPAWebhook(crd)
version := crd.Spec.Versions[0]
schema := version.Schema.OpenAPIV3Schema
secretRef := schema.Properties["spec"].Properties["secretRef"]
secretRef.Description = "Reference to a secret containing the credentials. The secret should contain the following data keys: AccessKeyID: <access key id> SecretAccessKey: <secret access key> SessionToken: Optional"
schema.Properties["spec"].Properties["secretRef"] = secretRef
crd.Spec.Versions[0] = version
}
var patches = map[string]crd.Patch{
// capi
"clusterclasses.cluster.x-k8s.io": patchCAPICoreWebhook,
"clusters.cluster.x-k8s.io": patchCAPICoreWebhook,
"kubeadmcontrolplanes.controlplane.cluster.x-k8s.io": patchCAPIControlPlaneWebhook,
"kubeadmconfigs.bootstrap.cluster.x-k8s.io": patchCAPIKubeadmBootstrapWebhook,
"kubeadmconfigtemplates.bootstrap.cluster.x-k8s.io": patchCAPIKubeadmBootstrapWebhook,
"machinedeployments.cluster.x-k8s.io": patchCAPICoreWebhook,
"machinehealthchecks.cluster.x-k8s.io": patchCAPICoreWebhook,
"machines.cluster.x-k8s.io": patchCAPICoreWebhook,
"machinesets.cluster.x-k8s.io": patchCAPICoreWebhook,
// capa
"awsclustercontrolleridentities.infrastructure.cluster.x-k8s.io": patchCAPAWebhook,
"awsclusterroleidentities.infrastructure.cluster.x-k8s.io": patchCAPAWebhook,
"awsclusters.infrastructure.cluster.x-k8s.io": patchCAPAWebhook,
"awsclusterstaticidentities.infrastructure.cluster.x-k8s.io": patchAWSClusterStaticIdentities,
"awsfargateprofiles.infrastructure.cluster.x-k8s.io": patchCAPAWebhook,
"awsmachinepools.infrastructure.cluster.x-k8s.io": patchCAPAWebhook,
"awsmachines.infrastructure.cluster.x-k8s.io": patchCAPAWebhook,
"awsmachinetemplates.infrastructure.cluster.x-k8s.io": patchCAPAWebhook,
"awsmanagedclusters.infrastructure.cluster.x-k8s.io": patchCAPAWebhook,
"awsmanagedcontrolplanes.controlplane.cluster.x-k8s.io": patchEKSControlPlaneWebhook,
"awsmanagedmachinepools.infrastructure.cluster.x-k8s.io": patchCAPAWebhook,
"eksconfigs.bootstrap.cluster.x-k8s.io": patchEKSConfigWebhook,
"eksconfigtemplates.bootstrap.cluster.x-k8s.io": patchEKSConfigWebhook,
// capz
"azureclusteridentities.infrastructure.cluster.x-k8s.io": patchCAPZWebhook,
"azureclusters.infrastructure.cluster.x-k8s.io": patchCAPZWebhook,
"azuremachines.infrastructure.cluster.x-k8s.io": patchCAPZWebhook,
"azuremachinetemplates.infrastructure.cluster.x-k8s.io": patchCAPZWebhook,
"azuremachinepoolmachines.infrastructure.cluster.x-k8s.io": patchCAPZWebhook,
"azuremachinepools.exp.infrastructure.cluster.x-k8s.io": patchCAPZWebhook,
"azuremachinepools.infrastructure.cluster.x-k8s.io": patchCAPZWebhook,
// giantswarm
"releases.release.giantswarm.io": patchReleaseValidation,
// capv
"vsphereclusters.infrastructure.cluster.x-k8s.io": patchCAPVWebhook,
"vsphereclustertemplates.infrastructure.cluster.x-k8s.io": patchCAPVWebhook,
"vspheredeploymentzones.infrastructure.cluster.x-k8s.io": patchCAPVWebhook,
"vspherefailuredomains.infrastructure.cluster.x-k8s.io": patchCAPVWebhook,
"vspheremachines.infrastructure.cluster.x-k8s.io": patchCAPVWebhook,
"vspheremachinetemplates.infrastructure.cluster.x-k8s.io": patchCAPVWebhook,
"vspherevms.infrastructure.cluster.x-k8s.io": patchCAPVWebhook,
}