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

Allow ReplicaSet and Job metadata generators to use partial meta objects #109

Merged
merged 1 commit into from
Oct 3, 2024
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
7 changes: 4 additions & 3 deletions kubernetes/metadata/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package metadata

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

Expand Down Expand Up @@ -65,7 +66,7 @@ func (jb *job) GenerateECS(obj kubernetes.Resource) mapstr.M {

// GenerateK8s generates job metadata from a resource object
func (jb *job) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) mapstr.M {
_, ok := obj.(*kubernetes.Job)
_, ok := obj.(metav1.Object)
if !ok {
return nil
}
Expand All @@ -81,12 +82,12 @@ func (jb *job) GenerateFromName(name string, opts ...FieldOptions) mapstr.M {
}

if obj, ok, _ := jb.store.GetByKey(name); ok {
jobObj, ok := obj.(*kubernetes.Job)
res, ok := obj.(kubernetes.Resource)
if !ok {
return nil
}

return jb.GenerateK8s(jobObj, opts...)
return jb.GenerateK8s(res, opts...)
}

return nil
Expand Down
42 changes: 41 additions & 1 deletion kubernetes/metadata/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestJob_Generate(t *testing.T) {
client := k8sfake.NewSimpleClientset()
boolean := true
tests := []struct {
input kubernetes.Resource
input *batchv1.Job
output mapstr.M
name string
}{
Expand Down Expand Up @@ -92,6 +92,10 @@ func TestJob_Generate(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.output, metagen.Generate(test.input))
objMeta := &metav1.PartialObjectMetadata{
ObjectMeta: test.input.ObjectMeta,
}
assert.Equal(t, test.output, metagen.Generate(objMeta))
})
}
}
Expand Down Expand Up @@ -144,6 +148,42 @@ func TestJob_GenerateFromName(t *testing.T) {
"namespace": defaultNs,
},
},
{
name: "test simple object with owner",
input: &metav1.PartialObjectMetadata{
ObjectMeta: metav1.ObjectMeta{
Name: name,
UID: types.UID(uid),
Namespace: defaultNs,
Labels: map[string]string{
"foo": "bar",
},
Annotations: map[string]string{},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "apps",
Kind: "CronJob",
Name: "nginx-job",
UID: "005f3b90-4b9d-12f8-acf0-31020a840144",
Controller: &boolean,
},
},
},
},
output: mapstr.M{
"job": mapstr.M{
"name": name,
"uid": uid,
},
"labels": mapstr.M{
"foo": "bar",
},
"cronjob": mapstr.M{
"name": "nginx-job",
},
"namespace": defaultNs,
},
},
}

for _, test := range tests {
Expand Down
7 changes: 4 additions & 3 deletions kubernetes/metadata/replicaset.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package metadata

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

Expand Down Expand Up @@ -67,7 +68,7 @@ func (rs *replicaset) GenerateECS(obj kubernetes.Resource) mapstr.M {

// GenerateK8s generates replicaset metadata from a resource object
func (rs *replicaset) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) mapstr.M {
_, ok := obj.(*kubernetes.ReplicaSet)
_, ok := obj.(metav1.Object)
if !ok {
return nil
}
Expand All @@ -83,12 +84,12 @@ func (rs *replicaset) GenerateFromName(name string, opts ...FieldOptions) mapstr
}

if obj, ok, _ := rs.store.GetByKey(name); ok {
replicaSet, ok := obj.(*kubernetes.ReplicaSet)
res, ok := obj.(kubernetes.Resource)
if !ok {
return nil
}

return rs.GenerateK8s(replicaSet, opts...)
return rs.GenerateK8s(res, opts...)
}

return nil
Expand Down
36 changes: 35 additions & 1 deletion kubernetes/metadata/replicaset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestReplicaset_Generate(t *testing.T) {
client := k8sfake.NewSimpleClientset()
boolean := true
tests := []struct {
input kubernetes.Resource
input *appsv1.ReplicaSet
output mapstr.M
name string
}{
Expand Down Expand Up @@ -111,6 +111,11 @@ func TestReplicaset_Generate(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.output, metagen.Generate(test.input))
// verify this works with just the metadata
objMeta := &metav1.PartialObjectMetadata{
ObjectMeta: test.input.ObjectMeta,
}
assert.Equal(t, test.output, metagen.Generate(objMeta))
})
}
}
Expand Down Expand Up @@ -181,6 +186,35 @@ func TestReplicase_GenerateFromName(t *testing.T) {
"namespace": defaultNs,
},
},
{
name: "test simple object with owner",
input: &metav1.PartialObjectMetadata{
ObjectMeta: metav1.ObjectMeta{
Name: "nginx-rs",
Namespace: defaultNs,
UID: uid,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "apps",
Kind: "Deployment",
Name: "nginx-deployment",
UID: "005f3b90-4b9d-12f8-acf0-31020a840144",
Controller: &boolean,
},
},
},
},
output: mapstr.M{
"replicaset": mapstr.M{
"name": "nginx-rs",
"uid": uid,
},
"deployment": mapstr.M{
"name": "nginx-deployment",
},
"namespace": defaultNs,
},
},
}

for _, test := range tests {
Expand Down
5 changes: 3 additions & 2 deletions kubernetes/metadata/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import (
"regexp"
"strings"

"github.com/elastic/elastic-agent-autodiscover/kubernetes"
"k8s.io/apimachinery/pkg/api/meta"

k8s "k8s.io/client-go/kubernetes"

"github.com/elastic/elastic-agent-autodiscover/kubernetes"
"github.com/elastic/elastic-agent-autodiscover/utils"
"github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/mapstr"
Expand Down Expand Up @@ -85,7 +86,7 @@ func (r *Resource) Generate(kind string, obj kubernetes.Resource, opts ...FieldO
}

// GenerateECS generates ECS metadata from a resource object
func (r *Resource) GenerateECS(obj kubernetes.Resource) mapstr.M {
func (r *Resource) GenerateECS(_ kubernetes.Resource) mapstr.M {
ecsMeta := mapstr.M{}
if r.clusterInfo.URL != "" {
_, _ = ecsMeta.Put("orchestrator.cluster.url", r.clusterInfo.URL)
Expand Down
Loading