-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[receiver/k8s_cluster] Do not store unused service data in k8s API ca…
…che (#23434) To reduce RAM utilization. Resolves #23433 This is the last PR to reduce the memory footprint of k8s API informers. Other objects are more static and usually don't have a lot of instances in user clusters, so there is no need to reduce their size.
- Loading branch information
1 parent
88437ba
commit d03519e
Showing
5 changed files
with
119 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package service // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/service" | ||
import ( | ||
"fmt" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/client-go/tools/cache" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/constants" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/metadata" | ||
) | ||
|
||
// Transform transforms the pod to remove the fields that we don't use to reduce RAM utilization. | ||
// IMPORTANT: Make sure to update this function before using new service fields. | ||
func Transform(service *corev1.Service) *corev1.Service { | ||
return &corev1.Service{ | ||
ObjectMeta: metadata.TransformObjectMeta(service.ObjectMeta), | ||
Spec: corev1.ServiceSpec{ | ||
Selector: service.Spec.Selector, | ||
}, | ||
} | ||
} | ||
|
||
// GetPodServiceTags returns a set of services associated with the pod. | ||
func GetPodServiceTags(pod *corev1.Pod, services cache.Store) map[string]string { | ||
properties := map[string]string{} | ||
|
||
for _, ser := range services.List() { | ||
serObj := ser.(*corev1.Service) | ||
if serObj.Namespace == pod.Namespace && | ||
labels.Set(serObj.Spec.Selector).AsSelectorPreValidated().Matches(labels.Set(pod.Labels)) { | ||
properties[fmt.Sprintf("%s%s", constants.K8sServicePrefix, serObj.Name)] = "" | ||
} | ||
} | ||
|
||
return properties | ||
} |
55 changes: 55 additions & 0 deletions
55
receiver/k8sclusterreceiver/internal/service/service_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package service | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestTransform(t *testing.T) { | ||
originalService := &corev1.Service{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "my-service", | ||
Namespace: "default", | ||
Labels: map[string]string{ | ||
"app": "my-app", | ||
}, | ||
Annotations: map[string]string{ | ||
"annotation1": "value1", | ||
}, | ||
}, | ||
Spec: corev1.ServiceSpec{ | ||
Selector: map[string]string{ | ||
"app": "my-app", | ||
}, | ||
Ports: []corev1.ServicePort{ | ||
{ | ||
Name: "http", | ||
Port: 80, | ||
Protocol: corev1.ProtocolTCP, | ||
}, | ||
}, | ||
Type: corev1.ServiceTypeClusterIP, | ||
}, | ||
} | ||
wantService := &corev1.Service{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "my-service", | ||
Namespace: "default", | ||
Labels: map[string]string{ | ||
"app": "my-app", | ||
}, | ||
}, | ||
Spec: corev1.ServiceSpec{ | ||
Selector: map[string]string{ | ||
"app": "my-app", | ||
}, | ||
}, | ||
} | ||
assert.EqualValues(t, wantService, Transform(originalService)) | ||
} |