Add support to play kube for --log-opt

Fixes: https://github.com/containers/podman/issues/11727

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
This commit is contained in:
Daniel J Walsh
2021-10-25 08:07:10 -04:00
parent dbe770e3ce
commit acd8b49000
11 changed files with 82 additions and 8 deletions

View File

@ -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")

View File

@ -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)

View File

@ -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.

View File

@ -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,
}

View File

@ -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
}

View File

@ -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

View File

@ -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
}

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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() {