<p><a href="https://github.com/kubernetes-client/python/blob/master/examples/ingress_create.py" rel="nofollow noreferrer">https://github.com/kubernetes-client/python/blob/master/examples/ingress_create.py</a></p>
<p>Above is the link I refer to</p>
<p>test3.py:</p>
<pre class="lang-py prettyprint-override"><code>import yaml
from kubernetes import client, config
config.load_kube_config()
txt = &quot;&quot;
with open(&quot;/home/xxxxxx/ide-ingress/b3a1e295-d77c-41b4-a2e6-5cb75b7b2f61.yaml&quot;, 'r') as f:
    for a in f.readlines():
        txt = txt + a
res = list(yaml.safe_load_all(txt))
deployment_dict = res[3]

def create_deployment(apps_v1_api):
    container = client.V1Container(
        name=deployment_dict.get(&quot;metadata&quot;).get(&quot;name&quot;),
        image=deployment_dict.get(&quot;spec&quot;).get(&quot;template&quot;).get(&quot;spec&quot;).get(&quot;containers&quot;)[0].get(&quot;image&quot;),
        image_pull_policy=deployment_dict.get(&quot;spec&quot;).get(&quot;template&quot;).get(&quot;spec&quot;).get(&quot;containers&quot;)[0].get(
            &quot;imagePullPolicy&quot;),
        ports=[client.V1ContainerPort(container_port=3000)],
    )
    template = client.V1PodTemplateSpec(
        metadata=client.V1ObjectMeta(labels=deployment_dict.get(&quot;metadata&quot;).get(&quot;labels&quot;)),
        spec=client.V1PodSpec(containers=[container]))
    spec = client.V1DeploymentSpec(
        selector=deployment_dict.get(&quot;spec&quot;).get(&quot;selector&quot;).get(&quot;matchLabels&quot;),
        replicas=1,
        template=template)
    deployment = client.V1Deployment(
        api_version=deployment_dict.get(&quot;apiVersion&quot;),
        kind=deployment_dict.get(&quot;kind&quot;),
        metadata=client.V1ObjectMeta(name=deployment_dict.get(&quot;metadata&quot;).get(&quot;name&quot;)),
        spec=spec)
    response3 = apps_v1_api.create_namespaced_deployment(
        namespace=deployment_dict.get(&quot;metadata&quot;).get(&quot;namespace&quot;), body=deployment
    )
    print(response3)

print(deployment_dict.get(&quot;spec&quot;).get(&quot;selector&quot;).get(&quot;matchLabels&quot;))

def main():
    config.load_kube_config()
    apps_v1_api = client.AppsV1Api()

    create_deployment(apps_v1_api)

if __name__ == &quot;__main__&quot;:
    main()
</code></pre>
<p>then I got the error :</p>
<pre class="lang-sh prettyprint-override"><code>{'app': 'b3a1e295-d77c-41b4-a2e6-5cb75b7b2f61'}

Traceback (most recent call last):
  File &quot;test3.py&quot;, line 109, in &lt;module&gt;
    main()
  File &quot;test3.py&quot;, line 104, in main
    create_deployment(apps_v1_api)
  File &quot;test3.py&quot;, line 93, in create_deployment
    namespace=deployment_dict.get(&quot;metadata&quot;).get(&quot;namespace&quot;), body=deployment
  File &quot;/srv/http/lib/python3.7/site-packages/kubernetes/client/api/apps_v1_api.py&quot;, line 290, in create_namespaced_deployment
    (data) = self.create_namespaced_deployment_with_http_info(namespace, body, **kwargs)  # noqa: E501
  File &quot;/srv/http/lib/python3.7/site-packages/kubernetes/client/api/apps_v1_api.py&quot;, line 381, in create_namespaced_deployment_with_http_info
    collection_formats=collection_formats)
  File &quot;/srv/http/lib/python3.7/site-packages/kubernetes/client/api_client.py&quot;, line 345, in call_api
    _preload_content, _request_timeout)
  File &quot;/srv/http/lib/python3.7/site-packages/kubernetes/client/api_client.py&quot;, line 176, in __call_api
    _request_timeout=_request_timeout)
  File &quot;/srv/http/lib/python3.7/site-packages/kubernetes/client/api_client.py&quot;, line 388, in request
    body=body)
  File &quot;/srv/http/lib/python3.7/site-packages/kubernetes/client/rest.py&quot;, line 278, in POST
    body=body)
  File &quot;/srv/http/lib/python3.7/site-packages/kubernetes/client/rest.py&quot;, line 231, in request
    raise ApiException(http_resp=r)
kubernetes.client.rest.ApiException: (422)
Reason: Unprocessable Entity
HTTP response headers: HTTPHeaderDict({'Cache-Control': 'no-cache, private', 'Content-Type': 'application/json', 'Date': 'Fri, 24 Jul 2020 03:19:51 GMT', 'Content-Length': '692'})
HTTP response body: {&quot;kind&quot;:&quot;Status&quot;,&quot;apiVersion&quot;:&quot;v1&quot;,&quot;metadata&quot;:{},&quot;status&quot;:&quot;Failure&quot;,&quot;message&quot;:&quot;Deployment.apps \&quot;b3a1e295-d77c-41b4-a2e6-5cb75b7b2f61\&quot; is invalid: spec.selector: Invalid value: v1.LabelSelector{MatchLabels:map[string]string(nil), MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: empty selector is invalid for deployment&quot;,&quot;reason&quot;:&quot;Invalid&quot;,&quot;details&quot;:{&quot;name&quot;:&quot;b3a1e295-d77c-41b4-a2e6-5cb75b7b2f61&quot;,&quot;group&quot;:&quot;apps&quot;,&quot;kind&quot;:&quot;Deployment&quot;,&quot;causes&quot;:[{&quot;reason&quot;:&quot;FieldValueInvalid&quot;,&quot;message&quot;:&quot;Invalid value: v1.LabelSelector{MatchLabels:map[string]string(nil), MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: empty selector is invalid for deployment&quot;,&quot;field&quot;:&quot;spec.selector&quot;}]},&quot;code&quot;:422}
</code></pre>
<p>The error message prompts me that an empty selector is invalid for deployment, but I printed my selector is also valuable. There should be value in the API, but an error was reported and no success, I don't know why.</p>
<p>the yaml file:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Namespace
metadata:
  name: b3a1e295-d77c-41b4-a2e6-5cb75b7b2f61
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  #name: rewrite
  name: b3a1e295-d77c-41b4-a2e6-5cb75b7b2f61
  namespace: b3a1e295-d77c-41b4-a2e6-5cb75b7b2f61
  annotations:
    #nginx.ingress.kubernetes.io/rewrite-target: /$2
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - host: b3a1e295-d77c-41b4-a2e6-5cb75b7b2f61.kfquantum.com
    http:
      paths:
      - path: /
        backend:
          serviceName: b3a1e295-d77c-41b4-a2e6-5cb75b7b2f61
          servicePort: 80
---
apiVersion: v1
kind: Service
metadata:
 name: b3a1e295-d77c-41b4-a2e6-5cb75b7b2f61
 namespace: b3a1e295-d77c-41b4-a2e6-5cb75b7b2f61
spec:
 ports:
 - port: 80
   targetPort: 3000
 selector:
   app: b3a1e295-d77c-41b4-a2e6-5cb75b7b2f61
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: b3a1e295-d77c-41b4-a2e6-5cb75b7b2f61
  name: b3a1e295-d77c-41b4-a2e6-5cb75b7b2f61
  namespace: b3a1e295-d77c-41b4-a2e6-5cb75b7b2f61
spec:
  selector:
    matchLabels:
      app: b3a1e295-d77c-41b4-a2e6-5cb75b7b2f61
  replicas: 1
  template:
    metadata:
      labels:
        app: b3a1e295-d77c-41b4-a2e6-5cb75b7b2f61
    spec:
      containers:
      - image: theiaide/theia-python
        imagePullPolicy: IfNotPresent
        name: b3a1e295-d77c-41b4-a2e6-5cb75b7b2f61
        ports:
        - containerPort: 3000
        volumeMounts:
        - mountPath: /home/project
          name: test-volume
        #download private image key dependence private-reg pod
        imagePullSecrets:
        - name: regcred
        # dns 8.8.8.8
        dnsPolicy: &quot;None&quot;
        dnsConfig:
          nameservers:
            - 8.8.8.8
          searches:
            - ns1.svc.cluster-domain.example
            - my.dns.search.suffix
          options:
            - name: ndots
              value: &quot;2&quot;
            - name: edns0
        volumes:
        - name: test-volume
          hostPath:
            path: /home/project/b3a1e295-d77c-41b4-a2e6-5cb75b7b2f61

</code></pre>

Layne Wu
<p>It seems like there is something wrong with this line 🤔:</p>
<pre class="lang-py prettyprint-override"><code>    spec = client.V1DeploymentSpec(
        selector=deployment_dict.get(&quot;spec&quot;).get(&quot;selector&quot;).get(&quot;matchLabels&quot;),
        replicas=1,
        template=template)
</code></pre>
<p>In this case, the <code>selector</code> will end up with just</p>
<pre class="lang-yaml prettyprint-override"><code>app: b3a1e295-d77c-41b4-a2e6-5cb75b7b2f61
</code></pre>
<p>So the K8s API thinks that there is no selector at all since it expects the <code>matchLabels</code> key.</p>
<p>But I believe you want this 🤔:</p>
<pre class="lang-yaml prettyprint-override"><code>    matchLabels:
      app: b3a1e295-d77c-41b4-a2e6-5cb75b7b2f61
</code></pre>
<p>So you could try:</p>
<pre class="lang-py prettyprint-override"><code>    spec = client.V1DeploymentSpec(
        selector=deployment_dict.get(&quot;spec&quot;).get(&quot;selector&quot;),
        replicas=1,
        template=template)
</code></pre>
<p>✌️☮️</p>
