Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to play kube for --log-opt #12086

Merged
merged 1 commit into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/podman/play/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 0 additions & 6 deletions docs/source/markdown/podman-create.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions docs/source/markdown/podman-play-kube.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/handlers/libpod/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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,
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/bindings/play/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
15 changes: 15 additions & 0 deletions pkg/bindings/play/types_kube_options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/domain/entities/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/domain/infra/abi/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions pkg/domain/infra/tunnel/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions pkg/specgen/generate/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
//
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions test/e2e/play_kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down