Skip to content

Instantly share code, notes, and snippets.

@eschwartz
Last active November 16, 2016 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eschwartz/4f16fcb963954d433753ffe32ce5d5ca to your computer and use it in GitHub Desktop.
Save eschwartz/4f16fcb963954d433753ffe32ce5d5ca to your computer and use it in GitHub Desktop.
Node.js ReplicationController model
const _ = require('lodash');
/**
* @param {{
* name: string,
* version: string,
* image: string,
* labels?: Object,
* replicas?: int,
* imagePullPolicy?: 'Always' | 'Never' | 'IfNotPresent',
* resources: {
* requests: { cpu: string, memory: string },
* limits: { cpu: string, memory: string },
* },
* env?: Object
* }} opts
* @returns {Object}
* @constructor
*/
function ReplicationController(opts) {
opts = _.defaultsDeep(opts, {}, {
replicas: 1,
imagePullPolicy: 'Always',
env: {},
resources: {
limits: {
cpu: '1',
memory: '1.5Gi'
},
requests: {
cpu: 0.5,
memory: '1Gi'
}
},
labels: {
name: opts.name,
version: opts.version
},
volumes: []
});
return {
apiVersion: 'v1',
kind: 'ReplicationController',
metadata: {
name: opts.name,
labels: opts.labels
},
spec: {
replicas: opts.replicas,
selector: {
name: opts.name
},
template: {
metadata: {
labels: opts.labels
},
spec: {
containers: [
{
name: opts.name,
image: opts.image,
imagePullPolicy: opts.imagePullPolicy,
resources: opts.resources,
ports: [
{
name: 'http',
containerPort: 80
}
],
env: _.map(opts.env, (value, name) => ({ name, value }))
}
]
}
}
}
};
}
module.exports = ReplicationController;
import * as _ from 'lodash';
function ReplicationController(opts: IOptions): Object {
opts = <IOptions>_.defaultsDeep(opts, {}, {
replicas: 1,
imagePullPolicy: 'Always',
env: {},
resources: {
limits: {
cpu: '1',
memory: '1.5Gi'
},
requests: {
cpu: 0.5,
memory: '1Gi'
}
},
labels: {
name: opts.name,
version: opts.version
},
volumes: []
});
return {
apiVersion: 'v1',
kind: 'ReplicationController',
metadata: {
name: opts.name,
labels: opts.labels
},
spec: {
replicas: opts.replicas,
selector: {
name: opts.name
},
template: {
metadata: {
labels: opts.labels
},
spec: {
containers: [
{
name: opts.name,
image: opts.image,
imagePullPolicy: opts.imagePullPolicy,
resources: opts.resources,
ports: [
{
name: 'http',
containerPort: 80
}
],
env: _.map(<any>opts.env, (value, name) => ({name, value})),
volumeMounts: opts.volumes.map(v => ({
name: v.name,
mountPath: v.mountPath
}))
}
],
volumes: opts.volumes.map(v => ({
name: v.name,
nfs: {
server: v.server,
path: v.path
}
}))
}
}
}
};
}
export interface IVolumeMountOptions {
name: string;
server: string;
path: string;
mountPath: string;
}
export interface IResourceLimitOptions {
cpu?: string;
memory?: string;
}
export interface IOptions {
name: string;
version: string;
image: string;
labels?: Object;
replicas?: number;
imagePullPolicy?: 'Always' | 'Never' | 'IfNotPresent';
resources?: {
requests?: IResourceLimitOptions;
limits?: IResourceLimitOptions;
};
env?: Object;
volumes?: IVolumeMountOptions[];
}
export default ReplicationController
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment