forked from ManageIQ/manageiq-pods
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ManageIQ#1003 from bdunne/node_affinity
Add Node affinity
- Loading branch information
Showing
10 changed files
with
132 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package miqutils | ||
|
||
import ( | ||
"context" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func FindPodByName(client client.Client, namespace string, name string) *corev1.Pod { | ||
podKey := types.NamespacedName{Namespace: namespace, Name: name} | ||
pod := &corev1.Pod{} | ||
client.Get(context.TODO(), podKey, pod) | ||
|
||
return pod | ||
} | ||
|
||
func FindReplicaSetByName(client client.Client, namespace string, name string) *appsv1.ReplicaSet { | ||
replicaSetKey := types.NamespacedName{Namespace: namespace, Name: name} | ||
replicaSet := &appsv1.ReplicaSet{} | ||
client.Get(context.TODO(), replicaSetKey, replicaSet) | ||
|
||
return replicaSet | ||
} | ||
|
||
func FindDeploymentByName(client client.Client, namespace string, name string) *appsv1.Deployment { | ||
deploymentKey := types.NamespacedName{Namespace: namespace, Name: name} | ||
deployment := &appsv1.Deployment{} | ||
client.Get(context.TODO(), deploymentKey, deployment) | ||
|
||
return deployment | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package miqutils | ||
|
||
import ( | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"os" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func OperatorNodeAffinityArchValues(deployment *appsv1.Deployment, client client.Client) []string { | ||
podName := os.Getenv("POD_NAME") | ||
pod := FindPodByName(client, deployment.ObjectMeta.Namespace, podName) | ||
values := []string{"amd64"} | ||
|
||
if pod.Spec.Affinity == nil { | ||
// In case we don't find the operator pod (local testing) or it doesn't have affinities | ||
return values | ||
} | ||
|
||
nodeSelectorTerms := pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms | ||
|
||
for _, selector := range nodeSelectorTerms { | ||
for _, matchExpression := range selector.MatchExpressions { | ||
if matchExpression.Key == "kubernetes.io/arch" { | ||
values = matchExpression.Values | ||
} | ||
} | ||
} | ||
|
||
return values | ||
} | ||
|
||
func SetDeploymentNodeAffinity(deployment *appsv1.Deployment, client client.Client) { | ||
operatorNodeAffinityArchValues := OperatorNodeAffinityArchValues(deployment, client) | ||
if len(operatorNodeAffinityArchValues) == 0 { | ||
// We're running local, can't find the operator pod, or it doesn't have any affinities to use as a template. Skip it. | ||
return | ||
} | ||
|
||
matchExpression := corev1.NodeSelectorRequirement{ | ||
Key: "kubernetes.io/arch", | ||
Operator: corev1.NodeSelectorOpIn, | ||
Values: operatorNodeAffinityArchValues, | ||
} | ||
|
||
matchExpressions := []corev1.NodeSelectorRequirement{matchExpression} | ||
|
||
nodeSelectorTerm := corev1.NodeSelectorTerm{ | ||
MatchExpressions: matchExpressions, | ||
} | ||
|
||
nodeSelectionTerms := []corev1.NodeSelectorTerm{nodeSelectorTerm} | ||
|
||
deployment.Spec.Template.Spec.Affinity = &corev1.Affinity{ | ||
NodeAffinity: &corev1.NodeAffinity{ | ||
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{ | ||
NodeSelectorTerms: nodeSelectionTerms, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters