Fix container machine setup script being dropped from artifact.#1891
Open
anxkhn wants to merge 1 commit into
Open
Fix container machine setup script being dropped from artifact.#1891anxkhn wants to merge 1 commit into
anxkhn wants to merge 1 commit into
Conversation
When fetching container machine resources from an OCI artifact, the setup script layer was decoded separately and then merged into the config with `if var resources, let setupScript`. Because MachineResources is a value type, that binding mutated a throwaway copy, so the fetched setup script was silently discarded and the returned resources kept a nil setupScript. As a result MachineBundle fell back to the default create-user.sh instead of the per-image script. Merge through the optional so the script survives on the returned value, and add unit tests for the merge behavior.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Type of Change
Motivation and Context
When a container machine image ships a custom setup script as an OCI artifact,
that script was silently dropped and the default
create-user.shwas usedinstead.
In
MachineClient.fetchMachineArtifact, the config layer is decoded intoresources: MachineResources?and the separate setup-script layer intosetupScript: String?. The two were merged with:MachineResourcesis a value type (astruct), soif var resourcesbinds amutable copy. Assigning
setupScriptmutates that throwaway copy, which isdiscarded at the end of the block. The function then returns the outer
resources, whosesetupScriptis stillnil. Downstream,MachineBundle.createseesresources?.setupScript == niland falls back tocopying the default
userSetupFile, so the per-image script fetched from theregistry is never applied.
The fix merges through the optional (
resources?.setupScript = setupScript) sothe value survives on the returned
MachineResources. The merge is extractedinto a small
merge(resources:setupScript:)helper, both to make the valuesemantics explicit and to give the logic a unit-testable seam
(
fetchMachineArtifactitself needs a live registry client).Testing
Added a
MachineAPIClientTeststarget with three tests for the merge:resourcesstays nil.swift test --filter MachineAPIClientTestspasses (3/3). Reverting the helper tothe original
if var resourcesbody makes the first test fail(
merged?.setupScriptisnil), confirming the test covers this bug. The fulltree builds with
swift build --build-tests, andswift format lint --strictis clean on the changed files.
Files changed (3 files, +56 -3)
Sources/Services/MachineAPIService/Client/MachineClient.swiftReplace the inline
if var resources, let setupScript { ... }merge with a callto a new
static func merge(resources:setupScript:) -> MachineResources?thatassigns through the optional. Doc comment notes the value-type pitfall.
Package.swiftAdd a
MachineAPIClientTeststest target depending onMachineAPIClient(this target had no unit tests before, only VM-dependent integration tests).
Tests/MachineAPIClientTests/MachineClientMergeTests.swift(new)Three Swift Testing (
import Testing) regression tests. Carries the repo'scanonical Apache-2.0 license header (byte-identical to existing tracked files).