-
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
kubernetes processor use correct os path separator #9205
Closed
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ package add_kubernetes_metadata | |
|
||
import ( | ||
"fmt" | ||
"os" | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
|
@@ -38,6 +40,7 @@ func init() { | |
} | ||
|
||
const LogPathMatcherName = "logs_path" | ||
const PathSeparator = string(os.PathSeparator) | ||
|
||
type LogPathMatcher struct { | ||
LogsPath string | ||
|
@@ -49,7 +52,7 @@ func newLogsPathMatcher(cfg common.Config) (add_kubernetes_metadata.Matcher, err | |
LogsPath string `config:"logs_path"` | ||
ResourceType string `config:"resource_type"` | ||
}{ | ||
LogsPath: "/var/lib/docker/containers/", | ||
LogsPath: defaultLogPath(), | ||
ResourceType: "container", | ||
} | ||
|
||
|
@@ -59,8 +62,8 @@ func newLogsPathMatcher(cfg common.Config) (add_kubernetes_metadata.Matcher, err | |
} | ||
|
||
logPath := config.LogsPath | ||
if logPath[len(logPath)-1:] != "/" { | ||
logPath = logPath + "/" | ||
if logPath[len(logPath)-1:] != PathSeparator { | ||
logPath = logPath + PathSeparator | ||
} | ||
resourceType := config.ResourceType | ||
|
||
|
@@ -92,10 +95,10 @@ func (f *LogPathMatcher) MetadataIndex(event common.MapStr) string { | |
if f.ResourceType == "pod" { | ||
// Specify a pod resource type when manually mounting log volumes and they end up under "/var/lib/kubelet/pods/" | ||
// This will extract only the pod UID, which offers less granularity of metadata when compared to the container ID | ||
if strings.HasPrefix(f.LogsPath, "/var/lib/kubelet/pods/") && strings.HasSuffix(source, ".log") { | ||
pathDirs := strings.Split(source, "/") | ||
if strings.HasPrefix(f.LogsPath, PodLogsPath()) && strings.HasSuffix(source, ".log") { | ||
pathDirs := strings.Split(source, PathSeparator) | ||
if len(pathDirs) > podUIDPos { | ||
podUID := strings.Split(source, "/")[podUIDPos] | ||
podUID := strings.Split(source, PathSeparator)[podUIDPos] | ||
|
||
logp.Debug("kubernetes", "Using pod uid: %s", podUID) | ||
return podUID | ||
|
@@ -106,7 +109,7 @@ func (f *LogPathMatcher) MetadataIndex(event common.MapStr) string { | |
} else { | ||
// In case of the Kubernetes log path "/var/log/containers/", | ||
// the container ID will be located right before the ".log" extension. | ||
if strings.HasPrefix(f.LogsPath, "/var/log/containers/") && strings.HasSuffix(source, ".log") && sourceLen >= containerIdLen+4 { | ||
if strings.HasPrefix(f.LogsPath, ContainerLogsPath()) && strings.HasSuffix(source, ".log") && sourceLen >= containerIdLen+4 { | ||
containerIDEnd := sourceLen - 4 | ||
cid := source[containerIDEnd-containerIdLen : containerIDEnd] | ||
logp.Debug("kubernetes", "Using container id: %s", cid) | ||
|
@@ -127,3 +130,24 @@ func (f *LogPathMatcher) MetadataIndex(event common.MapStr) string { | |
|
||
return "" | ||
} | ||
|
||
func defaultLogPath() string { | ||
if runtime.GOOS == "windows" { | ||
return "C:\\ProgramData\\Docker\\containers" | ||
} | ||
return "/var/lib/docker/containers/" | ||
} | ||
|
||
func PodLogsPath() string { | ||
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. exported function PodLogsPath should have comment or be unexported |
||
if runtime.GOOS == "windows" { | ||
return "C:\\var\\lib\\kubelet\\pods\\" | ||
} | ||
return "/var/lib/kubelet/pods/" | ||
} | ||
|
||
func ContainerLogsPath() string { | ||
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. exported function ContainerLogsPath should have comment or be unexported |
||
if runtime.GOOS == "windows" { | ||
return "C:\\var\\log\\containers\\" | ||
} | ||
return "/var/log/containers/" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
exported const PathSeparator should have comment or be unexported