Skip to content

Commit 2679304

Browse files
Merge pull request #25983 from matejvasek/fix-inherit-label
Fix: inheritlabels=true if query param absent
2 parents d03d994 + 4268c61 commit 2679304

File tree

3 files changed

+130
-78
lines changed

3 files changed

+130
-78
lines changed

pkg/api/handlers/compat/images_build.go

Lines changed: 78 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -95,83 +95,83 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
9595
}
9696

9797
query := struct {
98-
AddHosts string `schema:"extrahosts"`
99-
AdditionalCapabilities string `schema:"addcaps"`
100-
AdditionalBuildContexts string `schema:"additionalbuildcontexts"`
101-
AllPlatforms bool `schema:"allplatforms"`
102-
Annotations string `schema:"annotations"`
103-
AppArmor string `schema:"apparmor"`
104-
BuildArgs string `schema:"buildargs"`
105-
CacheFrom string `schema:"cachefrom"`
106-
CacheTo string `schema:"cacheto"`
107-
CacheTTL string `schema:"cachettl"`
108-
CgroupParent string `schema:"cgroupparent"`
109-
CompatVolumes bool `schema:"compatvolumes"`
110-
Compression uint64 `schema:"compression"`
111-
ConfigureNetwork string `schema:"networkmode"`
112-
CPPFlags string `schema:"cppflags"`
113-
CpuPeriod uint64 `schema:"cpuperiod"`
114-
CpuQuota int64 `schema:"cpuquota"`
115-
CpuSetCpus string `schema:"cpusetcpus"`
116-
CpuSetMems string `schema:"cpusetmems"`
117-
CpuShares uint64 `schema:"cpushares"`
118-
DNSOptions string `schema:"dnsoptions"`
119-
DNSSearch string `schema:"dnssearch"`
120-
DNSServers string `schema:"dnsservers"`
121-
Devices string `schema:"devices"`
122-
Dockerfile string `schema:"dockerfile"`
123-
DropCapabilities string `schema:"dropcaps"`
124-
Envs []string `schema:"setenv"`
125-
Excludes string `schema:"excludes"`
126-
ForceRm bool `schema:"forcerm"`
127-
From string `schema:"from"`
128-
GroupAdd []string `schema:"groupadd"`
129-
HTTPProxy bool `schema:"httpproxy"`
130-
IDMappingOptions string `schema:"idmappingoptions"`
131-
IdentityLabel bool `schema:"identitylabel"`
132-
Ignore bool `schema:"ignore"`
133-
InheritLabels bool `schema:"inheritlabels"`
134-
Isolation string `schema:"isolation"`
135-
Jobs int `schema:"jobs"`
136-
LabelOpts string `schema:"labelopts"`
137-
Labels string `schema:"labels"`
138-
LayerLabels []string `schema:"layerLabel"`
139-
Layers bool `schema:"layers"`
140-
LogRusage bool `schema:"rusage"`
141-
Manifest string `schema:"manifest"`
142-
MemSwap int64 `schema:"memswap"`
143-
Memory int64 `schema:"memory"`
144-
NamespaceOptions string `schema:"nsoptions"`
145-
NoCache bool `schema:"nocache"`
146-
NoHosts bool `schema:"nohosts"`
147-
OmitHistory bool `schema:"omithistory"`
148-
OSFeatures []string `schema:"osfeature"`
149-
OSVersion string `schema:"osversion"`
150-
OutputFormat string `schema:"outputformat"`
151-
Platform []string `schema:"platform"`
152-
Pull bool `schema:"pull"`
153-
PullPolicy string `schema:"pullpolicy"`
154-
Quiet bool `schema:"q"`
155-
Registry string `schema:"registry"`
156-
Rm bool `schema:"rm"`
157-
RusageLogFile string `schema:"rusagelogfile"`
158-
Remote string `schema:"remote"`
159-
Retry int `schema:"retry"`
160-
RetryDelay string `schema:"retry-delay"`
161-
Seccomp string `schema:"seccomp"`
162-
Secrets string `schema:"secrets"`
163-
SecurityOpt string `schema:"securityopt"`
164-
ShmSize int `schema:"shmsize"`
165-
SkipUnusedStages bool `schema:"skipunusedstages"`
166-
Squash bool `schema:"squash"`
167-
TLSVerify bool `schema:"tlsVerify"`
168-
Tags []string `schema:"t"`
169-
Target string `schema:"target"`
170-
Timestamp int64 `schema:"timestamp"`
171-
Ulimits string `schema:"ulimits"`
172-
UnsetEnvs []string `schema:"unsetenv"`
173-
UnsetLabels []string `schema:"unsetlabel"`
174-
Volumes []string `schema:"volume"`
98+
AddHosts string `schema:"extrahosts"`
99+
AdditionalCapabilities string `schema:"addcaps"`
100+
AdditionalBuildContexts string `schema:"additionalbuildcontexts"`
101+
AllPlatforms bool `schema:"allplatforms"`
102+
Annotations string `schema:"annotations"`
103+
AppArmor string `schema:"apparmor"`
104+
BuildArgs string `schema:"buildargs"`
105+
CacheFrom string `schema:"cachefrom"`
106+
CacheTo string `schema:"cacheto"`
107+
CacheTTL string `schema:"cachettl"`
108+
CgroupParent string `schema:"cgroupparent"`
109+
CompatVolumes bool `schema:"compatvolumes"`
110+
Compression uint64 `schema:"compression"`
111+
ConfigureNetwork string `schema:"networkmode"`
112+
CPPFlags string `schema:"cppflags"`
113+
CpuPeriod uint64 `schema:"cpuperiod"`
114+
CpuQuota int64 `schema:"cpuquota"`
115+
CpuSetCpus string `schema:"cpusetcpus"`
116+
CpuSetMems string `schema:"cpusetmems"`
117+
CpuShares uint64 `schema:"cpushares"`
118+
DNSOptions string `schema:"dnsoptions"`
119+
DNSSearch string `schema:"dnssearch"`
120+
DNSServers string `schema:"dnsservers"`
121+
Devices string `schema:"devices"`
122+
Dockerfile string `schema:"dockerfile"`
123+
DropCapabilities string `schema:"dropcaps"`
124+
Envs []string `schema:"setenv"`
125+
Excludes string `schema:"excludes"`
126+
ForceRm bool `schema:"forcerm"`
127+
From string `schema:"from"`
128+
GroupAdd []string `schema:"groupadd"`
129+
HTTPProxy bool `schema:"httpproxy"`
130+
IDMappingOptions string `schema:"idmappingoptions"`
131+
IdentityLabel bool `schema:"identitylabel"`
132+
Ignore bool `schema:"ignore"`
133+
InheritLabels types.OptionalBool `schema:"inheritlabels"`
134+
Isolation string `schema:"isolation"`
135+
Jobs int `schema:"jobs"`
136+
LabelOpts string `schema:"labelopts"`
137+
Labels string `schema:"labels"`
138+
LayerLabels []string `schema:"layerLabel"`
139+
Layers bool `schema:"layers"`
140+
LogRusage bool `schema:"rusage"`
141+
Manifest string `schema:"manifest"`
142+
MemSwap int64 `schema:"memswap"`
143+
Memory int64 `schema:"memory"`
144+
NamespaceOptions string `schema:"nsoptions"`
145+
NoCache bool `schema:"nocache"`
146+
NoHosts bool `schema:"nohosts"`
147+
OmitHistory bool `schema:"omithistory"`
148+
OSFeatures []string `schema:"osfeature"`
149+
OSVersion string `schema:"osversion"`
150+
OutputFormat string `schema:"outputformat"`
151+
Platform []string `schema:"platform"`
152+
Pull bool `schema:"pull"`
153+
PullPolicy string `schema:"pullpolicy"`
154+
Quiet bool `schema:"q"`
155+
Registry string `schema:"registry"`
156+
Rm bool `schema:"rm"`
157+
RusageLogFile string `schema:"rusagelogfile"`
158+
Remote string `schema:"remote"`
159+
Retry int `schema:"retry"`
160+
RetryDelay string `schema:"retry-delay"`
161+
Seccomp string `schema:"seccomp"`
162+
Secrets string `schema:"secrets"`
163+
SecurityOpt string `schema:"securityopt"`
164+
ShmSize int `schema:"shmsize"`
165+
SkipUnusedStages bool `schema:"skipunusedstages"`
166+
Squash bool `schema:"squash"`
167+
TLSVerify bool `schema:"tlsVerify"`
168+
Tags []string `schema:"t"`
169+
Target string `schema:"target"`
170+
Timestamp int64 `schema:"timestamp"`
171+
Ulimits string `schema:"ulimits"`
172+
UnsetEnvs []string `schema:"unsetenv"`
173+
UnsetLabels []string `schema:"unsetlabel"`
174+
Volumes []string `schema:"volume"`
175175
}{
176176
Dockerfile: "Dockerfile",
177177
IdentityLabel: true,
@@ -745,7 +745,7 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
745745
IDMappingOptions: &idMappingOptions,
746746
IgnoreUnrecognizedInstructions: query.Ignore,
747747
IgnoreFile: ignoreFile,
748-
InheritLabels: types.NewOptionalBool(query.InheritLabels),
748+
InheritLabels: query.InheritLabels,
749749
Isolation: isolation,
750750
Jobs: &jobs,
751751
Labels: labels,

pkg/api/handlers/decoder.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ package handlers
55
import (
66
"encoding/json"
77
"reflect"
8+
"strconv"
89
"strings"
910
"syscall"
1011
"time"
1112

13+
"github.com/containers/image/v5/types"
1214
"github.com/containers/podman/v5/libpod/define"
1315
"github.com/containers/podman/v5/pkg/util"
1416
"github.com/gorilla/schema"
@@ -28,6 +30,9 @@ func NewAPIDecoder() *schema.Decoder {
2830

2931
var Signal syscall.Signal
3032
d.RegisterConverter(Signal, convertSignal)
33+
34+
d.RegisterConverter(types.OptionalBoolUndefined, convertOptionalBool)
35+
3136
return d
3237
}
3338

@@ -39,6 +44,16 @@ func NewCompatAPIDecoder() *schema.Decoder {
3944
s = strings.ToLower(strings.TrimSpace(s))
4045
return reflect.ValueOf(s != "" && s != "0" && s != "no" && s != "false" && s != "none")
4146
})
47+
dec.RegisterConverter(types.OptionalBoolUndefined, func(s string) reflect.Value {
48+
if len(s) == 0 {
49+
return reflect.ValueOf(types.OptionalBoolUndefined)
50+
}
51+
s = strings.ToLower(strings.TrimSpace(s))
52+
if s != "0" && s != "no" && s != "false" && s != "none" {
53+
return reflect.ValueOf(types.OptionalBoolTrue)
54+
}
55+
return reflect.ValueOf(types.OptionalBoolFalse)
56+
})
4257

4358
return dec
4459
}
@@ -143,3 +158,14 @@ func convertSignal(query string) reflect.Value {
143158
}
144159
return reflect.ValueOf(signal)
145160
}
161+
162+
func convertOptionalBool(s string) reflect.Value {
163+
if len(s) == 0 {
164+
return reflect.ValueOf(types.OptionalBoolUndefined)
165+
}
166+
val, _ := strconv.ParseBool(s)
167+
if val {
168+
return reflect.ValueOf(types.OptionalBoolTrue)
169+
}
170+
return reflect.ValueOf(types.OptionalBoolFalse)
171+
}

test/apiv2/90-build.at

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -*- sh -*-
2+
#
3+
# Tests for build-related endpoints
4+
#
5+
6+
# test if default compat build contains labels from base image
7+
TMPD=$(mktemp -d podman-apiv2-test.build.XXXXXXXX)
8+
function cleanBuildTest() {
9+
podman rmi -a -f
10+
rm -rf "${TMPD}" &> /dev/null
11+
}
12+
CONTAINERFILE_TAR="${TMPD}/containerfile.tar"
13+
cat > $TMPD/containerfile << EOF
14+
FROM $IMAGE
15+
RUN echo hello
16+
EOF
17+
tar --format=posix -C $TMPD -cvf ${CONTAINERFILE_TAR} containerfile &> /dev/null
18+
19+
t POST "/build?dockerfile=containerfile&t=labeltest" $CONTAINERFILE_TAR 200 \
20+
'.aux|select(has("ID")).ID~^sha256:[0-9a-f]\{64\}$'
21+
22+
t GET images/labeltest/json 200 \
23+
.Config.Labels.created_by="test/system/build-testimage"
24+
cleanBuildTest
25+
26+
# vim: filetype=sh

0 commit comments

Comments
 (0)