Files
container/Sources/NativeBuilder/ContainerBuildParser/Docker/DockerInstructionVisitor.swift
T
Kathryn Baldauf 16f2630126 Add initial native builder code (#399)
We're working on making a pure swift container image build system that
leverages containerization. This PR represents our initial design and
initial work towards this goal.

The native builder is still in active development and most of the
implementation has not been started or completed. We will be opening a
series of issues that represent various (but not necessarily all) pieces
of work that need to be done here.

There are docs included in this PR that describe the overall design of
each component and outline some of our goals. The easiest way to view
the docs by themselves (since this is a massive PR) is to look at the
docs commit in the `Commits` tab.

We'd love any feedback! 

@wlan0

---------

Signed-off-by: Kathryn Baldauf <k_baldauf@apple.com>
2025-07-31 13:13:20 -07:00

140 lines
5.2 KiB
Swift

//===----------------------------------------------------------------------===//
// Copyright © 2025 Apple Inc. and the container project authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//
import ContainerBuildIR
protocol InstructionVisitor {
func visit(_ from: FromInstruction) throws
func visit(_ run: RunInstruction) throws
func visit(_ copy: CopyInstruction) throws
}
/// DockerInstructionVisitor visits each provided DockerInstruction and builds a
/// build graph from the instructions.
public class DockerInstructionVisitor: InstructionVisitor {
internal let graphBuilder: GraphBuilder
init() {
self.graphBuilder = GraphBuilder()
}
func buildGraph(from: [DockerInstruction]) throws -> BuildGraph {
for instruction in from {
try instruction.accept(self)
}
return try graphBuilder.build()
}
}
extension DockerInstructionVisitor {
func visit(_ from: FromInstruction) throws {
if let stageName = from.stageName {
try graphBuilder.stage(name: stageName, from: from.image, platform: from.platform)
} else {
try graphBuilder.stage(from: from.image, platform: from.platform)
}
}
func visit(_ run: RunInstruction) throws {
var mounts: [Mount] = []
for m in run.mounts {
guard let type = m.type else {
throw ParseError.unexpectedValue
}
let mountSource: MountSource?
switch m.type {
case .bind, .cache:
guard let source = m.source else {
throw ParseError.missingRequiredField(MountOptionNames.source.rawValue)
}
if let from = m.from, from != "" {
if let _ = graphBuilder.getStage(stageName: from) {
mountSource = .stage(.named(from), path: source)
} else if let context = graphBuilder.getBuildArg(key: from) {
mountSource = .context(context, path: source)
} else {
// mount source is an image name
guard let imageRef = ImageReference(parsing: from) else {
throw ParseError.invalidImage(from)
}
mountSource = .image(imageRef, path: source)
}
} else {
// from was not set or is empty, default is local source
mountSource = .local(source)
}
case .secret:
mountSource = .secret(m.id!)
case .ssh:
mountSource = .sshAgent
default:
// this covers .tmpfs case as well
mountSource = nil
}
guard let options = m.options else {
throw ParseError.unexpectedValue
}
guard let readonly = options.readonly else {
throw ParseError.unexpectedValue
}
let mountOptions = MountOptions(
readOnly: readonly,
uid: options.uid,
gid: options.gid,
mode: options.mode,
size: options.size,
sharing: options.sharing,
required: options.required)
let graphMount = Mount(
type: type,
target: m.target,
envTarget: m.env,
source: mountSource,
options: mountOptions)
mounts.append(graphMount)
}
try graphBuilder.run(run.command, shell: run.shell, mounts: mounts)
}
func visit(_ copy: CopyInstruction) throws {
// TODO katiewasnothere: plumb through "--link" option
if let from = copy.from {
var source: FilesystemSource
if let _ = graphBuilder.getStage(stageName: from) {
source = .stage(.named(from), paths: copy.sources)
} else if let context = graphBuilder.getBuildArg(key: from) {
source = .context(ContextSource(name: context, paths: copy.sources))
} else {
guard let imageRef = ImageReference(parsing: from) else {
throw ParseError.invalidImage(from)
}
source = .image(imageRef, paths: copy.sources)
}
try graphBuilder.copy(from: source, to: copy.destination, chown: copy.chown, chmod: copy.chmod)
return
}
try graphBuilder.copyFromContext(paths: copy.sources, to: copy.destination, chown: copy.chown, chmod: copy.chmod)
}
}