Skip to content

Commit

Permalink
Fix linter warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
swiatekm committed Oct 8, 2024
1 parent 54fe672 commit fd27cca
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 25 deletions.
47 changes: 26 additions & 21 deletions libbeat/autodiscover/providers/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,23 +239,26 @@ func (p *pod) GenerateHints(event bus.Event) bus.Event {
var kubeMeta, container mapstr.M

annotations := make(mapstr.M, 0)
rawMeta, ok := event["kubernetes"]
if ok {
kubeMeta = rawMeta.(mapstr.M)
// The builder base config can configure any of the field values of kubernetes if need be.
e["kubernetes"] = kubeMeta
if rawAnn, ok := kubeMeta["annotations"]; ok {
anns, _ := rawAnn.(mapstr.M)
if len(anns) != 0 {
annotations = anns.Clone()
rawMeta, found := event["kubernetes"]
if found {
kubeMetaMap, ok := rawMeta.(mapstr.M)
if ok {
kubeMeta = kubeMetaMap
// The builder base config can configure any of the field values of kubernetes if need be.
e["kubernetes"] = kubeMeta
if rawAnn, ok := kubeMeta["annotations"]; ok {
anns, _ := rawAnn.(mapstr.M)
if len(anns) != 0 {
annotations = anns.Clone()
}
}
}

// Look at all the namespace level default annotations and do a merge with priority going to the pod annotations.
if rawNsAnn, ok := kubeMeta["namespace_annotations"]; ok {
namespaceAnnotations, _ := rawNsAnn.(mapstr.M)
if len(namespaceAnnotations) != 0 {
annotations.DeepUpdateNoOverwrite(namespaceAnnotations)
// Look at all the namespace level default annotations and do a merge with priority going to the pod annotations.
if rawNsAnn, ok := kubeMeta["namespace_annotations"]; ok {
namespaceAnnotations, _ := rawNsAnn.(mapstr.M)
if len(namespaceAnnotations) != 0 {
annotations.DeepUpdateNoOverwrite(namespaceAnnotations)
}
}
}
}
Expand All @@ -269,12 +272,14 @@ func (p *pod) GenerateHints(event bus.Event) bus.Event {
e["ports"] = ports
}

if rawCont, ok := kubeMeta["container"]; ok {
container = rawCont.(mapstr.M)
// This would end up adding a runtime entry into the event. This would make sure
// that there is not an attempt to spin up a docker input for a rkt container and when a
// rkt input exists it would be natively supported.
e["container"] = container
if rawCont, found := kubeMeta["container"]; found {
if containerMap, ok := rawCont.(mapstr.M); ok {
container = containerMap
// This would end up adding a runtime entry into the event. This would make sure
// that there is not an attempt to spin up a docker input for a rkt container and when a
// rkt input exists it would be natively supported.
e["container"] = container
}
}

cname := utils.GetContainerName(container)
Expand Down
13 changes: 12 additions & 1 deletion libbeat/autodiscover/providers/kubernetes/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,19 @@ import (

func TestGenerateHints(t *testing.T) {
tests := []struct {
name string
event bus.Event
result bus.Event
}{
// Empty events should return empty hints
{
name: "empty",
event: bus.Event{},
result: bus.Event{},
},
// Only kubernetes payload must return only kubernetes as part of the hint
{
name: "only kubernetes",
event: bus.Event{
"kubernetes": mapstr.M{
"pod": mapstr.M{
Expand All @@ -71,6 +74,7 @@ func TestGenerateHints(t *testing.T) {
},
// Kubernetes payload with container info must be bubbled to top level
{
name: "kubernetes container info top level",
event: bus.Event{
"kubernetes": mapstr.M{
"container": mapstr.M{
Expand Down Expand Up @@ -102,6 +106,7 @@ func TestGenerateHints(t *testing.T) {
// not.to.include must not be part of hints
// period is annotated at both container and pod level. Container level value must be in hints
{
name: "multiple hints",
event: bus.Event{
"kubernetes": mapstr.M{
"annotations": getNestedAnnotations(mapstr.M{
Expand Down Expand Up @@ -163,6 +168,7 @@ func TestGenerateHints(t *testing.T) {
// Have one set of hints come from the pod and the other come from namespaces
// The resultant hints should have a combination of both
{
name: "hints from Pod and Namespace",
event: bus.Event{
"kubernetes": mapstr.M{
"annotations": getNestedAnnotations(mapstr.M{
Expand Down Expand Up @@ -227,6 +233,7 @@ func TestGenerateHints(t *testing.T) {
// Have one set of hints come from the pod and the same keys come from namespaces
// The resultant hints should honor only pods and not namespace.
{
name: "pod hints win over namespace",
event: bus.Event{
"kubernetes": mapstr.M{
"annotations": getNestedAnnotations(mapstr.M{
Expand Down Expand Up @@ -288,6 +295,7 @@ func TestGenerateHints(t *testing.T) {
// Have no hints on the pod and have namespace level defaults.
// The resultant hints should honor only namespace defaults.
{
name: "namespace defaults",
event: bus.Event{
"kubernetes": mapstr.M{
"namespace_annotations": getNestedAnnotations(mapstr.M{
Expand Down Expand Up @@ -339,7 +347,10 @@ func TestGenerateHints(t *testing.T) {
logger: logp.NewLogger("kubernetes.pod"),
}
for _, test := range tests {
assert.Equal(t, p.GenerateHints(test.event), test.result)
test := test
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.result, p.GenerateHints(test.event))
})
}
}

Expand Down
6 changes: 3 additions & 3 deletions libbeat/processors/add_kubernetes_metadata/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,15 @@ func (k *kubernetesAnnotator) init(config kubeAnnotatorConfig, cfg *config.C) {

watcher.AddEventHandler(kubernetes.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
pod := obj.(*kubernetes.Pod)
pod, _ := obj.(*kubernetes.Pod)
k.addPod(pod)
},
UpdateFunc: func(obj interface{}) {
pod := obj.(*kubernetes.Pod)
pod, _ := obj.(*kubernetes.Pod)
k.updatePod(pod)
},
DeleteFunc: func(obj interface{}) {
pod := obj.(*kubernetes.Pod)
pod, _ := obj.(*kubernetes.Pod)
k.removePod(pod)
},
})
Expand Down

0 comments on commit fd27cca

Please sign in to comment.