Repository navigation
feat: generalize Linux images with aks-node-controller generalize - #10151
Artur Khantimirov (r2k1) wants to merge 15 commits into
Conversation
|
There was an error handling pipeline event 2996aad2-e588-4574-8799-124f573520b4. |
Windows Unit Test Results 3 files 21 suites 1m 16s ⏱️ Results for commit 8e673b2. ♻️ This comment has been updated with latest results. |
There was a problem hiding this comment.
🟡 Changes recommended
Capture can fail for the frozen AzureLinuxV2 image because its ANC lacks the required generalize command, while unchecked leftover waagent state can expose certificates and extension data in captured images.
2 open issues
What changed in this PR
Centralizes Linux image-capture cleanup in aks-node-controller generalize, replacing separate implementations used by AgentBaker’s image builds and E2E VHD caching.
Changes:
- Adds image and
--keep-agentmodes, shared OS configuration, and Go tests. - Switches Packer, Azure Image Builder, and E2E capture paths to the command; removes replaced cleanup scripts.
- Consolidates kernel-module mitigation helpers and reapplies them after the build reboot.
| File | Description |
|---|---|
| vhdbuilder/prefetch/templates/optimize.json | Invokes generalize through the AIB deprovision hook. |
| vhdbuilder/packer/vhd-image-builder-mariner.json | Switches Azure Linux capture to generalize. |
| vhdbuilder/packer/vhd-image-builder-mariner-cvm.json | Switches Azure Linux CVM capture to generalize. |
| vhdbuilder/packer/vhd-image-builder-mariner-arm64.json | Switches Azure Linux ARM64 capture to generalize. |
| vhdbuilder/packer/vhd-image-builder-cvm.json | Switches Ubuntu CVM capture to generalize. |
| vhdbuilder/packer/vhd-image-builder-base.json | Switches base Ubuntu capture to generalize. |
| vhdbuilder/packer/vhd-image-builder-arm64-gen2.json | Switches Ubuntu ARM64 Gen2 capture to generalize. |
| vhdbuilder/packer/vhd-image-builder-arm64-gb.json | Switches Ubuntu ARM64 GB capture to generalize. |
| vhdbuilder/packer/vhd-image-builder-acl.json | Switches Azure Container Linux capture to generalize. |
| vhdbuilder/packer/vhd-image-builder-acl-arm64.json | Switches ACL ARM64 capture to generalize. |
| vhdbuilder/packer/test/linux-vhd-content-test.sh | Updates comments describing agent installation; no behavior change. |
| vhdbuilder/packer/post-install-dependencies.sh | Reapplies kernel-module policy after reboot. |
| vhdbuilder/packer/post-deprovision-walinuxagent.sh | Removes the replaced agent-installation shell script. |
| vhdbuilder/packer/packer_source.sh | Hosts shared mitigation helpers; removes obsolete script copying. |
| vhdbuilder/packer/install-dependencies.sh | Sources shared helpers instead of defining them locally. |
| vhdbuilder/packer/install_walinuxagent.py | Removes the replaced Python agent installer. |
| vhdbuilder/packer/deprovision.sh | Removes replaced deprovision and credential-cleanup helpers. |
| vhdbuilder/packer/cleanup-vhd.sh | Removes the replaced image-cleanup script. |
| spec/vhdbuilder/packer/post_deprovision_walinuxagent_spec.sh | Removes ShellSpec tests superseded by Go tests. |
| spec/vhdbuilder/packer/deprovision_spec.sh | Removes tests for deleted deprovision helpers. |
| spec/parts/linux/cloud-init/artifacts/cse_main_disable_modules_spec.sh | Points policy tests to the consolidated helpers. |
| e2e/scenario/provision.go | Uses generalize with --keep-agent for Linux capture. |
| aks-node-controller/repository_hotfix.go | Uses shared OS identifiers. |
| aks-node-controller/repository_hotfix_test.go | Updates tests to shared OS identifiers. |
| aks-node-controller/repository_hotfix_rpm.go | Uses the shared Azure Linux identifier. |
| aks-node-controller/README.md | Documents generalization steps and both modes. |
| aks-node-controller/main.go | Uses stderr-only generalize logging and shared paths. |
| aks-node-controller/hotfix.go | Uses shared OS and variant identifiers. |
| aks-node-controller/generalize/walinuxagent.go | Implements pinned-agent download, installation, and configuration. |
| aks-node-controller/generalize/walinuxagent_test.go | Tests agent downloads, extraction, pruning, and configuration. |
| aks-node-controller/generalize/sync_windows.go | Provides a Windows no-op sync implementation. |
| aks-node-controller/generalize/sync_linux.go | Implements Linux filesystem synchronization. |
| aks-node-controller/generalize/sync_darwin.go | Provides Darwin filesystem synchronization. |
| aks-node-controller/generalize/generalize.go | Implements ordered cleanup and generalization steps. |
| aks-node-controller/generalize/generalize_test.go | Tests cleanup modes, boot state, and failures. |
| aks-node-controller/embeddednodecustomdata.go | Reuses shared OS parsing and identifiers. |
| aks-node-controller/embeddednodecustomdata_test.go | Updates tests to the shared OS identifier. |
| aks-node-controller/const.go | Moves OS configuration out; defines the command name. |
| aks-node-controller/const_test.go | Moves OS configuration tests into config. |
| aks-node-controller/config/osrelease.go | Centralizes OS-release parsing and identifiers. |
| aks-node-controller/config/config.go | Centralizes fixed paths and OS-specific configuration. |
| aks-node-controller/config/config_test.go | Tests OS-specific configuration selection. |
| aks-node-controller/checkhotfix.go | Uses the shared NBC command path. |
| aks-node-controller/app.go | Registers generalize and adopts shared configuration. |
| aks-node-controller/app_test.go | Tests argument rejection and updates path references. |
Review effort: Balanced
|
There was an error handling pipeline event 29877877-e588-4574-8799-124f573520b4. |
|
There was an error handling pipeline event e449f131-6302-471d-92ab-f1f4c76db1bf. |
There was a problem hiding this comment.
🔵 Needs a closer look
The new generalize command breaks legacy image captures that lack a compatible binary, and the removed waagent emptiness check allows residual certificates or VM state to be captured.
2 open issues
1 resolved since last review
Review effort: Balanced
There was a problem hiding this comment.
🔵 Needs a closer look
The migration requires owner validation of the untested AIB deprovision hook and CVM capture behavior, plus confirmation that the external aks-rp hotfix-prefetch update lands before VHDs are released without cleanup-vhd.sh.
No open issues 🎉
3 resolved since last review
Review effort: Balanced
| // removeNvPCRCredentials removes top-level anchor files and symlinks in dir. A missing dir is not an error. | ||
| // The match ignores case because the ESP is FAT. /run/credentials/@encrypted is a RAM-only boot import; | ||
| // removing its boot source here prevents a reimport. | ||
| func removeNvPCRCredentials(dir string) error { |
There was a problem hiding this comment.
Please preserve #10076’s Ubuntu 26.04 + CVM gate specifically around NvPCR anchor removal. This code is currently running on all linux images, but is only relevant to 2604CVM.
There was a problem hiding this comment.
do we actually need to anything specific? I feel it should be fine as long as we know that we always want to remove these files/symlinks - this func already handles both cases where they exist and don't exist
There was a problem hiding this comment.
Yeah. I don't like branching.
I don't see harm of deleting it everywhere.
Next time we will add a new ubuntu version or a new flavour. And branching logic will require a constant updates.
There was a problem hiding this comment.
I agree with Cameron Meissner (@cameronmeissner) and will keep it ungated, on purpose:
- The anchor binds to the TPM of the build VM on any image that has one. A clone that keeps the anchor has a stale anchor, and systemd recreates it at boot.
- On images without NvPCR these paths do not exist, so the step does nothing.
removeNvPCRCredentialsignores a missing directory and removes only top-levelnvpcr-anchor.*.credfiles and symlinks. The SRK, NV indices, and disk-encryption credentials stay. - A gate on Ubuntu 26.04 + CVM adds an OS-version constant, and that constant drifts. A new image with systemd NvPCR (for example a later Ubuntu release or a TrustedLaunch variant) would then capture a stale anchor without a warning.
The removal logs each path that it removes, so the build logs show where it applies.
| {name: "RemoveNodeState", run: g.removeNodeState}, | ||
| {name: "ResetACLFirstBoot", run: g.resetACLFirstBoot}, | ||
| {name: "DeprovisionAgent", imageOnly: true, run: g.deprovisionAgent}, | ||
| {name: "InstallPinnedAgent", imageOnly: true, run: g.installPinnedWALinuxAgent}, |
There was a problem hiding this comment.
Both capture paths appear to retain the post-deprovision agent installation. Please confirm images produced with and without prefetch boot with the WALinuxAgent version specified in components.json. For prefetch, verify the final AIB-produced image. This can be done by just running the AB E2Es after both scenarios because thats part of standard node validation now.
There was a problem hiding this comment.
Checked both paths.
- Without prefetch: content tests
testWALinuxAgentInstalledandtestWALinuxAgentRunningVersionpassed in build 192753772. They boot the captured image and check the version from components.json. - With prefetch: the pipelines skip this step (
RUN_PREFETCH_OPTIMIZATIONis unset or False in 172842 and 165322), so I ran this PR'soptimize.jsonin a sandbox AIB against the u2204 and azl3 images from 192753772. This found a bug: AIB uploads/tmp/DeprovisioningScript.shas the SSH user before customizers run, andfs.protected_regularblocked oursudo tee(Permission denied). Fixed in 57a4747: the hook removes the file first. With the fix, both AIB builds succeeded, the log showsgeneralize finished, and VMs from the output VHDs runWALinuxAgent-2.16.0.2as the goal-state agent, withAutoUpdate.UpdateToLatestVersion=n. - Note: E2E
ValidateWaagentLogdoes not check the version.
| if [ -x "${anc}-hotfix" ]; then | ||
| anc="${anc}-hotfix" | ||
| fi | ||
| "$anc" generalize --keep-agent |
There was a problem hiding this comment.
This is PIS specific so I don't have a concrete answer.
Can PIS cache-image preparation use an older source VHD whose ANC lacks generalize --keep-agent? If so, how do we avoid invoking an unsupported command—do we stage an updated ANC or retain a legacy cleanup fallback? Or is this workflow guaranteed to use a VHD containing the new subcommand? Like PIS always use the latest VHD as base image?
There was a problem hiding this comment.
I will add a condition. "IF VHDVersion > X THEN use new approach ELSE use old"
Eventually we will delete old. I don't think we have large number of people using old VHDs
|
High level review looks good to me.
|
Replace cleanup-vhd.sh, deprovision.sh, post-deprovision-walinuxagent.sh and install_walinuxagent.py with one "aks-node-controller generalize" command. Packer and Azure Image Builder use image mode. VHD caching (PIS) uses --keep-agent, which keeps WALinuxAgent running for RunCommand. The code is in the new generalize package. Shared OS paths and os-release parsing move to the new config package. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Port #10125 to aks-node-controller generalize. waagent can stop at a failed cleanup action and still exit 0. Before deprovision, kill, disable and stop gcd.service and stop the agent units and extension slices (best effort). After deprovision, fail when the output contains "Failed to run 'deprovision". Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…lize - Run waagent through PATH, as vhdbuilder deprovision.sh did in #10125. - Parse the GAFamily manifest with a typed struct (Plugins>Plugin>Version, Uris>Uri), the layout WALinuxAgent reads. - Add a prefix-version case to the manifest test. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
- New first image step DownloadPinnedAgent saves and validates the zip in /opt/walinuxagent/downloads while DNS still works. A failed download now stops the build before any change. This removes the temporary resolv.conf handling. - InstallPinnedAgent extracts the local zip with os.CopyFS into a clean directory. zip.Reader keeps entries inside the directory; symlink entries fail. The 256 MiB per-entry limit is gone. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The fixed NvPCR anchor paths join buildArtifactPaths, and the boot-loader credential scan runs in RemoveBuildArtifacts. The separate RemoveNvPCRAnchors step is removed. Matching stays case-insensitive and read errors still fail the step. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
- Redact URL queries only at the fetch exit; drop unwrapURLError. - The SAS test now fails a request whose URL has a SAS token. The old test failed on the goal-state request, which has no token. - Remove single-use helpers (appendFile, writeFileKeepMode, removeIfExists) and constants; shorten the retry loop. - Make the command runner an unexported test hook. - Update the README step table for the NvPCR and download steps. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
… only off Windows Log each removed build artifact so VHD build logs show the NvPCR anchor removal. Replace four runtime Windows skips with a build constraint; this also fixes a goconst finding. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The hook ran rm -f "$0" before sudo. /tmp has the sticky bit and the hook is root-owned, so a non-root AIB caller would fail the delete and skip generalize. The old hook deleted it inside its sudo script. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The aks-rp hotfix prefetch AIB template runs /opt/azure/containers/cleanup-vhd.sh. Deleting the file broke that template for new VHDs. Keep the path as a small shim that installs the AIB deprovision hook for aks-node-controller generalize, and use it from optimize.json too, so both AIB templates share one entry point and aks-rp needs no change. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Revert the cleanup-vhd.sh shim. optimize.json writes /tmp/DeprovisioningScript.sh in one inline step, and the hook runs aks-node-controller generalize. The aks-rp hotfix prefetch template must write the same hook before the first VHD release from this branch. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
waagent can stop a partial cleanup and still exit 0. Fail when /var/lib/waagent is not empty after deprovision, as dev (#10125) did. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Move the vulnerable kernel module deny-rule helpers to tool_installs.sh. The OSGuard imagecustomizer build stages tool_installs.sh but not packer_source.sh, so install-dependencies.sh must not source it. Log generalize command errors the same way as other commands. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
AIB uploads /tmp/DeprovisioningScript.sh as the SSH user before customizers run. In sticky /tmp, fs.protected_regular stops root from opening that file with O_CREAT, so sudo tee failed with Permission denied. Remove the file first. AB#40892331 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Rebase onto dev after #10150. Use strings.SplitSeq, give gosec G302 directives a reason, drop gochecknoglobals directives, and ignore the darwin syscall.Sync error. AB#40892331 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
57a4747 to
8e673b2
Compare
|


Agent assessment
Self-reported assessment, not an independent review.
Assessed revision:
8e673b26464700294796bd7c647909195a9b6e5e↑ Sandbox file-hash comparison of old scripts vs new command matched on Ubuntu 22.04, 24.04, Azure Linux 3 (amd64, arm64) and ACL TL.
↑ VHD build 192753772 and E2E 192764613 on af3f55d, before the rebase onto dev: no failure caused by this PR;
Ubuntu2404_VHDCachingpassed.↑ Sandbox AIB runs of
optimize.jsonon u2204 and azl3:generalize finished; output VMs run WALinuxAgent 2.16.0.2 as goal-state agent. These runs found and verified the/tmp/DeprovisioningScript.shpermission fix.↑ After the rebase onto dev: golangci-lint v2.15.0 reports 0 issues (linux, windows, darwin);
configandgeneralizetests pass.↓ No VHD build or E2E on the rebased head yet.
↓ Workload-optimize step not run in sandbox; CI pipelines skip prefetch.
↓ CVM not compared.
↓ aks-rp hotfix AIB template must change before the first VHD release from this PR.
What this PR does / why we need it:
Linux image generalization now uses one command:
aks-node-controller generalize.Before: each capture path had its own generalization logic.
cleanup-vhd.sh,deprovision.sh,post-deprovision-walinuxagent.shandinstall_walinuxagent.py.optimize.json):cleanup-vhd.shplus theaib-deprovision.shhook thatdeprovision.shwrote.vhd/generalizelinux.go), copied into E2E.After: all paths call the same ANC command.
sudo /opt/azure/containers/aks-node-controller generalize.optimize.json): one inline customize step writes the deprovision hook/tmp/DeprovisioningScript.sh, which runsexec sudo aks-node-controller generalize. AIB runs the hook in place of its default deprovision. Generalize deletes the hook file as root.cleanup-vhd.shis deleted.aks-node-controller generalize --keep-agent. This mode does not stop or deprovision WALinuxAgent, because RunCommand reports status through it.The steps and the two modes are documented in
aks-node-controller/README.md("Generalizing a VM for image capture"). The command writes logs only to stderr, so no log file goes into the image.Other changes:
generalize_test.go,walinuxagent_test.go).cleanup-vhd.shmove totool_installs.sh. Before,install-dependencies.shandcleanup-vhd.shhad 2 copies that had drifted; now there is 1.post-install-dependencies.shcalls them after the reboot, with the same gates.tool_installs.shis also staged for AzureLinux OSGuard, which does not getpacker_source.sh.configpackage, sogeneralizecan use them.Follow-ups (not in this PR):
generalizelinux.go(PIS) to callaks-node-controller generalize --keep-agent. Older VHDs keep the old path.ev2/vhd/package/image_builder_template{,_arm64}.json, stepRunVHDCleanup) still runs/opt/azure/containers/cleanup-vhd.sh, which new VHDs do not have. Change the step to: ifcleanup-vhd.shexists (older VHD), run it as today; else write the same hook asoptimize.json.Verification:
cd aks-node-controller && go build ./... && go test ./...go-test). Locally,go test ./generalize/ ./config/passed.GOOS=windows go vet ./generalize/,GOOS=darwin go vet ./generalize/make validate-shelldevissue (#10148).devand #10150 at the same time), 5 arm64 scenarios without an image, 1FilesystemCorruptionfailure that also fails ondev.testEmudhraRootRemoval(same failure ondevbuild 191654280), 2404 arm64 missing package (#10148), 2004 CVMQuotaExceeded, ACL CVM preflight validation.FilesystemCorruptionvalidator failure that also failed ondev(fixed later by #10164).Ubuntu2404_VHDCaching(PIS path) passed.Which issue(s) this PR fixes:
Fixes #
AB#40892331