Skip to content

Commit

Permalink
feat(CRDs): add basic CRD support to the dynamic client (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
silasbw committed Mar 9, 2018
1 parent 597a5b4 commit c2aec51
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
20 changes: 20 additions & 0 deletions examples/crontabs-crd.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"kind": "CustomResourceDefinition",
"spec": {
"scope": "Namespaced",
"version": "v1",
"group": "stable.example.com",
"names": {
"shortNames": [
"ct"
],
"kind": "CronTab",
"plural": "crontabs",
"singular": "crontab"
}
},
"apiVersion": "apiextensions.k8s.io/v1beta1",
"metadata": {
"name": "crontabs.stable.example.com"
}
}
39 changes: 39 additions & 0 deletions examples/using-crds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// Use a Custom Resource Definition to extend the Kubernetes API and the client.
//
const { Client,config } = require('kubernetes-client');

const crd = require('./crontabs-crd.json');

async function main() {
try {
const client = new Client({ config: config.fromKubeconfig(), version: '1.9' });

//
// Create the CRD with the Kubernetes API
//
const create = await client.apis['apiextensions.k8s.io'].v1beta1.customresourcedefinitions.post({ body: crd });
console.log('Create: ', create);

//
// Add endpoints to our client
//
client.addCustomResourceDefinition(crd);

//
// List all the resources of the new type
//
const all = await client.apis['stable.example.com'].v1.namespaces('default').crontabs.get();
console.log('All: ', all);

//
// Get a specific resources.
//
const one = await client.apis['stable.example.com'].v1.namespaces('default').crontabs('foo').get();
console.log('One: ', one);
} catch (err) {
console.error('Error: ', err);
}
}

main();
37 changes: 37 additions & 0 deletions lib/swagger-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,43 @@ class Component {
});
}

/**
* Add endpoints for a CustomeResourceDefinition.
* @param {object} manifest - CustomerResourceDefinition manifest
*/
addCustomResourceDefinition(manifest) {
const group = manifest.spec.group;
const version = manifest.spec.version;
const name = manifest.spec.names.plural;

//
// Make just enough of Swagger spec to generate some useful endpoints.
//
const templatePath = `/apis/${ group }/${ version }/namespaces/{namespace}/${ name }/{name}`;
const templateOperations = ['delete', 'get', 'patch', 'post', 'put'].reduce((acc, method) => {
acc[method] = {
operationId: `${ method }${ name }`
};
return acc;
}, {});

const path = `/apis/${ group }/${ version }/namespaces/{namespace}/${ name }`;
const operations = {
get: {
operationId: `list${ name }`
}
};

const spec = {
paths: {
[path]: operations,
[templatePath]: templateOperations
}
};

this._addSpec(spec);
}

_walkSplits(endpoint) {
const splits = this.splits.slice();
const nextSplits = endpoint.splits.slice();
Expand Down
21 changes: 21 additions & 0 deletions test/swagger-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,26 @@ describe('lib.swagger', () => {
assume(client.foo.deployment).is.truthy();
assume(client.foo.deploy).is.truthy();
});

it('adds functions for Namespaced CustomResourceDefinitions', () => {
const client = new SwaggerClient({ spec: { paths: {}}});
const crd = {
spec: {
group: 'stable.example.com',
version: 'v1',
names: {
plural: 'foos'
}
}
};
client.addCustomResourceDefinition(crd);
assume(client.apis['stable.example.com'].v1.namespaces('default').foos.get).is.a('function');
assume(client.apis['stable.example.com'].v1.namespaces('default').foos('blah').get).is.a('function');
assume(client.apis['stable.example.com'].v1.namespaces('default').foos('blah').delete).is.a('function');
assume(client.apis['stable.example.com'].v1.namespaces('default').foos('blah').get).is.a('function');
assume(client.apis['stable.example.com'].v1.namespaces('default').foos('blah').patch).is.a('function');
assume(client.apis['stable.example.com'].v1.namespaces('default').foos('blah').post).is.a('function');
assume(client.apis['stable.example.com'].v1.namespaces('default').foos('blah').put).is.a('function');
});
});
});

0 comments on commit c2aec51

Please sign in to comment.