k8processor: Too broad regex used to extract deployment name #1158
Description
Describe the bug
When using k8processor to add deployment tag via ExtractionRules.Deployment
it sometimes incorrectly extracts an incorrect value by using regex which is too broadly defined:
opentelemetry-collector-contrib/processor/k8sprocessor/kube/client.go
Lines 53 to 55 in d9e18f8
This does not work for instance when:
- pod's owner is a daemonset, then its name looks like
fluent-bit-v6gn7
and the above regex will extractfluent
as the deployment name - pod's owner is a statefulset, then its name looks like
prometheus-collection-prometheus-oper-prometheus-0
and the above regex will extractprometheus-collection-prometheus-oper
as the deployment name
Steps to reproduce
Create pod with names as described above.
What did you expect to see?
Correct deployment name.
What did you see instead?
Incorrect deployment name.
What version did you use?
Version: 7f52fd77
What config did you use?
Config: N/A
Environment
OS: MacOS
Compiler(if manually compiled): go1.15.2
Trying to solve this problem using solely regexes instead of reaching out to k8s API might not be a bullet proof solution since even trying to adjust this regex might not work 100% of the time as some replicaSets have 8 and some have 10 character suffixes. Examples:
# rs
collection-kube-state-metrics-6fd6987d
# pod
collection-kube-state-metrics-6fd6987d-q5w6n
# rs
collection-prometheus-oper-operator-6964dbd849
# pod
collection-prometheus-oper-operator-6964dbd849-c4h6w
Even making the regexes limited in the number of characters in the replicaSet section might not work because without the information whether a particular pod is part of a ds, rs or sts we might still end up with an extracted name that we didn't want. Example:
Using the following ( a bit more constrained regex allowing 8
to 10
characters in replicaset ID)
// Extract deployment name from the pod name. Pod name is created using
// format: [deployment-name]-[Random-String-For-ReplicaSet]-[Random-String-For-Pod]
var dRegex = regexp.MustCompile(`^(.*)-[0-9a-zA-Z]{8,10}-[0-9a-zA-Z]*$`)
maybedeploymentmaybenot-8charapp-v6gn7
would still extract maybedeploymentmaybenot
- while it should extract maybedeploymentmaybenot-8charapp
as this pod is a part of a DaemonSet.