Skip to content

Commit

Permalink
Bug Fix Pod-Template Affinity Ignored due to empty Affinity K8S Object (
Browse files Browse the repository at this point in the history
#15787)

**Issue**:
KubernetesPodOperator pod-template files with pod affinities are ignored, even if no affinities are passed to the KubernetesPodOperator object. 

**Cause** 
During the pod-initialization an empty k8s.Affinity object is created if no affinities are supplied. This will later prevent the pod-template affinities to be used, because during the pod_reconciliation the empty k8s.Affinity object takes precedence. 
All other attributes such as
`self.k8s_resources = convert_resources(resources) if resources else {}`
`self.image_pull_secrets = convert_image_pull_secrets(image_pull_secrets) if image_pull_secrets else []`

handle it properly.
  • Loading branch information
jpyen authored May 20, 2021
1 parent a51a100 commit 733bec9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def __init__( # pylint: disable=too-many-arguments,too-many-locals
else:
self.node_selector = {}
self.annotations = annotations or {}
self.affinity = convert_affinity(affinity) if affinity else k8s.V1Affinity()
self.affinity = convert_affinity(affinity) if affinity else {}
self.k8s_resources = convert_resources(resources) if resources else {}
self.config_file = config_file
self.image_pull_secrets = convert_image_pull_secrets(image_pull_secrets) if image_pull_secrets else []
Expand Down
48 changes: 48 additions & 0 deletions tests/providers/cncf/kubernetes/operators/test_kubernetes_pod.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,25 @@ def test_pod_template_file(self):
labels:
foo: bar
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/role
operator: In
values:
- foo
- bar
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: kubernetes.io/role
operator: In
values:
- foo
- bar
containers:
- name: base
image: ubuntu:16.04
Expand Down Expand Up @@ -366,6 +385,35 @@ def test_pod_template_file(self):
assert pod.metadata.namespace == "mynamespace"
assert pod.spec.containers[0].image == "ubuntu:16.04"
assert pod.spec.containers[0].command == ["something"]
affinity = {
'node_affinity': {
'preferred_during_scheduling_ignored_during_execution': [
{
'preference': {
'match_expressions': [
{'key': 'kubernetes.io/role', 'operator': 'In', 'values': ['foo', 'bar']}
],
'match_fields': None,
},
'weight': 1,
}
],
'required_during_scheduling_ignored_during_execution': {
'node_selector_terms': [
{
'match_expressions': [
{'key': 'kubernetes.io/role', 'operator': 'In', 'values': ['foo', 'bar']}
],
'match_fields': None,
}
]
},
},
'pod_affinity': None,
'pod_anti_affinity': None,
}

assert pod.spec.affinity.to_dict() == affinity

# kwargs take precedence, however
image = "some.custom.image:andtag"
Expand Down

0 comments on commit 733bec9

Please sign in to comment.