Last active
November 16, 2016 17:22
-
-
Save eschwartz/4f16fcb963954d433753ffe32ce5d5ca to your computer and use it in GitHub Desktop.
Node.js ReplicationController model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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