-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codebuild): improved support for ARM build images (#19052)
Fixes #18916 Fixes #9817 ### Motivation CDK currently has poor and hidden support for using ARM build images for CodeBuild that do not match what you can do with the Console. Currently, CDK has under LinuxBuildImage two constants not mentioned in the documentation. The constants internally map to a hidden ArmBuildImage class, which provides support for the standard CodeBuild ARM build images. That is the extent of the support, making ARM a second class citizen compared to x86-64 Linux and Windows build images as, for example, you can't use custom aarch64 ECR images. ### Changes This pull request addresses the missing support by: - renaming the previously hidden class ArmBuildImage to LinuxArmBuildImage (in case there are Windows ARM Build Images in the future). - exporting LinuxArmBuildImage so it can be used. - adding the two ARM constants present in LinuxBuildImage also to LinuxArmBuildImage. The constants are also left under LinuxBuildImage to not break backwards compatibility. - adding the method fromEcrRepository() to support custom ARM build images. - making the LinuxArmBuildImage closer to the LinuxBuildImage and WindowsBuildImage (built with props instead of just image name). - updating documentation to show examples of ARM and highlighting the LinuxBuildImage is for x86-64. ### Testing The unit test for ARM image is still valid. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
5 changed files
with
458 additions
and
37 deletions.
There are no files selected for viewing
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
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
105 changes: 105 additions & 0 deletions
105
packages/@aws-cdk/aws-codebuild/lib/linux-arm-build-image.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import * as ecr from '@aws-cdk/aws-ecr'; | ||
import * as secretsmanager from '@aws-cdk/aws-secretsmanager'; | ||
import { BuildSpec } from './build-spec'; | ||
import { runScriptLinuxBuildSpec } from './private/run-script-linux-build-spec'; | ||
import { BuildEnvironment, ComputeType, IBuildImage, ImagePullPrincipalType } from './project'; | ||
|
||
/** | ||
* Construction properties of {@link LinuxArmBuildImage}. | ||
* Module-private, as the constructor of {@link LinuxArmBuildImage} is private. | ||
*/ | ||
interface LinuxArmBuildImageProps { | ||
readonly imageId: string; | ||
readonly imagePullPrincipalType?: ImagePullPrincipalType; | ||
readonly secretsManagerCredentials?: secretsmanager.ISecret; | ||
readonly repository?: ecr.IRepository; | ||
} | ||
|
||
/** | ||
* A CodeBuild image running aarch64 Linux. | ||
* | ||
* This class has a bunch of public constants that represent the CodeBuild ARM images. | ||
* | ||
* You can also specify a custom image using the static method: | ||
* | ||
* - LinuxBuildImage.fromEcrRepository(repo[, tag]) | ||
* | ||
* | ||
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-available.html | ||
*/ | ||
export class LinuxArmBuildImage implements IBuildImage { | ||
/** Image "aws/codebuild/amazonlinux2-aarch64-standard:1.0". */ | ||
public static readonly AMAZON_LINUX_2_STANDARD_1_0 = LinuxArmBuildImage.fromCodeBuildImageId('aws/codebuild/amazonlinux2-aarch64-standard:1.0'); | ||
/** Image "aws/codebuild/amazonlinux2-aarch64-standard:2.0". */ | ||
public static readonly AMAZON_LINUX_2_STANDARD_2_0 = LinuxArmBuildImage.fromCodeBuildImageId('aws/codebuild/amazonlinux2-aarch64-standard:2.0'); | ||
|
||
/** | ||
* Returns an ARM image running Linux from an ECR repository. | ||
* | ||
* NOTE: if the repository is external (i.e. imported), then we won't be able to add | ||
* a resource policy statement for it so CodeBuild can pull the image. | ||
* | ||
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-ecr.html | ||
* | ||
* @param repository The ECR repository | ||
* @param tag Image tag (default "latest") | ||
* @returns An aarch64 Linux build image from an ECR repository. | ||
*/ | ||
public static fromEcrRepository(repository: ecr.IRepository, tag: string = 'latest'): IBuildImage { | ||
return new LinuxArmBuildImage({ | ||
imageId: repository.repositoryUriForTag(tag), | ||
imagePullPrincipalType: ImagePullPrincipalType.SERVICE_ROLE, | ||
repository, | ||
}); | ||
} | ||
|
||
/** | ||
* Uses a Docker image provided by CodeBuild. | ||
* | ||
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-available.html | ||
* | ||
* @param id The image identifier | ||
* @example 'aws/codebuild/amazonlinux2-aarch64-standard:1.0' | ||
* @returns A Docker image provided by CodeBuild. | ||
*/ | ||
public static fromCodeBuildImageId(id: string): IBuildImage { | ||
return new LinuxArmBuildImage({ | ||
imageId: id, | ||
imagePullPrincipalType: ImagePullPrincipalType.CODEBUILD, | ||
}); | ||
} | ||
|
||
public readonly type = 'ARM_CONTAINER'; | ||
public readonly defaultComputeType = ComputeType.LARGE; | ||
public readonly imageId: string; | ||
public readonly imagePullPrincipalType?: ImagePullPrincipalType; | ||
public readonly secretsManagerCredentials?: secretsmanager.ISecret; | ||
public readonly repository?: ecr.IRepository; | ||
|
||
private constructor(props: LinuxArmBuildImageProps) { | ||
this.imageId = props.imageId; | ||
this.imagePullPrincipalType = props.imagePullPrincipalType; | ||
this.secretsManagerCredentials = props.secretsManagerCredentials; | ||
this.repository = props.repository; | ||
} | ||
|
||
/** | ||
* Validates by checking the BuildEnvironment computeType as aarch64 images only support ComputeType.SMALL and | ||
* ComputeType.LARGE | ||
* @param buildEnvironment BuildEnvironment | ||
*/ | ||
public validate(buildEnvironment: BuildEnvironment): string[] { | ||
const ret = []; | ||
if (buildEnvironment.computeType && | ||
buildEnvironment.computeType !== ComputeType.SMALL && | ||
buildEnvironment.computeType !== ComputeType.LARGE) { | ||
ret.push(`ARM images only support ComputeTypes '${ComputeType.SMALL}' and '${ComputeType.LARGE}' - ` + | ||
`'${buildEnvironment.computeType}' was given`); | ||
} | ||
return ret; | ||
} | ||
|
||
public runScriptBuildspec(entrypoint: string): BuildSpec { | ||
return runScriptLinuxBuildSpec(entrypoint); | ||
} | ||
} |
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
Oops, something went wrong.