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

packages/nixos: add IMDS setup script #988

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/by-name/cloud-api-adaptor/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
writeShellApplication,
gnugrep,
iptables,
iproute2,
sysctl,
gawk,
runCommand,
applyPatches,
makeWrapper,
Expand Down Expand Up @@ -102,6 +105,22 @@ buildGoModule rec {
"SC2153"
];
};

setup-nat-for-imds = writeShellApplication {
name = "setup-nat-for-imds";
runtimeInputs = [
iproute2
iptables
sysctl
gawk
];
# TODO(burgerdev): generalize for all link-local IPs and investigate routing simplification
text = builtins.readFile "${cloud-api-adaptor.src}/src/cloud-api-adaptor/podvm/files/usr/local/bin/setup-nat-for-imds.sh";
msanft marked this conversation as resolved.
Show resolved Hide resolved
meta = {
mainProgram = "setup-nat-for-imds";
homepage = "https://github.com/confidential-containers/cloud-api-adaptor/blob/main/src/cloud-api-adaptor/podvm/files/usr/local/bin/setup-nat-for-imds.sh";
msanft marked this conversation as resolved.
Show resolved Hide resolved
};
};
};

meta = {
Expand Down
20 changes: 20 additions & 0 deletions packages/nixos/azure.nix
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,25 @@ in
ExecStart = "${lib.getExe pkgs.azure-no-agent}";
};
};

systemd.services.setup-nat-for-imds = {
wantedBy = [ "multi-user.target" ];
requires = [ "[email protected]" ];
wants = [ "network-online.target" ];
after = [
"network-online.target"
"[email protected]"
];
description = "Setup NAT for IMDS";
serviceConfig = {
Type = "oneshot";
RemainAfterExit = "yes";
# TODO(msanft): Find out why just ordering this after network-online.target
# isn't sufficient. (Errors with saying that the network is unreachable)
Restart = "on-failure";
RestartSec = "5s";
ExecStart = "${lib.getExe pkgs.cloud-api-adaptor.setup-nat-for-imds}";
};
};
};
}
27 changes: 19 additions & 8 deletions packages/test-peerpods.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,17 @@ if [[ $found != true ]]; then
exit 1
fi

run_tests() {
pod="$(kubectl get pod -l app=alpine -o jsonpath='{.items[0].metadata.name}')"

# Check IMDS functionality.
# -f makes this fail on a 500 status code.
kubectl exec "$pod" -- curl -f -i -H "Metadata: true" http://169.254.169.254/metadata/THIM/amd/certification
}

cleanup() {
kubectl delete deploy nginx
kubectl wait --for=delete pod --selector=app=nginx --timeout=5m
kubectl delete deploy alpine
kubectl wait --for=delete pod --selector=app=alpine --timeout=5m
}

trap cleanup EXIT
Expand All @@ -52,26 +60,29 @@ kubectl apply -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
name: alpine
spec:
selector:
matchLabels:
app: nginx
app: alpine
replicas: 1
template:
metadata:
labels:
app: nginx
app: alpine
spec:
runtimeClassName: kata-remote
containers:
- name: nginx
image: nginx
- name: alpine
image: alpine/curl
imagePullPolicy: Always
command: ["sleep", "3600"]
EOF

if ! kubectl wait --for=condition=available --timeout=5m deployment/nginx; then
if ! kubectl wait --for=condition=available --timeout=5m deployment/alpine; then
kubectl describe pods
kubectl logs -n confidential-containers-system -l app=cloud-api-adaptor --tail=-1 --all-containers
exit 1
fi

run_tests