fix(placements) filter out empty items in the required node affinity array [BE-11022] (#12034)

Co-authored-by: testa113 <testa113>
pull/12044/head
Ali 2024-07-23 09:31:08 +12:00 committed by GitHub
parent 1900fb695d
commit 6eb9e906af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 44 additions and 38 deletions

View File

@ -181,46 +181,52 @@ function getUnmatchedRequiredNodeAffinities(node: Node, pod: Pod): Affinity[] {
?.requiredDuringSchedulingIgnoredDuringExecution; ?.requiredDuringSchedulingIgnoredDuringExecution;
const unmatchedRequiredNodeAffinities: Affinity[] = const unmatchedRequiredNodeAffinities: Affinity[] =
basicNodeAffinity?.nodeSelectorTerms.map( basicNodeAffinity?.nodeSelectorTerms
(selectorTerm) => .map(
selectorTerm.matchExpressions?.flatMap((matchExpression) => { (selectorTerm) =>
const exists = !!node.metadata?.labels?.[matchExpression.key]; selectorTerm.matchExpressions?.flatMap((matchExpression) => {
const isIn = const exists = !!node.metadata?.labels?.[matchExpression.key];
exists && const isIn =
_.includes(
matchExpression.values,
node.metadata?.labels?.[matchExpression.key]
);
// Check if the match expression is satisfied
if (
(matchExpression.operator === 'Exists' && exists) ||
(matchExpression.operator === 'DoesNotExist' && !exists) ||
(matchExpression.operator === 'In' && isIn) ||
(matchExpression.operator === 'NotIn' && !isIn) ||
(matchExpression.operator === 'Gt' &&
exists && exists &&
parseInt(node.metadata?.labels?.[matchExpression.key] || '', 10) > _.includes(
parseInt(matchExpression.values?.[0] || '', 10)) || matchExpression.values,
(matchExpression.operator === 'Lt' && node.metadata?.labels?.[matchExpression.key]
exists && );
parseInt(node.metadata?.labels?.[matchExpression.key] || '', 10) <
parseInt(matchExpression.values?.[0] || '', 10))
) {
return [];
}
// Return the unmatched affinity // Check if the match expression is satisfied
return [ if (
{ (matchExpression.operator === 'Exists' && exists) ||
key: matchExpression.key, (matchExpression.operator === 'DoesNotExist' && !exists) ||
operator: (matchExpression.operator === 'In' && isIn) ||
matchExpression.operator as KubernetesPodNodeAffinityNodeSelectorRequirementOperators, (matchExpression.operator === 'NotIn' && !isIn) ||
values: matchExpression.values?.join(', ') || '', (matchExpression.operator === 'Gt' &&
}, exists &&
]; parseInt(
}) || [] node.metadata?.labels?.[matchExpression.key] || '',
) || []; 10
) > parseInt(matchExpression.values?.[0] || '', 10)) ||
(matchExpression.operator === 'Lt' &&
exists &&
parseInt(
node.metadata?.labels?.[matchExpression.key] || '',
10
) < parseInt(matchExpression.values?.[0] || '', 10))
) {
return [];
}
// Return the unmatched affinity
return [
{
key: matchExpression.key,
operator:
matchExpression.operator as KubernetesPodNodeAffinityNodeSelectorRequirementOperators,
values: matchExpression.values?.join(', ') || '',
},
];
}) || []
)
.filter((unmatchedAffinity) => unmatchedAffinity.length > 0) || [];
return unmatchedRequiredNodeAffinities; return unmatchedRequiredNodeAffinities;
} }