Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(CRDs): add basic CRD support to the dynamic client #214

Merged
merged 2 commits into from
Mar 9, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new line ☘️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:) I call it AAFN (Add A F****** Newline).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😱

40 changes: 40 additions & 0 deletions examples/using-crds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Use a Custom Resource Definition to extend the Kubernetes API and the client.
//
const Client = require('kubernetes-client').SyncClient
Copy link
Contributor

@gergelyke gergelyke Mar 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const {Client, config} = require('kubernetes-client');

const config = require('kubernetes-client').config;

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');
});
});
});