Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[build] Cross compile docker images #128272

Merged
merged 15 commits into from
Mar 23, 2022
7 changes: 7 additions & 0 deletions src/dev/build/args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ it('build default and oss dist for current platform, without packages, by defaul
"createGenericFolders": true,
"createPlatformFolders": true,
"createRpmPackage": false,
"dockerCrossCompile": false,
"dockerPush": false,
"dockerTagQualifier": null,
"downloadCloudDependencies": true,
Expand Down Expand Up @@ -67,6 +68,7 @@ it('builds packages if --all-platforms is passed', () => {
"createGenericFolders": true,
"createPlatformFolders": true,
"createRpmPackage": true,
"dockerCrossCompile": false,
"dockerPush": false,
"dockerTagQualifier": null,
"downloadCloudDependencies": true,
Expand Down Expand Up @@ -98,6 +100,7 @@ it('limits packages if --rpm passed with --all-platforms', () => {
"createGenericFolders": true,
"createPlatformFolders": true,
"createRpmPackage": true,
"dockerCrossCompile": false,
"dockerPush": false,
"dockerTagQualifier": null,
"downloadCloudDependencies": true,
Expand Down Expand Up @@ -129,6 +132,7 @@ it('limits packages if --deb passed with --all-platforms', () => {
"createGenericFolders": true,
"createPlatformFolders": true,
"createRpmPackage": false,
"dockerCrossCompile": false,
"dockerPush": false,
"dockerTagQualifier": null,
"downloadCloudDependencies": true,
Expand Down Expand Up @@ -161,6 +165,7 @@ it('limits packages if --docker passed with --all-platforms', () => {
"createGenericFolders": true,
"createPlatformFolders": true,
"createRpmPackage": false,
"dockerCrossCompile": false,
"dockerPush": false,
"dockerTagQualifier": null,
"downloadCloudDependencies": true,
Expand Down Expand Up @@ -200,6 +205,7 @@ it('limits packages if --docker passed with --skip-docker-ubi and --all-platform
"createGenericFolders": true,
"createPlatformFolders": true,
"createRpmPackage": false,
"dockerCrossCompile": false,
"dockerPush": false,
"dockerTagQualifier": null,
"downloadCloudDependencies": true,
Expand Down Expand Up @@ -232,6 +238,7 @@ it('limits packages if --all-platforms passed with --skip-docker-ubuntu', () =>
"createGenericFolders": true,
"createPlatformFolders": true,
"createRpmPackage": true,
"dockerCrossCompile": false,
"dockerPush": false,
"dockerTagQualifier": null,
"downloadCloudDependencies": true,
Expand Down
3 changes: 3 additions & 0 deletions src/dev/build/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function readCliArgs(argv: string[]) {
'skip-os-packages',
'rpm',
'deb',
'docker-cross-compile',
'docker-images',
'docker-push',
'skip-docker-contexts',
Expand Down Expand Up @@ -52,6 +53,7 @@ export function readCliArgs(argv: string[]) {
rpm: null,
deb: null,
'docker-images': null,
'docker-cross-compile': false,
'docker-push': false,
'docker-tag-qualifier': null,
'version-qualifier': '',
Expand Down Expand Up @@ -112,6 +114,7 @@ export function readCliArgs(argv: string[]) {
const buildOptions: BuildOptions = {
isRelease: Boolean(flags.release),
versionQualifier: flags['version-qualifier'],
dockerCrossCompile: Boolean(flags['docker-cross-compile']),
dockerPush: Boolean(flags['docker-push']),
dockerTagQualifier: flags['docker-tag-qualifier'],
initialize: !Boolean(flags['skip-initialize']),
Expand Down
1 change: 1 addition & 0 deletions src/dev/build/build_distributables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as Tasks from './tasks';

export interface BuildOptions {
isRelease: boolean;
dockerCrossCompile: boolean;
dockerPush: boolean;
dockerTagQualifier: string | null;
downloadFreshNode: boolean;
Expand Down
1 change: 1 addition & 0 deletions src/dev/build/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ if (showHelp) {
--rpm {dim Only build the rpm packages}
--deb {dim Only build the deb packages}
--docker-images {dim Only build the Docker images}
--docker-cross-compile {dim Produce arm64 and amd64 Docker images}
--docker-contexts {dim Only build the Docker build contexts}
--skip-docker-ubi {dim Don't build the docker ubi image}
--skip-docker-ubuntu {dim Don't build the docker ubuntu image}
Expand Down
1 change: 1 addition & 0 deletions src/dev/build/lib/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const config = new Config(
buildSha: 'abcd1234',
buildVersion: '8.0.0',
},
false,
'',
false,
true
Expand Down
1 change: 1 addition & 0 deletions src/dev/build/lib/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const setup = async ({ targetAllPlatforms = true }: { targetAllPlatforms?: boole
return await Config.create({
isRelease: true,
targetAllPlatforms,
dockerCrossCompile: false,
dockerPush: false,
dockerTagQualifier: '',
});
Expand Down
11 changes: 11 additions & 0 deletions src/dev/build/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface Options {
isRelease: boolean;
targetAllPlatforms: boolean;
versionQualifier?: string;
dockerCrossCompile: boolean;
dockerTagQualifier: string | null;
dockerPush: boolean;
}
Expand All @@ -35,6 +36,7 @@ export class Config {
isRelease,
targetAllPlatforms,
versionQualifier,
dockerCrossCompile,
dockerTagQualifier,
dockerPush,
}: Options) {
Expand All @@ -51,6 +53,7 @@ export class Config {
versionQualifier,
pkg,
}),
dockerCrossCompile,
dockerTagQualifier,
dockerPush,
isRelease
Expand All @@ -63,6 +66,7 @@ export class Config {
private readonly nodeVersion: string,
private readonly repoRoot: string,
private readonly versionInfo: VersionInfo,
private readonly dockerCrossCompile: boolean,
private readonly dockerTagQualifier: string | null,
private readonly dockerPush: boolean,
public readonly isRelease: boolean
Expand Down Expand Up @@ -96,6 +100,13 @@ export class Config {
return this.dockerPush;
}

/**
* Get docker cross compile
*/
getDockerCrossCompile() {
return this.dockerCrossCompile;
}

/**
* Convert an absolute path to a relative path, based from the repo
*/
Expand Down
1 change: 1 addition & 0 deletions src/dev/build/lib/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const setup = async () => {
isRelease: true,
targetAllPlatforms: true,
versionQualifier: '-SNAPSHOT',
dockerCrossCompile: false,
dockerPush: false,
dockerTagQualifier: '',
});
Expand Down
28 changes: 17 additions & 11 deletions src/dev/build/tasks/download_cloud_dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,24 @@ export const DownloadCloudDependencies: Task = {
const version = config.getBuildVersion();
const buildId = id.match(/[0-9]\.[0-9]\.[0-9]-[0-9a-z]{8}/);
const buildIdUrl = buildId ? `${buildId[0]}/` : '';
const architecture = process.arch === 'arm64' ? 'arm64' : 'x86_64';
const url = `https://${subdomain}-no-kpi.elastic.co/${buildIdUrl}downloads/beats/${beat}/${beat}-${version}-linux-${architecture}.tar.gz`;
const checksum = await downloadToString({ log, url: url + '.sha512', expectStatus: 200 });
const destination = config.resolveFromRepo('.beats', Path.basename(url));
return downloadToDisk({
log,
url,
destination,
shaChecksum: checksum.split(' ')[0],
shaAlgorithm: 'sha512',
maxAttempts: 3,

const localArchitecture = [process.arch === 'arm64' ? 'arm64' : 'x86_64'];
const allArchitectures = ['arm64', 'x86_64'];
const architectures = config.getDockerCrossCompile() ? allArchitectures : localArchitecture;
const downloads = architectures.map(async (arch) => {
const url = `https://${subdomain}-no-kpi.elastic.co/${buildIdUrl}downloads/beats/${beat}/${beat}-${version}-linux-${arch}.tar.gz`;
const checksum = await downloadToString({ log, url: url + '.sha512', expectStatus: 200 });
const destination = config.resolveFromRepo('.beats', Path.basename(url));
return downloadToDisk({
log,
url,
destination,
shaChecksum: checksum.split(' ')[0],
shaAlgorithm: 'sha512',
maxAttempts: 3,
});
});
return Promise.all(downloads);
};

let buildId = '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ async function setup({ failOnUrl }: { failOnUrl?: string } = {}) {
const config = await Config.create({
isRelease: true,
targetAllPlatforms: true,
dockerCrossCompile: false,
dockerPush: false,
dockerTagQualifier: '',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ async function setup() {
const config = await Config.create({
isRelease: true,
targetAllPlatforms: true,
dockerCrossCompile: false,
dockerPush: false,
dockerTagQualifier: '',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ async function setup(actualShaSums?: Record<string, string>) {
const config = await Config.create({
isRelease: true,
targetAllPlatforms: true,
dockerCrossCompile: false,
dockerPush: false,
dockerTagQualifier: '',
});
Expand Down
3 changes: 2 additions & 1 deletion src/dev/build/tasks/os_packages/docker_generator/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export async function runDockerGenerator(

const dockerPush = config.getDockerPush();
const dockerTagQualifier = config.getDockerTagQualfiier();
const dockerCrossCompile = config.getDockerCrossCompile();
const publicArtifactSubdomain = config.isRelease ? 'artifacts' : 'snapshots-no-kpi';

const scope: TemplateContext = {
Expand Down Expand Up @@ -110,7 +111,7 @@ export async function runDockerGenerator(
arm64: 'aarch64',
};
const buildArchitectureSupported = hostTarget[process.arch] === flags.architecture;
if (flags.architecture && !buildArchitectureSupported) {
if (flags.architecture && !buildArchitectureSupported && !dockerCrossCompile) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function generator({
const dockerTargetName = `${imageTag}${imageFlavor}:${version}${
dockerTagQualifier ? '-' + dockerTagQualifier : ''
}`;
const dockerArchitecture = architecture === 'aarch64' ? 'linux/arm64' : 'linux/amd64';
return dedent(`
#!/usr/bin/env bash
#
Expand Down Expand Up @@ -59,7 +60,7 @@ function generator({
retry_docker_pull ${baseOSImage}

echo "Building: kibana${imageFlavor}-docker"; \\
docker build -t ${dockerTargetName} -f Dockerfile . || exit 1;
docker buildx build --platform ${dockerArchitecture} -t ${dockerTargetName} -f Dockerfile . || exit 1;

docker save ${dockerTargetName} | gzip -c > ${dockerTargetFilename}

Expand Down