Merge pull request #6211 from nalind/source-date-epoch-static-hostname

build: --source-date-epoch/--timestamp use static hostname/cid
This commit is contained in:
openshift-merge-bot[bot] 2025-06-10 13:55:54 +00:00 committed by GitHub
commit d14b4f8dc7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 72 additions and 3 deletions

View File

@ -308,12 +308,21 @@ func (i *containerImageRef) newDockerSchema2ManifestBuilder() (manifestBuilder,
if err := json.Unmarshal(i.dconfig, &dimage); err != nil { if err := json.Unmarshal(i.dconfig, &dimage); err != nil {
return nil, err return nil, err
} }
// Suppress the hostname and domainname if we're running with the
// equivalent of either --timestamp or --source-date-epoch.
if i.created != nil {
dimage.Config.Hostname = "sandbox"
dimage.Config.Domainname = ""
}
// Set the parent, but only if we want to be compatible with "classic" docker build. // Set the parent, but only if we want to be compatible with "classic" docker build.
if i.compatSetParent == types.OptionalBoolTrue { if i.compatSetParent == types.OptionalBoolTrue {
dimage.Parent = docker.ID(i.parent) dimage.Parent = docker.ID(i.parent)
} }
// Set the container ID and containerConfig in the docker format. // Set the container ID and containerConfig in the docker format.
dimage.Container = i.containerID dimage.Container = i.containerID
if i.created != nil {
dimage.Container = ""
}
if dimage.Config != nil { if dimage.Config != nil {
dimage.ContainerConfig = *dimage.Config dimage.ContainerConfig = *dimage.Config
} }

View File

@ -1034,9 +1034,14 @@ func (s *StageExecutor) prepare(ctx context.Context, from string, initializeIBCo
for _, p := range builder.Ports() { for _, p := range builder.Ports() {
ports[docker.Port(p)] = struct{}{} ports[docker.Port(p)] = struct{}{}
} }
hostname, domainname := builder.Hostname(), builder.Domainname()
containerName := builder.Container
if s.executor.timestamp != nil || s.executor.sourceDateEpoch != nil {
hostname, domainname, containerName = "sandbox", "", ""
}
dConfig := docker.Config{ dConfig := docker.Config{
Hostname: builder.Hostname(), Hostname: hostname,
Domainname: builder.Domainname(), Domainname: domainname,
User: builder.User(), User: builder.User(),
Env: builder.Env(), Env: builder.Env(),
Cmd: builder.Cmd(), Cmd: builder.Cmd(),
@ -1063,7 +1068,7 @@ func (s *StageExecutor) prepare(ctx context.Context, from string, initializeIBCo
dImage := docker.Image{ dImage := docker.Image{
Parent: builder.FromImageID, Parent: builder.FromImageID,
ContainerConfig: dConfig, ContainerConfig: dConfig,
Container: builder.Container, Container: containerName,
Author: builder.Maintainer(), Author: builder.Maintainer(),
Architecture: builder.Architecture(), Architecture: builder.Architecture(),
RootFS: rootfs, RootFS: rootfs,

View File

@ -7865,3 +7865,58 @@ EOF
layer=$((++layer)) layer=$((++layer))
done done
} }
@test "bud-with-timestamp-config-effects" {
_prefetch busybox
local timestamp=60
mkdir -p $TEST_SCRATCH_DIR/context
cat > $TEST_SCRATCH_DIR/context/Dockerfile << EOF
FROM busybox
RUN hostname | tee /hostname.txt
EOF
local config
local diff
local hostname
for cliflag in timestamp source-date-epoch ; do
run_buildah build --"$cliflag"=$timestamp --layers --no-cache -t dir:$TEST_SCRATCH_DIR/docker-layers-$cliflag --format=docker $TEST_SCRATCH_DIR/context
config=$(dir_image_config "$TEST_SCRATCH_DIR"/docker-layers-$cliflag)
run jq -r .config.Hostname "$TEST_SCRATCH_DIR"/docker-layers-$cliflag/"$config"
echo "$output"
test $status -eq 0
assert "$output" == sandbox
run jq -r .config.Domainname "$TEST_SCRATCH_DIR"/docker-layers-$cliflag/"$config"
echo "$output"
test $status -eq 0
assert "$output" == ""
run jq -r .container "$TEST_SCRATCH_DIR"/docker-layers-$cliflag/"$config"
echo "$output"
test $status -eq 0
assert "$output" == null
mkdir "$TEST_SCRATCH_DIR"/diff-docker-layers-$cliflag
diff=$(dir_image_last_diff "$TEST_SCRATCH_DIR"/docker-layers-$cliflag)
tar -C "$TEST_SCRATCH_DIR"/docker-layers-$cliflag -xf "$TEST_SCRATCH_DIR"/docker-layers-$cliflag/"$diff"
hostname=$(cat "$TEST_SCRATCH_DIR"/docker-layers-$cliflag/hostname.txt)
assert $hostname = sandbox "expected the hostname to be the static value 'sandbox'"
run_buildah build --"$cliflag"=$timestamp -t dir:"$TEST_SCRATCH_DIR"/docker-squashed-$cliflag --format=docker $TEST_SCRATCH_DIR/context
config=$(dir_image_config "$TEST_SCRATCH_DIR"/docker-squashed-$cliflag)
run jq -r .config.Hostname "$TEST_SCRATCH_DIR"/docker-squashed-$cliflag/"$config"
echo "$output"
test $status -eq 0
assert "$output" == sandbox
run jq -r .config.Domainname "$TEST_SCRATCH_DIR"/docker-squashed-$cliflag/"$config"
echo "$output"
test $status -eq 0
assert "$output" == ""
run jq -r .container "$TEST_SCRATCH_DIR"/docker-squashed-$cliflag/"$config"
echo "$output"
test $status -eq 0
assert "$output" == null
mkdir "$TEST_SCRATCH_DIR"/diff-docker-squashed-$cliflag
diff=$(dir_image_last_diff "$TEST_SCRATCH_DIR"/docker-squashed-$cliflag)
tar -C "$TEST_SCRATCH_DIR"/docker-squashed-$cliflag -xf "$TEST_SCRATCH_DIR"/docker-squashed-$cliflag/"$diff"
hostname=$(cat "$TEST_SCRATCH_DIR"/docker-squashed-$cliflag/hostname.txt)
assert $hostname = sandbox "expected the hostname to be the static value 'sandbox'"
done
}