@@ -16,27 +16,28 @@ import (
1616type flowIPDecoration struct {
1717 serviceName string
1818 serviceNamespace string
19- resourceAttrs map [attr.Name ]string
2019}
2120
22- // DynamicFlowAttrs maps dynamically selected application IPs to service identity and resource
23- // attributes for NetO11y and StatsO11y decoration.
21+ // DynamicFlowAttrs maps dynamically selected application IPs to service identity for NetO11y
22+ // and StatsO11y decoration.
2423type DynamicFlowAttrs struct {
2524 multiSel MultiSignalPIDSelector
2625 signalSel PIDSelector
2726 store * kube.Store
2827
29- mu sync.RWMutex
30- ipDecor map [string ]flowIPDecoration
28+ mu sync.RWMutex
29+ ipDecor map [string ]flowIPDecoration
30+ registeredPIDs map [app.PID ]struct {}
3131}
3232
3333// NewDynamicFlowAttrs creates a tracker for the given signal view and optional Kubernetes store.
3434func NewDynamicFlowAttrs (multiSel MultiSignalPIDSelector , signalSel PIDSelector , store * kube.Store ) * DynamicFlowAttrs {
3535 return & DynamicFlowAttrs {
36- multiSel : multiSel ,
37- signalSel : signalSel ,
38- store : store ,
39- ipDecor : map [string ]flowIPDecoration {},
36+ multiSel : multiSel ,
37+ signalSel : signalSel ,
38+ store : store ,
39+ ipDecor : map [string ]flowIPDecoration {},
40+ registeredPIDs : map [app.PID ]struct {}{},
4041 }
4142}
4243
@@ -83,31 +84,40 @@ func (d *DynamicFlowAttrs) loopAttrs(ctx context.Context) {
8384
8485func (d * DynamicFlowAttrs ) rebuild () {
8586 pids , ok := d .signalSel .GetPIDs ()
86- if ! ok {
87- d .mu .Lock ()
88- d .ipDecor = map [string ]flowIPDecoration {}
89- d .mu .Unlock ()
90- return
91- }
9287
9388 next := map [string ]flowIPDecoration {}
94- for _ , pid := range pids {
95- entry , ok := d .multiSel .GetPID (uint32 (pid ))
96- if ! ok {
97- continue
98- }
99- dec := decorationFromEntry (entry )
100- if dec .isEmpty () {
101- continue
102- }
103- for _ , ip := range ResolveContainerIPs (d .store , pid ) {
104- next [ip ] = dec
89+ storeRegisteredPIDs := map [app.PID ]struct {}{}
90+ if ok && d .store != nil {
91+ for _ , pid := range pids {
92+ entry , found := d .multiSel .GetPID (uint32 (pid ))
93+ if ! found {
94+ continue
95+ }
96+ dec := decorationFromEntry (entry )
97+ if dec .isEmpty () {
98+ continue
99+ }
100+ for _ , ip := range ResolveContainerIPs (d .store , pid ) {
101+ next [ip ] = dec
102+ }
103+ storeRegisteredPIDs [pid ] = struct {}{}
105104 }
106105 }
107106
108107 d .mu .Lock ()
108+ defer d .mu .Unlock ()
109+ for pid := range d .registeredPIDs {
110+ if _ , still := storeRegisteredPIDs [pid ]; ! still {
111+ if d .store != nil {
112+ d .store .DeleteProcess (pid )
113+ }
114+ delete (d .registeredPIDs , pid )
115+ }
116+ }
117+ for pid := range storeRegisteredPIDs {
118+ d .registeredPIDs [pid ] = struct {}{}
119+ }
109120 d .ipDecor = next
110- d .mu .Unlock ()
111121}
112122
113123func (d * DynamicFlowAttrs ) Apply (a * pipe.CommonAttrs ) {
@@ -133,21 +143,14 @@ func (d *DynamicFlowAttrs) Apply(a *pipe.CommonAttrs) {
133143}
134144
135145func decorationFromEntry (entry DynamicPIDEntry ) flowIPDecoration {
136- dec := flowIPDecoration {
146+ return flowIPDecoration {
137147 serviceName : entry .ServiceName ,
138148 serviceNamespace : entry .ServiceNamespace ,
139149 }
140- if len (entry .ResourceAttributes ) > 0 {
141- dec .resourceAttrs = make (map [attr.Name ]string , len (entry .ResourceAttributes ))
142- for k , v := range entry .ResourceAttributes {
143- dec .resourceAttrs [attr .Name (k )] = v
144- }
145- }
146- return dec
147150}
148151
149152func (d flowIPDecoration ) isEmpty () bool {
150- return d .serviceName == "" && d .serviceNamespace == "" && len ( d . resourceAttrs ) == 0
153+ return d .serviceName == "" && d .serviceNamespace == ""
151154}
152155
153156func applyFlowDecoration (dec flowIPDecoration , a * pipe.CommonAttrs , src bool ) {
@@ -158,15 +161,12 @@ func applyFlowDecoration(dec flowIPDecoration, a *pipe.CommonAttrs, src bool) {
158161 if dec .serviceNamespace != "" {
159162 a .Metadata [attr .ServiceNamespace ] = dec .serviceNamespace
160163 }
161- } else {
162- if dec .serviceName != "" {
163- a .Metadata [attr .ServicePeerName ] = dec .serviceName
164- }
165- if dec .serviceNamespace != "" {
166- a .Metadata [attr .ServicePeerNamespace ] = dec .serviceNamespace
167- }
164+ return
165+ }
166+ if dec .serviceName != "" {
167+ a .Metadata [attr .ServicePeerName ] = dec .serviceName
168168 }
169- for k , v := range dec . resourceAttrs {
170- a .Metadata [k ] = v
169+ if dec . serviceNamespace != "" {
170+ a .Metadata [attr . ServicePeerNamespace ] = dec . serviceNamespace
171171 }
172172}
0 commit comments