godaddy / kubernetes-client

Simplified Kubernetes API client for Node.js.
MIT License
962 stars 192 forks source link

Can't schedule jobs, server could not find the requested resource #104

Closed javierbq closed 7 years ago

javierbq commented 7 years ago

I am having trouble running the example job from the k8s documentation using the client.

const Api = require('kubernetes-client');
var config = Api.config.fromKubeconfig();
config.apiVersion = 'batch/v1';
const ext = new Api.Extensions(config);

ext.namespaces.jobs.post({
    body: {
      apiVersion: 'batch/v1',
      kind: 'Job',
      metadata: {
        name: 'pi'
      },
      spec: {
        template: {
          spec: {
            restartPolicy: 'Never',
            containers: [{
              image: 'perl',
              command: ['perl', '-Mbignum=bpi', '-wle', 'print bpi(2000)'],
              name: 'pi'
            }]
          },
          metadata: {
            name: 'pi'
          }
        }
      }
    }
  },
  function(err, job) {
    if (err) return console.log('Failed to creata job: ' + err);
    console.log(job);
  });

When I run the script I get the following error:

Failed to creata job: Error: the server could not find the requested resource

But I can schedule the job without problem using kubectl

$ curl -O https://raw.githubusercontent.com/kubernetes/kubernetes.github.io/master/docs/concepts/workloads/controllers/job.yaml
$ kubectl create -f job.yaml
job "pi" created

Am I missing something or I a not configuring the extension correctly?

javierbq commented 7 years ago

Figured it out.

roeyazroel commented 7 years ago

@javierbq how did you solve the issue?

Same here. 10x

obezuk commented 7 years ago

+1 Same issue, how did you solve it?

obezuk commented 7 years ago

Hi @roeyazroel,

I figured it out myself as well. The reasons is because the batch/v1 API isn't considered part of the "extensions" group.

Try constructing an API object as follows:

const K8S_BATCH_V1 = new Api.Batch({
  url: process.env.K8S_URL,
  version: 'v1',
});

You should then be able to execute batch job apis as normal, e.g.

K8S_BATCH_V1.ns('default').job.get ...