-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
components.go
589 lines (507 loc) · 21.1 KB
/
components.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
Copyright 2019 The Kubernetes 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 repository
import (
"fmt"
"strings"
"github.com/pkg/errors"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
rbacv1 "k8s.io/api/rbac/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
yaml "sigs.k8s.io/cluster-api/cmd/clusterctl/client/yamlprocessor"
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/scheme"
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/util"
utilyaml "sigs.k8s.io/cluster-api/util/yaml"
)
const (
namespaceKind = "Namespace"
clusterRoleKind = "ClusterRole"
clusterRoleBindingKind = "ClusterRoleBinding"
roleBindingKind = "RoleBinding"
certificateKind = "Certificate"
mutatingWebhookConfigurationKind = "MutatingWebhookConfiguration"
validatingWebhookConfigurationKind = "ValidatingWebhookConfiguration"
customResourceDefinitionKind = "CustomResourceDefinition"
)
// Components wraps a YAML file that defines the provider components
// to be installed in a management cluster (CRD, Controller, RBAC etc.)
// It is important to notice that clusterctl applies a set of processing steps to the “raw” component YAML read
// from the provider repositories:
// 1. Checks for all the variables in the component YAML file and replace with corresponding config values
// 2. Ensure all the provider components are deployed in the target namespace (apply only to namespaced objects)
// 3. Ensure all the ClusterRoleBinding which are referencing namespaced objects have the name prefixed with the namespace name
// 4. Adds labels to all the components in order to allow easy identification of the provider objects.
type Components interface {
// Provider holds configuration of the provider the provider components belong to.
config.Provider
// Version of the provider.
Version() string
// Variables required by the provider components.
// This value is derived by the component YAML.
Variables() []string
// Images required to install the provider components.
// This value is derived by the component YAML.
Images() []string
// TargetNamespace where the provider components will be installed.
// By default this value is derived by the component YAML, but it is possible to override it
// during the creation of the Components object.
TargetNamespace() string
// InventoryObject returns the clusterctl inventory object representing the provider that will be
// generated by this components.
InventoryObject() clusterctlv1.Provider
// Yaml return the provider components in the form of a YAML file.
Yaml() ([]byte, error)
// Objs return the components in the form of a list of Unstructured objects.
Objs() []unstructured.Unstructured
}
// components implement Components.
type components struct {
config.Provider
version string
variables []string
images []string
targetNamespace string
objs []unstructured.Unstructured
}
// ensure components implement Components.
var _ Components = &components{}
func (c *components) Version() string {
return c.version
}
func (c *components) Variables() []string {
return c.variables
}
func (c *components) Images() []string {
return c.images
}
func (c *components) TargetNamespace() string {
return c.targetNamespace
}
func (c *components) InventoryObject() clusterctlv1.Provider {
labels := getCommonLabels(c.Provider)
labels[clusterctlv1.ClusterctlCoreLabelName] = clusterctlv1.ClusterctlCoreLabelInventoryValue
return clusterctlv1.Provider{
TypeMeta: metav1.TypeMeta{
APIVersion: clusterctlv1.GroupVersion.String(),
Kind: "Provider",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: c.targetNamespace,
Name: c.ManifestLabel(),
Labels: labels,
},
ProviderName: c.Name(),
Type: string(c.Type()),
Version: c.version,
}
}
func (c *components) Objs() []unstructured.Unstructured {
return c.objs
}
func (c *components) Yaml() ([]byte, error) {
return utilyaml.FromUnstructured(c.objs)
}
// ComponentsAlterFn defines the function that is used to alter the components.Objs().
type ComponentsAlterFn func(objs []unstructured.Unstructured) ([]unstructured.Unstructured, error)
// AlterComponents provides a mechanism to alter the component.Objs from outside
// the repository module.
func AlterComponents(comps Components, alterFn ComponentsAlterFn) error {
c, ok := comps.(*components)
if !ok {
return errors.New("could not alter components as Components is not of the correct type")
}
alteredObjs, err := alterFn(c.Objs())
if err != nil {
return err
}
c.objs = alteredObjs
return nil
}
// ComponentsOptions represents specific inputs that are passed in to
// clusterctl library. These are user specified inputs.
type ComponentsOptions struct {
Version string
TargetNamespace string
// SkipTemplateProcess allows for skipping the call to the template processor, including also variable replacement in the component YAML.
// NOTE this works only if the rawYaml is a valid yaml by itself, like e.g when using envsubst/the simple processor.
SkipTemplateProcess bool
}
// ComponentsInput represents all the inputs required by NewComponents.
type ComponentsInput struct {
Provider config.Provider
ConfigClient config.Client
Processor yaml.Processor
RawYaml []byte
Options ComponentsOptions
}
// NewComponents returns a new objects embedding a component YAML file
//
// It is important to notice that clusterctl applies a set of processing steps to the “raw” component YAML read
// from the provider repositories:
// 1. Checks for all the variables in the component YAML file and replace with corresponding config values
// 2. The variables replacement can be skipped using the SkipTemplateProcess flag in the input options
// 3. Ensure all the provider components are deployed in the target namespace (apply only to namespaced objects)
// 4. Ensure all the ClusterRoleBinding which are referencing namespaced objects have the name prefixed with the namespace name
// 5. Adds labels to all the components in order to allow easy identification of the provider objects.
func NewComponents(input ComponentsInput) (Components, error) {
variables, err := input.Processor.GetVariables(input.RawYaml)
if err != nil {
return nil, err
}
// If requested, we are skipping the call to the template processor; however, it is important to
// notice that this could work only if the rawYaml is a valid yaml by itself.
processedYaml := input.RawYaml
if !input.Options.SkipTemplateProcess {
processedYaml, err = input.Processor.Process(input.RawYaml, input.ConfigClient.Variables().Get)
if err != nil {
return nil, errors.Wrap(err, "failed to perform variable substitution")
}
}
// Transform the yaml in a list of objects, so following transformation can work on typed objects (instead of working on a string/slice of bytes)
objs, err := utilyaml.ToUnstructured(processedYaml)
if err != nil {
return nil, errors.Wrap(err, "failed to parse yaml")
}
// Apply image overrides, if defined
objs, err = util.FixImages(objs, func(image string) (string, error) {
return input.ConfigClient.ImageMeta().AlterImage(input.Provider.ManifestLabel(), image)
})
if err != nil {
return nil, errors.Wrap(err, "failed to apply image overrides")
}
// Inspect the list of objects for the images required by the provider component.
images, err := util.InspectImages(objs)
if err != nil {
return nil, errors.Wrap(err, "failed to detect required images")
}
// inspect the list of objects for the default target namespace
// the default target namespace is the namespace object defined in the component yaml read from the repository, if any
defaultTargetNamespace, err := inspectTargetNamespace(objs)
if err != nil {
return nil, errors.Wrap(err, "failed to detect default target namespace")
}
// Ensures all the provider components are deployed in the target namespace (apply only to namespaced objects)
// if targetNamespace is not specified, then defaultTargetNamespace is used. In case both targetNamespace and defaultTargetNamespace
// are empty, an error is returned
if input.Options.TargetNamespace == "" {
input.Options.TargetNamespace = defaultTargetNamespace
}
if input.Options.TargetNamespace == "" {
return nil, errors.New("target namespace can't be defaulted. Please specify a target namespace")
}
// add a Namespace object if missing (ensure the targetNamespace will be created)
objs = addNamespaceIfMissing(objs, input.Options.TargetNamespace)
// fix Namespace name in all the objects
objs, err = fixTargetNamespace(objs, input.Options.TargetNamespace)
if err != nil {
return nil, errors.Wrap(err, "failed to set the TargetNamespace on the components")
}
// ensures all the ClusterRole and ClusterRoleBinding have the name prefixed with the namespace name and that
// all the clusterRole/clusterRoleBinding namespaced subjects refers to targetNamespace
// Nb. Making all the RBAC rules "namespaced" is required for supporting multi-tenancy
objs, err = fixRBAC(objs, input.Options.TargetNamespace)
if err != nil {
return nil, errors.Wrap(err, "failed to fix ClusterRoleBinding names")
}
// Add common labels.
objs = addCommonLabels(objs, input.Provider)
return &components{
Provider: input.Provider,
version: input.Options.Version,
variables: variables,
images: images,
targetNamespace: input.Options.TargetNamespace,
objs: objs,
}, nil
}
// inspectTargetNamespace identifies the name of the namespace object contained in the components YAML, if any.
// In case more than one Namespace object is identified, an error is returned.
func inspectTargetNamespace(objs []unstructured.Unstructured) (string, error) {
namespace := ""
for _, o := range objs {
// if the object has Kind Namespace
if o.GetKind() == namespaceKind {
// grab the name (or error if there is more than one Namespace object)
if namespace != "" {
return "", errors.New("Invalid manifest. There should be no more than one resource with Kind Namespace in the provider components yaml")
}
namespace = o.GetName()
}
}
return namespace, nil
}
// addNamespaceIfMissing adda a Namespace object if missing (this ensure the targetNamespace will be created).
func addNamespaceIfMissing(objs []unstructured.Unstructured, targetNamespace string) []unstructured.Unstructured {
namespaceObjectFound := false
for _, o := range objs {
// if the object has Kind Namespace, fix the namespace name
if o.GetKind() == namespaceKind {
namespaceObjectFound = true
}
}
// if there isn't an object with Kind Namespace, add it
if !namespaceObjectFound {
objs = append(objs, unstructured.Unstructured{
Object: map[string]interface{}{
"kind": namespaceKind,
"metadata": map[string]interface{}{
"name": targetNamespace,
},
},
})
}
return objs
}
// fixTargetNamespace ensures all the provider components are deployed in the target namespace (apply only to namespaced objects).
func fixTargetNamespace(objs []unstructured.Unstructured, targetNamespace string) ([]unstructured.Unstructured, error) {
for i := range objs {
o := objs[i]
// if the object has Kind Namespace, fix the namespace name
if o.GetKind() == namespaceKind {
o.SetName(targetNamespace)
}
originalNamespace := o.GetNamespace()
// if the object is namespaced, set the namespace name
if util.IsResourceNamespaced(o.GetKind()) {
o.SetNamespace(targetNamespace)
}
if o.GetKind() == mutatingWebhookConfigurationKind || o.GetKind() == validatingWebhookConfigurationKind || o.GetKind() == customResourceDefinitionKind {
var err error
o, err = fixWebhookNamespaceReferences(o, targetNamespace)
if err != nil {
return nil, err
}
}
if o.GetKind() == certificateKind {
var err error
o, err = fixCertificate(o, originalNamespace, targetNamespace)
if err != nil {
return nil, err
}
}
objs[i] = o
}
return objs, nil
}
func fixWebhookNamespaceReferences(o unstructured.Unstructured, targetNamespace string) (unstructured.Unstructured, error) {
annotations := o.GetAnnotations()
secretNamespacedName, ok := annotations["cert-manager.io/inject-ca-from"]
if ok {
secretNameSplit := strings.Split(secretNamespacedName, "/")
if len(secretNameSplit) != 2 {
return o, fmt.Errorf("object %s %s does not have a correct value for cert-manager.io/inject-ca-from", o.GetKind(), o.GetName())
}
annotations["cert-manager.io/inject-ca-from"] = targetNamespace + "/" + secretNameSplit[1]
o.SetAnnotations(annotations)
}
switch o.GetKind() {
case mutatingWebhookConfigurationKind:
return fixMutatingWebhookNamespaceReferences(o, targetNamespace)
case validatingWebhookConfigurationKind:
return fixValidatingWebhookNamespaceReferences(o, targetNamespace)
case customResourceDefinitionKind:
return fixCRDWebhookNamespaceReference(o, targetNamespace)
}
return o, errors.Errorf("failed to patch %s %s version", o.GroupVersionKind().Version, o.GetKind())
}
func fixMutatingWebhookNamespaceReferences(o unstructured.Unstructured, targetNamespace string) (unstructured.Unstructured, error) {
version := o.GroupVersionKind().Version
switch version {
case admissionregistrationv1beta1.SchemeGroupVersion.Version:
b := &admissionregistrationv1beta1.MutatingWebhookConfiguration{}
if err := scheme.Scheme.Convert(&o, b, nil); err != nil {
return o, err
}
for _, w := range b.Webhooks {
if w.ClientConfig.Service != nil {
w.ClientConfig.Service.Namespace = targetNamespace
}
}
return o, scheme.Scheme.Convert(b, &o, nil)
case admissionregistrationv1.SchemeGroupVersion.Version:
b := &admissionregistrationv1.MutatingWebhookConfiguration{}
if err := scheme.Scheme.Convert(&o, b, nil); err != nil {
return o, err
}
for _, w := range b.Webhooks {
if w.ClientConfig.Service != nil {
w.ClientConfig.Service.Namespace = targetNamespace
}
}
return o, scheme.Scheme.Convert(b, &o, nil)
}
return o, errors.Errorf("failed to patch %s MutatingWebhookConfiguration", version)
}
func fixValidatingWebhookNamespaceReferences(o unstructured.Unstructured, targetNamespace string) (unstructured.Unstructured, error) {
version := o.GroupVersionKind().Version
switch version {
case admissionregistrationv1beta1.SchemeGroupVersion.Version:
b := &admissionregistrationv1beta1.ValidatingWebhookConfiguration{}
if err := scheme.Scheme.Convert(&o, b, nil); err != nil {
return o, err
}
for _, w := range b.Webhooks {
if w.ClientConfig.Service != nil {
w.ClientConfig.Service.Namespace = targetNamespace
}
}
return o, scheme.Scheme.Convert(b, &o, nil)
case admissionregistrationv1.SchemeGroupVersion.Version:
b := &admissionregistrationv1.ValidatingWebhookConfiguration{}
if err := scheme.Scheme.Convert(&o, b, nil); err != nil {
return o, err
}
for _, w := range b.Webhooks {
if w.ClientConfig.Service != nil {
w.ClientConfig.Service.Namespace = targetNamespace
}
}
return o, scheme.Scheme.Convert(b, &o, nil)
}
return o, errors.Errorf("failed to patch %s ValidatingWebhookConfiguration", version)
}
func fixCRDWebhookNamespaceReference(o unstructured.Unstructured, targetNamespace string) (unstructured.Unstructured, error) {
version := o.GroupVersionKind().Version
switch version {
case apiextensionsv1beta1.SchemeGroupVersion.Version:
crd := &apiextensionsv1beta1.CustomResourceDefinition{}
if err := scheme.Scheme.Convert(&o, crd, nil); err != nil {
return o, err
}
if crd.Spec.Conversion != nil && crd.Spec.Conversion.WebhookClientConfig != nil && crd.Spec.Conversion.WebhookClientConfig.Service != nil {
crd.Spec.Conversion.WebhookClientConfig.Service.Namespace = targetNamespace
}
return o, scheme.Scheme.Convert(crd, &o, nil)
case apiextensionsv1.SchemeGroupVersion.Version:
crd := &apiextensionsv1.CustomResourceDefinition{}
if err := scheme.Scheme.Convert(&o, crd, nil); err != nil {
return o, err
}
if crd.Spec.Conversion != nil && crd.Spec.Conversion.Webhook != nil && crd.Spec.Conversion.Webhook.ClientConfig != nil && crd.Spec.Conversion.Webhook.ClientConfig.Service != nil {
crd.Spec.Conversion.Webhook.ClientConfig.Service.Namespace = targetNamespace
}
return o, scheme.Scheme.Convert(crd, &o, nil)
}
return o, errors.Errorf("failed to patch %s CustomResourceDefinition", version)
}
// fixCertificate fixes the dnsNames of cert-manager Certificates. The DNS names contain the dns names of the provider
// services (including the namespace) and thus have to be modified to use the target namespace instead.
func fixCertificate(o unstructured.Unstructured, originalNamespace, targetNamespace string) (unstructured.Unstructured, error) {
dnsNames, ok, err := unstructured.NestedStringSlice(o.UnstructuredContent(), "spec", "dnsNames")
if err != nil {
return o, errors.Wrapf(err, "failed to get .spec.dnsNames from Certificate %s/%s", o.GetNamespace(), o.GetName())
}
// Return if we don't find .spec.dnsNames.
if !ok {
return o, nil
}
// Iterate through dnsNames and adjust the namespace.
// The dnsNames slice usually looks like this:
// - $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc
// - $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc.cluster.local
for i, dnsName := range dnsNames {
dnsNames[i] = strings.Replace(dnsName, fmt.Sprintf(".%s.", originalNamespace), fmt.Sprintf(".%s.", targetNamespace), 1)
}
if err := unstructured.SetNestedStringSlice(o.UnstructuredContent(), dnsNames, "spec", "dnsNames"); err != nil {
return o, errors.Wrapf(err, "failed to set .spec.dnsNames to Certificate %s/%s", o.GetNamespace(), o.GetName())
}
return o, nil
}
// fixRBAC ensures all the ClusterRole and ClusterRoleBinding have the name prefixed with the namespace name and that
// all the clusterRole/clusterRoleBinding namespaced subjects refers to targetNamespace.
func fixRBAC(objs []unstructured.Unstructured, targetNamespace string) ([]unstructured.Unstructured, error) {
renamedClusterRoles := map[string]string{}
for _, o := range objs {
// if the object has Kind ClusterRole
if o.GetKind() == clusterRoleKind {
// assign a namespaced name
currentName := o.GetName()
newName := fmt.Sprintf("%s-%s", targetNamespace, currentName)
o.SetName(newName)
renamedClusterRoles[currentName] = newName
}
}
for i := range objs {
o := objs[i]
switch o.GetKind() {
case clusterRoleBindingKind: // if the object has Kind ClusterRoleBinding
// Convert Unstructured into a typed object
b := &rbacv1.ClusterRoleBinding{}
if err := scheme.Scheme.Convert(&o, b, nil); err != nil {
return nil, err
}
// assign a namespaced name
b.Name = fmt.Sprintf("%s-%s", targetNamespace, b.Name)
// ensure that namespaced subjects refers to targetNamespace
for k := range b.Subjects {
if b.Subjects[k].Namespace != "" {
b.Subjects[k].Namespace = targetNamespace
}
}
// if the referenced ClusterRole was renamed, change the RoleRef
if newName, ok := renamedClusterRoles[b.RoleRef.Name]; ok {
b.RoleRef.Name = newName
}
// Convert ClusterRoleBinding back to Unstructured
if err := scheme.Scheme.Convert(b, &o, nil); err != nil {
return nil, err
}
objs[i] = o
case roleBindingKind: // if the object has Kind RoleBinding
// Convert Unstructured into a typed object
b := &rbacv1.RoleBinding{}
if err := scheme.Scheme.Convert(&o, b, nil); err != nil {
return nil, err
}
// ensure that namespaced subjects refers to targetNamespace
for k := range b.Subjects {
if b.Subjects[k].Namespace != "" {
b.Subjects[k].Namespace = targetNamespace
}
}
// Convert RoleBinding back to Unstructured
if err := scheme.Scheme.Convert(b, &o, nil); err != nil {
return nil, err
}
objs[i] = o
}
}
return objs, nil
}
// addCommonLabels ensures all the provider components have a consistent set of labels.
func addCommonLabels(objs []unstructured.Unstructured, provider config.Provider) []unstructured.Unstructured {
for _, o := range objs {
labels := o.GetLabels()
if labels == nil {
labels = map[string]string{}
}
for k, v := range getCommonLabels(provider) {
labels[k] = v
}
o.SetLabels(labels)
}
return objs
}
func getCommonLabels(provider config.Provider) map[string]string {
return map[string]string{
clusterctlv1.ClusterctlLabelName: "",
clusterv1.ProviderLabelName: provider.ManifestLabel(),
}
}