Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add ECS orchestrator fields #1604

Merged
merged 7 commits into from
Dec 13, 2023
Merged
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
3 changes: 3 additions & 0 deletions dataprovider/providers/k8s/data_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const (
clusterNameField = "orchestrator.cluster.name"
clusterVersionField = "orchestrator.cluster.version"
clusterIdField = "orchestrator.cluster.id"
orchestratorType = "orchestrator.type"
orchestratorName = "kubernetes"
)

type DataProvider struct {
Expand All @@ -52,6 +54,7 @@ func (k DataProvider) EnrichEvent(event *beat.Event, _ fetching.ResourceMetadata
insertIfNotEmpty(clusterNameField, k.cluster, event),
insertIfNotEmpty(clusterIdField, k.clusterID, event),
insertIfNotEmpty(clusterVersionField, k.clusterVersion, event),
insertIfNotEmpty(orchestratorType, orchestratorName, event),
)
}

Expand Down
10 changes: 8 additions & 2 deletions dataprovider/providers/k8s/data_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ func TestK8sDataProvider_EnrichEvent(t *testing.T) {
options: []Option{
WithLogger(testhelper.NewLogger(t)),
},
want: map[string]interface{}{},
want: map[string]interface{}{
orchestratorType: "kubernetes",
},
}, {
name: "should return cluster version",
options: []Option{
Expand All @@ -49,6 +51,7 @@ func TestK8sDataProvider_EnrichEvent(t *testing.T) {
},
want: map[string]interface{}{
clusterVersionField: "test_version",
orchestratorType: "kubernetes",
},
}, {
name: "should return cluster name",
Expand All @@ -58,6 +61,7 @@ func TestK8sDataProvider_EnrichEvent(t *testing.T) {
},
want: map[string]interface{}{
clusterNameField: "test_cluster",
orchestratorType: "kubernetes",
},
}, {
name: "should return cluster id",
Expand All @@ -66,7 +70,8 @@ func TestK8sDataProvider_EnrichEvent(t *testing.T) {
WithClusterID("test_id"),
},
want: map[string]interface{}{
clusterIdField: "test_id",
clusterIdField: "test_id",
orchestratorType: "kubernetes",
},
}, {
name: "should return all fields",
Expand All @@ -80,6 +85,7 @@ func TestK8sDataProvider_EnrichEvent(t *testing.T) {
clusterIdField: "test_id",
clusterNameField: "test_cluster",
clusterVersionField: "test_version",
orchestratorType: "kubernetes",
},
},
}
Expand Down
19 changes: 18 additions & 1 deletion resources/fetching/fetchers/k8s/kube_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ const (
k8sObjMetadataField = "ObjectMeta"
k8sTypeMetadataField = "TypeMeta"
K8sObjType = "k8s_object"

ecsResourceTypeField = "orchestrator.resource.type"
ecsResourceIdField = "orchestrator.resource.id"
ecsResourceNameField = "orchestrator.resource.name"
)

func getKubeData(log *logp.Logger, watchers []kubernetes.Watcher, resCh chan fetching.ResourceInfo, cycleMetadata cycle.Metadata) {
Expand Down Expand Up @@ -82,7 +86,20 @@ func (r K8sResource) GetMetadata() (fetching.ResourceMetadata, error) {
}, nil
}

func (r K8sResource) GetElasticCommonData() (map[string]any, error) { return nil, nil }
func (r K8sResource) GetElasticCommonData() (map[string]any, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please be aware that this might conflict with other data provider fields, see #1605

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's probably fine because the fields are already in the form of orchestrator.resource.xyz but leaving this comment so you can verify that it's working as expected

metadata, err := r.GetMetadata()
if err != nil {
return nil, err
}

fields := map[string]any{
ecsResourceTypeField: metadata.SubType,
ecsResourceNameField: metadata.Name,
ecsResourceIdField: metadata.ID,
}

return fields, nil
}

func getK8sObjectMeta(log *logp.Logger, k8sObj reflect.Value) metav1.ObjectMeta {
metadata, ok := k8sObj.FieldByName(k8sObjMetadataField).Interface().(metav1.ObjectMeta)
Expand Down