• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kubevirt / hyperconverged-cluster-operator / 15999562862

01 Jul 2025 12:39PM UTC coverage: 75.441% (-0.2%) from 75.599%
15999562862

Pull #3602

github

web-flow
Merge 82f0113dd into c90e45477
Pull Request #3602: Add E2E test for multi-arch

246 of 351 new or added lines in 8 files covered. (70.09%)

6 existing lines in 3 files now uncovered.

6586 of 8730 relevant lines covered (75.44%)

1.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.69
/pkg/webhooks/validator/validator.go
1
package validator
2

3
import (
4
        "context"
5
        "errors"
6
        "fmt"
7
        "net/http"
8
        "reflect"
9
        "strings"
10
        "time"
11

12
        "github.com/go-logr/logr"
13
        openshiftconfigv1 "github.com/openshift/api/config/v1"
14
        "github.com/samber/lo"
15
        xsync "golang.org/x/sync/errgroup"
16
        admissionv1 "k8s.io/api/admission/v1"
17
        corev1 "k8s.io/api/core/v1"
18
        apierrors "k8s.io/apimachinery/pkg/api/errors"
19
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20
        "k8s.io/component-helpers/scheduling/corev1/nodeaffinity"
21
        "k8s.io/utils/strings/slices"
22
        "sigs.k8s.io/controller-runtime/pkg/client"
23
        "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
24

25
        networkaddonsv1 "github.com/kubevirt/cluster-network-addons-operator/pkg/apis/networkaddonsoperator/v1"
26
        kubevirtcorev1 "kubevirt.io/api/core/v1"
27
        cdiv1beta1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
28
        sspv1beta3 "kubevirt.io/ssp-operator/api/v1beta3"
29

30
        "github.com/kubevirt/hyperconverged-cluster-operator/api/v1beta1"
31
        "github.com/kubevirt/hyperconverged-cluster-operator/controllers/operands"
32
        "github.com/kubevirt/hyperconverged-cluster-operator/pkg/nodeinfo"
33
        hcoutil "github.com/kubevirt/hyperconverged-cluster-operator/pkg/util"
34
)
35

36
const (
37
        updateDryRunTimeOut = time.Second * 3
38
)
39

40
type ValidationWarning struct {
41
        warnings []string
42
}
43

44
func newValidationWarning(warnings []string) *ValidationWarning {
1✔
45
        return &ValidationWarning{
1✔
46
                warnings: warnings,
1✔
47
        }
1✔
48
}
1✔
49

50
func (v *ValidationWarning) Error() string {
×
51
        return ""
×
52
}
×
53

54
func (v *ValidationWarning) Warnings() []string {
×
55
        return v.warnings
×
56
}
×
57

58
type WebhookHandler struct {
59
        logger      logr.Logger
60
        cli         client.Client
61
        namespace   string
62
        isOpenshift bool
63
        decoder     admission.Decoder
64
}
65

66
var hcoTLSConfigCache *openshiftconfigv1.TLSSecurityProfile
67

68
func NewWebhookHandler(logger logr.Logger, cli client.Client, decoder admission.Decoder, namespace string, isOpenshift bool, hcoTLSSecurityProfile *openshiftconfigv1.TLSSecurityProfile) *WebhookHandler {
2✔
69
        hcoTLSConfigCache = hcoTLSSecurityProfile
2✔
70
        return &WebhookHandler{
2✔
71
                logger:      logger,
2✔
72
                cli:         cli,
2✔
73
                namespace:   namespace,
2✔
74
                isOpenshift: isOpenshift,
2✔
75
                decoder:     decoder,
2✔
76
        }
2✔
77
}
2✔
78

79
func (wh *WebhookHandler) Handle(ctx context.Context, req admission.Request) admission.Response {
1✔
80

1✔
81
        ctx = admission.NewContextWithRequest(ctx, req)
1✔
82

1✔
83
        // Get the object in the request
1✔
84
        obj := &v1beta1.HyperConverged{}
1✔
85

1✔
86
        dryRun := req.DryRun != nil && *req.DryRun
1✔
87

1✔
88
        var err error
1✔
89
        switch req.Operation {
1✔
90
        case admissionv1.Create:
1✔
91
                if err := wh.decoder.Decode(req, obj); err != nil {
2✔
92
                        return admission.Errored(http.StatusBadRequest, err)
1✔
93
                }
1✔
94

95
                err = wh.ValidateCreate(ctx, dryRun, obj)
1✔
96
        case admissionv1.Update:
1✔
97
                oldObj := &v1beta1.HyperConverged{}
1✔
98
                if err := wh.decoder.DecodeRaw(req.Object, obj); err != nil {
2✔
99
                        return admission.Errored(http.StatusBadRequest, err)
1✔
100
                }
1✔
101
                if err := wh.decoder.DecodeRaw(req.OldObject, oldObj); err != nil {
2✔
102
                        return admission.Errored(http.StatusBadRequest, err)
1✔
103
                }
1✔
104

105
                err = wh.ValidateUpdate(ctx, dryRun, obj, oldObj)
1✔
106
        case admissionv1.Delete:
1✔
107
                // In reference to PR: https://github.com/kubernetes/kubernetes/pull/76346
1✔
108
                // OldObject contains the object being deleted
1✔
109
                if err := wh.decoder.DecodeRaw(req.OldObject, obj); err != nil {
2✔
110
                        return admission.Errored(http.StatusBadRequest, err)
1✔
111
                }
1✔
112

113
                err = wh.ValidateDelete(ctx, dryRun, obj)
1✔
114
        default:
1✔
115
                return admission.Errored(http.StatusBadRequest, fmt.Errorf("unknown operation request %q", req.Operation))
1✔
116
        }
117

118
        // Check the error message first.
119
        if err != nil {
2✔
120
                var apiStatus apierrors.APIStatus
1✔
121
                if errors.As(err, &apiStatus) {
1✔
122
                        return validationResponseFromStatus(false, apiStatus.Status())
×
123
                }
×
124

125
                var vw *ValidationWarning
1✔
126
                if errors.As(err, &vw) {
1✔
127
                        return admission.Allowed("").WithWarnings(vw.Warnings()...)
×
128
                }
×
129

130
                return admission.Denied(err.Error())
1✔
131
        }
132

133
        // Return allowed if everything succeeded.
134
        return admission.Allowed("")
1✔
135
}
136

137
func (wh *WebhookHandler) ValidateCreate(_ context.Context, dryrun bool, hc *v1beta1.HyperConverged) error {
1✔
138
        wh.logger.Info("Validating create", "name", hc.Name, "namespace:", hc.Namespace)
1✔
139

1✔
140
        if err := wh.validateCertConfig(hc); err != nil {
1✔
141
                return err
×
142
        }
×
143

144
        if hc.Namespace != wh.namespace {
2✔
145
                return fmt.Errorf("invalid namespace for v1beta1.HyperConverged - please use the %s namespace", wh.namespace)
1✔
146
        }
1✔
147

148
        if err := wh.validateDataImportCronTemplates(hc); err != nil {
2✔
149
                return err
1✔
150
        }
1✔
151

152
        if err := wh.validateTLSSecurityProfiles(hc); err != nil {
2✔
153
                return err
1✔
154
        }
1✔
155

156
        if err := wh.validateMediatedDeviceTypes(hc); err != nil {
2✔
157
                return err
1✔
158
        }
1✔
159

160
        if err := wh.validateFeatureGatesOnCreate(hc); err != nil {
2✔
161
                return err
1✔
162
        }
1✔
163

164
        if err := wh.validateAffinity(hc); err != nil {
2✔
165
                return err
1✔
166
        }
1✔
167

168
        if _, err := operands.NewKubeVirt(hc); err != nil {
2✔
169
                return err
1✔
170
        }
1✔
171

172
        if _, err := operands.NewCDI(hc); err != nil {
2✔
173
                return err
1✔
174
        }
1✔
175

176
        if _, err := operands.NewNetworkAddons(hc); err != nil {
2✔
177
                return err
1✔
178
        }
1✔
179

180
        if _, _, err := operands.NewSSP(hc); err != nil {
2✔
181
                return err
1✔
182
        }
1✔
183

184
        if !dryrun {
2✔
185
                hcoTLSConfigCache = hc.Spec.TLSSecurityProfile
1✔
186
        }
1✔
187

188
        return nil
1✔
189
}
190

191
func (wh *WebhookHandler) getOperands(requested *v1beta1.HyperConverged) (*kubevirtcorev1.KubeVirt, *cdiv1beta1.CDI, *networkaddonsv1.NetworkAddonsConfig, error) {
1✔
192
        if err := wh.validateCertConfig(requested); err != nil {
2✔
193
                return nil, nil, nil, err
1✔
194
        }
1✔
195

196
        kv, err := operands.NewKubeVirt(requested)
1✔
197
        if err != nil {
2✔
198
                return nil, nil, nil, err
1✔
199
        }
1✔
200

201
        cdi, err := operands.NewCDI(requested)
1✔
202
        if err != nil {
2✔
203
                return nil, nil, nil, err
1✔
204
        }
1✔
205

206
        cna, err := operands.NewNetworkAddons(requested)
1✔
207
        if err != nil {
2✔
208
                return nil, nil, nil, err
1✔
209
        }
1✔
210

211
        return kv, cdi, cna, nil
1✔
212
}
213

214
// ValidateUpdate is the ValidateUpdate webhook implementation. It calls all the resources in parallel, to dry-run the
215
// upgrade.
216
func (wh *WebhookHandler) ValidateUpdate(ctx context.Context, dryrun bool, requested *v1beta1.HyperConverged, exists *v1beta1.HyperConverged) error {
1✔
217
        wh.logger.Info("Validating update", "name", requested.Name)
1✔
218

1✔
219
        if err := wh.validateDataImportCronTemplates(requested); err != nil {
1✔
220
                return err
×
221
        }
×
222

223
        if err := wh.validateTLSSecurityProfiles(requested); err != nil {
2✔
224
                return err
1✔
225
        }
1✔
226

227
        if err := wh.validateMediatedDeviceTypes(requested); err != nil {
2✔
228
                return err
1✔
229
        }
1✔
230

231
        if err := wh.validateFeatureGatesOnUpdate(requested, exists); err != nil {
2✔
232
                return err
1✔
233
        }
1✔
234

235
        if err := wh.validateAffinity(requested); err != nil {
1✔
NEW
236
                return err
×
NEW
237
        }
×
238

239
        // If no change is detected in the spec nor the annotations - nothing to validate
240
        if reflect.DeepEqual(exists.Spec, requested.Spec) &&
1✔
241
                reflect.DeepEqual(exists.Annotations, requested.Annotations) {
2✔
242
                return nil
1✔
243
        }
1✔
244

245
        kv, cdi, cna, err := wh.getOperands(requested)
1✔
246
        if err != nil {
2✔
247
                return err
1✔
248
        }
1✔
249

250
        toCtx, cancel := context.WithTimeout(ctx, updateDryRunTimeOut)
1✔
251
        defer cancel()
1✔
252

1✔
253
        eg, egCtx := xsync.WithContext(toCtx)
1✔
254
        opts := &client.UpdateOptions{DryRun: []string{metav1.DryRunAll}}
1✔
255

1✔
256
        resources := []client.Object{
1✔
257
                kv,
1✔
258
                cdi,
1✔
259
                cna,
1✔
260
        }
1✔
261

1✔
262
        if wh.isOpenshift {
2✔
263
                origGetControlPlaneArchitectures := nodeinfo.GetControlPlaneArchitectures
1✔
264
                origGetWorkloadsArchitectures := nodeinfo.GetWorkloadsArchitectures
1✔
265
                defer func() {
2✔
266
                        nodeinfo.GetControlPlaneArchitectures = origGetControlPlaneArchitectures
1✔
267
                        nodeinfo.GetWorkloadsArchitectures = origGetWorkloadsArchitectures
1✔
268
                }()
1✔
269

270
                nodeinfo.GetControlPlaneArchitectures = func() []string {
1✔
NEW
271
                        return requested.Status.NodeInfo.ControlPlaneNodesArchitecture
×
NEW
272
                }
×
273
                nodeinfo.GetWorkloadsArchitectures = func() []string {
1✔
NEW
274
                        return requested.Status.NodeInfo.WorkloadsArchitectures
×
NEW
275
                }
×
276

277
                ssp, _, err := operands.NewSSP(requested)
1✔
278
                if err != nil {
2✔
279
                        return err
1✔
280
                }
1✔
281
                resources = append(resources, ssp)
1✔
282
        }
283

284
        for _, obj := range resources {
2✔
285
                func(o client.Object) {
2✔
286
                        eg.Go(func() error {
2✔
287
                                return wh.updateOperatorCr(egCtx, requested, o, opts)
1✔
288
                        })
1✔
289
                }(obj)
290
        }
291

292
        err = eg.Wait()
1✔
293
        if err != nil {
2✔
294
                return err
1✔
295
        }
1✔
296

297
        if !dryrun {
2✔
298
                hcoTLSConfigCache = requested.Spec.TLSSecurityProfile
1✔
299
        }
1✔
300

301
        return nil
1✔
302
}
303

304
func (wh *WebhookHandler) updateOperatorCr(ctx context.Context, hc *v1beta1.HyperConverged, exists client.Object, opts *client.UpdateOptions) error {
1✔
305
        err := hcoutil.GetRuntimeObject(ctx, wh.cli, exists)
1✔
306
        if err != nil {
2✔
307
                wh.logger.Error(err, "failed to get object from kubernetes", "kind", exists.GetObjectKind())
1✔
308
                return err
1✔
309
        }
1✔
310

311
        switch existing := exists.(type) {
1✔
312
        case *kubevirtcorev1.KubeVirt:
1✔
313
                required, err := operands.NewKubeVirt(hc)
1✔
314
                if err != nil {
1✔
315
                        return err
×
316
                }
×
317
                required.Spec.DeepCopyInto(&existing.Spec)
1✔
318

319
        case *cdiv1beta1.CDI:
1✔
320
                required, err := operands.NewCDI(hc)
1✔
321
                if err != nil {
1✔
322
                        return err
×
323
                }
×
324
                required.Spec.DeepCopyInto(&existing.Spec)
1✔
325

326
        case *networkaddonsv1.NetworkAddonsConfig:
1✔
327
                required, err := operands.NewNetworkAddons(hc)
1✔
328
                if err != nil {
1✔
329
                        return err
×
330
                }
×
331
                required.Spec.DeepCopyInto(&existing.Spec)
1✔
332

333
        case *sspv1beta3.SSP:
1✔
334
                required, _, err := operands.NewSSP(hc)
1✔
335
                if err != nil {
1✔
336
                        return err
×
337
                }
×
338
                required.Spec.DeepCopyInto(&existing.Spec)
1✔
339
        }
340

341
        if err = wh.cli.Update(ctx, exists, opts); err != nil {
2✔
342
                wh.logger.Error(err, "failed to dry-run update the object", "kind", exists.GetObjectKind())
1✔
343
                return err
1✔
344
        }
1✔
345

346
        wh.logger.Info("dry-run update the object passed", "kind", exists.GetObjectKind())
1✔
347
        return nil
1✔
348
}
349

350
func (wh *WebhookHandler) ValidateDelete(ctx context.Context, dryrun bool, hc *v1beta1.HyperConverged) error {
1✔
351
        wh.logger.Info("Validating delete", "name", hc.Name, "namespace", hc.Namespace)
1✔
352

1✔
353
        kv := operands.NewKubeVirtWithNameOnly(hc)
1✔
354
        cdi := operands.NewCDIWithNameOnly(hc)
1✔
355

1✔
356
        for _, obj := range []client.Object{
1✔
357
                kv,
1✔
358
                cdi,
1✔
359
        } {
2✔
360
                _, err := hcoutil.EnsureDeleted(ctx, wh.cli, obj, hc.Name, wh.logger, true, false, true)
1✔
361
                if err != nil {
2✔
362
                        wh.logger.Error(err, "Delete validation failed", "GVK", obj.GetObjectKind().GroupVersionKind())
1✔
363
                        return err
1✔
364
                }
1✔
365
        }
366
        if !dryrun {
2✔
367
                hcoTLSConfigCache = nil
1✔
368
        }
1✔
369
        return nil
1✔
370
}
371

372
func (wh *WebhookHandler) validateCertConfig(hc *v1beta1.HyperConverged) error {
1✔
373
        minimalDuration := metav1.Duration{Duration: 10 * time.Minute}
1✔
374

1✔
375
        ccValues := make(map[string]time.Duration)
1✔
376
        ccValues["spec.certConfig.ca.duration"] = hc.Spec.CertConfig.CA.Duration.Duration
1✔
377
        ccValues["spec.certConfig.ca.renewBefore"] = hc.Spec.CertConfig.CA.RenewBefore.Duration
1✔
378
        ccValues["spec.certConfig.server.duration"] = hc.Spec.CertConfig.Server.Duration.Duration
1✔
379
        ccValues["spec.certConfig.server.renewBefore"] = hc.Spec.CertConfig.Server.RenewBefore.Duration
1✔
380

1✔
381
        for key, value := range ccValues {
2✔
382
                if value < minimalDuration.Duration {
2✔
383
                        return fmt.Errorf("%v: value is too small", key)
1✔
384
                }
1✔
385
        }
386

387
        if hc.Spec.CertConfig.CA.Duration.Duration < hc.Spec.CertConfig.CA.RenewBefore.Duration {
2✔
388
                return errors.New("spec.certConfig.ca: duration is smaller than renewBefore")
1✔
389
        }
1✔
390

391
        if hc.Spec.CertConfig.Server.Duration.Duration < hc.Spec.CertConfig.Server.RenewBefore.Duration {
2✔
392
                return errors.New("spec.certConfig.server: duration is smaller than renewBefore")
1✔
393
        }
1✔
394

395
        if hc.Spec.CertConfig.CA.Duration.Duration < hc.Spec.CertConfig.Server.Duration.Duration {
2✔
396
                return errors.New("spec.certConfig: ca.duration is smaller than server.duration")
1✔
397
        }
1✔
398

399
        return nil
1✔
400
}
401

402
func (wh *WebhookHandler) validateDataImportCronTemplates(hc *v1beta1.HyperConverged) error {
1✔
403

1✔
404
        for _, dict := range hc.Spec.DataImportCronTemplates {
2✔
405
                val, ok := dict.Annotations[hcoutil.DataImportCronEnabledAnnotation]
1✔
406
                val = strings.ToLower(val)
1✔
407
                if ok && val != "false" && val != "true" {
2✔
408
                        return fmt.Errorf(`the %s annotation of a dataImportCronTemplate must be either "true" or "false"`, hcoutil.DataImportCronEnabledAnnotation)
1✔
409
                }
1✔
410

411
                enabled := !ok || val == "true"
1✔
412

1✔
413
                if enabled && dict.Spec == nil {
2✔
414
                        return fmt.Errorf("dataImportCronTemplate spec is empty for an enabled DataImportCronTemplate")
1✔
415
                }
1✔
416
        }
417

418
        return nil
1✔
419
}
420

421
func (wh *WebhookHandler) validateTLSSecurityProfiles(hc *v1beta1.HyperConverged) error {
1✔
422
        tlsSP := hc.Spec.TLSSecurityProfile
1✔
423

1✔
424
        if tlsSP == nil || tlsSP.Custom == nil {
2✔
425
                return nil
1✔
426
        }
1✔
427

428
        if !isValidTLSProtocolVersion(tlsSP.Custom.MinTLSVersion) {
2✔
429
                return fmt.Errorf("invalid value for spec.tlsSecurityProfile.custom.minTLSVersion")
1✔
430
        }
1✔
431

432
        if tlsSP.Custom.MinTLSVersion < openshiftconfigv1.VersionTLS13 && !hasRequiredHTTP2Ciphers(tlsSP.Custom.Ciphers) {
2✔
433
                return fmt.Errorf("http2: TLSConfig.CipherSuites is missing an HTTP/2-required AES_128_GCM_SHA256 cipher (need at least one of ECDHE-RSA-AES128-GCM-SHA256 or ECDHE-ECDSA-AES128-GCM-SHA256)")
1✔
434
        } else if tlsSP.Custom.MinTLSVersion == openshiftconfigv1.VersionTLS13 && len(tlsSP.Custom.Ciphers) > 0 {
3✔
435
                return fmt.Errorf("custom ciphers cannot be selected when minTLSVersion is VersionTLS13")
1✔
436
        }
1✔
437

438
        return nil
1✔
439
}
440

441
func (wh *WebhookHandler) validateMediatedDeviceTypes(hc *v1beta1.HyperConverged) error {
1✔
442
        mdc := hc.Spec.MediatedDevicesConfiguration
1✔
443
        if mdc != nil {
2✔
444
                if len(mdc.MediatedDevicesTypes) > 0 && len(mdc.MediatedDeviceTypes) > 0 && !slices.Equal(mdc.MediatedDevicesTypes, mdc.MediatedDeviceTypes) { //nolint SA1019
2✔
445
                        return fmt.Errorf("mediatedDevicesTypes is deprecated, please use mediatedDeviceTypes instead")
1✔
446
                }
1✔
447
                for _, nmdc := range mdc.NodeMediatedDeviceTypes {
2✔
448
                        if len(nmdc.MediatedDevicesTypes) > 0 && len(nmdc.MediatedDeviceTypes) > 0 && !slices.Equal(nmdc.MediatedDevicesTypes, nmdc.MediatedDeviceTypes) { //nolint SA1019
2✔
449
                                return fmt.Errorf("mediatedDevicesTypes is deprecated, please use mediatedDeviceTypes instead")
1✔
450
                        }
1✔
451
                }
452
        }
453
        return nil
1✔
454
}
455

456
const (
457
        fgMovedWarning       = "spec.featureGates.%[1]s is deprecated and ignored. It will removed in a future version; use spec.%[1]s instead"
458
        fgDeprecationWarning = "spec.featureGates.%s is deprecated and ignored. It will be removed in a future version;"
459
)
460

461
func (wh *WebhookHandler) validateFeatureGatesOnCreate(hc *v1beta1.HyperConverged) error {
1✔
462
        warnings := wh.validateDeprecatedFeatureGates(hc)
1✔
463
        warnings = validateOldFGOnCreate(warnings, hc)
1✔
464

1✔
465
        if len(warnings) > 0 {
2✔
466
                return newValidationWarning(warnings)
1✔
467
        }
1✔
468

469
        return nil
1✔
470
}
471

472
func (wh *WebhookHandler) validateFeatureGatesOnUpdate(requested, exists *v1beta1.HyperConverged) error {
1✔
473
        warnings := wh.validateDeprecatedFeatureGates(requested)
1✔
474
        warnings = validateOldFGOnUpdate(warnings, requested, exists)
1✔
475

1✔
476
        if len(warnings) > 0 {
2✔
477
                return newValidationWarning(warnings)
1✔
478
        }
1✔
479

480
        return nil
1✔
481
}
482

483
func (wh *WebhookHandler) validateDeprecatedFeatureGates(hc *v1beta1.HyperConverged) []string {
1✔
484
        var warnings []string
1✔
485

1✔
486
        //nolint:staticcheck
1✔
487
        if hc.Spec.FeatureGates.WithHostPassthroughCPU != nil {
2✔
488
                warnings = append(warnings, fmt.Sprintf(fgDeprecationWarning, "withHostPassthroughCPU"))
1✔
489
        }
1✔
490

491
        //nolint:staticcheck
492
        if hc.Spec.FeatureGates.DeployTektonTaskResources != nil {
2✔
493
                warnings = append(warnings, fmt.Sprintf(fgDeprecationWarning, "deployTektonTaskResources"))
1✔
494
        }
1✔
495

496
        //nolint:staticcheck
497
        if hc.Spec.FeatureGates.NonRoot != nil {
2✔
498
                warnings = append(warnings, fmt.Sprintf(fgDeprecationWarning, "nonRoot"))
1✔
499
        }
1✔
500

501
        //nolint:staticcheck
502
        if hc.Spec.FeatureGates.EnableManagedTenantQuota != nil {
2✔
503
                warnings = append(warnings, fmt.Sprintf(fgDeprecationWarning, "enableManagedTenantQuota"))
1✔
504
        }
1✔
505

506
        return warnings
1✔
507
}
508

509
func (wh *WebhookHandler) validateAffinity(hc *v1beta1.HyperConverged) error {
1✔
510
        if hc.Spec.Workloads.NodePlacement != nil {
2✔
511
                if err := validateAffinity(hc.Spec.Workloads.NodePlacement.Affinity); err != nil {
2✔
512
                        return fmt.Errorf("invalid workloads node placement affinity: %v", err.Error())
1✔
513
                }
1✔
514
        }
515

516
        if hc.Spec.Infra.NodePlacement != nil {
2✔
517
                if err := validateAffinity(hc.Spec.Infra.NodePlacement.Affinity); err != nil {
2✔
518
                        return fmt.Errorf("invalid infra node placement affinity: %v", err.Error())
1✔
519
                }
1✔
520
        }
521

522
        return nil
1✔
523
}
524

525
func validateAffinity(affinity *corev1.Affinity) error {
1✔
526
        if affinity == nil || affinity.NodeAffinity == nil || affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution == nil {
2✔
527
                return nil
1✔
528
        }
1✔
529

530
        _, err := nodeaffinity.NewNodeSelector(affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution)
1✔
531

1✔
532
        return err
1✔
533
}
534

535
func validateOldFGOnCreate(warnings []string, hc *v1beta1.HyperConverged) []string {
1✔
536
        //nolint:staticcheck
1✔
537
        if hc.Spec.FeatureGates.EnableApplicationAwareQuota != nil {
2✔
538
                warnings = append(warnings, fmt.Sprintf(fgMovedWarning, "enableApplicationAwareQuota"))
1✔
539
        }
1✔
540

541
        //nolint:staticcheck
542
        if hc.Spec.FeatureGates.EnableCommonBootImageImport != nil {
2✔
543
                warnings = append(warnings, fmt.Sprintf(fgMovedWarning, "enableCommonBootImageImport"))
1✔
544
        }
1✔
545

546
        //nolint:staticcheck
547
        if hc.Spec.FeatureGates.DeployVMConsoleProxy != nil {
2✔
548
                warnings = append(warnings, fmt.Sprintf(fgMovedWarning, "deployVmConsoleProxy"))
1✔
549
        }
1✔
550

551
        return warnings
1✔
552
}
553

554
func validateOldFGOnUpdate(warnings []string, hc, prevHC *v1beta1.HyperConverged) []string {
1✔
555
        //nolint:staticcheck
1✔
556
        if oldFGChanged(hc.Spec.FeatureGates.EnableApplicationAwareQuota, prevHC.Spec.FeatureGates.EnableApplicationAwareQuota) {
2✔
557
                warnings = append(warnings, fmt.Sprintf(fgMovedWarning, "enableApplicationAwareQuota"))
1✔
558
        }
1✔
559

560
        //nolint:staticcheck
561
        if oldFGChanged(hc.Spec.FeatureGates.EnableCommonBootImageImport, prevHC.Spec.FeatureGates.EnableCommonBootImageImport) {
2✔
562
                warnings = append(warnings, fmt.Sprintf(fgMovedWarning, "enableCommonBootImageImport"))
1✔
563
        }
1✔
564

565
        //nolint:staticcheck
566
        if oldFGChanged(hc.Spec.FeatureGates.DeployVMConsoleProxy, prevHC.Spec.FeatureGates.DeployVMConsoleProxy) {
2✔
567
                warnings = append(warnings, fmt.Sprintf(fgMovedWarning, "deployVmConsoleProxy"))
1✔
568
        }
1✔
569

570
        return warnings
1✔
571
}
572

573
func oldFGChanged(newFG, prevFG *bool) bool {
1✔
574
        return newFG != nil && (prevFG == nil || *newFG != *prevFG)
1✔
575
}
1✔
576

577
func hasRequiredHTTP2Ciphers(ciphers []string) bool {
1✔
578
        var requiredHTTP2Ciphers = []string{
1✔
579
                "ECDHE-RSA-AES128-GCM-SHA256",
1✔
580
                "ECDHE-ECDSA-AES128-GCM-SHA256",
1✔
581
        }
1✔
582

1✔
583
        // lo.Some returns true if at least 1 element of a subset is contained into a collection
1✔
584
        return lo.Some[string](requiredHTTP2Ciphers, ciphers)
1✔
585
}
1✔
586

587
// validationResponseFromStatus returns a response for admitting a request with provided Status object.
588
func validationResponseFromStatus(allowed bool, status metav1.Status) admission.Response {
×
589
        resp := admission.Response{
×
590
                AdmissionResponse: admissionv1.AdmissionResponse{
×
591
                        Allowed: allowed,
×
592
                        Result:  &status,
×
593
                },
×
594
        }
×
595
        return resp
×
596
}
×
597

598
func SelectCipherSuitesAndMinTLSVersion() ([]string, openshiftconfigv1.TLSProtocolVersion) {
1✔
599
        ci := hcoutil.GetClusterInfo()
1✔
600
        profile := ci.GetTLSSecurityProfile(hcoTLSConfigCache)
1✔
601

1✔
602
        if profile.Custom != nil {
1✔
603
                return profile.Custom.Ciphers, profile.Custom.MinTLSVersion
×
604
        }
×
605

606
        return openshiftconfigv1.TLSProfiles[profile.Type].Ciphers, openshiftconfigv1.TLSProfiles[profile.Type].MinTLSVersion
1✔
607
}
608

609
func isValidTLSProtocolVersion(pv openshiftconfigv1.TLSProtocolVersion) bool {
1✔
610
        switch pv {
1✔
611
        case
612
                openshiftconfigv1.VersionTLS10,
613
                openshiftconfigv1.VersionTLS11,
614
                openshiftconfigv1.VersionTLS12,
615
                openshiftconfigv1.VersionTLS13:
1✔
616
                return true
1✔
617
        }
618
        return false
1✔
619
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc