Skip to content

Commit 9f854da

Browse files
author
Mikołaj Świątek
authored
[processor/k8sattributes] Store only necessary ReplicaSet data (open-telemetry#23338)
1 parent 77d8c70 commit 9f854da

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use this changelog template to create an entry for release notes.
2+
# If your change doesn't affect end users, such as a test fix or a tooling change,
3+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
4+
5+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
6+
change_type: enhancement
7+
8+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
9+
component: k8sattributesprocessor
10+
11+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
12+
note: Store only necessary ReplicaSet data
13+
14+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
15+
issues: [23226]
16+
17+
# (Optional) One or more lines of additional information to render under the primary note.
18+
# These lines will be padded with 2 spaces and then inserted directly into the document.
19+
# Use pipe (|) for multiline entries.
20+
subtext:

processor/k8sattributesprocessor/internal/kube/client.go

+29
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ func New(logger *zap.Logger, apiCfg k8sconfig.APIConfig, rules ExtractionRules,
119119
return removeUnnecessaryPodData(originalPod, c.Rules), nil
120120
},
121121
)
122+
if err != nil {
123+
return nil, err
124+
}
122125

123126
if c.extractNamespaceLabelsAnnotations() {
124127
c.namespaceInformer = newNamespaceInformer(c.kc)
@@ -131,6 +134,19 @@ func New(logger *zap.Logger, apiCfg k8sconfig.APIConfig, rules ExtractionRules,
131134
newReplicaSetInformer = newReplicaSetSharedInformer
132135
}
133136
c.replicasetInformer = newReplicaSetInformer(c.kc, c.Filters.Namespace)
137+
err = c.replicasetInformer.SetTransform(
138+
func(object interface{}) (interface{}, error) {
139+
originalReplicaset, success := object.(*apps_v1.ReplicaSet)
140+
if !success { // means this is a cache.DeletedFinalStateUnknown, in which case we do nothing
141+
return object, nil
142+
}
143+
144+
return removeUnnecessaryReplicaSetData(originalReplicaset), nil
145+
},
146+
)
147+
if err != nil {
148+
return nil, err
149+
}
134150
}
135151

136152
return c, err
@@ -846,6 +862,19 @@ func (c *WatchClient) addOrUpdateReplicaSet(replicaset *apps_v1.ReplicaSet) {
846862
c.m.Unlock()
847863
}
848864

865+
// This function removes all data from the ReplicaSet except what is required by extraction rules
866+
func removeUnnecessaryReplicaSetData(replicaset *apps_v1.ReplicaSet) *apps_v1.ReplicaSet {
867+
transformedReplicaset := apps_v1.ReplicaSet{
868+
ObjectMeta: meta_v1.ObjectMeta{
869+
Name: replicaset.GetName(),
870+
Namespace: replicaset.GetNamespace(),
871+
UID: replicaset.GetUID(),
872+
},
873+
}
874+
transformedReplicaset.SetOwnerReferences(replicaset.GetOwnerReferences())
875+
return &transformedReplicaset
876+
}
877+
849878
func (c *WatchClient) getReplicaSet(uid string) (*ReplicaSet, bool) {
850879
c.m.RLock()
851880
replicaset, ok := c.ReplicaSets[uid]

processor/k8sattributesprocessor/internal/kube/client_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -847,10 +847,11 @@ func TestExtractionRules(t *testing.T) {
847847
t.Run(tc.name, func(t *testing.T) {
848848
c.Rules = tc.rules
849849

850-
// manually call the data removal function here
850+
// manually call the data removal functions here
851851
// normally the informer does this, but fully emulating the informer in this test is annoying
852852
transformedPod := removeUnnecessaryPodData(pod, c.Rules)
853-
c.handleReplicaSetAdd(replicaset)
853+
transformedReplicaset := removeUnnecessaryReplicaSetData(replicaset)
854+
c.handleReplicaSetAdd(transformedReplicaset)
854855
c.handlePodAdd(transformedPod)
855856
p, ok := c.GetPod(newPodIdentifier("connection", "", pod.Status.PodIP))
856857
require.True(t, ok)
@@ -1001,10 +1002,11 @@ func TestReplicaSetExtractionRules(t *testing.T) {
10011002
c.Rules = tc.rules
10021003
replicaset.OwnerReferences = tc.ownerReferences
10031004

1004-
// manually call the data removal function here
1005+
// manually call the data removal functions here
10051006
// normally the informer does this, but fully emulating the informer in this test is annoying
10061007
transformedPod := removeUnnecessaryPodData(pod, c.Rules)
1007-
c.handleReplicaSetAdd(replicaset)
1008+
transformedReplicaset := removeUnnecessaryReplicaSetData(replicaset)
1009+
c.handleReplicaSetAdd(transformedReplicaset)
10081010
c.handlePodAdd(transformedPod)
10091011
p, ok := c.GetPod(newPodIdentifier("connection", "", pod.Status.PodIP))
10101012
require.True(t, ok)

0 commit comments

Comments
 (0)