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

Support more types #57

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 13 additions & 1 deletion lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

const Core = require('./core');
const Extensions = require('./extensions');
const Apps = require('./apps');
const Batch = require('./batch');
const Rbac = require('./rbac');

const groups = {
extensions: Extensions
extensions: Extensions,
apps: Apps,
batch: Batch,
'rbac.authorization.k8s.io': Rbac
};

class Api {
Expand All @@ -13,11 +19,17 @@ class Api {
* @param {object} options - Options to pass to client constructors
* @param {object} options.core - Optional default Core client
* @param {object} options.extensions - Optional default Extensions client
* @param {object} options.apps - Optional default Apps client
* @param {object} options.batch - Optional default Batch client
* @param {object} options.rbac - Optional default RBAC client
*/
constructor(options) {
this.options = options;
this.core = options.core || new Core(options);
this.extensions = options.extensions || new Extensions(options);
this.apps = options.apps || new Apps(options);
this.batch = options.batch || new Batch(options);
this.rbac = options.rbac || new Rbac(options);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions lib/apps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const ApiGroup = require('./api-group');

class Apps extends ApiGroup {
constructor(options) {
const genericTypes = [
// Deprecated name of statefulsets in kubernetes 1.4
'petsets',
'statefulsets'
];
options = Object.assign({}, options, {
path: 'apis/apps',
version: options.version || 'v1beta1',
Copy link
Contributor

Choose a reason for hiding this comment

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

How should we handle the petset -> statefulset rename 1?

The simplest thing would be to add statefulset to the genericTypes array and add a comment about the rename. Any better ideas?

genericTypes: genericTypes
});
super(options);
}
}

module.exports = Apps;
21 changes: 21 additions & 0 deletions lib/batch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const ApiGroup = require('./api-group');

class Batch extends ApiGroup {
constructor(options) {
const genericTypes = [
'cronjobs',
// Deprecated name for cronjobs in kubernetes 1.4
'scheduledjobs'
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar rename issue as above -- scheduledjobs -> cronjobs 1

];
options = Object.assign({}, options, {
path: 'apis/batch',
version: options.version || 'v1',
genericTypes: genericTypes
});
super(options);
}
}

module.exports = Batch;
12 changes: 11 additions & 1 deletion lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ module.exports.aliasResources = function (resourceObject) {
// http://kubernetes.io/docs/user-guide/kubectl-overview/
// and anything else we think is useful.
const resourceAliases = {
clusterroles: [],
Copy link
Contributor

Choose a reason for hiding this comment

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

should we include petsets and scheduledjobs?

clusterrolebindings: [],
componentstatuses: ['cs'],
configmaps: ['cm'],
cronjobs: [],
daemonsets: ['ds'],
deployments: ['deploy'],
events: ['ev'],
Expand All @@ -19,13 +22,20 @@ module.exports.aliasResources = function (resourceObject) {
nodes: ['no'],
persistentvolumes: ['pv'],
persistentvolumeclaims: ['pvc'],
// Deprecated name of statefulsets in kubernetes 1.4
petsets: [],
pods: ['po'],
replicationcontrollers: ['rc'],
replicasets: ['rs'],
resourcequotas: ['quota'],
roles: [],
rolebindings: [],
// Deprecated name of cronjobs in kubernetes 1.4
scheduledjobs: [],
secrets: [],
serviceaccounts: [],
services: ['svc']
services: ['svc'],
statefulsets: []
};

const esPlurals = {
Expand Down
3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module.exports.Api = require('./api');
module.exports.Core = core;
module.exports.Extensions = require('./extensions');
module.exports.config = require('./config')
module.exports.Apps = require('./apps');
module.exports.Batch = require('./batch');
module.exports.Rbac = require('./rbac');
module.exports.testUtils = {
aliasResources: require('./common').aliasResources
};
12 changes: 11 additions & 1 deletion lib/namespaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ class Namespaces extends BaseObject {
// Generic objects we don't implement special functionality for.
//
const genericTypes = [
'clusterroles',
Copy link
Contributor

Choose a reason for hiding this comment

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

should we include petsets and scheduledjobs?

'clusterrolebindings',
'configmaps',
'cronjobs',
'daemonsets',
'deployments',
'endpoints',
Expand All @@ -47,11 +50,18 @@ class Namespaces extends BaseObject {
'jobs',
'limitranges',
'persistentvolumeclaims',
// Deprecated name of statefulsets in kubernetes 1.4,
'petsets',
'replicasets',
'resourcequotas',
'roles',
'rolebindings',
// Deprecated name of cronjobs in kubernetes 1.4
'scheduledjobs',
'secrets',
'serviceaccounts',
'services'
'services',
'statefulsets'
];
for (const type of genericTypes) {
this[type] = new BaseObject(
Expand Down
22 changes: 22 additions & 0 deletions lib/rbac.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const ApiGroup = require('./api-group');

class Rbac extends ApiGroup {
constructor(options) {
const genericTypes = [
'clusterroles',
'clusterrolebindings',
'roles',
'rolebindings'
];
options = Object.assign({}, options, {
path: 'apis/rbac.authorization.k8s.io',
version: options.version || 'v1alpha1',
genericTypes: genericTypes
});
super(options);
}
}

module.exports = Rbac;
62 changes: 24 additions & 38 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');

const Core = require('../lib/core');
const Extensions = require('../lib/extensions');
const Api = require('../lib/api');

const defaultName = process.env.NAMESPACE || 'integration-tests';
Expand Down Expand Up @@ -61,6 +59,23 @@ function newName() {
return `${ defaultName }-${ buffer.toString('hex') }`;
}

function injectApis(moduleExports, options, versionOverride) {
// APIs and their default version
const apis = {
'api': [ require('../lib/core'), 'v1' ],
'extensions': [ require('../lib/extensions'), 'v1beta1' ]
}
Object.keys(apis).forEach(apiName => {
const api = apis[apiName];
moduleExports[apiName] = new (Function.prototype.bind.call(api[0], null, Object.assign({}, options, {
version: versionOverride || api[1],
})));
});

// Add the apiGroup with the same options but no version
moduleExports.apiGroup = new Api(options);
}

function changeNameInt(cb) {
let url;
let ca;
Expand Down Expand Up @@ -97,31 +112,13 @@ function changeNameInt(cb) {
const currentName = newName();
module.exports.currentName = currentName;

module.exports.api = new Core({
url: url,
ca: ca,
cert: cert,
key: key,
version: process.env.VERSION || 'v1',
namespace: currentName
});

module.exports.extensions = new Extensions({
url: url,
ca: ca,
cert: cert,
key: key,
version: process.env.VERSION || 'v1beta1',
namespace: currentName
});

module.exports.apiGroup = new Api({
injectApis(module.exports, {
url: url,
ca: ca,
cert: cert,
key: key,
namespace: currentName
});
}, process.env.VERSION);

module.exports.api.ns.post({
body: {
Expand All @@ -147,25 +144,14 @@ function changeNameInt(cb) {
}

function changeNameUnit() {
const mockUrl = 'http://mock.kube.api';
const currentName = newName();
module.exports.currentName = currentName;

module.exports.api = new Core({
url: 'http://mock.kube.api',
version: process.env.VERSION || 'v1',
namespace: currentName
});

module.exports.extensions = new Extensions({
url: 'http://mock.kube.api',
version: process.env.VERSION || 'v1beta1',
namespace: currentName
});

module.exports.apiGroup = new Api({
url: 'http://mock.kube.api',
namespace: currentName
});
injectApis(module.exports, {
url: mockUrl,
namespace: currentName
}, process.env.VERSION);
}

function changeName(cb) {
Expand Down