init_container = k8s.V1Container( name="init-container", image="ubuntu:16.04", env=init_environments, volume_mounts=init_container_volume_mounts, command=["bash", "-cx"], args=["echo 10"], ) affinity = k8s.V1Affinity( node_affinity=k8s.V1NodeAffinity( preferred_during_scheduling_ignored_during_execution=[ k8s.V1PreferredSchedulingTerm( weight=1, preference=k8s.V1NodeSelectorTerm(match_expressions=[ k8s.V1NodeSelectorRequirement( key="disktype", operator="in", values=["ssd"]) ]), ) ]), pod_affinity=k8s.V1PodAffinity( required_during_scheduling_ignored_during_execution=[ k8s.V1WeightedPodAffinityTerm( weight=1, pod_affinity_term=k8s.V1PodAffinityTerm( label_selector=k8s.V1LabelSelector(match_expressions=[ k8s.V1LabelSelectorRequirement( key="security", operator="In", values="S1") ]), topology_key="failure-domain.beta.kubernetes.io/zone", ),
def test_create_with_affinity(self): name_base = "test" affinity = { "nodeAffinity": { "preferredDuringSchedulingIgnoredDuringExecution": [ { "weight": 1, "preference": { "matchExpressions": [{"key": "disktype", "operator": "In", "values": ["ssd"]}] }, } ] } } k = KubernetesPodOperator( namespace="default", image="ubuntu:16.04", cmds=["bash", "-cx"], arguments=["echo 10"], labels={"foo": "bar"}, name=name_base, task_id="task", in_cluster=False, do_xcom_push=False, cluster_context="default", affinity=affinity, ) pod = k.create_pod_request_obj() sanitized_pod = self.sanitize_for_serialization(pod) assert isinstance(pod.spec.affinity, k8s.V1Affinity) assert sanitized_pod["spec"]["affinity"] == affinity k8s_api_affinity = k8s.V1Affinity( node_affinity=k8s.V1NodeAffinity( preferred_during_scheduling_ignored_during_execution=[ k8s.V1PreferredSchedulingTerm( weight=1, preference=k8s.V1NodeSelectorTerm( match_expressions=[ k8s.V1NodeSelectorRequirement(key="disktype", operator="In", values=["ssd"]) ] ), ) ] ), ) k = KubernetesPodOperator( namespace="default", image="ubuntu:16.04", cmds=["bash", "-cx"], arguments=["echo 10"], labels={"foo": "bar"}, name=name_base, task_id="task", in_cluster=False, do_xcom_push=False, cluster_context="default", affinity=k8s_api_affinity, ) pod = k.create_pod_request_obj() sanitized_pod = self.sanitize_for_serialization(pod) assert isinstance(pod.spec.affinity, k8s.V1Affinity) assert sanitized_pod["spec"]["affinity"] == affinity
def test_create_with_affinity(self): name_base = 'test' affinity = { 'nodeAffinity': { 'preferredDuringSchedulingIgnoredDuringExecution': [{ "weight": 1, "preference": { "matchExpressions": [{ "key": "disktype", "operator": "In", "values": ["ssd"] }] }, }] } } k = KubernetesPodOperator( namespace='default', image="ubuntu:16.04", cmds=["bash", "-cx"], arguments=["echo 10"], labels={"foo": "bar"}, name=name_base, task_id="task", in_cluster=False, do_xcom_push=False, cluster_context='default', affinity=affinity, ) result = k.create_pod_request_obj() client = ApiClient() self.assertEqual(type(result.spec.affinity), k8s.V1Affinity) self.assertEqual( client.sanitize_for_serialization(result)['spec']['affinity'], affinity) k8s_api_affinity = k8s.V1Affinity(node_affinity=k8s.V1NodeAffinity( preferred_during_scheduling_ignored_during_execution=[ k8s.V1PreferredSchedulingTerm( weight=1, preference=k8s.V1NodeSelectorTerm(match_expressions=[ k8s.V1NodeSelectorRequirement( key="disktype", operator="In", values=["ssd"]) ]), ) ]), ) k = KubernetesPodOperator( namespace='default', image="ubuntu:16.04", cmds=["bash", "-cx"], arguments=["echo 10"], labels={"foo": "bar"}, name=name_base, task_id="task", in_cluster=False, do_xcom_push=False, cluster_context='default', affinity=k8s_api_affinity, ) result = k.create_pod_request_obj() self.assertEqual(type(result.spec.affinity), k8s.V1Affinity) self.assertEqual( client.sanitize_for_serialization(result)['spec']['affinity'], affinity)