Skip to content

Commit

Permalink
Add option to select teraslice image for k8s-env command (#3529)
Browse files Browse the repository at this point in the history
This PR makes the following changes:
- Add a `teraslice-image` tag to the `k8s-env` command
- Instead of building from source, the desired image will be re-tagged
as `teraslice-workspace:e2e` and used instead. This is the image tag
expected by the `k8s/masterConfig/teraslice.yml` and
`k8s/workerConfig/teraslice.yml` files.
- Update documentation for the k8s-env command
- Fix markdown error in `docs/jobs/configuration.md`

ref: #3528
  • Loading branch information
busma13 authored Jan 22, 2024
1 parent 1d43bc7 commit 537a65b
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 18 deletions.
55 changes: 54 additions & 1 deletion docs/development/k8s.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,43 @@ job with the following commands (NOTE: `earl` is an alternative name for the
`teraslice-cli`).

```bash
# launch teraslice and services
# build teraslice from local repository and launch teraslice and elasticsearch
# from the teraslice root directory:
yarn k8s

# from any other directory:
TEST_ELASTICSEARCH='true' ELASTICSEARCH_PORT='9200' yarn run ts-scripts k8s-env
```

If running on an ARM based processor the default elasticsearch (6.8.6) will fail.
Set the ELASTICSEARCH_VERSION env variable to a version with an arm image.

```bash
# from the teraslice root directory:
ELASTICSEARCH_VERSION=7.9.3 yarn k8s

# from any other directory:
ELASTICSEARCH_VERSION=7.9.3 TEST_ELASTICSEARCH=true ELASTICSEARCH_PORT=9200 yarn run ts-scripts k8s-env
```

If you want to run a specific teraslice docker image, instead of building from your local repository:

```bash
# from the teraslice root directory:
yarn k8s --teraslice-image=terascope/teraslice:v0.91.0-nodev18.18.2

# from any other directory:
TEST_ELASTICSEARCH=\'true\' ELASTICSEARCH_PORT=\'9200\' $0 k8s-env --teraslice-image=terascope/teraslice:v0.91.0-nodev18.18.2
```

If you want to run additional services you must set the appropriate environmental variables. Currently only elasticsearch and kafka are supported (see issue [#3530](https://github.com/terascope/teraslice/issues/3530)).

```bash
# from the teraslice root directory:
yarn k8s:kafka

# from any other directory:
TEST_ELASTICSEARCH=true ELASTICSEARCH_PORT=9200 TEST_KAFKA=true KAFKA_PORT=9092 ts-scripts k8s-env
```

After about 5 minutes, Teraslice will be running and listening on port `5678`
Expand Down Expand Up @@ -270,10 +305,28 @@ git checkout examples/jobs/data_generator.json
If you are iterating on development changes to Teraslice itself and need to
rebuild and redeploy the Teraslice master, you can use the following command:

NOTE: this does not reset state in the elasticsearch store

```bash
# from the teraslice root directory:
yarn k8s:rebuild

# from any other directory:
yarn run ts-scripts k8s-env --rebuild
```

If you need to restart Teraslice without rebuilding you can use the following command:

NOTE: this does not reset state in the elasticsearch store

```bash
# from the teraslice root directory:
yarn k8s:restart

# from any other directory:
yarn run ts-scripts k8s-env --rebuild --skip-build
```

## Extras

### Teraslice Kubernetes Job Structure
Expand Down
2 changes: 1 addition & 1 deletion docs/jobs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ The first operation in the [operations](#operations) list, reads from a particul
| `apis` | An array containing all the [apis](#apis) as well as their configurations. | `Array` | required |
| `probation_window` | time in ms that the execution controller checks for failed slices, if there are none then it updates the state of the execution to running (this is only when lifecycle is set to persistent) | `Number` | optional |
| `env_vars` | environment variables to set on each the teraslice worker. Setting `NODE_OPTIONS` will override the k8s memory settings for the pod. | `{ "EXAMPLE": "TEST" }` | optional |
| `log_level` | log level that will be used for all loggers associated with this job. Overwrites log_level from `terafoundation`. | `'trace' \| 'debug' \| 'info' \| 'warn' \| 'error' \| 'fatal'` | optional |
| `log_level` | log level that will be used for all loggers associated with this job. Overwrites log_level from `terafoundation`. | `trace` | `debug` | `info` | `warn` | `error` | `fatal` | optional |

### operations

Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
"build:watch": "yarn run build --watch",
"bump": "ts-scripts bump",
"docs": "ts-scripts docs",
"k8s": "TEST_ELASTICSEARCH='true' ELASTICSEARCH_PORT='9200' ts-scripts k8s-env",
"k8s:kafka": "TEST_ELASTICSEARCH='true' ELASTICSEARCH_PORT='9200' TEST_KAFKA='true' KAFKA_PORT='9092' ts-scripts k8s-env",
"k8s:noBuild": "TEST_ELASTICSEARCH='true' ELASTICSEARCH_PORT='9200' SKIP_DOCKER_BUILD_IN_K8S='true' ts-scripts k8s-env",
"k8s:rebuild": "ts-scripts k8s-env --rebuild='true'",
"k8s": "TEST_ELASTICSEARCH=true ELASTICSEARCH_PORT=9200 ts-scripts k8s-env",
"k8s:kafka": "TEST_ELASTICSEARCH=true ELASTICSEARCH_PORT=9200 TEST_KAFKA=true KAFKA_PORT=9092 ts-scripts k8s-env",
"k8s:noBuild": "TEST_ELASTICSEARCH=true ELASTICSEARCH_PORT=9200 SKIP_DOCKER_BUILD_IN_K8S=true ts-scripts k8s-env",
"k8s:rebuild": "ts-scripts k8s-env --rebuild",
"k8s:restart": "ts-scripts k8s-env --rebuild --skip-build",
"lint": "eslint --cache --ext .js,.jsx,.ts,.tsx .",
"lint:fix": "yarn lint --fix && yarn sync",
"setup": "yarn $YARN_SETUP_ARGS && yarn run build --force",
Expand Down
2 changes: 1 addition & 1 deletion packages/scripts/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@terascope/scripts",
"displayName": "Scripts",
"version": "0.67.0",
"version": "0.68.0",
"description": "A collection of terascope monorepo scripts",
"homepage": "https://github.com/terascope/teraslice/tree/master/packages/scripts#readme",
"bugs": {
Expand Down
15 changes: 11 additions & 4 deletions packages/scripts/src/cmds/k8s-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ const cmd: CommandModule = {
describe: 'Run a local kubernetes dev environment using kind.',
builder(yargs) {
return yargs
.example('TEST_ELASTICSEARCH=\'true\' ELASTICSEARCH_PORT=\'9200\' $0 k8s-env', 'Start a kind kubernetes cluster running teraslice and elasticsearch.')
.example('TEST_ELASTICSEARCH=\'true\' ELASTICSEARCH_PORT=\'9200\' $0 k8s-env', 'Start a kind kubernetes cluster running teraslice from your local repository and elasticsearch.')
.example('TEST_ELASTICSEARCH=\'true\' ELASTICSEARCH_PORT=\'9200\' $0 k8s-env --teraslice-image=terascope/teraslice:v0.91.0-nodev18.18.2', 'Start a kind kubernetes cluster running teraslice from a specific docker image and elasticsearch.')
.example('TEST_ELASTICSEARCH=\'true\' ELASTICSEARCH_PORT=\'9200\' TEST_KAFKA=\'true\' KAFKA_PORT=\'9092\' $0 k8s-env', 'Start a kind kubernetes cluster running teraslice, elasticsearch, kafka, and zookeeper.')
.example('TEST_ELASTICSEARCH=\'true\' ELASTICSEARCH_PORT=\'9200\' SKIP_DOCKER_BUILD_IN_K8S=\'true\' $0 k8s-env', 'Start a kind kubernetes cluster, but skip building a new teraslice docker image.')
.example('$0 k8s-env --rebuild=\'true\'', 'Rebuild teraslice and redeploy to k8s cluster. ES store data is retained.')
.example('TEST_ELASTICSEARCH=\'true\' ELASTICSEARCH_PORT=\'9200\' $0 k8s-env --skip-build', 'Start a kind kubernetes cluster, but skip building a new teraslice docker image.')
.example('$0 k8s-env --rebuild', 'Rebuild teraslice and redeploy to k8s cluster. ES store data is retained.')
.option('elasticsearch-version', {
description: 'The elasticsearch version to use',
type: 'string',
Expand Down Expand Up @@ -66,6 +67,11 @@ const cmd: CommandModule = {
description: 'Version of kubernetes to use in the kind cluster.',
type: 'string',
default: config.K8S_VERSION
})
.option('teraslice-image', {
description: 'Skip build and run teraslice using this image.',
type: 'string',
default: config.TERASLICE_IMAGE
});
},
handler(argv) {
Expand All @@ -82,7 +88,8 @@ const cmd: CommandModule = {
skipBuild: Boolean(argv['skip-build']),
tsPort: argv['ts-port'] as number,
clusterName: argv['cluster-name'] as string,
k8sVersion: argv['k8s-version'] as string
k8sVersion: argv['k8s-version'] as string,
terasliceImage: argv['teraslice-image'] as string
};

if (Boolean(argv.rebuild) === true) {
Expand Down
3 changes: 2 additions & 1 deletion packages/scripts/src/helpers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,6 @@ export const NODE_VERSION = process.env.NODE_VERSION || '18.18.2';

export const {
TEST_PLATFORM = 'native',
K8S_VERSION = undefined
K8S_VERSION = undefined,
TERASLICE_IMAGE = undefined
} = process.env;
16 changes: 10 additions & 6 deletions packages/scripts/src/helpers/k8s-env/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,11 @@ export async function rebuildTeraslice(options: k8sEnvOptions) {
}

async function buildAndTagTerasliceImage(options:k8sEnvOptions) {
let devImage;
if (options.skipBuild) {
devImage = getDevDockerImage(options.nodeVersion);
let runImage;
if (options.terasliceImage) {
runImage = options.terasliceImage;
} else if (options.skipBuild) {
runImage = getDevDockerImage(options.nodeVersion);
} else {
try {
const publishOptions: PublishOptions = {
Expand All @@ -132,15 +134,17 @@ async function buildAndTagTerasliceImage(options:k8sEnvOptions) {
nodeVersion: options.nodeVersion,
type: PublishType.Dev
};
devImage = await buildDevDockerImage(publishOptions);
runImage = await buildDevDockerImage(publishOptions);
} catch (err) {
throw new Error(`Docker image build failed: ${err}`);
}
}

try {
await dockerTag(devImage, e2eImage);
signale.pending(`Tagging image ${runImage} as ${e2eImage}`);
await dockerTag(runImage, e2eImage);
signale.success(`Image ${runImage} re-tagged as ${e2eImage}`);
} catch (err) {
throw new Error(`Failed to tag docker image ${devImage} as ${e2eImage}: ${err}`);
throw new Error(`Failed to tag docker image ${runImage} as ${e2eImage}: ${err}`);
}
}
1 change: 1 addition & 0 deletions packages/scripts/src/helpers/k8s-env/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface k8sEnvOptions {
tsPort: number;
clusterName: string;
k8sVersion: string;
terasliceImage?: string;
}

// TODO: create a common parent for each resource type,
Expand Down

0 comments on commit 537a65b

Please sign in to comment.