Skip to content

Commit

Permalink
feat(CRDs): add Cluster scope support (#307)
Browse files Browse the repository at this point in the history
Closes #215
  • Loading branch information
popomore authored and silasbw committed Aug 7, 2018
1 parent 2014d27 commit cb9f7e4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/apply-deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const deploymentManifest = require('./nginx-deployment.json');

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

try {
const create = await client.apis.apps.v1.namespaces('default').deployments.post({ body: deploymentManifest });
console.log('Create:', create);
Expand Down
9 changes: 5 additions & 4 deletions lib/swagger-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,26 +156,27 @@ class Component {
const version = manifest.spec.version;
const name = manifest.spec.names.plural;
const spec = { paths: {}};
const namespace = manifest.spec.scope === 'Cluster' ? '' : '/namespaces/{namespace}';

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

const path = `/apis/${ group }/${ version }/namespaces/{namespace}/${ name }`;
const path = `/apis/${ group }/${ version }${ namespace }/${ name }`;
spec.paths[path] = ['get', 'post'].reduce((acc, method) => {
acc[method] = { operationId: `${ method }${ name }` };
return acc;
}, {});

const watchPaths = {
watchCluster: `/apis/${ group }/${ version }/watch/${ name }`,
watchNamespace: `/apis/${ group }/${ version }/namespaces/{namespace}/watch/${ name }`,
watchResource: `/apis/${ group }/${ version }/namespaces/{namespace}/watch/${ name }/{name}`
watchNamespace: `/apis/${ group }/${ version }${ namespace }/watch/${ name }`,
watchResource: `/apis/${ group }/${ version }${ namespace }/watch/${ name }/{name}`
};
Object.keys(watchPaths).forEach(operationId => {
const watchPath = watchPaths[operationId];
Expand Down
26 changes: 26 additions & 0 deletions test/swagger-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ describe('lib.swagger-client', () => {
const client = new Client({ spec: { paths: {}}, http: {}});
const crd = {
spec: {
scope: 'Namespaced',
group: 'stable.example.com',
version: 'v1',
names: {
Expand All @@ -213,6 +214,31 @@ describe('lib.swagger-client', () => {
assume(client.apis['stable.example.com'].v1.namespaces('default').watch.foos.getStream).is.a('function');
assume(client.apis['stable.example.com'].v1.namespaces('default').watch.foos('blah').getStream).is.a('function');
});

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

0 comments on commit cb9f7e4

Please sign in to comment.