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

Checkk8swatchers #69

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions kubernetes/metadata/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package metadata

import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8s "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"

Expand All @@ -43,10 +45,12 @@ func NewNamespaceMetadataGenerator(cfg *config.C, namespaces cache.Store, client

// Generate generates pod metadata from a resource object
// Metadata map is in the following form:
// {
// "kubernetes": {},
// "some.ecs.field": "asdf"
// }
//
// {
// "kubernetes": {},
// "some.ecs.field": "asdf"
// }
//
// All Kubernetes fields that need to be stored under kuberentes. prefix are populetad by
// GenerateK8s method while fields that are part of ECS are generated by GenerateECS method
func (n *namespace) Generate(obj kubernetes.Resource, opts ...FieldOptions) mapstr.M {
Expand All @@ -65,12 +69,15 @@ func (n *namespace) GenerateECS(obj kubernetes.Resource) mapstr.M {

// GenerateK8s generates namespace metadata from a resource object
func (n *namespace) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) mapstr.M {
_, ok := obj.(*kubernetes.Namespace)
var namespace *v1.Namespace
originalNamespace, ok := obj.(*kubernetes.Namespace)
if !ok {
return nil
} else {
namespace = removeUnnecessaryNamespaceData(originalNamespace)
}

meta := n.resource.GenerateK8s(resource, obj, opts...)
meta := n.resource.GenerateK8s(resource, namespace, opts...)
meta = flattenMetadata(meta)

// Add extra fields in here if need be
Expand Down Expand Up @@ -128,3 +135,15 @@ func flattenMetadata(in mapstr.M) mapstr.M {

return out
}

func removeUnnecessaryNamespaceData(namespace *v1.Namespace) *v1.Namespace {
transformernamespace := &v1.Namespace{
TypeMeta: metav1.TypeMeta{
Kind: namespace.Kind,
APIVersion: namespace.APIVersion,
},
ObjectMeta: namespace.ObjectMeta,
}

return transformernamespace
}
33 changes: 27 additions & 6 deletions kubernetes/metadata/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/elastic/elastic-agent-autodiscover/kubernetes"
"github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/mapstr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type node struct {
Expand All @@ -42,10 +43,12 @@ func NewNodeMetadataGenerator(cfg *config.C, nodes cache.Store, client k8s.Inter

// Generate generates node metadata from a resource object
// Metadata map is in the following form:
// {
// "kubernetes": {},
// "some.ecs.field": "asdf"
// }
//
// {
// "kubernetes": {},
// "some.ecs.field": "asdf"
// }
//
// All Kubernetes fields that need to be stored under kuberentes. prefix are populetad by
// GenerateK8s method while fields that are part of ECS are generated by GenerateECS method
func (n *node) Generate(obj kubernetes.Resource, opts ...FieldOptions) mapstr.M {
Expand All @@ -64,12 +67,15 @@ func (n *node) GenerateECS(obj kubernetes.Resource) mapstr.M {

// GenerateK8s generates node metadata from a resource object
func (n *node) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) mapstr.M {
node, ok := obj.(*kubernetes.Node)
var node *v1.Node
originalNode, ok := obj.(*kubernetes.Node)
if !ok {
return nil
} else {
node = removeUnnecessaryNodeData(originalNode)
}

meta := n.resource.GenerateK8s("node", obj, opts...)
meta := n.resource.GenerateK8s("node", node, opts...)
// Add extra fields in here if need be
hostname := getHostName(node)
if hostname != "" {
Expand Down Expand Up @@ -105,3 +111,18 @@ func getHostName(node *v1.Node) string {
}
return ""
}

func removeUnnecessaryNodeData(node *v1.Node) *v1.Node {
transformernode := &v1.Node{
TypeMeta: metav1.TypeMeta{
Kind: node.Kind,
APIVersion: node.APIVersion,
},
ObjectMeta: node.ObjectMeta,
Status: v1.NodeStatus{
Addresses: node.Status.Addresses,
},
}

return transformernode
}
23 changes: 22 additions & 1 deletion kubernetes/metadata/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package metadata

import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8s "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"

Expand Down Expand Up @@ -84,9 +86,12 @@ func (p *pod) GenerateECS(obj kubernetes.Resource) mapstr.M {

// GenerateK8s generates pod metadata from a resource object
func (p *pod) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) mapstr.M {
po, ok := obj.(*kubernetes.Pod)
var po *v1.Pod
originalPod, ok := obj.(*kubernetes.Pod)
if !ok {
return nil
} else {
po = removeUnnecessaryPodData(originalPod)
}

out := p.resource.GenerateK8s("pod", obj, opts...)
Expand Down Expand Up @@ -156,3 +161,19 @@ func (p *pod) GenerateFromName(name string, opts ...FieldOptions) mapstr.M {

return nil
}

func removeUnnecessaryPodData(pod *v1.Pod) *v1.Pod {
transformernode := &v1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: pod.Kind,
APIVersion: pod.APIVersion,
},
ObjectMeta: pod.ObjectMeta,
Spec: v1.PodSpec{
NodeName: pod.Spec.NodeName,
},
Status: v1.PodStatus{PodIP: pod.Status.PodIP},
}

return transformernode
}
Loading