Skip to content

Commit 2df09fd

Browse files
committed
build: add support for inherit-labels
Allows users to specify if they want to inherit labels from base image or not. Signed-off-by: flouthoc <[email protected]>
1 parent 1051965 commit 2df09fd

File tree

8 files changed

+73
-4
lines changed

8 files changed

+73
-4
lines changed

define/build.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ type BuildOptions struct {
236236
// ID mapping options to use if we're setting up our own user namespace
237237
// when handling RUN instructions.
238238
IDMappingOptions *IDMappingOptions
239+
// InheritLabels allows users to specify if they want
240+
// to inherit labels from base image or not.
241+
InheritLabels types.OptionalBool
239242
// AddCapabilities is a list of capabilities to add to the default set when
240243
// handling RUN instructions.
241244
AddCapabilities []string

docs/buildah-build.1.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,10 @@ Path to an alternative .containerignore (.dockerignore) file.
497497
Write the built image's ID to the file. When `--platform` is specified more
498498
than once, attempting to use this option will trigger an error.
499499

500+
**--inherit-labels** *bool-value*
501+
502+
Inherit the labels from the base image (default true).
503+
500504
**--ipc** *how*
501505

502506
Sets the configuration for IPC namespaces when handling `RUN` instructions.

imagebuildah/executor.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ type Executor struct {
8282
additionalTags []string
8383
log func(format string, args ...interface{}) // can be nil
8484
in io.Reader
85+
inheritLabels types.OptionalBool
8586
out io.Writer
8687
err io.Writer
8788
signaturePolicyPath string
@@ -261,6 +262,7 @@ func newExecutor(logger *logrus.Logger, logPrefix string, store storage.Store, o
261262
err: options.Err,
262263
reportWriter: writer,
263264
isolation: options.Isolation,
265+
inheritLabels: options.InheritLabels,
264266
namespaceOptions: options.NamespaceOptions,
265267
configureNetwork: options.ConfigureNetwork,
266268
cniPluginPath: options.CNIPluginPath,

imagebuildah/stage_executor.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,11 @@ func (s *StageExecutor) prepare(ctx context.Context, from string, initializeIBCo
10781078
RootFS: rootfs,
10791079
}
10801080
dImage.Config = &dImage.ContainerConfig
1081+
if s.executor.inheritLabels == types.OptionalBoolFalse {
1082+
// If user has selected `--inherit-labels=false` lets not
1083+
// inherit labels from base image.
1084+
dImage.Config.Labels = nil
1085+
}
10811086
err = ib.FromImage(&dImage, node)
10821087
if err != nil {
10831088
if err2 := builder.Delete(); err2 != nil {
@@ -1881,6 +1886,11 @@ func (s *StageExecutor) getCreatedBy(node *parser.Node, addedContentSummary stri
18811886
if node == nil {
18821887
return "/bin/sh", nil
18831888
}
1889+
inheritLabels := ""
1890+
// If --inherit-label was manually set to false then update history.
1891+
if s.executor.inheritLabels == types.OptionalBoolFalse {
1892+
inheritLabels = "|inheritLabels=false"
1893+
}
18841894
switch strings.ToUpper(node.Value) {
18851895
case "ARG":
18861896
for _, variable := range strings.Fields(node.Original) {
@@ -1889,7 +1899,7 @@ func (s *StageExecutor) getCreatedBy(node *parser.Node, addedContentSummary stri
18891899
}
18901900
}
18911901
buildArgs := s.getBuildArgsKey()
1892-
return "/bin/sh -c #(nop) ARG " + buildArgs, nil
1902+
return "/bin/sh -c #(nop) ARG " + buildArgs + inheritLabels, nil
18931903
case "RUN":
18941904
shArg := ""
18951905
buildArgs := s.getBuildArgsResolvedForRun()
@@ -1965,16 +1975,16 @@ func (s *StageExecutor) getCreatedBy(node *parser.Node, addedContentSummary stri
19651975
if buildArgs != "" {
19661976
result = result + "|" + strconv.Itoa(len(strings.Split(buildArgs, " "))) + " " + buildArgs + " "
19671977
}
1968-
result = result + "/bin/sh -c " + shArg + heredoc + appendCheckSum
1978+
result = result + "/bin/sh -c " + shArg + heredoc + appendCheckSum + inheritLabels
19691979
return result, nil
19701980
case "ADD", "COPY":
19711981
destination := node
19721982
for destination.Next != nil {
19731983
destination = destination.Next
19741984
}
1975-
return "/bin/sh -c #(nop) " + strings.ToUpper(node.Value) + " " + addedContentSummary + " in " + destination.Value + " ", nil
1985+
return "/bin/sh -c #(nop) " + strings.ToUpper(node.Value) + " " + addedContentSummary + " in " + destination.Value + " " + inheritLabels, nil
19761986
default:
1977-
return "/bin/sh -c #(nop) " + node.Original, nil
1987+
return "/bin/sh -c #(nop) " + node.Original + inheritLabels, nil
19781988
}
19791989
}
19801990

pkg/cli/build.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ func GenBuildOptions(c *cobra.Command, inputArgs []string, iopts BuildOptions) (
378378
IIDFile: iopts.Iidfile,
379379
IgnoreFile: iopts.IgnoreFile,
380380
In: stdin,
381+
InheritLabels: types.NewOptionalBool(iopts.InheritLabels),
381382
Isolation: isolation,
382383
Jobs: &iopts.Jobs,
383384
Labels: iopts.Label,

pkg/cli/common.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ type BudResults struct {
7171
Format string
7272
From string
7373
Iidfile string
74+
InheritLabels bool
7475
Label []string
7576
LayerLabel []string
7677
Logfile string
@@ -230,6 +231,7 @@ func GetBudFlags(flags *BudResults) pflag.FlagSet {
230231
fs.StringVar(&flags.CertDir, "cert-dir", "", "use certificates at the specified path to access the registry")
231232
fs.BoolVar(&flags.Compress, "compress", false, "this is a legacy option, which has no effect on the image")
232233
fs.BoolVar(&flags.CompatVolumes, "compat-volumes", false, "preserve the contents of VOLUMEs during RUN instructions")
234+
fs.BoolVar(&flags.InheritLabels, "inherit-labels", true, "inherit the labels from the base image")
233235
fs.StringArrayVar(&flags.CPPFlags, "cpp-flag", []string{}, "set additional flag to pass to C preprocessor (cpp)")
234236
fs.StringVar(&flags.Creds, "creds", "", "use `[username[:password]]` for accessing the registry")
235237
fs.StringVarP(&flags.CWOptions, "cw", "", "", "confidential workload `options`")

tests/bud.bats

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2670,6 +2670,49 @@ _EOF
26702670
expect_output "$want_output"
26712671
}
26722672

2673+
@test "bud and test inherit-labels" {
2674+
run_buildah --version
2675+
local -a output_fields=($output)
2676+
buildah_version=${output_fields[2]}
2677+
run_buildah build $WITH_POLICY_JSON -t exp -f $BUDFILES/base-with-labels/Containerfile
2678+
2679+
run_buildah inspect --format '{{ index .Docker.Config.Labels "license"}}' exp
2680+
expect_output "MIT" "license must be MIT from fedora base image"
2681+
run_buildah inspect --format '{{ index .Docker.Config.Labels "name"}}' exp
2682+
expect_output "fedora-minimal" "name must be fedora from base image"
2683+
2684+
run_buildah build $WITH_POLICY_JSON --inherit-labels=false --label hello=world -t exp -f $BUDFILES/base-with-labels/Containerfile
2685+
# no labels should be inherited from base image only the, buildah version label
2686+
# and `hello=world` which we just added using cli flag
2687+
want_output='map["hello":"world" "io.buildah.version":"'$buildah_version'"]'
2688+
run_buildah inspect --format '{{printf "%q" .Docker.Config.Labels}}' exp
2689+
expect_output "$want_output"
2690+
2691+
# Try building another file with multiple layers
2692+
run_buildah build $WITH_POLICY_JSON --layers -t exp -f $BUDFILES/base-with-labels/Containerfile.layer
2693+
run_buildah inspect --format '{{ index .Docker.Config.Labels "license"}}' exp
2694+
expect_output "MIT" "license must be MIT from fedora base image"
2695+
run_buildah inspect --format '{{ index .Docker.Config.Labels "name"}}' exp
2696+
expect_output "world" "name must be world from Containerfile"
2697+
2698+
# Now build same file with --inherit-labels=false and verify if we are not using the cache again.
2699+
run_buildah build $WITH_POLICY_JSON --layers --inherit-labels=false -t exp -f $BUDFILES/base-with-labels/Containerfile.layer
2700+
# Should not contain `Using cache` at all since
2701+
assert "$output" !~ "Using cache"
2702+
want_output='map["io.buildah.version":"'$buildah_version'" "name":"world"]'
2703+
run_buildah inspect --format '{{printf "%q" .Docker.Config.Labels}}' exp
2704+
expect_output "$want_output"
2705+
2706+
# Now build same file with --inherit-labels=true and verify if using the cache
2707+
run_buildah build $WITH_POLICY_JSON --layers --inherit-labels=true -t exp -f $BUDFILES/base-with-labels/Containerfile.layer
2708+
# Should contain `Using cache` at all since
2709+
expect_output --substring " Using cache"
2710+
run_buildah inspect --format '{{ index .Docker.Config.Labels "license"}}' exp
2711+
expect_output "MIT" "license must be MIT from fedora base image"
2712+
run_buildah inspect --format '{{ index .Docker.Config.Labels "name"}}' exp
2713+
expect_output "world" "name must be world from Containerfile"
2714+
}
2715+
26732716
@test "build using intermediate images should not inherit label" {
26742717
_prefetch alpine
26752718

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM registry.fedoraproject.org/fedora-minimal
2+
LABEL name world
3+
RUN echo world
4+
RUN echo hello

0 commit comments

Comments
 (0)