|
| 1 | +/* |
| 2 | +Copyright 2026 Flant JSC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package precheck |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + . "github.com/onsi/ginkgo/v2" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + crclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 26 | + |
| 27 | + "github.com/deckhouse/virtualization/test/e2e/internal/framework" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + affinityTolerationPrecheckEnvName = "AFFINITY_TOLERATION_PRECHECK" |
| 32 | + nodeGroupLabelKey = "node.deckhouse.io/group" |
| 33 | + kvmLabelKey = "virtualization.deckhouse.io/kvm-enabled" |
| 34 | + |
| 35 | + minReadyKVMMasterNodes = 1 |
| 36 | + minReadyKVMWorkerNodes = 2 |
| 37 | +) |
| 38 | + |
| 39 | +// affinityTolerationPrecheck implements Precheck interface for VM affinity/toleration test cluster requirements. |
| 40 | +type affinityTolerationPrecheck struct{} |
| 41 | + |
| 42 | +func (a *affinityTolerationPrecheck) Label() string { |
| 43 | + return PrecheckAffinityToleration |
| 44 | +} |
| 45 | + |
| 46 | +func (a *affinityTolerationPrecheck) Run(ctx context.Context, f *framework.Framework) error { |
| 47 | + if !isCheckEnabled(affinityTolerationPrecheckEnvName) { |
| 48 | + _, _ = GinkgoWriter.Write([]byte("Affinity/toleration precheck is disabled.\n")) |
| 49 | + return nil |
| 50 | + } |
| 51 | + |
| 52 | + masterNodes, err := listReadyNodesByLabels(ctx, f, map[string]string{ |
| 53 | + kvmLabelKey: "true", |
| 54 | + nodeGroupLabelKey: "master", |
| 55 | + }) |
| 56 | + if err != nil { |
| 57 | + return fmt.Errorf("%s=no to disable this precheck: failed to list ready KVM-enabled master nodes: %w", affinityTolerationPrecheckEnvName, err) |
| 58 | + } |
| 59 | + if len(masterNodes) < minReadyKVMMasterNodes { |
| 60 | + return fmt.Errorf("%s=no to disable this precheck: at least %d ready KVM-enabled master node is required, got %d", affinityTolerationPrecheckEnvName, minReadyKVMMasterNodes, len(masterNodes)) |
| 61 | + } |
| 62 | + |
| 63 | + workerNodes, err := listReadyNodesByLabels(ctx, f, map[string]string{ |
| 64 | + kvmLabelKey: "true", |
| 65 | + nodeGroupLabelKey: "worker", |
| 66 | + }) |
| 67 | + if err != nil { |
| 68 | + return fmt.Errorf("%s=no to disable this precheck: failed to list ready KVM-enabled worker nodes: %w", affinityTolerationPrecheckEnvName, err) |
| 69 | + } |
| 70 | + if len(workerNodes) < minReadyKVMWorkerNodes { |
| 71 | + return fmt.Errorf("%s=no to disable this precheck: at least %d ready KVM-enabled worker nodes are required, got %d", affinityTolerationPrecheckEnvName, minReadyKVMWorkerNodes, len(workerNodes)) |
| 72 | + } |
| 73 | + |
| 74 | + return nil |
| 75 | +} |
| 76 | + |
| 77 | +func listReadyNodesByLabels(ctx context.Context, f *framework.Framework, labels map[string]string) ([]corev1.Node, error) { |
| 78 | + nodes := &corev1.NodeList{} |
| 79 | + err := f.GenericClient().List(ctx, nodes, crclient.MatchingLabels(labels)) |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + |
| 84 | + readyNodes := make([]corev1.Node, 0, len(nodes.Items)) |
| 85 | + for _, node := range nodes.Items { |
| 86 | + for _, condition := range node.Status.Conditions { |
| 87 | + if condition.Type == corev1.NodeReady && condition.Status == corev1.ConditionTrue { |
| 88 | + readyNodes = append(readyNodes, node) |
| 89 | + break |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return readyNodes, nil |
| 95 | +} |
| 96 | + |
| 97 | +func init() { |
| 98 | + RegisterPrecheck(&affinityTolerationPrecheck{}, false) |
| 99 | +} |
0 commit comments