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

Add k8s cluster identifier #26056

Merged
merged 34 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
480d046
wip
ChrsMark May 27, 2021
f301562
Refactor and add orchestrator.cluster.url
ChrsMark Jun 1, 2021
5a697b5
Merge remote-tracking branch 'upstream/master' into add_k8s_cluster_i…
ChrsMark Jun 1, 2021
54994fb
wip
ChrsMark Jun 4, 2021
c2555dd
Update methods to get cluster Identifiers
ChrsMark Jun 4, 2021
fd6d2e6
Remove unused function
ChrsMark Jun 4, 2021
5e75367
wip
ChrsMark Jun 7, 2021
2df343a
wip
ChrsMark Jun 7, 2021
943502e
wip
ChrsMark Jun 7, 2021
b960f84
wip
ChrsMark Jun 7, 2021
1bfd7ea
wip
ChrsMark Jun 7, 2021
2b5c55a
stable commit
ChrsMark Jun 7, 2021
bebec37
fixups
ChrsMark Jun 7, 2021
3facd8a
remove unused changes
ChrsMark Jun 7, 2021
bbd7b70
Add configmaps in clusterrole
ChrsMark Jun 8, 2021
3b2761f
Tune add_k8s_metadata processor
ChrsMark Jun 8, 2021
25fd5a5
Merge remote-tracking branch 'upstream/master' into add_k8s_cluster_i…
ChrsMark Jun 8, 2021
744afe4
cleanup
ChrsMark Jun 8, 2021
8429489
Move GKE metadata code to cloud metadata lib
ChrsMark Jun 8, 2021
85b9a1e
fix struct
ChrsMark Jun 8, 2021
34747af
Add orchestator.cluster.name only if in k8s
ChrsMark Jun 9, 2021
8a8c6d1
Improve cloud_metadata
ChrsMark Jun 9, 2021
018c7ea
Fix k8s roles
ChrsMark Jun 9, 2021
4103b87
Fix metricsets
ChrsMark Jun 9, 2021
e6f41b4
Fixup
ChrsMark Jun 9, 2021
d1c9ef6
fmt
ChrsMark Jun 9, 2021
04697d1
review changes
ChrsMark Jun 10, 2021
c06926a
Add changelog
ChrsMark Jun 10, 2021
503436c
Merge remote-tracking branch 'upstream/master' into add_k8s_cluster_i…
ChrsMark Jun 16, 2021
eb115c9
fixes
ChrsMark Jun 16, 2021
5a03920
fix expected files
ChrsMark Jun 16, 2021
1ee92c8
fix tests
ChrsMark Jun 16, 2021
c69f878
small fix
ChrsMark Jun 16, 2021
e720e74
formating
ChrsMark Jun 16, 2021
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
1 change: 1 addition & 0 deletions deploy/kubernetes/auditbeat-kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ rules:
- nodes
- namespaces
- pods
- configmaps
verbs: ["get", "list", "watch"]
- apiGroups: ["apps"]
resources:
Expand Down
1 change: 1 addition & 0 deletions deploy/kubernetes/auditbeat/auditbeat-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ rules:
- nodes
- namespaces
- pods
- configmaps
verbs: ["get", "list", "watch"]
- apiGroups: ["apps"]
resources:
Expand Down
1 change: 1 addition & 0 deletions deploy/kubernetes/elastic-agent-standalone-kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ rules:
- events
- pods
- services
- configmaps
ChrsMark marked this conversation as resolved.
Show resolved Hide resolved
verbs: ["get", "list", "watch"]
# Enable this rule only if planing to use kubernetes_secrets provider
#- apiGroups: [""]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rules:
- events
- pods
- services
- configmaps
verbs: ["get", "list", "watch"]
# Enable this rule only if planing to use kubernetes_secrets provider
#- apiGroups: [""]
Expand Down
1 change: 1 addition & 0 deletions deploy/kubernetes/filebeat-kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ rules:
- namespaces
- pods
- nodes
- configmaps
verbs:
- get
- watch
Expand Down
1 change: 1 addition & 0 deletions deploy/kubernetes/filebeat/filebeat-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ rules:
- namespaces
- pods
- nodes
- configmaps
verbs:
- get
- watch
Expand Down
1 change: 1 addition & 0 deletions deploy/kubernetes/heartbeat-kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ rules:
- namespaces
- pods
- services
- configmaps
verbs: ["get", "list", "watch"]
- apiGroups: ["apps"]
resources:
Expand Down
1 change: 1 addition & 0 deletions deploy/kubernetes/heartbeat/heartbeat-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ rules:
- namespaces
- pods
- services
- configmaps
verbs: ["get", "list", "watch"]
- apiGroups: ["apps"]
resources:
Expand Down
1 change: 1 addition & 0 deletions deploy/kubernetes/metricbeat-kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ rules:
- events
- pods
- services
- configmaps
verbs: ["get", "list", "watch"]
# Enable this rule only if planing to use Kubernetes keystore
#- apiGroups: [""]
Expand Down
1 change: 1 addition & 0 deletions deploy/kubernetes/metricbeat/metricbeat-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rules:
- events
- pods
- services
- configmaps
verbs: ["get", "list", "watch"]
# Enable this rule only if planing to use Kubernetes keystore
#- apiGroups: [""]
Expand Down
10 changes: 5 additions & 5 deletions libbeat/autodiscover/providers/kubernetes/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func NewNodeEventer(uuid uuid.UUID, cfg *common.Config, client k8s.Interface, pu
config: config,
uuid: uuid,
publish: publish,
metagen: metadata.NewNodeMetadataGenerator(cfg, watcher.Store()),
metagen: metadata.NewNodeMetadataGenerator(cfg, watcher.Store(), client),
logger: logger,
watcher: watcher,
}
Expand Down Expand Up @@ -177,7 +177,9 @@ func (n *node) emit(node *kubernetes.Node, flag string) {
eventID := fmt.Sprint(node.GetObjectMeta().GetUID())
meta := n.metagen.Generate(node)

kubemeta := meta.Clone()
kubemetaMap, _ := meta.GetValue("kubernetes")
kubemeta, _ := kubemetaMap.(common.MapStr)
kubemeta = kubemeta.Clone()
// Pass annotations to all events so that it can be used in templating and by annotation builders.
annotations := common.MapStr{}
for k, v := range node.GetObjectMeta().GetAnnotations() {
Expand All @@ -190,9 +192,7 @@ func (n *node) emit(node *kubernetes.Node, flag string) {
flag: true,
"host": host,
"kubernetes": kubemeta,
"meta": common.MapStr{
"kubernetes": meta,
},
"meta": meta,
}
n.publish([]bus.Event{event})
}
Expand Down
4 changes: 3 additions & 1 deletion libbeat/autodiscover/providers/kubernetes/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
k8sfake "k8s.io/client-go/kubernetes/fake"

"github.com/elastic/beats/v7/libbeat/autodiscover/template"
"github.com/elastic/beats/v7/libbeat/common"
Expand Down Expand Up @@ -111,6 +112,7 @@ func TestGenerateHints_Node(t *testing.T) {
}

func TestEmitEvent_Node(t *testing.T) {
client := k8sfake.NewSimpleClientset()
name := "metricbeat"
nodeIP := "192.168.0.1"
uid := "005f3b90-4b9d-12f8-acf0-31020a840133"
Expand Down Expand Up @@ -310,7 +312,7 @@ func TestEmitEvent_Node(t *testing.T) {
t.Fatal(err)
}

metaGen := metadata.NewNodeMetadataGenerator(common.NewConfig(), nil)
metaGen := metadata.NewNodeMetadataGenerator(common.NewConfig(), nil, client)
config := defaultConfig()
p := &Provider{
config: config,
Expand Down
22 changes: 12 additions & 10 deletions libbeat/autodiscover/providers/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func (p *pod) emit(pod *kubernetes.Pod, flag string) {
// configured port, a single event is created, with the port set to 0.
// Host and port information is only included if the container is
// running.
// If the container ID is unkown, only "stop" events are generated.
// If the container ID is unknown, only "stop" events are generated.
// It also returns a map with the named ports.
func (p *pod) containerPodEvents(flag string, pod *kubernetes.Pod, c *containerInPod, annotations, namespaceAnnotations common.MapStr) ([]bus.Event, common.MapStr) {
if c.id == "" && flag != "stop" {
Expand All @@ -361,7 +361,9 @@ func (p *pod) containerPodEvents(flag string, pod *kubernetes.Pod, c *containerI
}

// Information that can be used in discovering a workload
kubemeta := meta.Clone()
kubemetaMap, _ := meta.GetValue("kubernetes")
kubemeta, _ := kubemetaMap.(common.MapStr)
kubemeta = kubemeta.Clone()
kubemeta["annotations"] = annotations
kubemeta["container"] = common.MapStr{
"id": c.id,
Expand All @@ -383,17 +385,17 @@ func (p *pod) containerPodEvents(flag string, pod *kubernetes.Pod, c *containerI

var events []bus.Event
portsMap := common.MapStr{}

meta.Put("container", cmeta)

for _, port := range ports {
event := bus.Event{
"provider": p.uuid,
"id": eventID,
flag: true,
"kubernetes": kubemeta,
// Actual metadata that will enrich the event.
"meta": common.MapStr{
"kubernetes": meta,
"container": cmeta,
},
"meta": meta,
}
// Include network information only if the container is running,
// so templates that need network don't generate a config.
Expand All @@ -417,7 +419,9 @@ func (p *pod) podEvent(flag string, pod *kubernetes.Pod, ports common.MapStr, in
meta := p.metagen.Generate(pod)

// Information that can be used in discovering a workload
kubemeta := meta.Clone()
kubemetaMap, _ := meta.GetValue("kubernetes")
kubemeta, _ := kubemetaMap.(common.MapStr)
kubemeta = kubemeta.Clone()
kubemeta["annotations"] = annotations
if len(namespaceAnnotations) != 0 {
kubemeta["namespace_annotations"] = namespaceAnnotations
Expand All @@ -429,9 +433,7 @@ func (p *pod) podEvent(flag string, pod *kubernetes.Pod, ports common.MapStr, in
"id": fmt.Sprint(pod.GetObjectMeta().GetUID()),
flag: true,
"kubernetes": kubemeta,
"meta": common.MapStr{
"kubernetes": meta,
},
"meta": meta,
}

// Include network information only if the pod has an IP and there is any
Expand Down
6 changes: 4 additions & 2 deletions libbeat/autodiscover/providers/kubernetes/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
k8sfake "k8s.io/client-go/kubernetes/fake"

"github.com/elastic/beats/v7/libbeat/autodiscover/template"
"github.com/elastic/beats/v7/libbeat/common"
Expand Down Expand Up @@ -331,7 +332,7 @@ func TestGenerateHints(t *testing.T) {
}
}

func TestEmitEvent(t *testing.T) {
func TestPod_EmitEvent(t *testing.T) {
name := "filebeat"
namespace := "default"
podIP := "127.0.0.1"
Expand Down Expand Up @@ -1898,14 +1899,15 @@ func TestEmitEvent(t *testing.T) {
},
}

client := k8sfake.NewSimpleClientset()
for _, test := range tests {
t.Run(test.Message, func(t *testing.T) {
mapper, err := template.NewConfigMapper(nil, nil, nil)
if err != nil {
t.Fatal(err)
}

metaGen := metadata.NewPodMetadataGenerator(common.NewConfig(), nil, nil, nil, nil)
metaGen := metadata.NewPodMetadataGenerator(common.NewConfig(), nil, client, nil, nil)
p := &Provider{
config: defaultConfig(),
bus: bus.New(logp.NewLogger("bus"), "test"),
Expand Down
12 changes: 6 additions & 6 deletions libbeat/autodiscover/providers/kubernetes/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ func NewServiceEventer(uuid uuid.UUID, cfg *common.Config, client k8s.Interface,
return nil, fmt.Errorf("couldn't create watcher for %T due to error %+v", &kubernetes.Namespace{}, err)
}

namespaceMeta = metadata.NewNamespaceMetadataGenerator(metaConf.Namespace, namespaceWatcher.Store())
namespaceMeta = metadata.NewNamespaceMetadataGenerator(metaConf.Namespace, namespaceWatcher.Store(), client)

p := &service{
config: config,
uuid: uuid,
publish: publish,
metagen: metadata.NewServiceMetadataGenerator(cfg, watcher.Store(), namespaceMeta),
metagen: metadata.NewServiceMetadataGenerator(cfg, watcher.Store(), namespaceMeta, client),
namespaceWatcher: namespaceWatcher,
logger: logger,
watcher: watcher,
Expand Down Expand Up @@ -193,7 +193,9 @@ func (s *service) emit(svc *kubernetes.Service, flag string) {
eventID := fmt.Sprint(svc.GetObjectMeta().GetUID())
meta := s.metagen.Generate(svc)

kubemeta := meta.Clone()
kubemetaMap, _ := meta.GetValue("kubernetes")
kubemeta, _ := kubemetaMap.(common.MapStr)
kubemeta = kubemeta.Clone()
// Pass annotations to all events so that it can be used in templating and by annotation builders.
annotations := common.MapStr{}
for k, v := range svc.GetObjectMeta().GetAnnotations() {
Expand Down Expand Up @@ -223,9 +225,7 @@ func (s *service) emit(svc *kubernetes.Service, flag string) {
"host": host,
"port": int(port.Port),
"kubernetes": kubemeta,
"meta": common.MapStr{
"kubernetes": meta,
},
"meta": meta,
}
events = append(events, event)
}
Expand Down
4 changes: 3 additions & 1 deletion libbeat/autodiscover/providers/kubernetes/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
k8sfake "k8s.io/client-go/kubernetes/fake"

"github.com/elastic/beats/v7/libbeat/autodiscover/template"
"github.com/elastic/beats/v7/libbeat/common"
Expand Down Expand Up @@ -232,6 +233,7 @@ func TestGenerateHints_Service(t *testing.T) {
}

func TestEmitEvent_Service(t *testing.T) {
client := k8sfake.NewSimpleClientset()
name := "metricbeat"
namespace := "default"
clusterIP := "192.168.0.1"
Expand Down Expand Up @@ -395,7 +397,7 @@ func TestEmitEvent_Service(t *testing.T) {
t.Fatal(err)
}

metaGen := metadata.NewServiceMetadataGenerator(common.NewConfig(), nil, nil)
metaGen := metadata.NewServiceMetadataGenerator(common.NewConfig(), nil, nil, client)

p := &Provider{
config: defaultConfig(),
Expand Down
1 change: 1 addition & 0 deletions libbeat/common/kubernetes/metadata/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import "github.com/elastic/beats/v7/libbeat/common"

// Config declares supported configuration for metadata generation
type Config struct {
KubeConfig string `config:"kube_config"`
IncludeLabels []string `config:"include_labels"`
ExcludeLabels []string `config:"exclude_labels"`
IncludeAnnotations []string `config:"include_annotations"`
Expand Down
Loading