Skip to content

Commit

Permalink
Fix network data
Browse files Browse the repository at this point in the history
  • Loading branch information
mariomac committed Sep 26, 2024
1 parent 69188fd commit ed144b7
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 41 deletions.
6 changes: 3 additions & 3 deletions pkg/internal/discover/watcher_kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@ func withMetadata(pp processAttrs, info *kube.PodInfo) processAttrs {

if info.Owner != nil {
ret.metadata[attr.Name(info.Owner.LabelName).Prom()] = info.Owner.Name
topName, topLabel := info.Owner.TopOwnerNameLabel()
ret.metadata[attr.Name(topLabel).Prom()] = topName
ret.metadata[services.AttrOwnerName] = topName
topOwner := info.Owner.TopOwner()
ret.metadata[attr.Name(topOwner.LabelName).Prom()] = topOwner.Name
ret.metadata[services.AttrOwnerName] = topOwner.Name
}
return ret
}
4 changes: 2 additions & 2 deletions pkg/internal/kube/informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ func (k *Metadata) AddNodeEventHandler(h cache.ResourceEventHandler) error {
}

func (i *PodInfo) ServiceName() string {
if on, _ := i.Owner.TopOwnerNameLabel(); on != "" {
return on
if to := i.Owner.TopOwner(); to != nil {
return to.Name
}
return i.Name
}
36 changes: 18 additions & 18 deletions pkg/internal/kube/owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,30 @@ func unrecognizedOwner(or *metav1.OwnerReference) *Owner {
}
}

// TopOwnerNameLabel returns the top-level name and metadata label in the owner chain.
// TopOwner returns the top Owner in the owner chain.
// For example, if the owner is a ReplicaSet, it will return the Deployment name.
func (o *Owner) TopOwnerNameLabel() (string, OwnerLabel) {
if o == nil {
return "", ""
}
if o.LabelName == OwnerReplicaSet {
// we have two levels of ownership at most
if o.Owner != nil {
return o.Owner.Name, o.Owner.LabelName
}
// if the replicaset informer is disabled, we can't get the owner deployment,
// so we will heuristically extract it from the ReplicaSet Name (and cache it)
topOwnerName := o.Name
if idx := strings.LastIndexByte(topOwnerName, '-'); idx > 0 {
topOwnerName = topOwnerName[:idx]
func (o *Owner) TopOwner() *Owner {
// we have two levels of ownership at most
if o != nil && o.LabelName == OwnerReplicaSet && o.Owner == nil {
// we heuristically extract the Deployment name from the replicaset name
if idx := strings.LastIndexByte(o.Name, '-'); idx > 0 {
o.Owner = &Owner{
Name: topOwnerName,
Name: o.Name[:idx],
LabelName: OwnerDeployment,
Kind: "Deployment",
}
return topOwnerName, OwnerDeployment
} else {
// just caching the own replicaset as owner, in order to cache the result
o.Owner = o
}
return o.Owner
}

// just return the highest existing owner (two levels of ownership maximum)
if o == nil || o.Owner == nil {
return o
}
return o.Name, o.LabelName
return o.Owner
}

func (o *Owner) String() string {
Expand Down
34 changes: 20 additions & 14 deletions pkg/internal/kube/owner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,33 @@ func TestTopOwnerLabel(t *testing.T) {
type testCase struct {
expectedLabel OwnerLabel
expectedName string
expectedKind string
owner *Owner
}
for _, tc := range []testCase{
{owner: nil},
{expectedLabel: OwnerDaemonSet, expectedName: "ds",
owner: &Owner{LabelName: OwnerDaemonSet, Name: "ds"}},
{expectedLabel: OwnerDeployment, expectedName: "rs-without-dep-meta",
owner: &Owner{LabelName: OwnerReplicaSet, Name: "rs-without-dep-meta-34fb1fa3a"}},
{expectedLabel: OwnerDeployment, expectedName: "dep",
owner: &Owner{LabelName: OwnerReplicaSet, Name: "dep-34fb1fa3a",
Owner: &Owner{LabelName: OwnerDeployment, Name: "dep"}}},
{expectedLabel: OwnerDaemonSet, expectedName: "ds", expectedKind: "DaemonSet",
owner: &Owner{LabelName: OwnerDaemonSet, Name: "ds", Kind: "DaemonSet"}},
{expectedLabel: OwnerDeployment, expectedName: "rs-without-dep-meta", expectedKind: "Deployment",
owner: &Owner{LabelName: OwnerReplicaSet, Name: "rs-without-dep-meta-34fb1fa3a", Kind: "ReplicaSet"}},
{expectedLabel: OwnerDeployment, expectedName: "dep", expectedKind: "Deployment",
owner: &Owner{LabelName: OwnerReplicaSet, Name: "dep-34fb1fa3a", Kind: "ReplicaSet",
Owner: &Owner{LabelName: OwnerDeployment, Name: "dep", Kind: "Deployment"}}},
} {
t.Run(tc.expectedName, func(t *testing.T) {
name, label := tc.owner.TopOwnerNameLabel()
assert.Equal(t, tc.expectedName, name)
assert.Equal(t, tc.expectedLabel, label)
topOwner := tc.owner.TopOwner()
assert.Equal(t, tc.expectedName, topOwner.Name)
assert.Equal(t, tc.expectedLabel, topOwner.LabelName)
assert.Equal(t, tc.expectedKind, topOwner.Kind)

// check that the output is consistent (e.g. after ReplicaSet owner data is cached)
name, label = tc.owner.TopOwnerNameLabel()
assert.Equal(t, tc.expectedName, name)
assert.Equal(t, tc.expectedLabel, label)
topOwner = tc.owner.TopOwner()
assert.Equal(t, tc.expectedName, topOwner.Name)
assert.Equal(t, tc.expectedLabel, topOwner.LabelName)
assert.Equal(t, tc.expectedKind, topOwner.Kind)
})
}
}

func TestTopOwner_Nil(t *testing.T) {
assert.Nil(t, (*Owner)(nil).TopOwner())
}
5 changes: 3 additions & 2 deletions pkg/internal/netolly/transform/k8s/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,12 @@ func (n *decorator) decorate(flow *ebpf.Record, prefix, ip string) bool {
}
return false
}
topOwner := ipinfo.Owner.TopOwner()
flow.Attrs.Metadata[attr.Name(prefix+attrSuffixNs)] = meta.Namespace
flow.Attrs.Metadata[attr.Name(prefix+attrSuffixName)] = meta.Name
flow.Attrs.Metadata[attr.Name(prefix+attrSuffixType)] = ipinfo.Kind
flow.Attrs.Metadata[attr.Name(prefix+attrSuffixOwnerName)] = ipinfo.Owner.Name
flow.Attrs.Metadata[attr.Name(prefix+attrSuffixOwnerType)] = ipinfo.Owner.Kind
flow.Attrs.Metadata[attr.Name(prefix+attrSuffixOwnerName)] = topOwner.Name
flow.Attrs.Metadata[attr.Name(prefix+attrSuffixOwnerType)] = topOwner.Kind
if ipinfo.HostIP != "" {
flow.Attrs.Metadata[attr.Name(prefix+attrSuffixHostIP)] = ipinfo.HostIP
if ipinfo.HostName != "" {
Expand Down
4 changes: 2 additions & 2 deletions pkg/transform/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ func (md *metadataDecorator) appendMetadata(span *request.Span, info *kube.PodIn
}
if info.Owner != nil {
span.ServiceID.Metadata[attr.Name(info.Owner.LabelName)] = info.Owner.Name
topName, topLabel := info.Owner.TopOwnerNameLabel()
span.ServiceID.Metadata[attr.Name(topLabel)] = topName
topOwner := info.Owner.TopOwner()
span.ServiceID.Metadata[attr.Name(topOwner.LabelName)] = topOwner.Name
}
// override hostname by the Pod name
span.ServiceID.HostName = info.Name
Expand Down

0 comments on commit ed144b7

Please sign in to comment.