python kubernetes API error when trying to mount volume

8/5/2019

I am using python with python-kubernetes with a minikube running locally, e.g there are no cloud issues.

I am trying to create a job and provide it with data to run on. I would like to provide it with a mount of a directory with my local machine data.

I am using this example and trying to add a mount volume This is my code after adding the keyword volume_mounts (I tried multiple places, multiple keywords and nothing works)

from os import path

import yaml

from kubernetes import client, config

JOB_NAME = "pi"


def create_job_object():
    # Configureate Pod template container
    container = client.V1Container(
        name="pi",
        image="perl",
        volume_mounts=["/home/user/data"],
        command=["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"])
    # Create and configurate a spec section
    template = client.V1PodTemplateSpec(
        metadata=client.V1ObjectMeta(labels={
            "app": "pi"}),
        spec=client.V1PodSpec(restart_policy="Never",
                              containers=[container]))
    # Create the specification of deployment
    spec = client.V1JobSpec(
        template=template,
        backoff_limit=0)
    # Instantiate the job object
    job = client.V1Job(
        api_version="batch/v1",
        kind="Job",
        metadata=client.V1ObjectMeta(name=JOB_NAME),
        spec=spec)

    return job


def create_job(api_instance, job):
    # Create job
    api_response = api_instance.create_namespaced_job(
        body=job,
        namespace="default")
    print("Job created. status='%s'" % str(api_response.status))


def update_job(api_instance, job):
    # Update container image
    job.spec.template.spec.containers[0].image = "perl"
    # Update the job
    api_response = api_instance.patch_namespaced_job(
        name=JOB_NAME,
        namespace="default",
        body=job)
    print("Job updated. status='%s'" % str(api_response.status))


def delete_job(api_instance):
    # Delete job
    api_response = api_instance.delete_namespaced_job(
        name=JOB_NAME,
        namespace="default",
        body=client.V1DeleteOptions(
            propagation_policy='Foreground',
            grace_period_seconds=5))
    print("Job deleted. status='%s'" % str(api_response.status))


def main():
    # Configs can be set in Configuration class directly or using helper
    # utility. If no argument provided, the config will be loaded from
    # default location.
    config.load_kube_config()
    batch_v1 = client.BatchV1Api()
    # Create a job object with client-python API. The job we
    # created is same as the `pi-job.yaml` in the /examples folder.


    job = create_job_object()

    create_job(batch_v1, job)

    update_job(batch_v1, job)

    delete_job(batch_v1)


if __name__ == '__main__':
    main()

I get this error

HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Job in version \"v1\" cannot be handled as a Job: v1.Job.Spec: v1.JobSpec.Template: v1.PodTemplateSpec.Spec: v1.PodSpec.Containers: []v1.Container: v1.Container.VolumeMounts: []v1.VolumeMount: readObjectStart: expect { or n, but found \", error found in #10 byte of ...|ounts\": [\"/home/user|..., bigger context ...| \"image\": \"perl\", \"name\": \"pi\", \"volumeMounts\": [\"/home/user/data\"]}], \"restartPolicy\": \"Never\"}}}}|...","reason":"BadRequest","code":400

What am i missing here?

Is there another way to expose data to the job?

edit: trying to use client.V1Volumemount I am trying to add this code, and add mount object in different init functions eg.

mount = client.V1VolumeMount(mount_path="/data", name="shai")

client.V1Container
client.V1PodTemplateSpec
client.V1JobSpec
client.V1Job

under multiple keywords, it all results in errors, is this the correct object to use? How shell I use it if at all?

edit: trying to pass volume_mounts as a list with the following code suggested in the answers:

def create_job_object():
    # Configureate Pod template container
    container = client.V1Container(
        name="pi",
        image="perl",
        volume_mounts=["/home/user/data"],
        command=["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"])
    # Create and configurate a spec section
    template = client.V1PodTemplateSpec(
        metadata=client.V1ObjectMeta(labels={
            "app": "pi"}),
        spec=client.V1PodSpec(restart_policy="Never",
                              containers=[container]))
    # Create the specification of deployment
    spec = client.V1JobSpec(
        template=template,
        backoff_limit=0)
    # Instantiate the job object
    job = client.V1Job(
        api_version="batch/v1",
        kind="Job",
        metadata=client.V1ObjectMeta(name=JOB_NAME),
        spec=spec)

    return job

And still getting a similar error

kubernetes.client.rest.ApiException: (422) Reason: Unprocessable Entity HTTP response headers: HTTPHeaderDict({'Content-Type': 'application/json', 'Date': 'Tue, 06 Aug 2019 06:19:13 GMT', 'Content-Length': '401'}) HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Job.batch \"pi\" is invalid: spec.template.spec.containers[0].volumeMounts[0].name: Not found: \"d\"","reason":"Invalid","details":{"name":"pi","group":"batch","kind":"Job","causes":[{"reason":"FieldValueNotFound","message":"Not found: \"d\"","field":"spec.template.spec.containers[0].volumeMounts[0].name"}]},"code":422}

-- thebeancounter
devops
docker
kubernetes
minikube
python

1 Answer

8/5/2019

The V1Container call is expecting a list of V1VolumeMount objects for volume_mounts parameter but you passed in a list of string:

Code:

def create_job_object():
    volume_mount = client.V1VolumeMount(
        mount_path="/home/user/data"
        # other optional arguments, see the volume mount doc link below
    )
    # Configureate Pod template container
    container = client.V1Container(
name="pi",
        image="perl",
        volume_mounts=[volume_mount],
        command=["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"])
    # Create and configurate a spec section
    template = client.V1PodTemplateSpec(
        metadata=client.V1ObjectMeta(labels={
            "app": "pi"}),
        spec=client.V1PodSpec(restart_policy="Never",
                              containers=[container]))
    ....

references:

-- congbaoguier
Source: StackOverflow