Skip to content

Commit

Permalink
refactor(ApiGroup): support custom root group resources
Browse files Browse the repository at this point in the history
The current code assumes every root group resources (e.g., /api/v1/nodes) is a
BaseObject. This adds support for custom resources (e.g., Pod or Deployment).
This is a ground work for:

#47
  • Loading branch information
Silas Boyd-Wickizer committed May 14, 2017
1 parent 11b8a6d commit 88a90e9
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 24 deletions.
28 changes: 20 additions & 8 deletions lib/api-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,30 @@ class ApiGroup {
});

//
// Create resources that live at the root (e.g., /api/v1/nodes)
// Create "group" resources that live at the root (e.g., /api/v1/nodes)
//
for (const type of options.genericTypes) {
this[type] = new BaseObject({
name: type,
parentPath: this.path,
api: this
});
}
options.groupTypes.forEach(type => this.addResource(type));

aliasResources(this);
}

/**
* Add a resource (e.g., nodes) to the ApiGroup group
* @param {string|object} options - resource name or options object
* @param {string} options.name - resource name
* @param {fn} options.Constructor - constructor for new resource
*/
addResource(options) {
if (typeof options === 'string') {
options = { name: options, Constructor: BaseObject };
}
this[options.name] = new options.Constructor({
api: this,
name: options.name,
parentPath: this.path
});
}

/**
* Return a full Kuberentes API path (including the version)
* @param {string} path - version-less path
Expand Down
4 changes: 2 additions & 2 deletions lib/apps.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ const ApiGroup = require('./api-group');

class Apps extends ApiGroup {
constructor(options) {
const genericTypes = [
const groupTypes = [
// Deprecated name of statefulsets in kubernetes 1.4
'petsets',
'statefulsets'
];
options = Object.assign({}, options, {
path: 'apis/apps',
version: options.version || 'v1beta1',
genericTypes: genericTypes
groupTypes: groupTypes
});
super(options);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const ApiGroup = require('./api-group');

class Batch extends ApiGroup {
constructor(options) {
const genericTypes = [
const groupTypes = [
'cronjobs',
'jobs',
// Deprecated name for cronjobs in kubernetes 1.4
Expand All @@ -13,7 +13,7 @@ class Batch extends ApiGroup {
options = Object.assign({}, options, {
path: 'apis/batch',
version: options.version || 'v1',
genericTypes: genericTypes
groupTypes: groupTypes
});
super(options);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const ApiGroup = require('./api-group');

class Core extends ApiGroup {
constructor(options) {
const genericTypes = [
const groupTypes = [
'componentstatuses',
'configmaps',
'endpoints',
Expand All @@ -24,7 +24,7 @@ class Core extends ApiGroup {
options = Object.assign({}, options, {
path: 'api',
version: options.version || 'v1',
genericTypes: genericTypes
groupTypes: groupTypes
});
super(options);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const ApiGroup = require('./api-group');

class Extensions extends ApiGroup {
constructor(options) {
const genericTypes = [
const groupTypes = [
'daemonsets',
'deployments',
'horizontalpodautoscalers',
Expand All @@ -16,7 +16,7 @@ class Extensions extends ApiGroup {
options = Object.assign({}, options, {
path: 'apis/extensions',
version: options.version || 'v1beta1',
genericTypes: genericTypes
groupTypes: groupTypes
});
super(options);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/rbac.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const ApiGroup = require('./api-group');

class Rbac extends ApiGroup {
constructor(options) {
const genericTypes = [
const groupTypes = [
'clusterroles',
'clusterrolebindings',
'roles',
Expand All @@ -13,7 +13,7 @@ class Rbac extends ApiGroup {
options = Object.assign({}, options, {
path: 'apis/rbac.authorization.k8s.io',
version: options.version || 'v1alpha1',
genericTypes: genericTypes
groupTypes: groupTypes
});
super(options);
}
Expand Down
8 changes: 2 additions & 6 deletions lib/third-party-resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ThirdPartyResource extends ApiGroup {
options = Object.assign({}, options, {
path: `apis/${ options.group }`,
version: options.version || 'v1',
genericTypes: []
groupTypes: []
});
super(options);

Expand All @@ -19,11 +19,7 @@ class ThirdPartyResource extends ApiGroup {

addResource(resourceName) {
this.namespace.addResource(resourceName);
this[resourceName] = new BaseObject({
api: this,
name: resourceName,
parentPath: this.path
});
super.addResource(resourceName);
return this;
}
}
Expand Down

0 comments on commit 88a90e9

Please sign in to comment.