Skip to content

Commit f2dff41

Browse files
committed
Support log_tag defaults from containers.conf
Fixes: containers#10204 Signed-off-by: Daniel J Walsh <[email protected]>
1 parent 510509b commit f2dff41

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

pkg/specgen/generate/container.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import (
77

88
"github.com/containers/common/libimage"
99
"github.com/containers/podman/v3/libpod"
10+
"github.com/containers/podman/v3/libpod/define"
1011
ann "github.com/containers/podman/v3/pkg/annotations"
1112
envLib "github.com/containers/podman/v3/pkg/env"
1213
"github.com/containers/podman/v3/pkg/signal"
1314
"github.com/containers/podman/v3/pkg/specgen"
1415
spec "github.com/opencontainers/runtime-spec/specs-go"
1516
"github.com/pkg/errors"
17+
"github.com/sirupsen/logrus"
1618
"golang.org/x/sys/unix"
1719
)
1820

@@ -203,6 +205,17 @@ func CompleteSpec(ctx context.Context, r *libpod.Runtime, s *specgen.SpecGenerat
203205
if len(s.LogConfiguration.Driver) < 1 {
204206
s.LogConfiguration.Driver = rtc.Containers.LogDriver
205207
}
208+
if len(rtc.Containers.LogTag) > 0 {
209+
if s.LogConfiguration.Driver != define.JSONLogging {
210+
if s.LogConfiguration.Options == nil {
211+
s.LogConfiguration.Options = make(map[string]string)
212+
}
213+
214+
s.LogConfiguration.Options["tag"] = rtc.Containers.LogTag
215+
} else {
216+
logrus.Warnf("log_tag %q is not allowed with %q log_driver", rtc.Containers.LogTag, define.JSONLogging)
217+
}
218+
}
206219

207220
warnings, err := verifyContainerResources(s)
208221
if err != nil {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[containers]
2+
3+
log_driver="journald"
4+
log_tag="{{.ImageName}}"

test/e2e/containers_conf_test.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,34 @@ var _ = Describe("Podman run", func() {
167167
verifyNSHandling("/proc/self/ns/cgroup", "--cgroupns")
168168
})
169169

170+
It("using journald for container with container log_tag", func() {
171+
SkipIfInContainer("journalctl inside a container doesn't work correctly")
172+
os.Setenv("CONTAINERS_CONF", "config/containers-journald.conf")
173+
if IsRemote() {
174+
podmanTest.RestartRemoteService()
175+
}
176+
logc := podmanTest.Podman([]string{"run", "-d", ALPINE, "sh", "-c", "echo podman; sleep 0.1; echo podman; sleep 0.1; echo podman"})
177+
logc.WaitWithDefaultTimeout()
178+
Expect(logc.ExitCode()).To(Equal(0))
179+
cid := logc.OutputToString()
180+
181+
wait := podmanTest.Podman([]string{"wait", cid})
182+
wait.WaitWithDefaultTimeout()
183+
Expect(wait.ExitCode()).To(Equal(0))
184+
185+
cmd := exec.Command("journalctl", "--no-pager", "-o", "json", "--output-fields=CONTAINER_TAG", fmt.Sprintf("CONTAINER_ID_FULL=%s", cid))
186+
out, err := cmd.CombinedOutput()
187+
Expect(err).To(BeNil())
188+
Expect(string(out)).To(ContainSubstring("alpine"))
189+
})
190+
170191
It("podman containers.conf additionalvolumes", func() {
171192
conffile := filepath.Join(podmanTest.TempDir, "container.conf")
172193
tempdir, err = CreateTempDirInTempDir()
173-
if err != nil {
174-
os.Exit(1)
175-
}
194+
Expect(err).To(BeNil())
195+
176196
err := ioutil.WriteFile(conffile, []byte(fmt.Sprintf("[containers]\nvolumes=[\"%s:%s:Z\",]\n", tempdir, tempdir)), 0755)
177-
if err != nil {
178-
os.Exit(1)
179-
}
197+
Expect(err).To(BeNil())
180198

181199
os.Setenv("CONTAINERS_CONF", conffile)
182200
if IsRemote() {

0 commit comments

Comments
 (0)