-
Notifications
You must be signed in to change notification settings - Fork 8
feat(aws): BuildKit builder for Docker image assets #165
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
Open
so0k
wants to merge
1
commit into
main
Choose a base branch
from
feat/buildkit-docker-asset
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,109 @@ | ||
| // Minimal hand-written bindings for the cruxstack/buildkit provider (no published cdktn package). | ||
| import { TerraformProvider, TerraformResource } from "cdktn"; | ||
| import { Construct } from "constructs"; | ||
|
|
||
| const PROVIDER_SOURCE = "cruxstack/buildkit"; | ||
| const PROVIDER_VERSION = "0.0.1"; | ||
|
|
||
| export interface BuildkitProviderConfig { | ||
| readonly buildkitAddress: string; | ||
| } | ||
|
|
||
| /** | ||
| * `provider "buildkit"` pinned to one explicit daemon: no auto-discovery, no embedded buildkitd. | ||
| */ | ||
| export class BuildkitProvider extends TerraformProvider { | ||
| constructor( | ||
| scope: Construct, | ||
| id: string, | ||
| private readonly config: BuildkitProviderConfig, | ||
| ) { | ||
| super(scope, id, { | ||
| terraformResourceType: "buildkit", | ||
| terraformGeneratorMetadata: { | ||
| providerName: "buildkit", | ||
| providerVersionConstraint: PROVIDER_VERSION, | ||
| }, | ||
| terraformProviderSource: PROVIDER_SOURCE, | ||
| }); | ||
| } | ||
|
|
||
| protected synthesizeAttributes(): { [name: string]: any } { | ||
| return { | ||
| buildkit_address: this.config.buildkitAddress, | ||
| buildkit_autodiscover: false, | ||
| embedded_buildkitd: false, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| export interface BuildkitCacheEntry { | ||
| readonly type: string; | ||
| readonly attrs?: { [key: string]: string }; | ||
| } | ||
|
|
||
| export interface BuildkitImageConfig { | ||
| readonly provider: BuildkitProvider; | ||
| readonly context: string; | ||
| readonly dockerfile: string; | ||
| readonly platforms: string[]; | ||
| readonly registry: string; | ||
| readonly repository: string; | ||
| readonly tags: string[]; | ||
| readonly args?: { [key: string]: string }; | ||
| readonly secrets?: { [key: string]: string }; | ||
| readonly target?: string; | ||
| readonly cacheFrom?: BuildkitCacheEntry[]; | ||
| readonly cacheTo?: BuildkitCacheEntry[]; | ||
| readonly triggers?: { [key: string]: string }; | ||
| } | ||
|
|
||
| /** | ||
| * `resource "buildkit_image"`: solve on buildkitd and push via the image exporter. | ||
| */ | ||
| export class BuildkitImage extends TerraformResource { | ||
| constructor( | ||
| scope: Construct, | ||
| id: string, | ||
| private readonly config: BuildkitImageConfig, | ||
| ) { | ||
| super(scope, id, { | ||
| terraformResourceType: "buildkit_image", | ||
| terraformGeneratorMetadata: { | ||
| providerName: "buildkit", | ||
| providerVersionConstraint: PROVIDER_VERSION, | ||
| }, | ||
| provider: config.provider, | ||
| }); | ||
| } | ||
|
|
||
| /** Tag reference of the pushed image; depends on the build. */ | ||
| public get tagUrl(): string { | ||
| return this.getStringAttribute("published.0.tag_url"); | ||
| } | ||
|
|
||
| /** Manifest digest of the pushed image. */ | ||
| public get imageDigest(): string { | ||
| return this.getStringAttribute("image_digest"); | ||
| } | ||
|
|
||
| protected synthesizeAttributes(): { [name: string]: any } { | ||
| const c = this.config; | ||
| const cache = (entries?: BuildkitCacheEntry[]) => | ||
| entries?.map((e) => ({ type: e.type, attrs: e.attrs })); | ||
| return { | ||
| context: c.context, | ||
| dockerfile: c.dockerfile, | ||
| platforms: c.platforms, | ||
| args: c.args, | ||
| secrets: c.secrets, | ||
| target: c.target, | ||
| triggers: c.triggers, | ||
| publish: [ | ||
| { registry: c.registry, repository: c.repository, tags: c.tags }, | ||
| ], | ||
| cache_from: cache(c.cacheFrom), | ||
| cache_to: cache(c.cacheTo), | ||
| }; | ||
| } | ||
| } |
This file contains hidden or 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,116 @@ | ||
| import * as path from "path"; | ||
| import { | ||
| image as dockerImage, | ||
| registryImage as dockerRegistryImage, | ||
| } from "@cdktn/provider-docker"; | ||
| import { App, TerraformOutput, Testing } from "cdktn"; | ||
| import "cdktn/lib/testing/adapters/jest"; | ||
| import { DockerBuildSecret } from "../../../../src/"; | ||
| import { DockerAssetBuilder } from "../../../../src/aws/aws-asset-manager"; | ||
| import { AwsStack } from "../../../../src/aws/aws-stack"; | ||
| import { | ||
| DockerImageAsset, | ||
| NetworkMode, | ||
| Platform, | ||
| } from "../../../../src/aws/storage/assets/image-asset"; | ||
| import { Template } from "../../../assertions"; | ||
|
|
||
| const TEST_OUTDIR = path.join(__dirname, "cdk.out"); | ||
| const CDKTFJSON_PATH = path.join(__dirname, "fixtures", "app", "cdktf.json"); | ||
| const REPO = "data.aws_ecr_repository.ExistingAssetRepository"; | ||
|
|
||
| describe("image asset with DockerAssetBuilder.BUILDKIT", () => { | ||
| let stack: AwsStack; | ||
| beforeEach(() => { | ||
| const app = Testing.stubVersion( | ||
| new App({ | ||
| outdir: TEST_OUTDIR, | ||
| stackTraces: false, | ||
| context: { cdktfJsonPath: CDKTFJSON_PATH }, | ||
| }), | ||
| ); | ||
| stack = new AwsStack(app, "MyStack", { | ||
| providerConfig: { region: "us-east-1" }, | ||
| assetOptions: { | ||
| repositoryName: "existing-repo", | ||
| dockerBuilder: DockerAssetBuilder.BUILDKIT, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| test("synthesizes buildkit_image + pinned provider, no docker resources", () => { | ||
| const image = new DockerImageAsset(stack, "Image", { | ||
| directory: path.join(__dirname, "demo-image-custom-docker-file"), | ||
| file: "Dockerfile.Custom", | ||
| platform: Platform.LINUX_ARM64, | ||
| buildArgs: { A: "b" }, | ||
| buildSecrets: { TOKEN: DockerBuildSecret.fromSrc("/run/token") }, | ||
| target: "final", | ||
| cacheFrom: [{ type: "registry", params: { ref: "repo/cache" } }], | ||
| }); | ||
| new TerraformOutput(stack, "ImageUri", { value: image.imageUri }); | ||
|
|
||
| const template = new Template(stack); | ||
| template.resourceCountIs(dockerImage.Image, 0); | ||
| template.resourceCountIs(dockerRegistryImage.RegistryImage, 0); | ||
| template.toMatchObject({ | ||
| terraform: { | ||
| required_providers: { | ||
| buildkit: { source: "cruxstack/buildkit", version: "0.0.1" }, | ||
| }, | ||
| }, | ||
| provider: { | ||
| buildkit: [ | ||
| { | ||
| buildkit_address: "unix:///run/buildkit/buildkitd.sock", | ||
| buildkit_autodiscover: false, | ||
| embedded_buildkitd: false, | ||
| }, | ||
| ], | ||
| }, | ||
| resource: { | ||
| buildkit_image: { | ||
| DockerAsset_Buildkit: { | ||
| provider: "buildkit", | ||
| context: `assets/DockerAsset/${image.assetHash}`, | ||
| dockerfile: "Dockerfile.Custom", | ||
| platforms: ["linux/arm64"], | ||
| args: { A: "b" }, | ||
| secrets: { TOKEN: '${file("/run/token")}' }, | ||
| target: "final", | ||
| publish: [ | ||
| { | ||
| registry: `\${element(split("/", ${REPO}.repository_url), 0)}`, | ||
| repository: `\${${REPO}.name}`, | ||
| tags: [image.assetHash], | ||
| }, | ||
| ], | ||
| cache_from: [{ type: "registry", attrs: { ref: "repo/cache" } }], | ||
| triggers: { source_hash: image.assetHash }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| expect(template.outputByName("ImageUri")).toEqual({ | ||
| value: "${buildkit_image.DockerAsset_Buildkit.published.0.tag_url}", | ||
| }); | ||
| }); | ||
|
|
||
| test("requires an explicit platform", () => { | ||
| expect(() => { | ||
| new DockerImageAsset(stack, "Image", { | ||
| directory: path.join(__dirname, "demo-image"), | ||
| }); | ||
| }).toThrow(/requires an explicit platform/); | ||
| }); | ||
|
|
||
| test("rejects options buildkit_image cannot express", () => { | ||
| expect(() => { | ||
| new DockerImageAsset(stack, "Image", { | ||
| directory: path.join(__dirname, "demo-image"), | ||
| platform: Platform.LINUX_ARM64, | ||
| networkMode: NetworkMode.HOST, | ||
| }); | ||
| }).toThrow(/does not support buildSsh, outputs or networkMode/); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't do this