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

Backport #29133 to 7.17 #33165

Merged
merged 12 commits into from
Sep 29, 2022
11 changes: 4 additions & 7 deletions libbeat/autodiscover/providers/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func NewPodEventer(uuid uuid.UUID, cfg *common.Config, client k8s.Interface, pub
HonorReSyncs: true,
}, nil)
if err != nil {
return nil, fmt.Errorf("couldn't create watcher for %T due to error %+v", &kubernetes.Pod{}, err)
return nil, fmt.Errorf("couldn't create watcher for %T due to error %w", &kubernetes.Pod{}, err)
}

options := kubernetes.WatchOptions{
Expand All @@ -97,9 +97,6 @@ func NewPodEventer(uuid uuid.UUID, cfg *common.Config, client k8s.Interface, pub
options.Namespace = config.Namespace
}
metaConf := config.AddResourceMetadata
if metaConf == nil {
metaConf = metadata.GetDefaultResourceMetadataConfig()
}
nodeWatcher, err := kubernetes.NewWatcher(client, &kubernetes.Node{}, options, nil)
if err != nil {
logger.Errorf("couldn't create watcher for %T due to error %+v", &kubernetes.Node{}, err)
Expand Down Expand Up @@ -395,7 +392,7 @@ func (p *pod) containerPodEvents(flag string, pod *kubernetes.Pod, c *containerI
var events []bus.Event
portsMap := common.MapStr{}

meta.Put("container", cmeta)
_, _ = meta.Put("container", cmeta)

for _, port := range ports {
event := bus.Event{
Expand Down Expand Up @@ -461,7 +458,7 @@ func (p *pod) podEvent(flag string, pod *kubernetes.Pod, ports common.MapStr, in
func podAnnotations(pod *kubernetes.Pod) common.MapStr {
annotations := common.MapStr{}
for k, v := range pod.GetObjectMeta().GetAnnotations() {
safemapstr.Put(annotations, k, v)
_ = safemapstr.Put(annotations, k, v)
}
return annotations
}
Expand All @@ -484,7 +481,7 @@ func podNamespaceAnnotations(pod *kubernetes.Pod, watcher kubernetes.Watcher) co

annotations := common.MapStr{}
for k, v := range namespace.GetAnnotations() {
safemapstr.Put(annotations, k, v)
_ = safemapstr.Put(annotations, k, v)
}
return annotations
}
Expand Down
4 changes: 2 additions & 2 deletions libbeat/autodiscover/providers/kubernetes/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1907,7 +1907,7 @@ func TestPod_EmitEvent(t *testing.T) {
t.Fatal(err)
}

metaGen := metadata.NewPodMetadataGenerator(common.NewConfig(), nil, client, nil, nil)
metaGen := metadata.NewPodMetadataGenerator(common.NewConfig(), nil, client, nil, nil, nil)
p := &Provider{
config: defaultConfig(),
bus: bus.New(logp.NewLogger("bus"), "test"),
Expand Down Expand Up @@ -2040,7 +2040,7 @@ func getNestedAnnotations(in common.MapStr) common.MapStr {
out := common.MapStr{}

for k, v := range in {
out.Put(k, v)
_, _ = out.Put(k, v)
}
return out
}
16 changes: 8 additions & 8 deletions libbeat/common/kubernetes/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type ClusterConfiguration struct {
// WithFields FieldOption allows adding specific fields into the generated metadata
func WithFields(key string, value interface{}) FieldOptions {
return func(meta common.MapStr) {
safemapstr.Put(meta, key, value)
_ = safemapstr.Put(meta, key, value)
}
}

Expand All @@ -77,10 +77,10 @@ func WithFields(key string, value interface{}) FieldOptions {
func WithMetadata(kind string) FieldOptions {
return func(meta common.MapStr) {
if meta["labels"] != nil {
safemapstr.Put(meta, strings.ToLower(kind)+".labels", meta["labels"])
_ = safemapstr.Put(meta, strings.ToLower(kind)+".labels", meta["labels"])
}
if meta["annotations"] != nil {
safemapstr.Put(meta, strings.ToLower(kind)+".annotations", meta["annotations"])
_ = safemapstr.Put(meta, strings.ToLower(kind)+".annotations", meta["annotations"])
}
}
}
Expand All @@ -101,7 +101,7 @@ func GetPodMetaGen(
if namespaceWatcher != nil && metaConf.Namespace.Enabled() {
namespaceMetaGen = NewNamespaceMetadataGenerator(metaConf.Namespace, namespaceWatcher.Store(), namespaceWatcher.Client())
}
metaGen := NewPodMetadataGenerator(cfg, podWatcher.Store(), podWatcher.Client(), nodeMetaGen, namespaceMetaGen)
metaGen := NewPodMetadataGenerator(cfg, podWatcher.Store(), podWatcher.Client(), nodeMetaGen, namespaceMetaGen, metaConf)

return metaGen
}
Expand All @@ -110,7 +110,7 @@ func GetPodMetaGen(
func GetKubernetesClusterIdentifier(cfg *common.Config, client k8sclient.Interface) (ClusterInfo, error) {
// try with kube config file
var config Config
config.Unmarshal(cfg)
_ = config.Unmarshal(cfg)
clusterInfo, err := getClusterInfoFromKubeConfigFile(config.KubeConfig)
if err == nil {
return clusterInfo, nil
Expand All @@ -130,7 +130,7 @@ func getClusterInfoFromKubeadmConfigMap(client k8sclient.Interface) (ClusterInfo
}
cm, err := client.CoreV1().ConfigMaps("kube-system").Get(context.TODO(), "kubeadm-config", metav1.GetOptions{})
if err != nil {
return clusterInfo, fmt.Errorf("unable to get cluster identifiers from kubeadm-config: %+v", err)
return clusterInfo, fmt.Errorf("unable to get cluster identifiers from kubeadm-config: %w", err)
}
p, ok := cm.Data["ClusterConfiguration"]
if !ok {
Expand Down Expand Up @@ -163,12 +163,12 @@ func getClusterInfoFromKubeConfigFile(kubeconfig string) (ClusterInfo, error) {

cfg, err := kubernetes.BuildConfig(kubeconfig)
if err != nil {
return ClusterInfo{}, fmt.Errorf("unable to build kube config due to error: %+v", err)
return ClusterInfo{}, fmt.Errorf("unable to build kube config due to error: %w", err)
}

kube_cfg, err := clientcmd.LoadFromFile(kubeconfig)
if err != nil {
return ClusterInfo{}, fmt.Errorf("unable to load kube_config due to error: %+v", err)
return ClusterInfo{}, fmt.Errorf("unable to load kube_config due to error: %w", err)
}

for key, element := range kube_cfg.Clusters {
Expand Down
5 changes: 4 additions & 1 deletion libbeat/common/kubernetes/metadata/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ func TestNamespace_Generate(t *testing.T) {
UID: types.UID(uid),
Labels: map[string]string{
"foo": "bar",
"key": "value",
},
Annotations: map[string]string{
"spam": "baz",
"key": "value",
},
},
TypeMeta: metav1.TypeMeta{
Expand Down Expand Up @@ -88,6 +90,7 @@ func TestNamespace_Generate(t *testing.T) {
}

cfg, err := common.NewConfigFrom(Config{
IncludeLabels: []string{"foo"},
IncludeAnnotations: []string{"spam"},
})
if err != nil {
Expand Down Expand Up @@ -162,7 +165,7 @@ func TestNamespace_GenerateFromName(t *testing.T) {
}

namespaces := cache.NewStore(cache.MetaNamespaceKeyFunc)
namespaces.Add(test.input)
_ = namespaces.Add(test.input)
metagen := NewNamespaceMetadataGenerator(cfg, namespaces, client)

accessor, err := meta.Accessor(test.input)
Expand Down
39 changes: 23 additions & 16 deletions libbeat/common/kubernetes/metadata/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ import (
)

type pod struct {
store cache.Store
client k8s.Interface
node MetaGen
namespace MetaGen
resource *Resource
store cache.Store
client k8s.Interface
node MetaGen
namespace MetaGen
resource *Resource
addResourceMetadata *AddResourceMetadataConfig
}

// NewPodMetadataGenerator creates a metagen for pod resources
Expand All @@ -42,13 +43,19 @@ func NewPodMetadataGenerator(
pods cache.Store,
client k8s.Interface,
node MetaGen,
namespace MetaGen) MetaGen {
namespace MetaGen,
addResourceMetadata *AddResourceMetadataConfig) MetaGen {

if addResourceMetadata == nil {
addResourceMetadata = GetDefaultResourceMetadataConfig()
}
return &pod{
resource: NewResourceMetadataGenerator(cfg, client),
store: pods,
node: node,
namespace: namespace,
client: client,
resource: NewResourceMetadataGenerator(cfg, client),
store: pods,
node: node,
namespace: namespace,
client: client,
addResourceMetadata: addResourceMetadata,
}
}

Expand Down Expand Up @@ -88,19 +95,19 @@ func (p *pod) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) common.
if rsName, ok := rsName.(string); ok {
dep := p.getRSDeployment(rsName, po.GetNamespace())
if dep != "" {
out.Put("deployment.name", dep)
_, _ = out.Put("deployment.name", dep)
}
}

if p.node != nil {
meta := p.node.GenerateFromName(po.Spec.NodeName, WithMetadata("node"))
if meta != nil {
out.Put("node", meta["node"])
_, _ = out.Put("node", meta["node"])
} else {
out.Put("node.name", po.Spec.NodeName)
_, _ = out.Put("node.name", po.Spec.NodeName)
}
} else {
out.Put("node.name", po.Spec.NodeName)
_, _ = out.Put("node.name", po.Spec.NodeName)
}

if p.namespace != nil {
Expand All @@ -113,7 +120,7 @@ func (p *pod) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) common.
}

if po.Status.PodIP != "" {
out.Put("pod.ip", po.Status.PodIP)
_, _ = out.Put("pod.ip", po.Status.PodIP)
}

return out
Expand Down
Loading