Skip to content

Commit

Permalink
Merge pull request #2611 from terascope/k8s-add-external-ports
Browse files Browse the repository at this point in the history
add basic external_ports functionality
  • Loading branch information
godber authored Apr 13, 2021
2 parents 7113517 + a192d0d commit a16dd2d
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 147 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "teraslice-workspace",
"displayName": "Teraslice",
"version": "0.74.0",
"version": "0.75.0",
"private": true,
"homepage": "https://github.com/terascope/teraslice",
"bugs": {
Expand Down
2 changes: 1 addition & 1 deletion packages/job-components/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@terascope/job-components",
"displayName": "Job Components",
"version": "0.49.0",
"version": "0.50.0",
"description": "A teraslice library for validating jobs schemas, registering apis, and defining and running new Job APIs",
"homepage": "https://github.com/terascope/teraslice/tree/master/packages/job-components#readme",
"bugs": {
Expand Down
2 changes: 2 additions & 0 deletions packages/job-components/src/interfaces/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ export interface ValidatedJobConfig {
/** This will only be available in the context of k8s */
ephemeral_storage?: boolean;
/** This will only be available in the context of k8s */
exposed_ports?: number[];
/** This will only be available in the context of k8s */
memory?: number;
/** This will only be available in the context of k8s */
memory_execution_controller?: number;
Expand Down
31 changes: 31 additions & 0 deletions packages/job-components/src/job-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import { Context } from './interfaces';
const cpuCount = os.cpus().length;
const workers = cpuCount < 5 ? cpuCount : 5;

/**
* This schema is for a Teraslice Job definition.
* @param context Teraslice context object
* @returns Complete convict schema for the Teraslice Job
*/
export function jobSchema(context: Context): convict.Schema<any> {
const schemas: convict.Schema<any> = {
active: {
Expand Down Expand Up @@ -238,6 +243,26 @@ export function jobSchema(context: Context): convict.Schema<any> {
format: Boolean
};

schemas.external_ports = {
doc: 'A numerical array of ports that should be exposed as external ports on the pods',
default: undefined,
format(arr) {
// TODO: What should we really do to validate this? It can be
// omitted, an empty array, or an array with numbers. It can't
// contain anything other than numbers. Processors should be able
// to have reserved ports. That is, if a job has port X but a
// processor requires port X this code should throw an error.
if (arr != null) {
if (!Array.isArray(arr)) {
throw new Error('external_ports is required to be an array');
}
if (arr.includes(45680)) {
throw new Error('Port 45680 cannot be included in external_ports, it is reserved by Teraslice.');
}
}
}
};

schemas.memory = {
doc: 'memory, in bytes, to reserve per teraslice worker in kubernetes',
default: undefined,
Expand Down Expand Up @@ -281,6 +306,9 @@ export function jobSchema(context: Context): convict.Schema<any> {

export const makeJobSchema = jobSchema;

/**
* This is the schema for a Teraslice Operation.
*/
export const opSchema: convict.Schema<any> = {
_op: {
default: '',
Expand All @@ -307,6 +335,9 @@ export const opSchema: convict.Schema<any> = {
},
};

/**
* This is the schema for a Teraslice API.
*/
export const apiSchema: convict.Schema<any> = {
_name: {
default: '',
Expand Down
4 changes: 2 additions & 2 deletions packages/teraslice-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "teraslice-cli",
"displayName": "Teraslice CLI",
"version": "0.40.0",
"version": "0.41.0",
"description": "Command line manager for teraslice jobs, assets, and cluster references.",
"keywords": [
"teraslice"
Expand Down Expand Up @@ -48,7 +48,7 @@
"pretty-bytes": "^5.6.0",
"prompts": "^2.4.1",
"signale": "^1.4.0",
"teraslice-client-js": "^0.37.0",
"teraslice-client-js": "^0.38.0",
"tmp": "^0.2.0",
"tty-table": "^4.1.3",
"yargs": "^16.2.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/teraslice-client-js/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "teraslice-client-js",
"displayName": "Teraslice Client (JavaScript)",
"version": "0.37.0",
"version": "0.38.0",
"description": "A Node.js client for teraslice jobs, assets, and cluster references.",
"keywords": [
"elasticsearch",
Expand Down Expand Up @@ -31,7 +31,7 @@
"test:watch": "ts-scripts test --watch . --"
},
"dependencies": {
"@terascope/job-components": "^0.49.0",
"@terascope/job-components": "^0.50.0",
"auto-bind": "^4.0.0",
"got": "^11.8.2"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/teraslice-op-test-harness/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
"bluebird": "^3.7.2"
},
"devDependencies": {
"@terascope/job-components": "^0.49.0"
"@terascope/job-components": "^0.50.0"
},
"peerDependencies": {
"@terascope/job-components": "^0.49.0"
"@terascope/job-components": "^0.50.0"
},
"publishConfig": {
"access": "public",
Expand Down
4 changes: 2 additions & 2 deletions packages/teraslice-test-harness/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
"fs-extra": "^9.1.0"
},
"devDependencies": {
"@terascope/job-components": "^0.49.0"
"@terascope/job-components": "^0.50.0"
},
"peerDependencies": {
"@terascope/job-components": "^0.49.0"
"@terascope/job-components": "^0.50.0"
},
"engines": {
"node": ">=10.16.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class K8sResource {
this._setAssetsVolume();
this._setImagePullSecret();
this._setEphemeralStorage();
this._setExternalPorts();

if (resourceName === 'worker') {
this._setAntiAffinity();
Expand Down Expand Up @@ -180,6 +181,15 @@ class K8sResource {
}
}

_setExternalPorts() {
if (this.execution.external_ports) {
_.forEach(this.execution.external_ports, (port) => {
this.resource.spec.template.spec.containers[0].ports
.push({ containerPort: port });
});
}
}

_setImagePullSecret() {
if (this.terasliceConfig.kubernetes_image_pull_secret) {
this.resource.spec.template.spec.imagePullSecrets = [
Expand Down
4 changes: 4 additions & 0 deletions packages/teraslice/lib/config/schemas/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const {

const workerCount = require('os').cpus().length;

/**
* This schema object is for the Teraslice configuration settings coming from
* its configuration file.
*/
const schema = {
api_response_timeout: {
doc: 'HTTP response timeout for the Teraslice API server',
Expand Down
4 changes: 2 additions & 2 deletions packages/teraslice/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "teraslice",
"displayName": "Teraslice",
"version": "0.74.0",
"version": "0.75.0",
"description": "Distributed computing platform for processing JSON data",
"homepage": "https://github.com/terascope/teraslice#readme",
"bugs": {
Expand Down Expand Up @@ -38,7 +38,7 @@
},
"dependencies": {
"@terascope/elasticsearch-api": "^2.19.0",
"@terascope/job-components": "^0.49.0",
"@terascope/job-components": "^0.50.0",
"@terascope/teraslice-messaging": "^0.21.0",
"@terascope/utils": "^0.37.0",
"async-mutex": "^0.3.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,72 @@ describe('k8sResource', () => {
});
});

describe('teraslice job with one valid external_ports set', () => {
it('generates k8s worker deployment with containerPort on container', () => {
execution.external_ports = [9090];
const kr = new K8sResource(
'deployments', 'worker', terasliceConfig, execution
);

// eslint-disable-next-line no-console
// console.log(yaml.dump(kr.resource.spec.template.spec.containers[0].ports));
expect(kr.resource.spec.template.spec.containers[0].ports)
.toEqual([
{ containerPort: 45680 },
{ containerPort: 9090 }
]);
});

it('generates k8s execution controller job with containerPort on container', () => {
execution.external_ports = [9090];
const kr = new K8sResource(
'jobs', 'execution_controller', terasliceConfig, execution
);

// eslint-disable-next-line no-console
// console.log(yaml.dump(kr.resource.spec.template.spec.containers[0].ports));
expect(kr.resource.spec.template.spec.containers[0].ports)
.toEqual([
{ containerPort: 45680 },
{ containerPort: 9090 }
]);
});
});

describe('teraslice job with two valid external_ports set', () => {
it('generates k8s worker deployment with containerPort on container', () => {
execution.external_ports = [9090, 9091];
const kr = new K8sResource(
'deployments', 'worker', terasliceConfig, execution
);

// eslint-disable-next-line no-console
// console.log(yaml.dump(kr.resource.spec.template.spec.containers[0].ports));
expect(kr.resource.spec.template.spec.containers[0].ports)
.toEqual([
{ containerPort: 45680 },
{ containerPort: 9090 },
{ containerPort: 9091 }
]);
});

it('generates k8s execution controller job with containerPort on container', () => {
execution.external_ports = [9090, 9091];
const kr = new K8sResource(
'jobs', 'execution_controller', terasliceConfig, execution
);

// eslint-disable-next-line no-console
// console.log(yaml.dump(kr.resource.spec.template.spec.containers[0].ports));
expect(kr.resource.spec.template.spec.containers[0].ports)
.toEqual([
{ containerPort: 45680 },
{ containerPort: 9090 },
{ containerPort: 9091 }
]);
});
});

describe('execution_controller job', () => {
it('has valid resource object.', () => {
const kr = new K8sResource(
Expand Down
Loading

0 comments on commit a16dd2d

Please sign in to comment.