From acd8b4900058c364a660a4a01319a8d9cd097896 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Mon, 25 Oct 2021 08:07:10 -0400 Subject: Add support to play kube for --log-opt Fixes: https://github.com/containers/podman/issues/11727 Signed-off-by: Daniel J Walsh --- cmd/podman/play/kube.go | 8 ++++++++ docs/source/markdown/podman-create.1.md | 6 ------ docs/source/markdown/podman-play-kube.1.md | 17 +++++++++++++++++ pkg/api/handlers/libpod/play.go | 2 ++ pkg/bindings/play/types.go | 2 ++ pkg/bindings/play/types_kube_options.go | 15 +++++++++++++++ pkg/domain/entities/play.go | 2 ++ pkg/domain/infra/abi/play.go | 2 ++ pkg/domain/infra/tunnel/play.go | 3 +++ pkg/specgen/generate/kube/kube.go | 24 ++++++++++++++++++++++++ test/e2e/play_kube_test.go | 9 +++++++-- 11 files changed, 82 insertions(+), 8 deletions(-) diff --git a/cmd/podman/play/kube.go b/cmd/podman/play/kube.go index e6869efd3..581b29113 100644 --- a/cmd/podman/play/kube.go +++ b/cmd/podman/play/kube.go @@ -80,6 +80,14 @@ func init() { flags.StringVar(&kubeOptions.LogDriver, logDriverFlagName, "", "Logging driver for the container") _ = kubeCmd.RegisterFlagCompletionFunc(logDriverFlagName, common.AutocompleteLogDriver) + logOptFlagName := "log-opt" + flags.StringSliceVar( + &kubeOptions.LogOptions, + logOptFlagName, []string{}, + "Logging driver options", + ) + _ = kubeCmd.RegisterFlagCompletionFunc(logOptFlagName, common.AutocompleteLogOpt) + flags.BoolVar(&kubeOptions.NoHosts, "no-hosts", false, "Do not create /etc/hosts within the pod's containers, instead use the version from the image") flags.BoolVarP(&kubeOptions.Quiet, "quiet", "q", false, "Suppress output information when pulling images") flags.BoolVar(&kubeOptions.TLSVerifyCLI, "tls-verify", true, "Require HTTPS and verify certificates when contacting registries") diff --git a/docs/source/markdown/podman-create.1.md b/docs/source/markdown/podman-create.1.md index 6e2ab1fc5..ea9042861 100644 --- a/docs/source/markdown/podman-create.1.md +++ b/docs/source/markdown/podman-create.1.md @@ -529,12 +529,6 @@ It supports the same keys as **podman inspect --format**. This option is currently supported only by the **journald** log driver. -`--log-opt tag="{{.ImageName}}"` - -It supports the same keys as `podman inspect --format`. - -It is currently supported only by the journald log driver. - #### **--mac-address**=*address* Container MAC address (e.g. 92:d0:c6:0a:29:33) diff --git a/docs/source/markdown/podman-play-kube.1.md b/docs/source/markdown/podman-play-kube.1.md index d4770a538..4b825ef95 100644 --- a/docs/source/markdown/podman-play-kube.1.md +++ b/docs/source/markdown/podman-play-kube.1.md @@ -112,6 +112,23 @@ Assign a static ip address to the pod. This option can be specified several time Set logging driver for all created containers. +#### **--log-opt**=*name*=*value* + +Set custom logging configuration. The following *name*s are supported: + +- **path**: specify a path to the log file +(e.g. **--log-opt path=/var/log/container/mycontainer.json**); + +- **max-size**: specify a max size of the log file +(e.g. **--log-opt max-size=10mb**); + +- **tag**: specify a custom log tag for the container +(e.g. **--log-opt tag="{{.ImageName}}"**. + +It supports the same keys as **podman inspect --format**. + +This option is currently supported only by the **journald** log driver. + #### **--mac-address**=*MAC address* Assign a static mac address to the pod. This option can be specified several times when play kube creates more than one pod. diff --git a/pkg/api/handlers/libpod/play.go b/pkg/api/handlers/libpod/play.go index 851e0f6c8..f943fc240 100644 --- a/pkg/api/handlers/libpod/play.go +++ b/pkg/api/handlers/libpod/play.go @@ -26,6 +26,7 @@ func PlayKube(w http.ResponseWriter, r *http.Request) { Network string `schema:"network"` TLSVerify bool `schema:"tlsVerify"` LogDriver string `schema:"logDriver"` + LogOptions []string `schema:"logOptions"` Start bool `schema:"start"` StaticIPs []string `schema:"staticIPs"` StaticMACs []string `schema:"staticMACs"` @@ -106,6 +107,7 @@ func PlayKube(w http.ResponseWriter, r *http.Request) { NoHosts: query.NoHosts, Quiet: true, LogDriver: query.LogDriver, + LogOptions: query.LogOptions, StaticIPs: staticIPs, StaticMACs: staticMACs, } diff --git a/pkg/bindings/play/types.go b/pkg/bindings/play/types.go index fdfc4a6fa..011f7f9ca 100644 --- a/pkg/bindings/play/types.go +++ b/pkg/bindings/play/types.go @@ -37,6 +37,8 @@ type KubeOptions struct { ConfigMaps *[]string // LogDriver for the container. For example: journald LogDriver *string + // LogOptions for the container. For example: journald + LogOptions *[]string // Start - don't start the pod if false Start *bool } diff --git a/pkg/bindings/play/types_kube_options.go b/pkg/bindings/play/types_kube_options.go index 1a6324302..344771e0c 100644 --- a/pkg/bindings/play/types_kube_options.go +++ b/pkg/bindings/play/types_kube_options.go @@ -228,6 +228,21 @@ func (o *KubeOptions) GetLogDriver() string { return *o.LogDriver } +// WithLogOptions set field LogOptions to given value +func (o *KubeOptions) WithLogOptions(value []string) *KubeOptions { + o.LogOptions = &value + return o +} + +// GetLogOptions returns value of field LogOptions +func (o *KubeOptions) GetLogOptions() []string { + if o.LogOptions == nil { + var z []string + return z + } + return *o.LogOptions +} + // WithStart set field Start to given value func (o *KubeOptions) WithStart(value bool) *KubeOptions { o.Start = &value diff --git a/pkg/domain/entities/play.go b/pkg/domain/entities/play.go index 715d8acaf..ad35dfe25 100644 --- a/pkg/domain/entities/play.go +++ b/pkg/domain/entities/play.go @@ -46,6 +46,8 @@ type PlayKubeOptions struct { ConfigMaps []string // LogDriver for the container. For example: journald LogDriver string + // LogOptions for the log driver for the container. + LogOptions []string // Start - don't start the pod if false Start types.OptionalBool } diff --git a/pkg/domain/infra/abi/play.go b/pkg/domain/infra/abi/play.go index 751d6cc05..4d21751d1 100644 --- a/pkg/domain/infra/abi/play.go +++ b/pkg/domain/infra/abi/play.go @@ -333,6 +333,7 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY NetNSIsHost: p.NetNS.IsHost(), SecretsManager: secretsManager, LogDriver: options.LogDriver, + LogOptions: options.LogOptions, Labels: labels, InitContainerType: define.AlwaysInitContainer, } @@ -371,6 +372,7 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY NetNSIsHost: p.NetNS.IsHost(), SecretsManager: secretsManager, LogDriver: options.LogDriver, + LogOptions: options.LogOptions, Labels: labels, } specGen, err := kube.ToSpecGen(ctx, &specgenOpts) diff --git a/pkg/domain/infra/tunnel/play.go b/pkg/domain/infra/tunnel/play.go index 0b1c3d2ca..75952ce2c 100644 --- a/pkg/domain/infra/tunnel/play.go +++ b/pkg/domain/infra/tunnel/play.go @@ -13,6 +13,9 @@ func (ic *ContainerEngine) PlayKube(ctx context.Context, path string, opts entit options.WithCertDir(opts.CertDir).WithQuiet(opts.Quiet).WithSignaturePolicy(opts.SignaturePolicy).WithConfigMaps(opts.ConfigMaps) options.WithLogDriver(opts.LogDriver).WithNetwork(opts.Network).WithSeccompProfileRoot(opts.SeccompProfileRoot) options.WithStaticIPs(opts.StaticIPs).WithStaticMACs(opts.StaticMACs) + if len(opts.LogOptions) > 0 { + options.WithLogOptions(opts.LogOptions) + } options.WithNoHosts(opts.NoHosts) if s := opts.SkipTLSVerify; s != types.OptionalBoolUndefined { options.WithSkipTLSVerify(s == types.OptionalBoolTrue) diff --git a/pkg/specgen/generate/kube/kube.go b/pkg/specgen/generate/kube/kube.go index 6eebc6376..c502a6e62 100644 --- a/pkg/specgen/generate/kube/kube.go +++ b/pkg/specgen/generate/kube/kube.go @@ -19,6 +19,7 @@ import ( "github.com/containers/podman/v3/pkg/specgen" "github.com/containers/podman/v3/pkg/specgen/generate" "github.com/containers/podman/v3/pkg/util" + "github.com/docker/go-units" spec "github.com/opencontainers/runtime-spec/specs-go" "github.com/pkg/errors" v1 "k8s.io/api/core/v1" @@ -116,6 +117,8 @@ type CtrSpecGenOptions struct { SecretsManager *secrets.SecretsManager // LogDriver which should be used for the container LogDriver string + // LogOptions log options which should be used for the container + LogOptions []string // Labels define key-value pairs of metadata Labels map[string]string // @@ -144,6 +147,27 @@ func ToSpecGen(ctx context.Context, opts *CtrSpecGenOptions) (*specgen.SpecGener Driver: opts.LogDriver, } + for _, o := range opts.LogOptions { + split := strings.SplitN(o, "=", 2) + if len(split) < 2 { + return nil, errors.Errorf("invalid log option %q", o) + } + switch strings.ToLower(split[0]) { + case "driver": + s.LogConfiguration.Driver = split[1] + case "path": + s.LogConfiguration.Path = split[1] + case "max-size": + logSize, err := units.FromHumanSize(split[1]) + if err != nil { + return nil, err + } + s.LogConfiguration.Size = logSize + default: + s.LogConfiguration.Options[split[0]] = split[1] + } + } + s.InitContainerType = opts.InitContainerType setupSecurityContext(s, opts.Container) diff --git a/test/e2e/play_kube_test.go b/test/e2e/play_kube_test.go index 8aeba9d75..d5b913743 100644 --- a/test/e2e/play_kube_test.go +++ b/test/e2e/play_kube_test.go @@ -2421,14 +2421,19 @@ MemoryReservation: {{ .HostConfig.MemoryReservation }}`}) err := generateKubeYaml("pod", pod, kubeYaml) Expect(err).To(BeNil()) - kube := podmanTest.Podman([]string{"play", "kube", "--log-driver", "journald", kubeYaml}) + kube := podmanTest.Podman([]string{"play", "kube", "--log-opt=max-size=10k", "--log-driver", "journald", kubeYaml}) kube.WaitWithDefaultTimeout() Expect(kube).Should(Exit(0)) - inspect := podmanTest.Podman([]string{"inspect", getCtrNameInPod(pod), "--format", "'{{ .HostConfig.LogConfig.Type }}'"}) + cid := getCtrNameInPod(pod) + inspect := podmanTest.Podman([]string{"inspect", cid, "--format", "'{{ .HostConfig.LogConfig.Type }}'"}) inspect.WaitWithDefaultTimeout() Expect(inspect).Should(Exit(0)) Expect(inspect.OutputToString()).To(ContainSubstring("journald")) + inspect = podmanTest.Podman([]string{"container", "inspect", "--format", "{{.HostConfig.LogConfig.Size}}", cid}) + inspect.WaitWithDefaultTimeout() + Expect(inspect).To(Exit(0)) + Expect(inspect.OutputToString()).To(Equal("10kB")) }) It("podman play kube test only creating the containers", func() { -- cgit v1.2.3-54-g00ecf