<p>We are trying to use deploy a service using knative with the python client library of Kubernetes. We are using the following yaml file:</p>

<pre><code>apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: test-{{ test_id }}
  namespace: default
spec:
  template:
    spec:
      containers:
        - image: test-deployment:latest
          resources:
            limits:
              cpu: 50m
              memory: 128Mi
            requests:
              cpu: 50m
              memory: 128Mi
      containerConcurrency: 1
</code></pre>

<p>If we deploy using the command line tool of kubernetes, it works fine.</p>

<pre><code>kubectl create -f test.yaml
</code></pre>

<p>With the python client library, we are doing:</p>

<pre><code>import kubernetes
import yaml
import uuid

from jinja2 import Template
from urllib3 import exceptions as urllib_exceptions

api = kubernetes.client.CoreV1Api(api_client=kubernetes.config.load_kube_config(context=cluster))

with open(deployment_yaml_path, 'r') as file_reader:
    file_content = file_reader.read()

deployment_template = Template(file_content)

deployment_template = yaml.safe_load(template.render({
    'test_id': str(uuid.uuid4())
}))

deployment = kubernetes.client.V1Service(
    api_version=deployment_template['apiVersion'],
    kind="Service",
    metadata=deployment_template['metadata'],
    spec=deployment_template['spec']
)

try:
    response = api.create_namespaced_service(body=deployment, namespace='default')
except (kubernetes.client.rest.ApiException, urllib_exceptions.HTTPError):
    raise TestError
</code></pre>

<p>However, we are getting this error:</p>

<pre><code>Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Audit-Id': 'a1968276-e16b-44f4-a40d-5eb5eaee9d47', 'Content-Type': 'application/json', 'Date': 'Thu, 23 Apr 2020 08:29:36 GMT', 'Content-Length': '347'})
HTTP response body: {
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {

  },
  "status": "Failure",
  "message": "Service in version \"v1\" cannot be handled as a Service: no kind \"Service\" is registered for version \"serving.knative.dev/v1\" in scheme \"k8s.io/kubernetes/pkg/api/legacyscheme/scheme.go:30\"",
  "reason": "BadRequest",
  "code": 400
}
</code></pre>

<p>Is there a way to deploy a service with knative? As far as I understood knative service is different than the normal Kubernetes service. I don't know whether the problem is that I'm trying to deploy the service in a wrong way or whether the Kubernetes python client library doesn't support this deployment yet.</p>

<p>Edit:</p>

<pre><code>Python Client Library: kubernetes==11.0.0

Kubernetes: 
Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.4", GitCommit:"67d2fcf276fcd9cf743ad4be9a9ef5828adc082f", GitTreeState:"clean", BuildDate:"2019-09-18T14:51:13Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"15+", GitVersion:"v1.15.11-gke.5", GitCommit:"a5bf731ea129336a3cf32c3375317b3a626919d7", GitTreeState:"clean", BuildDate:"2020-03-31T02:49:49Z", GoVersion:"go1.12.17b4", Compiler:"gc", Platform:"linux/amd64"}
</code></pre>

user3279394
<p><code>kubernetes.client.V1Service</code> is a reference to the Kubernetes &quot;Service&quot; concept, which is a selector across pods that appears as a network endpoint, rather than the Knative &quot;Service&quot; concept, which is the entire application which provides functionality over the network.</p>
<p>Based on <a href="https://github.com/kubernetes-client/python/blob/master/examples/custom_object.py" rel="nofollow noreferrer">this example from the <code>kubernetes-client/python</code> repo</a>, you need to do something like this to get and use a client for Knative services:</p>
<pre class="lang-py prettyprint-override"><code>api = kubernetes.client.CustomObjectsApi()
try:
    resource = api.create_namespaced_custom_object(
        group=&quot;serving.knative.dev&quot;,
        version=&quot;v1&quot;,
        plural=&quot;services&quot;,
        namespace=&quot;default&quot;,
        body=deployment_template)
except (kubernetes.client.rest.ApiException, urllib_exceptions.HTTPError):
    raise TestError
</code></pre>
<p>If you're going to be doing this a lot, you might want to make a helper that takes arguments similar to <code>create_namespaced_service</code>, and possibly also a wrapper object similar to <code>kubernetes.client.V1Service</code> to simplify creating Knative Services.</p>
