Skip to content

Commit 36ebd8c

Browse files
authored
feat: allow prefer scheduling additional pods on same node as main pods (#489)
1 parent a936831 commit 36ebd8c

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

api/v1/cosmosfullnode_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ type AdditionalPodSpec struct {
337337
Metadata Metadata `json:"metadata"`
338338

339339
corev1.PodSpec `json:",inline"`
340+
341+
// Whether to prefer the same node as the main pod for the additional pod.
342+
// This is useful for pods that are not part of the main pod's deployment, but should still be co-located.
343+
// +optional
344+
PreferSameNode bool `json:"preferSameNode,omitempty"`
340345
}
341346

342347
type FullNodeProbeStrategy string

config/crd/bases/cosmos.strange.love_cosmosfullnodes.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3379,6 +3379,11 @@ spec:
33793379
One of Never, PreemptLowerPriority.
33803380
Defaults to PreemptLowerPriority if unset.
33813381
type: string
3382+
preferSameNode:
3383+
description: |-
3384+
Whether to prefer the same node as the main pod for the additional pod.
3385+
This is useful for pods that are not part of the main pod's deployment, but should still be co-located.
3386+
type: boolean
33823387
priority:
33833388
description: |-
33843389
The priority value. Various system components use this field to find the

internal/fullnode/pod_builder.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,8 @@ func buildAdditionalPod(
560560
labels := defaultLabels(crd)
561561
labels[kube.NameLabel] = appName(crd) + "-" + podSpec.Name
562562

563+
belongsTo := instanceName(crd, ordinal)
564+
563565
pod := &corev1.Pod{
564566
TypeMeta: metav1.TypeMeta{
565567
Kind: "Pod",
@@ -574,12 +576,30 @@ func buildAdditionalPod(
574576
Spec: podSpec.PodSpec,
575577
}
576578

579+
if podSpec.PreferSameNode {
580+
pod.Spec.Affinity = &corev1.Affinity{
581+
PodAffinity: &corev1.PodAffinity{
582+
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{
583+
{
584+
Weight: 100,
585+
PodAffinityTerm: corev1.PodAffinityTerm{
586+
LabelSelector: &metav1.LabelSelector{
587+
MatchLabels: map[string]string{
588+
kube.InstanceLabel: belongsTo,
589+
},
590+
},
591+
TopologyKey: "kubernetes.io/hostname",
592+
},
593+
},
594+
},
595+
},
596+
}
597+
}
598+
577599
// Apply common labels and annotations
578600
preserveMergeInto(pod.Labels, podSpec.Metadata.Labels)
579601
preserveMergeInto(pod.Annotations, podSpec.Metadata.Annotations)
580602

581-
belongsTo := instanceName(crd, ordinal)
582-
583603
pod.Labels[kube.InstanceLabel] = name
584604
pod.Labels[kube.BelongsToLabel] = belongsTo
585605

0 commit comments

Comments
 (0)