Skip to content

Commit 57def96

Browse files
Merge pull request #2491 from 2004joshua/loglocation
add log_path option to container configuration
2 parents c0d717e + bfc2d91 commit 57def96

File tree

8 files changed

+87
-0
lines changed

8 files changed

+87
-0
lines changed

docs/containers.conf.5.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,14 @@ the user system_u, and the role system_r.
294294

295295
Logging driver for the container. Currently available options are k8s-file, journald, none and passthrough, with json-file aliased to k8s-file for scripting compatibility. The journald driver is used by default if the systemd journal is readable and writable. Otherwise, the k8s-file driver is used.
296296

297+
**log_path**=""
298+
299+
Default path for container logs to be stored in. When empty, logs will be stored
300+
in the container's default storage and removed when the container is removed.
301+
A subdirectory named with the container ID will be created under the specified
302+
path, and the log file will have the default name `ctr.log` within that directory.
303+
This option can be overridden by the `--log-opt` flag.
304+
297305
**log_size_max**=-1
298306

299307
Maximum size allowed for the container's log file. Negative numbers indicate

pkg/config/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ type ContainersConfig struct {
172172
// LogDriver for the container. For example: k8s-file and journald
173173
LogDriver string `toml:"log_driver,omitempty"`
174174

175+
// LogPath is the path to the container log file.
176+
LogPath string `toml:"log_path,omitempty"`
177+
175178
// LogSizeMax is the maximum number of bytes after which the log file
176179
// will be truncated. It can be expressed as a human-friendly string
177180
// that is parsed to bytes.
@@ -882,6 +885,10 @@ func (c *ContainersConfig) Validate() error {
882885
return err
883886
}
884887

888+
if err := c.validateLogPath(); err != nil {
889+
return err
890+
}
891+
885892
if c.LogSizeMax >= 0 && c.LogSizeMax < OCIBufSize {
886893
return fmt.Errorf("log size max should be negative or >= %d", OCIBufSize)
887894
}

pkg/config/config_local.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,20 @@ func (c *ContainersConfig) validateUmask() error {
105105
return nil
106106
}
107107

108+
func (c *ContainersConfig) validateLogPath() error {
109+
if c.LogPath == "" {
110+
return nil
111+
}
112+
if !filepath.IsAbs(c.LogPath) {
113+
return fmt.Errorf("log_path must be an absolute path - instead got %q", c.LogPath)
114+
}
115+
if strings.ContainsAny(c.LogPath, "\x00") {
116+
return fmt.Errorf("log_path contains null bytes - got %q", c.LogPath)
117+
}
118+
119+
return nil
120+
}
121+
108122
func isRemote() bool {
109123
return false
110124
}

pkg/config/config_local_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,4 +588,40 @@ var _ = Describe("Config Local", func() {
588588
gomega.Expect(err).ToNot(gomega.HaveOccurred())
589589
gomega.Expect(config2.Machine.Rosetta).To(gomega.BeFalse())
590590
})
591+
It("should validate log_path correctly", func() {
592+
defConf, err := defaultConfig()
593+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
594+
gomega.Expect(defConf).NotTo(gomega.BeNil())
595+
596+
// Test empty path
597+
defConf.Containers.LogPath = ""
598+
err = defConf.Containers.Validate()
599+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
600+
601+
// Test absolute path
602+
defConf.Containers.LogPath = "/var/log/containers"
603+
err = defConf.Containers.Validate()
604+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
605+
606+
// Should fail on relative path
607+
defConf.Containers.LogPath = "./logs"
608+
err = defConf.Containers.Validate()
609+
gomega.Expect(err).To(gomega.HaveOccurred())
610+
gomega.Expect(err.Error()).To(gomega.ContainSubstring("absolute path"))
611+
612+
// Test path with null byte
613+
defConf.Containers.LogPath = "/var/log\x00/containers"
614+
err = defConf.Containers.Validate()
615+
gomega.Expect(err).To(gomega.HaveOccurred())
616+
gomega.Expect(err.Error()).To(gomega.ContainSubstring("null bytes"))
617+
})
618+
It("should parse log_path from config file", func() {
619+
config, err := New(nil)
620+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
621+
gomega.Expect(config.Containers.LogPath).To(gomega.Equal(""))
622+
623+
config2, err := NewConfig("testdata/containers_default.conf")
624+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
625+
gomega.Expect(config2.Containers.LogPath).To(gomega.Equal("/var/log/containers"))
626+
})
591627
})

pkg/config/config_remote.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ func (c *ContainersConfig) validateTZ() error {
3535
func (c *ContainersConfig) validateUmask() error {
3636
return nil
3737
}
38+
39+
func (c *ContainersConfig) validateLogPath() error {
40+
return nil
41+
}

pkg/config/containers.conf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,14 @@ default_sysctls = [
216216
#
217217
#log_driver = "k8s-file"
218218

219+
# Default path for container logs to be stored in. When empty, logs will be stored
220+
# in the container's default storage and removed when the container is removed.
221+
# A subdirectory named with the container ID will be created under the specified
222+
# path, and the log file will have the default name `ctr.log` within that directory.
223+
# This option can be overridden by the `--log-opt` flag.
224+
#
225+
#log_path = ""
226+
219227
# Maximum size allowed for the container log file. Negative numbers indicate
220228
# that no size limit is imposed. If positive, it must be >= 8192 to match or
221229
# exceed conmon's read buffer. The file is truncated and re-opened so the

pkg/config/containers.conf-freebsd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ default_sysctls = [
169169
#
170170
#log_driver = "k8s-file"
171171

172+
# Default path for container logs to be stored in. When empty, logs will be stored
173+
# in the container's default storage and removed when the container is removed.
174+
# A subdirectory named with the container ID will be created under the specified
175+
# path, and the log file will have the default name `ctr.log` within that directory.
176+
# This option can be overridden by the `--log-opt` flag.
177+
#
178+
#log_path = ""
179+
172180
# Maximum size allowed for the container log file. Negative numbers indicate
173181
# that no size limit is imposed. If positive, it must be >= 8192 to match or
174182
# exceed conmon's read buffer. The file is truncated and re-opened so the

pkg/config/testdata/containers_default.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ label = true
8585
# limit is never exceeded.
8686
log_size_max = -1
8787

88+
log_path = "/var/log/containers"
89+
8890
mounts= [
8991
"type=glob,source=/tmp/test2*,ro=true",
9092
"type=bind,source=/etc/services,destination=/etc/services,ro",

0 commit comments

Comments
 (0)