-
Notifications
You must be signed in to change notification settings - Fork 5
fix(vm): propagate PV nodeAffinity to VM pod for correct scheduling #2127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/vm/hotplug-block-devices-via-spec
Are you sure you want to change the base?
Changes from all commits
e4c33e7
f6ce136
a9f6c87
4a79c41
b3603fe
97c9376
4028c4c
193316a
b317327
2efb5fd
2ec90ef
53028f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| Copyright 2026 Flant JSC | ||
|
|
||
| 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 nodeaffinity | ||
|
|
||
| import corev1 "k8s.io/api/core/v1" | ||
|
|
||
| func IntersectTerms(perPVTerms [][]corev1.NodeSelectorTerm) []corev1.NodeSelectorTerm { | ||
| if len(perPVTerms) == 0 { | ||
| return nil | ||
| } | ||
| result := perPVTerms[0] | ||
| for i := 1; i < len(perPVTerms); i++ { | ||
| result = CrossProductTerms(result, perPVTerms[i]) | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| func CrossProductTerms(a, b []corev1.NodeSelectorTerm) []corev1.NodeSelectorTerm { | ||
| var result []corev1.NodeSelectorTerm | ||
| for _, termA := range a { | ||
| for _, termB := range b { | ||
| merged := corev1.NodeSelectorTerm{ | ||
| MatchExpressions: append( | ||
| append([]corev1.NodeSelectorRequirement{}, termA.MatchExpressions...), | ||
| termB.MatchExpressions..., | ||
| ), | ||
| } | ||
| if len(termA.MatchFields) > 0 || len(termB.MatchFields) > 0 { | ||
| merged.MatchFields = append( | ||
| append([]corev1.NodeSelectorRequirement{}, termA.MatchFields...), | ||
| termB.MatchFields..., | ||
| ) | ||
| } | ||
| result = append(result, merged) | ||
| } | ||
| } | ||
| return result | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,7 +29,9 @@ import ( | |
|
|
||
| "github.com/deckhouse/virtualization-controller/pkg/common/annotations" | ||
| kvvmutil "github.com/deckhouse/virtualization-controller/pkg/common/kvvm" | ||
| "github.com/deckhouse/virtualization-controller/pkg/common/nodeaffinity" | ||
| "github.com/deckhouse/virtualization-controller/pkg/common/object" | ||
| "github.com/deckhouse/virtualization-controller/pkg/controller/indexer" | ||
| "github.com/deckhouse/virtualization-controller/pkg/controller/powerstate" | ||
| "github.com/deckhouse/virtualization-controller/pkg/controller/reconciler" | ||
| "github.com/deckhouse/virtualization/api/core/v1alpha2" | ||
|
|
@@ -54,6 +56,7 @@ type VirtualMachineState interface { | |
| VMOPs(ctx context.Context) ([]*v1alpha2.VirtualMachineOperation, error) | ||
| Shared(fn func(s *Shared)) | ||
| ReadWriteOnceVirtualDisks(ctx context.Context) ([]*v1alpha2.VirtualDisk, error) | ||
| PVNodeAffinityTerms(ctx context.Context) ([]corev1.NodeSelectorTerm, error) | ||
| USBDevice(ctx context.Context, name string) (*v1alpha2.USBDevice, error) | ||
| USBDevicesByName(ctx context.Context) (map[string]*v1alpha2.USBDevice, error) | ||
| } | ||
|
|
@@ -386,6 +389,115 @@ func (s *state) ReadWriteOnceVirtualDisks(ctx context.Context) ([]*v1alpha2.Virt | |
| return nonMigratableVirtualDisks, nil | ||
| } | ||
|
|
||
| func (s *state) PVNodeAffinityTerms(ctx context.Context) ([]corev1.NodeSelectorTerm, error) { | ||
| refs := s.collectBlockDeviceRefs(ctx) | ||
|
|
||
| var perPVTerms [][]corev1.NodeSelectorTerm | ||
| namespace := s.vm.Current().GetNamespace() | ||
|
|
||
| for _, ref := range refs { | ||
| pvcName, err := s.resolvePVCName(ctx, ref.Kind, ref.Name) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("resolve PVC name for %s/%s: %w", ref.Kind, ref.Name, err) | ||
| } | ||
| if pvcName == "" { | ||
| continue | ||
| } | ||
|
|
||
| terms, err := s.pvNodeAffinityTermsForPVC(ctx, pvcName, namespace) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("get PV node affinity for PVC %s: %w", pvcName, err) | ||
| } | ||
| if terms == nil { | ||
| continue | ||
|
loktev-d marked this conversation as resolved.
|
||
| } | ||
| perPVTerms = append(perPVTerms, terms) | ||
| } | ||
|
|
||
| return nodeaffinity.IntersectTerms(perPVTerms), nil | ||
| } | ||
|
|
||
| func (s *state) collectBlockDeviceRefs(ctx context.Context) []blockDeviceRef { | ||
| seen := make(map[blockDeviceRef]struct{}) | ||
| var refs []blockDeviceRef | ||
|
|
||
| for _, bd := range s.vm.Current().Spec.BlockDeviceRefs { | ||
| ref := blockDeviceRef{Name: bd.Name, Kind: bd.Kind} | ||
| if _, ok := seen[ref]; !ok { | ||
| seen[ref] = struct{}{} | ||
| refs = append(refs, ref) | ||
| } | ||
| } | ||
|
|
||
| var vmbdaList v1alpha2.VirtualMachineBlockDeviceAttachmentList | ||
| err := s.client.List(ctx, &vmbdaList, | ||
| client.InNamespace(s.vm.Current().GetNamespace()), | ||
| client.MatchingFields{indexer.IndexFieldVMBDAByVM: s.vm.Current().GetName()}, | ||
| ) | ||
| if err != nil { | ||
| return refs | ||
| } | ||
|
|
||
| for _, vmbda := range vmbdaList.Items { | ||
| if vmbda.Status.Phase != v1alpha2.BlockDeviceAttachmentPhaseAttached { | ||
| continue | ||
| } | ||
| ref := blockDeviceRef{ | ||
| Name: vmbda.Spec.BlockDeviceRef.Name, | ||
| Kind: v1alpha2.BlockDeviceKind(vmbda.Spec.BlockDeviceRef.Kind), | ||
| } | ||
| if _, ok := seen[ref]; !ok { | ||
| seen[ref] = struct{}{} | ||
| refs = append(refs, ref) | ||
| } | ||
| } | ||
|
|
||
| return refs | ||
| } | ||
|
|
||
| func (s *state) resolvePVCName(ctx context.Context, kind v1alpha2.BlockDeviceKind, name string) (string, error) { | ||
| switch kind { | ||
| case v1alpha2.DiskDevice: | ||
| vd, err := s.VirtualDisk(ctx, name) | ||
| if err != nil || vd == nil { | ||
| return "", err | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if err == nil && vd == nil, return "", nil. |
||
| } | ||
| return vd.Status.Target.PersistentVolumeClaim, nil | ||
| case v1alpha2.ImageDevice: | ||
| vi, err := s.VirtualImage(ctx, name) | ||
| if err != nil || vi == nil { | ||
| return "", err | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same thing |
||
| } | ||
| if vi.Spec.Storage != v1alpha2.StorageKubernetes && vi.Spec.Storage != v1alpha2.StoragePersistentVolumeClaim { | ||
| return "", nil | ||
| } | ||
| return vi.Status.Target.PersistentVolumeClaim, nil | ||
| default: | ||
| return "", nil | ||
| } | ||
| } | ||
|
|
||
| func (s *state) pvNodeAffinityTermsForPVC(ctx context.Context, pvcName, namespace string) ([]corev1.NodeSelectorTerm, error) { | ||
| pvc, err := object.FetchObject(ctx, types.NamespacedName{ | ||
| Name: pvcName, Namespace: namespace, | ||
| }, s.client, &corev1.PersistentVolumeClaim{}) | ||
| if err != nil || pvc == nil || pvc.Spec.VolumeName == "" { | ||
| return nil, err | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same again |
||
| } | ||
|
|
||
| pv, err := object.FetchObject(ctx, types.NamespacedName{ | ||
| Name: pvc.Spec.VolumeName, | ||
| }, s.client, &corev1.PersistentVolume{}) | ||
| if err != nil || pv == nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if pv.Spec.NodeAffinity != nil && pv.Spec.NodeAffinity.Required != nil && len(pv.Spec.NodeAffinity.Required.NodeSelectorTerms) > 0 { | ||
| return pv.Spec.NodeAffinity.Required.NodeSelectorTerms, nil | ||
| } | ||
| return nil, nil | ||
| } | ||
|
|
||
| func (s *state) USBDevice(ctx context.Context, name string) (*v1alpha2.USBDevice, error) { | ||
| return object.FetchObject(ctx, types.NamespacedName{ | ||
| Name: name, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.