Skip to content

Commit 34efd7b

Browse files
authored
feat(al2023): add packer scripts to support Nitro Sandboxes (#2884)
* feat(al2023): add optional pod isolation runtime Adds an opt-in pod isolation image variant for AL2023, gated on enable_pod_isolation (default false). Pods that use the isolated-sandbox or confidential-sandbox RuntimeClass run inside their own Firecracker microVM as a Nitro Enclave sidecar; every other pod keeps running under runc. install-pod-isolation.sh: - installs the containerd shim from the runtime release archive and the guest image (EIF plus its dm-verity disk) from the same bundle as the other node components through the aws_s3 helper (so a public bucket needs no instance profile), verifying each against its .sha256 sidecar - installs Firecracker and the jailer from the upstream Firecracker release (pod_isolation_firecracker_version, default v1.18.0), verifying the tarball against the release's published .sha256.txt - installs iproute-tc and erofs-utils, which the shim execs - asserts the stock kernel is at least pod_isolation_min_kernel, the first release with the Nitro sandbox extensions, so an older base image fails the build instead of producing nodes that refuse every sandboxed pod - reserves the enclave memory pool with nitro_enclaves.mempool - installs the CRI proxy unit, and a NodeConfig drop-in that registers both runtime handlers, keeps compressed layers (discard_unpacked_layers = false), points kubelet at the proxy and labels the node eks.amazonaws.com/nitro-sandbox=true The image is built on an enclave-enabled instance and defaults to m8i.4xlarge / m8g.4xlarge. Supported for Kubernetes 1.36 and 1.37. Includes a unit test that the NodeConfig drop-in parses strictly. * fix(al2023): address review feedback on pod isolation - describe the memory pool the way the Nitro Sandbox docs do: reserved at boot with nitro_enclaves.mempool, each sandbox takes its full memory from it while running, and a sandbox fails to start without it - drop iproute-tc: the shim no longer execs tc (DNS rate limiting moved to eBPF). Keep erofs-utils for mkfs.erofs - drop rootfs_path and nitro_sandbox from nitro-sandbox.toml; the shim reads neither, and attaches rootfs.ext4 from the kernel's directory - drop After=cri-proxy.service from the kubelet drop-in; Before= in cri-proxy.service already orders them - drop the larger build instance default; the build itself is not heavy * fix(al2023): make the pod isolation kernel and mempool constants pod_isolation_min_kernel and pod_isolation_enclave_mempool become constants in install-pod-isolation.sh: they follow the kernel and the runtime, not the builder. pod_isolation_firecracker_version now defaults to "latest", resolved from the release page, and the build fails if it resolves, or is set, to a release older than v1.18.0, the first that can drive a Nitro Sandbox.
1 parent 2ca56de commit 34efd7b

11 files changed

Lines changed: 523 additions & 1 deletion

File tree

‎Makefile‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ endif
5353
AMI_VARIANT := $(AMI_VARIANT)-$(enable_accelerator)
5454
endif
5555

56+
# Pod isolation is a separate image rather than a flag on the default one: it
57+
# patches containerd's discard_unpacked_layers for every runtime on the node,
58+
# so the name has to say so.
59+
ifeq ($(enable_pod_isolation), true)
60+
AMI_VARIANT := $(AMI_VARIANT)-pod-isolation
61+
endif
62+
5663
ami_name ?= $(AMI_VARIANT)-node-$(K8S_VERSION_MINOR)-$(AMI_VERSION)
5764

5865
ifeq ($(aws_region), cn-northwest-1)

‎doc/usage/al2023.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
| `enable_efa` | Valid options are ```true``` or ```false```. Wheather or not to install the software needed to use AWS Elastic Fabric Adapter (EFA) network interfaces. |
2323
| `enable_fips` | Install openssl and enable fips related kernel parameters |
2424
| `enable_nvidia_gdrcopy_driver` | |
25+
| `enable_pod_isolation` | Valid options are ```true``` or ```false```. Defaults to ```false``` and stays ```false``` for published AMIs. Whether or not to install the pod isolation runtime, which runs pods under the `isolated-sandbox` and `confidential-sandbox` runtime classes inside their own Firecracker microVM. |
2526
| `encrypted` | |
2627
| `iam_instance_profile` | The name of an IAM instance profile to launch the EC2 instance with. |
2728
| `install_containerd_from_s3` | |
@@ -37,6 +38,7 @@
3738
| `nvidia_grid_runfile_bucket_name` | Bucket name for sourcing the Nvidia GRID runfile |
3839
| `nvidia_repository_url` | YUM/DNF Repository override for the NVIDIA driver packages |
3940
| `pause_container_image` | Image ref for the pause container image |
41+
| `pod_isolation_firecracker_version` | [Firecracker release](https://github.com/firecracker-microvm/firecracker/releases) that Firecracker and the jailer are installed from: ```latest``` (the default) or a version such as ```v1.18.0```. The build fails if it resolves to a release older than v1.18.0. The tarball is verified against the release's published `.sha256.txt`. |
4042
| `remote_folder` | Directory path for shell provisioner scripts on the builder instance |
4143
| `runc_version` | |
4244
| `security_group_id` | |
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package api
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/pelletier/go-toml/v2"
9+
"github.com/stretchr/testify/assert"
10+
"sigs.k8s.io/yaml"
11+
)
12+
13+
// dropInPath is the pod isolation NodeConfig drop-in as it ships in the packer
14+
// overlay. The AMI installs it to /etc/eks/nodeadm.d/10-pod-isolation.yaml,
15+
// where nodeadm-run.service reads it as a config source.
16+
const dropInPath = "../../../templates/al2023/runtime/pod-isolation/10-pod-isolation.yaml"
17+
18+
// TestPodIsolationDropInParses guards the drop-in against silent breakage. It is
19+
// the only NodeConfig the AMI ships itself, and nothing else would catch a typo
20+
// in a field name: an unparseable or misspelled drop-in does not fail the packer
21+
// build, it fails at node boot after the AMI is already published.
22+
func TestPodIsolationDropInParses(t *testing.T) {
23+
data, err := os.ReadFile(filepath.Clean(dropInPath))
24+
assert.NoError(t, err)
25+
26+
var nodeConfig NodeConfig
27+
// UnmarshalStrict rejects unknown fields, so a field name that does not
28+
// exist on NodeConfig fails here rather than being silently dropped.
29+
assert.NoError(t, yaml.UnmarshalStrict(data, &nodeConfig))
30+
31+
assert.Equal(t, "node.eks.aws/v1alpha1", nodeConfig.APIVersion)
32+
assert.Equal(t, "NodeConfig", nodeConfig.Kind)
33+
34+
// The containerd half: both runtime handlers plus the content-store setting
35+
// the shim depends on, as valid TOML.
36+
var containerdConfig map[string]interface{}
37+
assert.NoError(t, toml.Unmarshal([]byte(nodeConfig.Spec.Containerd.Config), &containerdConfig))
38+
assert.Contains(t, string(nodeConfig.Spec.Containerd.Config), "runtimes.isolated-sandbox")
39+
assert.Contains(t, string(nodeConfig.Spec.Containerd.Config), "runtimes.confidential-sandbox")
40+
assert.Contains(t, string(nodeConfig.Spec.Containerd.Config), "discard_unpacked_layers = false")
41+
42+
// The kubelet half. Setting any kubelet config is also what makes nodeadm
43+
// pass --config-dir to kubelet, so an empty map here would mean the
44+
// endpoint override never reaches kubelet at all.
45+
assert.NotEmpty(t, nodeConfig.Spec.Kubelet.Config)
46+
assert.Contains(t, nodeConfig.Spec.Kubelet.Config, "containerRuntimeEndpoint")
47+
48+
// Without this label a sandboxed pod is rejected by the NodeAffinity
49+
// predicate, because admission puts the label in the pod's node selector.
50+
// The node still reports sandbox capacity, so the failure does not look like
51+
// a missing label.
52+
assert.Contains(t, nodeConfig.Spec.Kubelet.Flags,
53+
"--node-labels=eks.amazonaws.com/nitro-sandbox=true")
54+
}
55+
56+
// TestPodIsolationDropInMergesWithUserData asserts the drop-in adds to a
57+
// cluster's own containerd config rather than replacing it. Drop-ins take
58+
// precedence over user data, so a plain string override here would silently
59+
// discard whatever the cluster had configured.
60+
func TestPodIsolationDropInMergesWithUserData(t *testing.T) {
61+
data, err := os.ReadFile(filepath.Clean(dropInPath))
62+
assert.NoError(t, err)
63+
64+
var dropIn NodeConfig
65+
assert.NoError(t, yaml.UnmarshalStrict(data, &dropIn))
66+
67+
userData := NodeConfig{
68+
Spec: NodeConfigSpec{
69+
Containerd: ContainerdOptions{
70+
Config: `[plugins.'io.containerd.cri.v1.runtime']
71+
enable_unprivileged_ports = true`,
72+
},
73+
Kubelet: KubeletOptions{
74+
Flags: []string{"--node-labels=example.com/pool=general"},
75+
},
76+
},
77+
}
78+
79+
assert.NoError(t, userData.Merge(&dropIn))
80+
81+
merged := string(userData.Spec.Containerd.Config)
82+
assert.Contains(t, merged, "enable_unprivileged_ports", "user data setting must survive the merge")
83+
assert.Contains(t, merged, "isolated-sandbox", "drop-in runtime handler must be present")
84+
85+
// Kubelet flags append rather than replace, so a cluster that sets its own
86+
// --node-labels keeps them alongside the sandbox label. Replacing instead
87+
// would strip a cluster's labels off every sandbox-capable node.
88+
assert.Contains(t, userData.Spec.Kubelet.Flags, "--node-labels=example.com/pool=general")
89+
assert.Contains(t, userData.Spec.Kubelet.Flags, "--node-labels=eks.amazonaws.com/nitro-sandbox=true")
90+
}
Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
#!/usr/bin/env bash
2+
3+
# Installs the pod isolation runtime, which runs a pod inside its own Firecracker
4+
# microVM as a Nitro Enclave sidecar instead of beside its neighbours under runc.
5+
# A pod opts in through the isolated-sandbox or confidential-sandbox RuntimeClass.
6+
#
7+
# Nodes must be launched with EnclaveOptions.Enabled=true.
8+
9+
set -o pipefail
10+
set -o nounset
11+
set -o errexit
12+
13+
if [ "${ENABLE_POD_ISOLATION}" != "true" ]; then
14+
echo "Pod isolation is not enabled, skipping"
15+
exit 0
16+
fi
17+
18+
# The runtime and guest image are published for these Kubernetes minor versions
19+
# only.
20+
case "$(echo "${KUBERNETES_VERSION}" | cut -d. -f1,2)" in
21+
1.36 | 1.37) ;;
22+
*)
23+
echo "ERROR: pod isolation supports Kubernetes 1.36 and 1.37, not ${KUBERNETES_VERSION}" >&2
24+
exit 1
25+
;;
26+
esac
27+
28+
################################################################################
29+
### Configuration ##############################################################
30+
################################################################################
31+
32+
# The first stock Amazon Linux kernel with the Nitro sandbox extensions, as a dnf
33+
# version constraint.
34+
readonly MIN_KERNEL="1:6.18.51"
35+
36+
# The Nitro Sandbox memory pool, as a nitro_enclaves.mempool= kernel argument.
37+
readonly ENCLAVE_MEMPOOL="95%-1G"
38+
39+
# The first Firecracker release that can drive a Nitro Sandbox.
40+
readonly MIN_FIRECRACKER_VERSION="v1.18.0"
41+
42+
# The runtime and guest image come from the same bundle as the other node
43+
# components (see install-worker.sh), as <artifact>/linux/<arch>/ under it.
44+
readonly BUNDLE_S3_URI="s3://${BINARY_BUCKET_NAME}/${KUBERNETES_VERSION}/${KUBERNETES_BUILD_DATE}"
45+
46+
# Guest boot artifacts.
47+
readonly IMAGES_DIR="/var/lib/nitro-sandbox-images"
48+
49+
# Jailer chroot base, per-VM drive images, sockets and the shim log.
50+
readonly STATE_DIR="/var/lib/nitro-sandbox"
51+
52+
# cri-proxy and the sandbox device plugin are subcommands of this binary, so
53+
# neither installs a binary of its own.
54+
readonly SHIM_BINARY="containerd-shim-nitro-sandbox-v2"
55+
56+
readonly OVERLAY="${WORKING_DIR}/pod-isolation"
57+
58+
# `uname -m` names, as Firecracker and the jailer are keyed.
59+
case "$(uname -m)" in
60+
x86_64 | aarch64) ARCH="$(uname -m)" ;;
61+
*)
62+
echo "ERROR: unsupported architecture $(uname -m)" >&2
63+
exit 1
64+
;;
65+
esac
66+
readonly ARCH
67+
68+
# The artifact bucket keys by Go-style `amd64` / `arm64`, not `uname -m` names.
69+
case "${ARCH}" in
70+
x86_64) GUEST_ARCH="amd64" ;;
71+
aarch64) GUEST_ARCH="arm64" ;;
72+
esac
73+
readonly GUEST_ARCH
74+
75+
echo "Installing the pod isolation runtime for ${ARCH} from ${BUNDLE_S3_URI}"
76+
77+
sudo install -d -m 0755 "${IMAGES_DIR}" "${STATE_DIR}"
78+
79+
# shellcheck disable=SC1090
80+
source "${WORKING_DIR}/helpers/aws-clients.sh"
81+
82+
# Download one object from a given S3 base URI. Integrity is the caller's
83+
# problem; see `fetch_from` for the `.sha256`-verified variant. aws_s3 retries
84+
# unsigned, so a public bucket is readable without an instance profile.
85+
download_from() {
86+
local base="$1" key="$2" dest="$3" mode="$4"
87+
local tmp="${WORKING_DIR}/download.$$"
88+
89+
echo "Fetching ${base}/${key}"
90+
if ! aws_s3 cp --quiet --region "${BINARY_BUCKET_REGION}" "${base}/${key}" "${tmp}"; then
91+
echo "ERROR: could not fetch ${base}/${key}." >&2
92+
return 1
93+
fi
94+
sudo mv "${tmp}" "${dest}"
95+
sudo chown root:root "${dest}"
96+
sudo chmod "${mode}" "${dest}"
97+
}
98+
99+
# Download from a given S3 base URI and verify against the `.sha256` sidecar
100+
# beside the object. Compared by hash value, not `sha256sum -c`, since the name
101+
# the sidecar records is relative to where the checksums were generated.
102+
fetch_from() {
103+
local base="$1" key="$2" dest="$3" mode="$4"
104+
local expected actual
105+
106+
download_from "${base}" "${key}" "${dest}" "${mode}"
107+
108+
expected=$(aws_s3 cp --quiet --region "${BINARY_BUCKET_REGION}" "${base}/${key}.sha256" - | awk '{print $1}')
109+
actual=$(sudo sha256sum "${dest}" | awk '{print $1}')
110+
if [ "${expected}" != "${actual}" ]; then
111+
echo "ERROR: checksum mismatch for ${key}" >&2
112+
echo " expected ${expected}" >&2
113+
echo " actual ${actual}" >&2
114+
return 1
115+
fi
116+
echo " -> ${dest}"
117+
}
118+
119+
################################################################################
120+
### Packages ###################################################################
121+
################################################################################
122+
# The shim execs `mkfs.erofs` to build the erofs image the guest mounts for a
123+
# container's root filesystem. Without it, container start fails with ENOENT,
124+
# which reads like a missing file rather than a missing package.
125+
sudo dnf install -y erofs-utils
126+
127+
################################################################################
128+
### Host kernel ################################################################
129+
################################################################################
130+
# The sandbox extensions to the nitro_enclaves driver are in the stock Amazon
131+
# Linux kernel from ${MIN_KERNEL} onwards, so no kernel is
132+
# installed from elsewhere. Earlier kernels carry the driver but not the
133+
# extensions: the sidecar ioctls are missing, the node advertises no sandbox
134+
# capacity, and nothing can be scheduled. Standalone enclaves work there;
135+
# sidecars do not.
136+
#
137+
# The base image usually already satisfies this, in which case dnf reports
138+
# nothing to do. Asserting it here rather than assuming it means an older base
139+
# image fails the build instead of producing an AMI that registers fine and then
140+
# refuses every sandboxed pod.
141+
sudo dnf install -y "kernel6.18 >= ${MIN_KERNEL}"
142+
143+
################################################################################
144+
### Boot arguments #############################################################
145+
################################################################################
146+
# The Nitro Sandbox memory pool must be reserved at boot with the
147+
# nitro_enclaves.mempool kernel parameter; a sandbox fails to start if the pool is
148+
# not reserved. The shim checks it before starting every sandbox, so a node
149+
# without it registers fine and then refuses every sandboxed pod. Each sandbox
150+
# takes its full memory from the pool when it starts and returns it only when it
151+
# terminates.
152+
set-kernel-arg "nitro_enclaves.mempool=${ENCLAVE_MEMPOOL}"
153+
154+
################################################################################
155+
### Runtime ####################################################################
156+
################################################################################
157+
158+
# The Firecracker release to install: POD_ISOLATION_FIRECRACKER_VERSION, or the
159+
# newest release when that is "latest". Either way it must be at least
160+
# MIN_FIRECRACKER_VERSION.
161+
resolve_firecracker_version() {
162+
local version="${POD_ISOLATION_FIRECRACKER_VERSION}"
163+
164+
if [ "${version}" = "latest" ]; then
165+
# releases/latest redirects to releases/tag/<version>.
166+
version=$(curl --fail --silent --show-error --location --retry 3 --output /dev/null \
167+
--write-out '%{url_effective}' https://github.com/firecracker-microvm/firecracker/releases/latest)
168+
version="${version##*/}"
169+
fi
170+
if [ -z "${version}" ] || [ "$(printf '%s\n%s\n' "${MIN_FIRECRACKER_VERSION}" "${version}" | sort -V | head -n 1)" != "${MIN_FIRECRACKER_VERSION}" ]; then
171+
echo "ERROR: Firecracker ${version:-<none>} is older than ${MIN_FIRECRACKER_VERSION}" >&2
172+
return 1
173+
fi
174+
echo "${version}"
175+
}
176+
177+
# The VMM each isolated pod runs inside, and the jailer that confines it, from
178+
# the upstream Firecracker release. The tarball is checked against the
179+
# .sha256.txt published beside it before anything is extracted.
180+
install_firecracker() {
181+
local version
182+
version=$(resolve_firecracker_version)
183+
local name="firecracker-${version}-${ARCH}.tgz"
184+
local url="https://github.com/firecracker-microvm/firecracker/releases/download/${version}/${name}"
185+
local dir="${WORKING_DIR}/firecracker"
186+
local expected actual
187+
188+
mkdir -p "${dir}"
189+
echo "Fetching ${url}"
190+
curl --fail --silent --show-error --location --retry 3 --output "${dir}/${name}" "${url}"
191+
expected=$(curl --fail --silent --show-error --location --retry 3 "${url}.sha256.txt" | awk '{print $1}')
192+
actual=$(sha256sum "${dir}/${name}" | awk '{print $1}')
193+
if [ -z "${expected}" ] || [ "${expected}" != "${actual}" ]; then
194+
echo "ERROR: checksum mismatch for ${name}" >&2
195+
echo " expected ${expected}" >&2
196+
echo " actual ${actual}" >&2
197+
return 1
198+
fi
199+
200+
# The tarball holds release-<version>-<arch>/{firecracker,jailer}-<version>-<arch>.
201+
tar -xzf "${dir}/${name}" -C "${dir}"
202+
for binary in firecracker jailer; do
203+
sudo install -m 0755 "${dir}/release-${version}-${ARCH}/${binary}-${version}-${ARCH}" "/usr/bin/${binary}"
204+
echo " -> /usr/bin/${binary}"
205+
done
206+
207+
rm -rf "${dir}"
208+
}
209+
210+
install_firecracker
211+
212+
# The runtime containerd hands a sandbox to, in the slot runc occupies for an
213+
# ordinary pod. The release archive is checked against its `.sha256` sidecar like
214+
# the other bundle artifacts.
215+
#
216+
# The archive also carries the guest agent, the ocicrypt keyprovider, both
217+
# genpolicy binaries and the policy evaluator. Only the shim is installed: the
218+
# agent lives in the guest image, and the rest are workstation tools.
219+
install_runtime_archive() {
220+
local dir="${WORKING_DIR}/runtime"
221+
222+
mkdir -p "${dir}"
223+
fetch_from "${BUNDLE_S3_URI}/nitro-sandboxes-runtime/linux/${GUEST_ARCH}" \
224+
"nitro-sandboxes-runtime.tar.gz" "${dir}/nitro-sandboxes-runtime.tar.gz" 0644
225+
226+
tar -xzf "${dir}/nitro-sandboxes-runtime.tar.gz" -C "${dir}" "${SHIM_BINARY}"
227+
sudo install -m 0755 "${dir}/${SHIM_BINARY}" "/usr/bin/${SHIM_BINARY}"
228+
echo " -> /usr/bin/${SHIM_BINARY}"
229+
230+
rm -rf "${dir}"
231+
}
232+
233+
install_runtime_archive
234+
235+
# The guest, as an EIF plus its disk. Firecracker loads the EIF directly with the kernel command line baked in,
236+
# which is why nitro-sandbox.toml sets no boot_args. The pair must come from one
237+
# build: the disk is a dm-verity image whose root hash is in the EIF's measured
238+
# command line, so a mismatch fails at guest boot.
239+
fetch_from "${BUNDLE_S3_URI}/nitro-sandbox-guest/linux/${GUEST_ARCH}" "nitro-sandbox-guest.eif" "${IMAGES_DIR}/latest.eif" 0644
240+
fetch_from "${BUNDLE_S3_URI}/nitro-sandbox-guest/linux/${GUEST_ARCH}" "nitro-sandbox-guest-disk.img" "${IMAGES_DIR}/latest-disk.img" 0644
241+
242+
# The shim attaches the guest disk as "rootfs.ext4" from the directory holding
243+
# the kernel it was given.
244+
sudo ln -sf latest-disk.img "${IMAGES_DIR}/rootfs.ext4"
245+
246+
# The guest agent needs no host install; it is part of the guest image.
247+
248+
sudo install -D -m 0644 "${OVERLAY}/nitro-sandbox.toml" /etc/containerd/nitro-sandbox.toml
249+
250+
# The shim creates a TAP device per microVM; without tun, sandbox creation fails.
251+
sudo install -D -m 0644 "${OVERLAY}/nitro-sandbox-modules.conf" /etc/modules-load.d/nitro-sandbox.conf
252+
253+
################################################################################
254+
### CRI proxy ##################################################################
255+
################################################################################
256+
# Routes isolated pods to the right runtime in front of kubelet and advertises
257+
# how many sandboxes the node can hold.
258+
sudo install -D -m 0644 "${OVERLAY}/cri-proxy.service" /etc/systemd/system/cri-proxy.service
259+
sudo install -D -m 0644 "${OVERLAY}/restart-with-cri-proxy.conf" \
260+
/etc/systemd/system/kubelet.service.d/restart-with-cri-proxy.conf
261+
262+
# Enabled, not started: there is no containerd to talk to on the builder.
263+
sudo systemctl daemon-reload
264+
sudo systemctl enable cri-proxy.service
265+
266+
################################################################################
267+
### containerd and kubelet configuration #######################################
268+
################################################################################
269+
# nodeadm regenerates /etc/containerd/config.toml and the kubelet config from
270+
# templates on every boot, so edits made here would be discarded before
271+
# containerd first starts. The runtime handlers, discard_unpacked_layers and
272+
# kubelet's containerRuntimeEndpoint ship as a NodeConfig drop-in instead, which
273+
# nodeadm reads as a config source and merges over user data.
274+
sudo install -D -m 0644 "${OVERLAY}/10-pod-isolation.yaml" /etc/eks/nodeadm.d/10-pod-isolation.yaml
275+
276+
echo "Pod isolation runtime installed"

0 commit comments

Comments
 (0)