-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
clusterserviceversion_updaters.go
695 lines (617 loc) · 23.8 KB
/
clusterserviceversion_updaters.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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
// Copyright 2020 The Operator-SDK 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 clusterserviceversion
import (
"encoding/json"
"errors"
"fmt"
"sort"
"strings"
"time"
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/operator-framework/api/pkg/validation"
"github.com/operator-framework/operator-registry/pkg/registry"
log "github.com/sirupsen/logrus"
admissionregv1 "k8s.io/api/admissionregistration/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/operator-framework/operator-sdk/internal/generate/collector"
"github.com/operator-framework/operator-sdk/internal/util/k8sutil"
)
// ApplyTo applies relevant manifests in c to csv, sorts the applied updates,
// and validates the result.
func ApplyTo(c *collector.Manifests, csv *operatorsv1alpha1.ClusterServiceVersion, extraSAs []string) error {
// Apply manifests to the CSV object.
if err := apply(c, csv, extraSAs); err != nil {
return err
}
// Set fields required by namespaced operators. This is a no-op for cluster-scoped operators.
setNamespacedFields(csv)
return validate(csv)
}
// apply applies relevant manifests in c to csv.
func apply(c *collector.Manifests, csv *operatorsv1alpha1.ClusterServiceVersion, extraSAs []string) error {
strategy := getCSVInstallStrategy(csv)
switch strategy.StrategyName {
case operatorsv1alpha1.InstallStrategyNameDeployment:
inPerms, inCPerms, _ := c.SplitCSVPermissionsObjects(extraSAs)
applyRoles(c, inPerms, &strategy.StrategySpec, extraSAs)
applyClusterRoles(c, inCPerms, &strategy.StrategySpec, extraSAs)
applyDeployments(c, &strategy.StrategySpec)
}
csv.Spec.InstallStrategy = strategy
// Update createdAt timestamp annotation since the CSV has been updated.
csv.ObjectMeta.Annotations["createdAt"] = time.Now().UTC().Format(time.RFC3339)
applyCustomResourceDefinitions(c, csv)
if err := applyCustomResources(c, csv); err != nil {
return fmt.Errorf("error applying Custom Resource examples to CSV %s: %v", csv.GetName(), err)
}
applyWebhooks(c, csv)
return nil
}
// Get install strategy from csv.
func getCSVInstallStrategy(csv *operatorsv1alpha1.ClusterServiceVersion) operatorsv1alpha1.NamedInstallStrategy {
// Default to a deployment strategy if none found.
if csv.Spec.InstallStrategy.StrategyName == "" {
csv.Spec.InstallStrategy.StrategyName = operatorsv1alpha1.InstallStrategyNameDeployment
}
return csv.Spec.InstallStrategy
}
// This service account exists in every namespace as the default.
const defaultServiceAccountName = "default"
// applyRoles applies Roles to strategy's permissions field by combining Roles bound to ServiceAccounts
// into one set of permissions.
func applyRoles(c *collector.Manifests, objs []client.Object, strategy *operatorsv1alpha1.StrategyDetailsDeployment, extraSAs []string) { //nolint:dupl
roleSet := make(map[string]rbacv1.Role)
cRoleSet := make(map[string]rbacv1.ClusterRole)
for i := range objs {
switch t := objs[i].(type) {
case *rbacv1.Role:
roleSet[t.GetName()] = *t
case *rbacv1.ClusterRole:
cRoleSet[t.GetName()] = *t
}
}
// Collect all role and cluster role names by their corresponding service accounts via bindings. This lets us
// look up all service accounts a role is bound to and create one set of permissions per service account.
saToPermissions := initPermissionSet(c.Deployments, extraSAs)
for _, binding := range c.RoleBindings {
for _, subject := range binding.Subjects {
perm, hasSA := saToPermissions[subject.Name]
if subject.Kind != "ServiceAccount" || !hasSA {
continue
}
var (
rules []rbacv1.PolicyRule
hasRole bool
)
switch binding.RoleRef.Kind {
case "Role":
role, has := roleSet[binding.RoleRef.Name]
rules = role.Rules
hasRole = has
case "ClusterRole":
role, has := cRoleSet[binding.RoleRef.Name]
rules = role.Rules
hasRole = has
default:
continue
}
if hasRole {
perm.Rules = append(perm.Rules, rules...)
saToPermissions[subject.Name] = perm
}
}
}
// Apply relevant roles to each service account.
perms := []operatorsv1alpha1.StrategyDeploymentPermissions{}
for _, perm := range saToPermissions {
if len(perm.Rules) != 0 {
perms = append(perms, perm)
}
}
sort.Slice(perms, func(i, j int) bool {
return perms[i].ServiceAccountName < perms[j].ServiceAccountName
})
strategy.Permissions = perms
}
// applyClusterRoles applies ClusterRoles to strategy's clusterPermissions field by combining ClusterRoles
// bound to ServiceAccounts into one set of clusterPermissions.
func applyClusterRoles(c *collector.Manifests, objs []client.Object, strategy *operatorsv1alpha1.StrategyDetailsDeployment, extraSAs []string) { //nolint:dupl
roleSet := make(map[string]rbacv1.ClusterRole)
for i := range objs {
switch t := objs[i].(type) {
case *rbacv1.ClusterRole:
roleSet[t.GetName()] = *t
}
}
// Collect all role names by their corresponding service accounts via bindings. This lets us
// look up all service accounts a role is bound to and create one set of permissions per service account.
saToPermissions := initPermissionSet(c.Deployments, extraSAs)
for _, binding := range c.ClusterRoleBindings {
for _, subject := range binding.Subjects {
perm, hasSA := saToPermissions[subject.Name]
if !hasSA || subject.Kind != "ServiceAccount" {
continue
}
if role, hasRole := roleSet[binding.RoleRef.Name]; hasRole {
perm.Rules = append(perm.Rules, role.Rules...)
saToPermissions[subject.Name] = perm
}
}
}
// Apply relevant roles to each service account.
perms := []operatorsv1alpha1.StrategyDeploymentPermissions{}
for _, perm := range saToPermissions {
if len(perm.Rules) != 0 {
perms = append(perms, perm)
}
}
sort.Slice(perms, func(i, j int) bool {
return perms[i].ServiceAccountName < perms[j].ServiceAccountName
})
strategy.ClusterPermissions = perms
}
// initPermissionSet initializes a map of ServiceAccount name to permissions, which are empty.
func initPermissionSet(deps []appsv1.Deployment, extraSAs []string) map[string]operatorsv1alpha1.StrategyDeploymentPermissions {
saToPermissions := make(map[string]operatorsv1alpha1.StrategyDeploymentPermissions)
for _, dep := range deps {
saName := dep.Spec.Template.Spec.ServiceAccountName
if saName == "" {
saName = defaultServiceAccountName
}
saToPermissions[saName] = operatorsv1alpha1.StrategyDeploymentPermissions{ServiceAccountName: saName}
}
for _, extraSA := range extraSAs {
saToPermissions[extraSA] = operatorsv1alpha1.StrategyDeploymentPermissions{ServiceAccountName: extraSA}
}
return saToPermissions
}
// applyDeployments updates strategy's deployments with the Deployments
// in the collector.
func applyDeployments(c *collector.Manifests, strategy *operatorsv1alpha1.StrategyDetailsDeployment) {
depSpecs := []operatorsv1alpha1.StrategyDeploymentSpec{}
for _, dep := range c.Deployments {
depSpecs = append(depSpecs, operatorsv1alpha1.StrategyDeploymentSpec{
Name: dep.GetName(),
Spec: dep.Spec,
Label: dep.Labels,
})
}
strategy.DeploymentSpecs = depSpecs
}
const (
// WatchNamespaceEnv is a constant for internal use.
WatchNamespaceEnv = "WATCH_NAMESPACE"
// TargetNamespacesRef references the target namespaces a CSV is installed in.
// This is required by legacy project Deployments.
TargetNamespacesRef = "metadata.annotations['olm.targetNamespaces']"
)
// setNamespacedFields sets static fields in a CSV required by namespaced
// operators.
func setNamespacedFields(csv *operatorsv1alpha1.ClusterServiceVersion) {
for _, dep := range csv.Spec.InstallStrategy.StrategySpec.DeploymentSpecs {
// Set WATCH_NAMESPACE if it exists in a deployment spec..
envVar := newFieldRefEnvVar(WatchNamespaceEnv, TargetNamespacesRef)
setContainerEnvVarIfExists(&dep.Spec, envVar)
}
}
// setContainerEnvVarIfExists overwrites all references to ev.Name with ev.
func setContainerEnvVarIfExists(spec *appsv1.DeploymentSpec, ev corev1.EnvVar) {
for _, c := range spec.Template.Spec.Containers {
for i := 0; i < len(c.Env); i++ {
if c.Env[i].Name == ev.Name {
c.Env[i] = ev
}
}
}
}
// newFieldRefEnvVar creates a new environment variable referencing fieldPath.
func newFieldRefEnvVar(name, fieldPath string) corev1.EnvVar {
return corev1.EnvVar{
Name: name,
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: fieldPath,
},
},
}
}
// applyCustomResourceDefinitions updates csv's customresourcedefinitions.owned
// with CustomResourceDefinitions in the collector.
// customresourcedefinitions.required are left as-is, since they are
// manually-defined values.
func applyCustomResourceDefinitions(c *collector.Manifests, csv *operatorsv1alpha1.ClusterServiceVersion) {
ownedDescs := []operatorsv1alpha1.CRDDescription{}
descMap := map[registry.DefinitionKey]operatorsv1alpha1.CRDDescription{}
for _, owned := range csv.Spec.CustomResourceDefinitions.Owned {
defKey := registry.DefinitionKey{
Name: owned.Name,
Version: owned.Version,
Kind: owned.Kind,
}
descMap[defKey] = owned
}
var defKeys []registry.DefinitionKey
v1crdKeys := k8sutil.DefinitionsForV1CustomResourceDefinitions(c.V1CustomResourceDefinitions...)
defKeys = append(defKeys, v1crdKeys...)
v1beta1crdKeys := k8sutil.DefinitionsForV1beta1CustomResourceDefinitions(c.V1beta1CustomResourceDefinitions...)
defKeys = append(defKeys, v1beta1crdKeys...)
// crdDescriptions don't have a 'group' field.
for i := 0; i < len(defKeys); i++ {
defKeys[i].Group = ""
}
for _, defKey := range defKeys {
if owned, ownedExists := descMap[defKey]; ownedExists {
ownedDescs = append(ownedDescs, owned)
} else {
ownedDescs = append(ownedDescs, operatorsv1alpha1.CRDDescription{
Name: defKey.Name,
Version: defKey.Version,
Kind: defKey.Kind,
})
}
}
csv.Spec.CustomResourceDefinitions.Owned = ownedDescs
}
// applyWebhooks updates csv's webhookDefinitions with any mutating and validating webhooks in the collector.
func applyWebhooks(c *collector.Manifests, csv *operatorsv1alpha1.ClusterServiceVersion) {
webhookDescriptions := []operatorsv1alpha1.WebhookDescription{}
for _, webhook := range c.ValidatingWebhooks {
var validatingServiceName string
depName, svc := findMatchingDeploymentAndServiceForWebhook(c, webhook.ClientConfig)
if svc != nil {
validatingServiceName = svc.GetName()
}
if validatingServiceName == "" && depName == "" {
log.Infof("No service found for validating webhook %q", webhook.Name)
} else if depName == "" {
log.Infof("No deployment is selected by service %q for validating webhook %q", validatingServiceName, webhook.Name)
}
webhookDescriptions = append(webhookDescriptions, validatingToWebhookDescription(webhook, depName, svc))
}
for _, webhook := range c.MutatingWebhooks {
var mutatingServiceName string
depName, svc := findMatchingDeploymentAndServiceForWebhook(c, webhook.ClientConfig)
if svc != nil {
mutatingServiceName = svc.GetName()
}
if mutatingServiceName == "" && depName == "" {
log.Infof("No service found for mutating webhook %q", webhook.Name)
} else if depName == "" {
log.Infof("No deployment is selected by service %q for mutating webhook %q", mutatingServiceName, webhook.Name)
}
webhookDescriptions = append(webhookDescriptions, mutatingToWebhookDescription(webhook, depName, svc))
}
for _, svc := range c.Services {
crdToConfigMap := getConvWebhookCRDNamesAndConfig(c, svc.GetName())
if len(crdToConfigMap) != 0 {
depName := findMatchingDepNameFromService(c, &svc)
des := conversionToWebhookDescription(crdToConfigMap, depName, &svc)
webhookDescriptions = append(webhookDescriptions, des...)
}
}
// Sorts the WebhookDescriptions based on natural order of webhookDescriptions Type
sort.Slice(webhookDescriptions, func(i, j int) bool {
return webhookDescriptions[i].GenerateName < webhookDescriptions[j].GenerateName
})
csv.Spec.WebhookDefinitions = webhookDescriptions
}
// conversionToWebhookDescription takes in a map of {crdNames, apiextv.WebhookConversion} and groups
// all the crds with same port and path. It then creates a webhook description for each unique combination of
// port and path.
// For example: if we have the following map: {crd1:[portX+pathX], crd2: [portX+pathX], crd3: [portY:partY]},
// we will create 2 webhook descriptions: one with [portX+pathX]:[crd1, crd2] and the other with [portY:pathY]:[crd3]
func conversionToWebhookDescription(crdToConfig map[string]apiextv1.WebhookConversion, depName string, ws *corev1.Service) []operatorsv1alpha1.WebhookDescription {
des := make([]operatorsv1alpha1.WebhookDescription, 0)
// this is a map of serviceportAndPath configs, and the respective CRDs.
webhookDescriptions := crdGroups(crdToConfig)
for serviceConfig, crds := range webhookDescriptions {
// we need this to get the conversionReviewVersions.
// here, we assume all crds having same servicePortAndPath config will have
// same conversion review versions.
config, ok := crdToConfig[crds[0]]
if !ok {
log.Infof("Webhook config for CRD %q not found", crds[0])
continue
}
description := operatorsv1alpha1.WebhookDescription{
Type: operatorsv1alpha1.ConversionWebhook,
ConversionCRDs: crds,
AdmissionReviewVersions: config.ConversionReviewVersions,
WebhookPath: &serviceConfig.Path,
DeploymentName: depName,
GenerateName: getGenerateName(crds),
SideEffects: func() *admissionregv1.SideEffectClass {
seNone := admissionregv1.SideEffectClassNone
return &seNone
}(),
}
if len(description.AdmissionReviewVersions) == 0 {
log.Infof("ConversionReviewVersion not found for the deployment %q", depName)
}
var webhookServiceRefPort int32 = 443
if serviceConfig.Port != nil {
webhookServiceRefPort = *serviceConfig.Port
}
if ws != nil {
for _, port := range ws.Spec.Ports {
if webhookServiceRefPort == port.Port {
description.ContainerPort = port.Port
description.TargetPort = &port.TargetPort
break
}
}
}
if description.DeploymentName == "" {
if config.ClientConfig.Service != nil {
description.DeploymentName = strings.TrimSuffix(config.ClientConfig.Service.Name, "-service")
}
}
description.WebhookPath = &serviceConfig.Path
des = append(des, description)
}
return des
}
// serviceportPath is refers to the group of webhook service and
// path names and port.
type serviceportPath struct {
Port *int32
Path string
}
// crdGroups groups the crds with similar service port and name. It returns a map of serviceportPath
// and the corresponding crd names.
func crdGroups(crdToConfig map[string]apiextv1.WebhookConversion) map[serviceportPath][]string {
uniqueConfig := make(map[serviceportPath][]string)
for crdName, config := range crdToConfig {
serviceportPath := serviceportPath{
Port: config.ClientConfig.Service.Port,
Path: *config.ClientConfig.Service.Path,
}
uniqueConfig[serviceportPath] = append(uniqueConfig[serviceportPath], crdName)
}
return uniqueConfig
}
func getConvWebhookCRDNamesAndConfig(c *collector.Manifests, serviceName string) map[string]apiextv1.WebhookConversion {
if serviceName == "" {
return nil
}
crdToConfig := make(map[string]apiextv1.WebhookConversion)
for _, crd := range c.V1CustomResourceDefinitions {
if crd.Spec.Conversion != nil {
whConv := crd.Spec.Conversion.Webhook
if whConv != nil && whConv.ClientConfig != nil && whConv.ClientConfig.Service != nil {
if whConv.ClientConfig.Service.Name == serviceName {
crdToConfig[crd.GetName()] = *whConv
}
}
}
}
for _, crd := range c.V1beta1CustomResourceDefinitions {
whConv := crd.Spec.Conversion
if whConv != nil && whConv.WebhookClientConfig != nil && whConv.WebhookClientConfig.Service != nil {
if whConv.WebhookClientConfig.Service.Name == serviceName {
v1whConv := apiextv1.WebhookConversion{
ClientConfig: &apiextv1.WebhookClientConfig{Service: &apiextv1.ServiceReference{}},
ConversionReviewVersions: crd.Spec.Conversion.ConversionReviewVersions,
}
if path := whConv.WebhookClientConfig.Service.Path; path != nil {
v1whConv.ClientConfig.Service.Path = new(string)
*v1whConv.ClientConfig.Service.Path = *path
}
crdToConfig[crd.GetName()] = v1whConv
}
}
}
return crdToConfig
}
// The default AdmissionReviewVersions set in a CSV if not set in the source webhook.
var defaultAdmissionReviewVersions = []string{"v1beta1"}
// validatingToWebhookDescription transforms webhook into a WebhookDescription.
func validatingToWebhookDescription(webhook admissionregv1.ValidatingWebhook, depName string, ws *corev1.Service) operatorsv1alpha1.WebhookDescription {
description := operatorsv1alpha1.WebhookDescription{
Type: operatorsv1alpha1.ValidatingAdmissionWebhook,
GenerateName: webhook.Name,
Rules: webhook.Rules,
FailurePolicy: webhook.FailurePolicy,
MatchPolicy: webhook.MatchPolicy,
ObjectSelector: webhook.ObjectSelector,
SideEffects: webhook.SideEffects,
TimeoutSeconds: webhook.TimeoutSeconds,
AdmissionReviewVersions: webhook.AdmissionReviewVersions,
}
if len(description.AdmissionReviewVersions) == 0 {
description.AdmissionReviewVersions = defaultAdmissionReviewVersions
}
if description.SideEffects == nil {
seNone := admissionregv1.SideEffectClassNone
description.SideEffects = &seNone
}
if serviceRef := webhook.ClientConfig.Service; serviceRef != nil {
var webhookServiceRefPort int32 = 443
if serviceRef.Port != nil {
webhookServiceRefPort = *serviceRef.Port
}
if ws != nil {
for _, port := range ws.Spec.Ports {
if webhookServiceRefPort == port.Port {
description.ContainerPort = port.Port
description.TargetPort = &port.TargetPort
break
}
}
}
description.DeploymentName = depName
if description.DeploymentName == "" {
description.DeploymentName = strings.TrimSuffix(serviceRef.Name, "-service")
}
description.WebhookPath = serviceRef.Path
}
return description
}
// mutatingToWebhookDescription transforms webhook into a WebhookDescription.
func mutatingToWebhookDescription(webhook admissionregv1.MutatingWebhook, depName string, ws *corev1.Service) operatorsv1alpha1.WebhookDescription {
description := operatorsv1alpha1.WebhookDescription{
Type: operatorsv1alpha1.MutatingAdmissionWebhook,
GenerateName: webhook.Name,
Rules: webhook.Rules,
FailurePolicy: webhook.FailurePolicy,
MatchPolicy: webhook.MatchPolicy,
ObjectSelector: webhook.ObjectSelector,
SideEffects: webhook.SideEffects,
TimeoutSeconds: webhook.TimeoutSeconds,
AdmissionReviewVersions: webhook.AdmissionReviewVersions,
ReinvocationPolicy: webhook.ReinvocationPolicy,
}
if len(description.AdmissionReviewVersions) == 0 {
description.AdmissionReviewVersions = defaultAdmissionReviewVersions
}
if description.SideEffects == nil {
seNone := admissionregv1.SideEffectClassNone
description.SideEffects = &seNone
}
if serviceRef := webhook.ClientConfig.Service; serviceRef != nil {
var webhookServiceRefPort int32 = 443
if serviceRef.Port != nil {
webhookServiceRefPort = *serviceRef.Port
}
if ws != nil {
for _, port := range ws.Spec.Ports {
if webhookServiceRefPort == port.Port {
description.ContainerPort = port.Port
description.TargetPort = &port.TargetPort
break
}
}
}
description.DeploymentName = depName
if description.DeploymentName == "" {
description.DeploymentName = strings.TrimSuffix(serviceRef.Name, "-service")
}
description.WebhookPath = serviceRef.Path
}
return description
}
// findMatchingDeploymentAndServiceForWebhook matches a Service to a webhook's client config (if it uses a service)
// and uses that service to find the deployment name.
func findMatchingDeploymentAndServiceForWebhook(c *collector.Manifests, wcc admissionregv1.WebhookClientConfig) (depName string, ws *corev1.Service) {
// Return if a service reference is not specified, since a URL will be in that case.
if wcc.Service == nil {
return depName, ws
}
// Find the matching service, if any. The webhook server may be externally managed
// if no service is created by the operator.
for i, service := range c.Services {
if service.GetName() == wcc.Service.Name {
ws = &c.Services[i]
break
}
}
if ws == nil {
return depName, ws
}
// Only ExternalName-type services cannot have selectors.
if ws.Spec.Type == corev1.ServiceTypeExternalName {
return depName, ws
}
// If a selector does not exist, there is either an Endpoint or EndpointSlice object accompanying
// the service so it should not be added to the CSV.
if len(ws.Spec.Selector) == 0 {
return depName, ws
}
depName = findMatchingDepNameFromService(c, ws)
return depName, ws
}
// findMatchingDepNameFromService matches the provided service to a deployment by comparing label selectors (if
// Service uses label selectors).
func findMatchingDepNameFromService(c *collector.Manifests, ws *corev1.Service) (depName string) {
// Match service against pod labels, in which the webhook server will be running
for _, dep := range c.Deployments {
podTemplateLabels := dep.Spec.Template.GetLabels()
if len(podTemplateLabels) == 0 {
continue
}
depName = dep.GetName()
// Check that all labels match.
for key, serviceValue := range ws.Spec.Selector {
if podTemplateValue, hasKey := podTemplateLabels[key]; !hasKey || podTemplateValue != serviceValue {
depName = ""
break
}
}
if depName != "" {
break
}
}
return depName
}
// applyCustomResources updates csv's "alm-examples" annotation with the
// Custom Resources in the collector.
func applyCustomResources(c *collector.Manifests, csv *operatorsv1alpha1.ClusterServiceVersion) error {
examples := []json.RawMessage{}
for _, cr := range c.CustomResources {
crBytes, err := cr.MarshalJSON()
if err != nil {
return err
}
examples = append(examples, json.RawMessage(crBytes))
}
examplesJSON, err := json.MarshalIndent(examples, "", " ")
if err != nil {
return err
}
if csv.GetAnnotations() == nil {
csv.SetAnnotations(make(map[string]string))
}
csv.GetAnnotations()["alm-examples"] = string(examplesJSON)
return nil
}
// validate will validate csv using the api validation library.
// More info: https://github.com/operator-framework/api
func validate(csv *operatorsv1alpha1.ClusterServiceVersion) error {
if csv == nil {
return errors.New("empty ClusterServiceVersion")
}
hasErrors := false
results := validation.ClusterServiceVersionValidator.Validate(csv)
for _, r := range results {
for _, w := range r.Warnings {
log.Warnf("ClusterServiceVersion validation: [%s] %s", w.Type, w.Detail)
}
for _, e := range r.Errors {
log.Errorf("ClusterServiceVersion validation: [%s] %s", e.Type, e.Detail)
}
if r.HasError() {
hasErrors = true
}
}
if hasErrors {
return errors.New("invalid generated ClusterServiceVersion")
}
return nil
}
// generateName takes in a list of crds, and returns a conversion webhook generator name.
func getGenerateName(crds []string) string {
sort.Strings(crds)
joinedResourceNames := strings.Builder{}
for _, name := range crds {
if name != "" {
joinedResourceNames.WriteString(strings.Split(name, ".")[0])
}
}
return fmt.Sprintf("c%s.kb.io", joinedResourceNames.String())
}