Skip to content

Commit cb9f7e4

Browse files
popomoresilasbw
authored andcommittedAug 7, 2018
feat(CRDs): add Cluster scope support (#307)
Closes #215
1 parent 2014d27 commit cb9f7e4

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed
 

‎examples/apply-deploy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const deploymentManifest = require('./nginx-deployment.json');
88

99
async function applyDeploy() {
1010
const client = new Client({ config: config.fromKubeconfig(), version: '1.9' });
11-
11+
1212
try {
1313
const create = await client.apis.apps.v1.namespaces('default').deployments.post({ body: deploymentManifest });
1414
console.log('Create:', create);

‎lib/swagger-client.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -156,26 +156,27 @@ class Component {
156156
const version = manifest.spec.version;
157157
const name = manifest.spec.names.plural;
158158
const spec = { paths: {}};
159+
const namespace = manifest.spec.scope === 'Cluster' ? '' : '/namespaces/{namespace}';
159160

160161
//
161162
// Make just enough of Swagger spec to generate some useful endpoints.
162163
//
163-
const templatePath = `/apis/${ group }/${ version }/namespaces/{namespace}/${ name }/{name}`;
164+
const templatePath = `/apis/${ group }/${ version }${ namespace }/${ name }/{name}`;
164165
spec.paths[templatePath] = ['delete', 'get', 'patch', 'put'].reduce((acc, method) => {
165166
acc[method] = { operationId: `${ method }Template${ name }` };
166167
return acc;
167168
}, {});
168169

169-
const path = `/apis/${ group }/${ version }/namespaces/{namespace}/${ name }`;
170+
const path = `/apis/${ group }/${ version }${ namespace }/${ name }`;
170171
spec.paths[path] = ['get', 'post'].reduce((acc, method) => {
171172
acc[method] = { operationId: `${ method }${ name }` };
172173
return acc;
173174
}, {});
174175

175176
const watchPaths = {
176177
watchCluster: `/apis/${ group }/${ version }/watch/${ name }`,
177-
watchNamespace: `/apis/${ group }/${ version }/namespaces/{namespace}/watch/${ name }`,
178-
watchResource: `/apis/${ group }/${ version }/namespaces/{namespace}/watch/${ name }/{name}`
178+
watchNamespace: `/apis/${ group }/${ version }${ namespace }/watch/${ name }`,
179+
watchResource: `/apis/${ group }/${ version }${ namespace }/watch/${ name }/{name}`
179180
};
180181
Object.keys(watchPaths).forEach(operationId => {
181182
const watchPath = watchPaths[operationId];

‎test/swagger-client.test.js

+26
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ describe('lib.swagger-client', () => {
194194
const client = new Client({ spec: { paths: {}}, http: {}});
195195
const crd = {
196196
spec: {
197+
scope: 'Namespaced',
197198
group: 'stable.example.com',
198199
version: 'v1',
199200
names: {
@@ -213,6 +214,31 @@ describe('lib.swagger-client', () => {
213214
assume(client.apis['stable.example.com'].v1.namespaces('default').watch.foos.getStream).is.a('function');
214215
assume(client.apis['stable.example.com'].v1.namespaces('default').watch.foos('blah').getStream).is.a('function');
215216
});
217+
218+
it('adds functions for Cluster CustomResourceDefinitions', () => {
219+
const client = new Client({ spec: { paths: {}}, http: {}});
220+
const crd = {
221+
spec: {
222+
scope: 'Cluster',
223+
group: 'stable.example.com',
224+
version: 'v1',
225+
names: {
226+
plural: 'foos'
227+
}
228+
}
229+
};
230+
client.addCustomResourceDefinition(crd);
231+
assume(client.apis['stable.example.com'].v1.foos.get).is.a('function');
232+
assume(client.apis['stable.example.com'].v1.foos.post).is.a('function');
233+
assume(client.apis['stable.example.com'].v1.foos('blah').get).is.a('function');
234+
assume(client.apis['stable.example.com'].v1.foos('blah').delete).is.a('function');
235+
assume(client.apis['stable.example.com'].v1.foos('blah').get).is.a('function');
236+
assume(client.apis['stable.example.com'].v1.foos('blah').patch).is.a('function');
237+
assume(client.apis['stable.example.com'].v1.foos('blah').put).is.a('function');
238+
assume(client.apis['stable.example.com'].v1.watch.foos.getStream).is.a('function');
239+
assume(client.apis['stable.example.com'].v1.watch.foos.getStream).is.a('function');
240+
assume(client.apis['stable.example.com'].v1.watch.foos('blah').getStream).is.a('function');
241+
});
216242
});
217243
});
218244
});

0 commit comments

Comments
 (0)
Please sign in to comment.