Skip to content

Commit

Permalink
add --dev option to k8s-env command
Browse files Browse the repository at this point in the history
  • Loading branch information
sotojn committed Jun 21, 2024
1 parent d3c435d commit 8f257b7
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 9 deletions.
8 changes: 7 additions & 1 deletion packages/scripts/src/cmds/k8s-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ const cmd: CommandModule = {
default: 'elasticsearch-next',
choices: ['elasticsearch-next', 's3'],
})
.option('dev', {
description: 'Mounts local teraslice to k8s resources for faster development.',
type: 'boolean',
default: false
})
.check((args) => {
if (args['asset-storage'] === 's3' && process.env.TEST_MINIO !== 'true') {
throw new Error('You chose "s3" as an asset storage but don\'t have the minio service enabled.\n'
Expand All @@ -112,7 +117,8 @@ const cmd: CommandModule = {
kindClusterName: argv['cluster-name'] as string,
k8sVersion: argv['k8s-version'] as string,
terasliceImage: argv['teraslice-image'] as string,
assetStorage: argv['asset-storage'] as string
assetStorage: argv['asset-storage'] as string,
dev: Boolean(argv.dev)
};

if (Boolean(argv.rebuild) === true) {
Expand Down
19 changes: 17 additions & 2 deletions packages/scripts/src/helpers/k8s-env/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import execa from 'execa';
import {
dockerTag,
isKindInstalled,
Expand Down Expand Up @@ -38,7 +39,7 @@ export async function launchK8sEnv(options: K8sEnvOptions) {

signale.pending('Creating kind cluster');
try {
await kind.createCluster(options.tsPort);
await kind.createCluster(options.tsPort, options.dev);
} catch (err) {
signale.error(err);
// Do not destroy existing cluster if that was the cause of failure
Expand Down Expand Up @@ -68,6 +69,19 @@ export async function launchK8sEnv(options: K8sEnvOptions) {

await kind.loadTerasliceImage(e2eImage);

// If --dev is true, we must run yarn setup before creating resources
// We need a local node_modules folder built to add it as a volume
if (options.dev) {
signale.info('Running yarn setup...');
try {
execa.commandSync('yarn setup');
} catch (err) {
signale.fatal(err);
await kind.destroyCluster();
process.exit(1);
}
}

await ensureServices('k8s-env', {
...options,
debug: false,
Expand Down Expand Up @@ -144,7 +158,8 @@ async function buildAndTagTerasliceImage(options:K8sEnvOptions) {
dryRun: true,
nodeSuffix: true,
nodeVersion: options.nodeVersion,
type: PublishType.Dev
type: PublishType.Dev,
useDevFile: options.dev
};
runImage = await buildDevDockerImage(publishOptions);
} catch (err) {
Expand Down
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 @@ -14,6 +14,7 @@ export interface K8sEnvOptions {
assetStorage: string;
terasliceImage?: string;
resetStore?: boolean;
dev: boolean;
}

// TODO: create a common parent for each resource type,
Expand Down
144 changes: 144 additions & 0 deletions packages/scripts/src/helpers/k8s-env/k8s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,147 @@ export class K8s {
}
}

mountLocalTeraslice(masterDeployment: k8sClient.V1Deployment) {
if (masterDeployment.spec?.template.spec?.containers[0].volumeMounts) {
masterDeployment.spec.template.spec.containers[0].volumeMounts.push({
name: 'packages',
mountPath: '/app/source/packages'
});
masterDeployment.spec.template.spec.containers[0].volumeMounts.push({
name: 'scripts',
mountPath: '/app/source/scripts'
});
masterDeployment.spec.template.spec.containers[0].volumeMounts.push({
name: 'types',
mountPath: '/app/source/types'
});
masterDeployment.spec.template.spec.containers[0].volumeMounts.push({
name: 'yarn',
mountPath: '/app/source/.yarn'
});
masterDeployment.spec.template.spec.containers[0].volumeMounts.push({
name: 'yarnci',
mountPath: '/app/source/.yarnclean'
});
masterDeployment.spec.template.spec.containers[0].volumeMounts.push({
name: 'packagejson',
mountPath: '/app/source/package.json'
});
masterDeployment.spec.template.spec.containers[0].volumeMounts.push({
name: 'yarnlock',
mountPath: '/app/source/yarn.lock'
});
masterDeployment.spec.template.spec.containers[0].volumeMounts.push({
name: 'tsconfigjson',
mountPath: '/app/source/tsconfig.json'
});
masterDeployment.spec.template.spec.containers[0].volumeMounts.push({
name: 'yarnrc',
mountPath: '/app/source/.yarnrc'
});
masterDeployment.spec.template.spec.containers[0].volumeMounts.push({
name: 'servicejs',
mountPath: '/app/source/service.js'
});
masterDeployment.spec.template.spec.containers[0].volumeMounts.push({
name: 'nodemodules',
mountPath: '/app/source/node_modules'
});
}
if (masterDeployment.spec?.template.spec?.volumes) {
masterDeployment.spec.template.spec.volumes.push({
name: 'packages',
hostPath: {
path: '/packages',
type: 'Directory'
}
});
masterDeployment.spec.template.spec.volumes.push({
name: 'scripts',
hostPath: {
path: '/scripts',
type: 'Directory'
}
});
masterDeployment.spec.template.spec.volumes.push({
name: 'types',
hostPath: {
path: '/types',
type: 'Directory'
}
});
masterDeployment.spec.template.spec.volumes.push({
name: 'yarn',
hostPath: {
path: '/.yarn',
type: 'Directory'
}
});
masterDeployment.spec.template.spec.volumes.push({
name: 'yarnci',
hostPath: {
path: '/.yarnclean.ci',
type: 'File'
}
});
masterDeployment.spec.template.spec.volumes.push({
name: 'packagejson',
hostPath: {
path: '/package.json',
type: 'File'
}
});
masterDeployment.spec.template.spec.volumes.push({
name: 'yarnlock',
hostPath: {
path: '/yarn.lock',
type: 'File'
}
});
masterDeployment.spec.template.spec.volumes.push({
name: 'tsconfigjson',
hostPath: {
path: '/tsconfig.json',
type: 'File'
}
});
masterDeployment.spec.template.spec.volumes.push({
name: 'yarnrc',
hostPath: {
path: '/.yarnrc',
type: 'File'
}
});
masterDeployment.spec.template.spec.volumes.push({
name: 'servicejs',
hostPath: {
path: '/service.js',
type: 'File'
}
});
masterDeployment.spec.template.spec.volumes.push({
name: 'nodemodules',
hostPath: {
path: '/node_modules',
type: 'Directory'
}
});
}
/// Potentially add an entrypoint override
if (masterDeployment.spec?.template.spec?.containers[0].args) {
masterDeployment.spec.template.spec.containers[0].args = ['./scripts/entrypoint-dev.sh'];
}
/// Pass in env so master passes volumes to ex's and workers
if (masterDeployment.spec?.template.spec?.containers[0].env) {
masterDeployment.spec.template.spec.containers[0].env = [
{
name: 'MOUNT_LOCAL_TERASLICE',
value: 'true'
}
];
}
}

async deployK8sTeraslice(wait = false, options: K8sEnvOptions | undefined = undefined) {
signale.pending('Begin teraslice deployment...');
const e2eK8sDir = getE2eK8sDir();
Expand Down Expand Up @@ -114,6 +255,9 @@ export class K8s {
if (yamlTSMasterDeployment.spec?.template.spec?.containers[0]) {
yamlTSMasterDeployment.spec.template.spec.containers[0].image = `teraslice-workspace:e2e-nodev${config.NODE_VERSION}`;
}
if (options?.dev) {
this.mountLocalTeraslice(yamlTSMasterDeployment);
}
const response = await this.k8sAppsV1Api.createNamespacedDeployment('ts-dev1', yamlTSMasterDeployment);
logger.debug('deployK8sTeraslice yamlTSMasterDeployment: ', response.body);
} catch (err) {
Expand Down
8 changes: 5 additions & 3 deletions packages/scripts/src/helpers/kind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Kind {
this.k8sVersion = k8sVersion;
}

async createCluster(teraslicePort = TERASLICE_PORT): Promise<void> {
async createCluster(teraslicePort = TERASLICE_PORT, devMode: boolean = false): Promise<void> {
this.kindVersion = await this.getKindVersion();

const e2eK8sDir = getE2eK8sDir();
Expand All @@ -33,10 +33,12 @@ export class Kind {
let configPath: string;

// clusterName must match 'name' in kind config yaml file
if (this.clusterName === 'k8s-env') {
configPath = path.join(e2eK8sDir, 'kindConfigDefaultPorts.yaml');
if (this.clusterName === 'k8s-env' && devMode) {
configPath = path.join(e2eK8sDir, 'kindConfigDefaultPortsDev.yaml');
} else if (this.clusterName === 'k8s-e2e') {
configPath = path.join(e2eK8sDir, 'kindConfigTestPorts.yaml');
} else if (this.clusterName === 'k8s-env') {
configPath = path.join(e2eK8sDir, 'kindConfigDefaultPorts.yaml');
} else {
signale.error(`No config file for cluster with name ${this.clusterName}`);
process.exit(1);
Expand Down
1 change: 1 addition & 0 deletions packages/scripts/src/helpers/publish/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export interface PublishOptions {
*/
publishOutdatedPackages?: boolean;
nodeVersion?: string;
useDevFile?: boolean;
}
2 changes: 1 addition & 1 deletion packages/scripts/src/helpers/publish/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export async function buildDevDockerImage(
signale.pending(`building docker image ${devImage}`);

try {
await dockerBuild(devImage, cacheFromPrev ? [devImage] : [], undefined, `NODE_VERSION=${publishOptions.nodeVersion}`);
await dockerBuild(devImage, cacheFromPrev ? [devImage] : [], undefined, `NODE_VERSION=${publishOptions.nodeVersion}`, publishOptions.useDevFile);
} catch (err) {
throw new TSError(err, {
message: `Failed to build ${devImage} docker image`,
Expand Down
6 changes: 4 additions & 2 deletions packages/scripts/src/helpers/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,8 @@ export async function dockerBuild(
tag: string,
cacheFrom?: string[],
target?: string,
buildArg?: string
buildArg?: string,
useDevFile?: boolean
): Promise<void> {
const cacheFromArgs: string[] = [];

Expand All @@ -403,10 +404,11 @@ export async function dockerBuild(

const targetArgs: string[] = target ? ['--target', target] : [];
const buildsArgs: string[] = buildArg ? ['--build-arg', buildArg] : [];
const dockerFilePath = useDevFile ? ['-f', 'Dockerfile.dev', '.'] : ['.'];

await fork({
cmd: 'docker',
args: ['build', ...cacheFromArgs, ...targetArgs, ...buildsArgs, '--tag', tag, '.'],
args: ['build', ...cacheFromArgs, ...targetArgs, ...buildsArgs, '--tag', tag, ...dockerFilePath],
});
}

Expand Down

0 comments on commit 8f257b7

Please sign in to comment.