Repository: vllm-project/aibrix
File: pkg/controller/kvcache/kvcache_controller.go (Go)

Code before:
				{
					LabelSelector: &metav1.LabelSelector{
						MatchExpressions: []metav1.LabelSelectorRequirement{
							{
								Key:      "model.aibrix.ai/name",
								Operator: metav1.LabelSelectorOpIn,
								Values:   []string{value},
							},
						},
					},
					TopologyKey: "kubernetes.io/hostname",
				},
			},
		}
		affinity.PodAffinity = podAffinity
	}

	// Add pod anti-affinity handling
	if value, ok := kvCache.Annotations[KVCacheAnnotationPodAntiAffinityKey]; ok {
		podAntiAffinity := &corev1.PodAntiAffinity{
			RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{
				{
					LabelSelector: &metav1.LabelSelector{
						MatchExpressions: []metav1.LabelSelectorRequirement{
							{
								Key:      "model.aibrix.ai/name",
								Operator: metav1.LabelSelectorOpIn,
								Values:   []string{value},
							},
						},
					},
					TopologyKey: "kubernetes.io/hostname",
				},
			},
		}
		affinity.PodAntiAffinity = podAntiAffinity
	}

	// Adding a default anti-affinity between KVCache pods
	// to ensure high availability by distributing replicas across nodes
	if kvCache.Spec.Replicas > 1 {
		// If no explicit anti-affinity is set and we have multiple replicas,
		// set a preferred anti-affinity for KVCache pods of the same deployment
		if affinity.PodAntiAffinity == nil {
			affinity.PodAntiAffinity = &corev1.PodAntiAffinity{
				PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{
					{
						Weight: 100,
						PodAffinityTerm: corev1.PodAffinityTerm{
							LabelSelector: &metav1.LabelSelector{
								MatchLabels: map[string]string{

Review comment (performance):
what's the usage here? do you want to apply anti-affinity to other engine pods or just kv cache pod? 

the cache pod doesn't have `model.aibrix.ai/name` labels, right? 

Code after:
				{
					LabelSelector: &metav1.LabelSelector{
						MatchExpressions: []metav1.LabelSelectorRequirement{
							{
								Key:      "model.aibrix.ai/name",
								Operator: metav1.LabelSelectorOpIn,
								Values:   []string{value},
							},
						},
					},
					TopologyKey: "kubernetes.io/hostname",
				},
			},
		}
		affinity.PodAffinity = podAffinity
	}

	if _, ok := kvCache.Annotations[KVCacheAnnotationPodAntiAffinity]; ok {
		podAntiAffinity := &corev1.PodAntiAffinity{
			RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{
				{
					LabelSelector: &metav1.LabelSelector{
						MatchLabels: map[string]string{
							KVCacheLabelKeyIdentifier: kvCache.Name,
							KVCacheLabelKeyRole:       KVCacheLabelValueRoleCache,
						},
					},
					TopologyKey: "kubernetes.io/hostname",
				},
			},
		}
		affinity.PodAntiAffinity = podAntiAffinity
	}

	deployment := &appsv1.Deployment{
		ObjectMeta: metav1.ObjectMeta{
			Name:      kvCache.Name,
			Namespace: kvCache.Namespace,
			Labels: map[string]string{
				KVCacheLabelKeyIdentifier: kvCache.Name,
				KVCacheLabelKeyRole:       "cache",
			},
			OwnerReferences: []metav1.OwnerReference{
				*metav1.NewControllerRef(kvCache, orchestrationv1alpha1.GroupVersion.WithKind("KVCache")),
			},
		},
		Spec: appsv1.DeploymentSpec{
			Replicas: &kvCache.Spec.Replicas,
			Selector: &metav1.LabelSelector{
				// TODO: update metadata
				MatchLabels: map[string]string{