-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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 container
input, deprecate docker
in favor of it
#12162
Changes from 2 commits
cdf0ec4
67644b2
47d4329
59137f7
dfd29cd
cf735ca
2933c41
a56e7f5
d7f3dda
9b686dd
2872f37
1236d5b
5ff8036
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package container | ||
|
||
var defaultConfig = config{ | ||
Stream: "all", | ||
Format: "auto", | ||
} | ||
|
||
type config struct { | ||
// Stream can be all, stdout or stderr | ||
Stream string `config:"stream"` | ||
|
||
// Fore CRI format (don't perform autodetection) | ||
exekias marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// TODO remove in favor of format, below | ||
CRIForce bool `config:"cri.force"` | ||
|
||
// TODO Format can be auto, cri, json-file | ||
exekias marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Format string `config:"format"` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package container | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/elastic/beats/filebeat/channel" | ||
"github.com/elastic/beats/filebeat/input" | ||
"github.com/elastic/beats/filebeat/input/log" | ||
"github.com/elastic/beats/libbeat/common" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func init() { | ||
err := input.Register("container", NewInput) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// NewInput creates a new container input | ||
func NewInput( | ||
cfg *common.Config, | ||
outletFactory channel.Connector, | ||
context input.Context, | ||
) (input.Input, error) { | ||
// Wrap log input with custom docker settings | ||
config := defaultConfig | ||
if err := cfg.Unpack(&config); err != nil { | ||
return nil, errors.Wrap(err, "reading container input config") | ||
} | ||
|
||
if err := checkStream(config.Stream); err != nil { | ||
return nil, err | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better implement |
||
|
||
// Set partial line joining to true (both json-file and CRI) | ||
if err := cfg.SetBool("docker-json.partial", -1, true); err != nil { | ||
return nil, errors.Wrap(err, "update input config") | ||
} | ||
|
||
if err := cfg.SetBool("docker-json.cri_flags", -1, true); err != nil { | ||
return nil, errors.Wrap(err, "update input config") | ||
} | ||
|
||
// Allow stream selection (stdout/stderr/all) | ||
if err := cfg.SetString("docker-json.stream", -1, config.Stream); err != nil { | ||
return nil, errors.Wrap(err, "update input config") | ||
} | ||
|
||
if err := cfg.SetBool("docker-json.force_cri_logs", -1, config.CRIForce); err != nil { | ||
return nil, errors.Wrap(err, "update input config") | ||
} | ||
|
||
// Set symlinks to true as CRI-O paths could point to symlinks instead of the actual path. | ||
if err := cfg.SetBool("symlinks", -1, true); err != nil { | ||
return nil, errors.Wrap(err, "update input config") | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A many setters can be replaced by a single call to 'Merge':
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for this, I didn't know it could be so simple! |
||
|
||
// Add stream to meta to ensure different state per stream | ||
if config.Stream != "all" { | ||
if context.Meta == nil { | ||
context.Meta = map[string]string{} | ||
} | ||
context.Meta["stream"] = config.Stream | ||
} | ||
|
||
return log.NewInput(cfg, outletFactory, context) | ||
} | ||
|
||
func checkStream(val string) error { | ||
exekias marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for _, s := range []string{"all", "stdout", "stderr"} { | ||
if s == val { | ||
return nil | ||
} | ||
} | ||
|
||
return fmt.Errorf("Invalid value for stream: %s, supported values are: all, stdout, stderr", val) | ||
} | ||
|
||
func checkFormat(val string) error { | ||
kaiyan-sheng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val = strings.ToLower(val) | ||
for _, s := range []string{"auto", "docker", "cri"} { | ||
if s == val { | ||
return nil | ||
} | ||
} | ||
|
||
return fmt.Errorf("Invalid value for format: %s, supported values are: auto, docker, cri", val) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use a less docker-specific path here? Or add a
runtime
option that sets up the proper defaults depending on the runtime?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm currently working on a different PR to make autodiscover more CRI friendly, this is meant to keep it working as of now