<pre><code>from kubernetes import client, config
config.load_kube_config()
api = client.AppsV1Api()

deployment = api.read_namespaced_deployment(name='foo', namespace='bar')

</code></pre>
<p>i tried to add affinity object to deployment spec i got this error</p>
<pre><code>deployment.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms = [{'nodeSelectorTerms':[{'matchExpressions':[{'key': 'kubernetes.io/hostname','operator': 'NotIn','values': [&quot;awesome-node&quot;]}]}]}]
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
AttributeError: 'V1DeploymentSpec' object has no attribute 'affinity'
</code></pre>

<p>You're looking at the wrong place. Affinity belongs to pod template spec (<code>deployment.spec.template.spec.affinity</code>) while you're looking at deployment spec (<code>deployment.spec.affinity</code>).</p>
<p>Here's how to completely replace existing affinity (even if it's None):</p>
<pre class="lang-py prettyprint-override"><code>from kubernetes import client, config
config.load_kube_config()
api = client.AppsV1Api()

# read current state
deployment = api.read_namespaced_deployment(name='foo', namespace='bar')

# check current state
#print(deployment.spec.template.spec.affinity)

# create affinity objects
terms = client.models.V1NodeSelectorTerm(
    match_expressions=[
        {'key': 'kubernetes.io/hostname',
         'operator': 'NotIn',
         'values': [&quot;awesome-node&quot;]}
    ]
)
node_selector = client.models.V1NodeSelector(node_selector_terms=[terms])
node_affinity = client.models.V1NodeAffinity(
    required_during_scheduling_ignored_during_execution=node_selector
)
affinity = client.models.V1Affinity(node_affinity=node_affinity)

# replace affinity in the deployment object
deployment.spec.template.spec.affinity = affinity

# finally, push the updated deployment configuration to the API-server
api.replace_namespaced_deployment(name=deployment.metadata.name,
                                  namespace=deployment.metadata.namespace,
                                  body=deployment)
</code></pre>
