Skip to content
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

add pod affinity and antiffinity rules #202

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion controllers/flux/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,48 @@ func NewMiniClusterJob(cluster *api.MiniCluster) (*batchv1.Job, error) {
ImagePullSecrets: getImagePullSecrets(cluster),
ServiceAccountName: cluster.Spec.Pod.ServiceAccountName,
NodeSelector: cluster.Spec.Pod.NodeSelector,
}},
Affinity: &corev1.Affinity{
// Prefer to schedule pods on the same zone
PodAffinity: &corev1.PodAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{
{
PodAffinityTerm: corev1.PodAffinityTerm{
LabelSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: podLabelAppName, // added in getPodLabels
Operator: metav1.LabelSelectorOpIn,
Values: []string{cluster.Name},
},
},
},
TopologyKey: "topology.kubernetes.io/zone",
},
},
},
},
// Prefer to schedule pods on different nodes
PodAntiAffinity: &corev1.PodAntiAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{
{
PodAffinityTerm: corev1.PodAffinityTerm{
LabelSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: podLabelAppName, // added in getPodLabels
Operator: metav1.LabelSelectorOpIn,
Values: []string{cluster.Name},
},
},
},
TopologyKey: "kubernetes.io/hostname",
},
},
},
},
},
},
},
},
}

Expand Down
2 changes: 1 addition & 1 deletion controllers/flux/minicluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (r *MiniClusterReconciler) ensureMiniCluster(
}

// Service name corresponds to container, but selector is pod-specific
selector := map[string]string{"app.kubernetes.io/name": cluster.Name}
selector := map[string]string{podLabelAppName: cluster.Name}
result, err = r.exposeService(ctx, cluster, container.Name, selector, container.Ports)
if err != nil {
return result, err
Expand Down
4 changes: 3 additions & 1 deletion controllers/flux/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ import (
api "github.com/flux-framework/flux-operator/api/v1alpha1"
)

const podLabelAppName = "app.kubernetes.io/name"

// Get labels for any pod in the cluster
func getPodLabels(cluster *api.MiniCluster) map[string]string {
if cluster.Spec.Pod.Labels == nil {
cluster.Spec.Pod.Labels = map[string]string{}
}
podLabels := cluster.Spec.Pod.Labels
podLabels["namespace"] = cluster.Namespace
podLabels["app.kubernetes.io/name"] = cluster.Name
podLabels[podLabelAppName] = cluster.Name
return podLabels
}

Expand Down