From a3b67a7ca51788677b8e202bda427f25df4cc453 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Sat, 7 May 2022 07:18:26 -0400 Subject: [PATCH] Verify logdriver and eventslogger will work together Fixes:https://github.com/containers/podman/issues/12175 Signed-off-by: Daniel J Walsh --- pkg/config/config.go | 31 ++++++++++++++++++++ pkg/config/config_test.go | 27 ++++++++++++++++- pkg/config/testdata/containers_override.conf | 2 +- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 25572968f..6254c6846 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -13,6 +13,7 @@ import ( "github.com/BurntSushi/toml" "github.com/containers/common/libnetwork/types" "github.com/containers/common/pkg/capabilities" + "github.com/containers/common/pkg/util" "github.com/containers/storage/pkg/unshare" units "github.com/docker/go-units" selinux "github.com/opencontainers/selinux/go-selinux" @@ -639,6 +640,10 @@ func NewConfig(userConfigPath string) (*Config, error) { return nil, err } + if err := LoggingValid(config.Containers.LogDriver, config.Engine.EventsLogger); err != nil { + return nil, err + } + return config, nil } @@ -1305,3 +1310,29 @@ func (e eventsLogMaxSize) MarshalText() ([]byte, error) { } return []byte(fmt.Sprintf("%d", e)), nil } + +func LoggingValid(logDriver string, eventsLogger string) error { + + validLogDriver := []string{"file", "k8s-file", "journald", "none"} + validEventsLogger := []string{"file", "journald", "none"} + if !util.StringInSlice(logDriver, validLogDriver) { + return errors.Errorf("invalid LogDriver %q must be one of %q", logDriver, strings.Join(validLogDriver, ", ")) + } + if !util.StringInSlice(eventsLogger, validEventsLogger) { + return errors.Errorf("invalid EventsLogger %q must be one of %q", eventsLogger, strings.Join(validEventsLogger, ", ")) + } + logErr := errors.Errorf("invalid logging combination LogDriver %q is not allowed with EventsLogger %q", logDriver, eventsLogger) + switch logDriver { + case "none": + break + case "file", "k8s-file": + if eventsLogger == "journald" { + return logErr + } + case "journald": + if eventsLogger == "file" { + return logErr + } + } + return nil +} diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 2eb49ea9a..ca1c84e1a 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -396,7 +396,7 @@ image_copy_tmp_dir="storage"` gomega.Expect(err).To(gomega.BeNil()) gomega.Expect(config).ToNot(gomega.BeNil()) gomega.Expect(config.Containers.ApparmorProfile).To(gomega.Equal("overridden-default")) - gomega.Expect(config.Containers.LogDriver).To(gomega.Equal("journald")) + gomega.Expect(config.Containers.LogDriver).To(gomega.Equal("file")) gomega.Expect(config.Containers.LogTag).To(gomega.Equal("{{.Name}}|{{.ID}}")) gomega.Expect(config.Containers.LogSizeMax).To(gomega.Equal(int64(100000))) gomega.Expect(config.Engine.ImageParallelCopies).To(gomega.Equal(uint(10))) @@ -812,4 +812,29 @@ env=["foo=bar"]` gomega.Expect(string(b)).To(gomega. Equal("[containers]\n\n[engine]\n\n[machine]\n\n[network]\n\n[secrets]\n\n[configmaps]\n")) }) + + It("test LoggingValid", func() { + err := LoggingValid("bogus", "file") + gomega.Expect(err).To(gomega.HaveOccurred()) + gomega.Expect(err.Error()).To(gomega.Equal("invalid LogDriver \"bogus\" must be one of \"file, k8s-file, journald, none\"")) + err = LoggingValid("file", "bogus") + gomega.Expect(err).To(gomega.HaveOccurred()) + gomega.Expect(err.Error()).To(gomega.Equal("invalid EventsLogger \"bogus\" must be one of \"file, journald, none\"")) + + for _, logDriver := range []string{"file", "k8s-file", "journald"} { + for _, eventsLogger := range []string{"file", "journald", "none"} { + err := LoggingValid(logDriver, eventsLogger) + switch { + case eventsLogger == "file" && logDriver == "k8s-file": + fallthrough + case eventsLogger == "none", logDriver == "none": + fallthrough + case eventsLogger == logDriver: + gomega.Expect(err).ToNot(gomega.HaveOccurred()) + default: + gomega.Expect(err).To(gomega.HaveOccurred()) + } + } + } + }) }) diff --git a/pkg/config/testdata/containers_override.conf b/pkg/config/testdata/containers_override.conf index b04eef5a2..834694114 100644 --- a/pkg/config/testdata/containers_override.conf +++ b/pkg/config/testdata/containers_override.conf @@ -1,7 +1,7 @@ [containers] apparmor_profile = "overridden-default" -log_driver = "journald" +log_driver = "file" log_tag="{{.Name}}|{{.ID}}" log_size_max = 100000