-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trim unneeded fields stored in informers to reduce memory footprint
Unused fields stored in informers can occupy significant memory usage. The informer provides a transformer func to allow mutate the objects before they are stored, via which we can trim unneeded fields. In general, managedFields is never used for all types of objects. In addition, we trim some unneeded fields from Pod objects given their unused fields take the most considerable space. It can reduce the memory usage of 1k simple Pod by approximately 5m. Signed-off-by: Quan Tian <[email protected]>
- Loading branch information
Showing
4 changed files
with
205 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2024 Antrea Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package k8s | ||
|
||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
"k8s.io/client-go/tools/cache" | ||
) | ||
|
||
// NewTrimmer returns a cache.TransformFunc that can be used to trim objects stored in informers. | ||
// The function must be idempotent before client-go v0.31.0 to avoid a race condition happening when objects were | ||
// accessed during Resync operation, see https://github.com/kubernetes/kubernetes/issues/124337. | ||
// But it's generally more efficient to avoid trimming the same object more than once. | ||
func NewTrimmer(extraTrimmers ...cache.TransformFunc) cache.TransformFunc { | ||
return func(obj interface{}) (interface{}, error) { | ||
accessor, err := meta.Accessor(obj) | ||
if err != nil { | ||
return obj, nil | ||
} | ||
// It means the objects has been trimmed. | ||
if accessor.GetManagedFields() == nil { | ||
return obj, nil | ||
} | ||
// Trim common fields for all objects. | ||
accessor.SetManagedFields(nil) | ||
|
||
// Trim type specific fields for each type. | ||
for _, trimmer := range extraTrimmers { | ||
trimmer(obj) | ||
} | ||
return obj, nil | ||
} | ||
} | ||
|
||
func TrimPod(obj interface{}) (interface{}, error) { | ||
pod, ok := obj.(*corev1.Pod) | ||
if !ok { | ||
return obj, nil | ||
} | ||
|
||
pod.OwnerReferences = nil | ||
pod.Spec.Volumes = nil | ||
pod.Spec.InitContainers = nil | ||
for i := range pod.Spec.Containers { | ||
container := &pod.Spec.Containers[i] | ||
container.Command = nil | ||
container.Args = nil | ||
container.EnvFrom = nil | ||
container.Env = nil | ||
container.VolumeMounts = nil | ||
container.VolumeDevices = nil | ||
container.LivenessProbe = nil | ||
container.ReadinessProbe = nil | ||
container.StartupProbe = nil | ||
container.Lifecycle = nil | ||
container.SecurityContext = nil | ||
} | ||
pod.Spec.EphemeralContainers = nil | ||
pod.Spec.Affinity = nil | ||
pod.Spec.Tolerations = nil | ||
pod.Spec.ResourceClaims = nil | ||
|
||
pod.Status.Conditions = nil | ||
pod.Status.StartTime = nil | ||
pod.Status.InitContainerStatuses = nil | ||
pod.Status.ContainerStatuses = nil | ||
pod.Status.EphemeralContainerStatuses = nil | ||
pod.Status.ResourceClaimStatuses = nil | ||
return pod, nil | ||
} |
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,112 @@ | ||
// Copyright 2024 Antrea Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package k8s | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/tools/cache" | ||
) | ||
|
||
func TestTrimK8sObject(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
trimmer cache.TransformFunc | ||
obj interface{} | ||
want interface{} | ||
}{ | ||
{ | ||
name: "pod", | ||
trimmer: NewTrimmer(TrimPod), | ||
obj: &corev1.Pod{ | ||
TypeMeta: metav1.TypeMeta{}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-pod", | ||
Namespace: "default", | ||
UID: "test-uid", | ||
OwnerReferences: []metav1.OwnerReference{ | ||
{ | ||
APIVersion: "apps/v1", | ||
Kind: "DaemonSet", | ||
Name: "test-daemonset", | ||
UID: "5a39d3c8-0f5f-4aad-94bf-315c4fe11320", | ||
}, | ||
}, | ||
ManagedFields: []metav1.ManagedFieldsEntry{ | ||
{ | ||
APIVersion: "v1", | ||
FieldsType: "FieldsV1", | ||
}, | ||
}, | ||
}, | ||
Spec: corev1.PodSpec{ | ||
InitContainers: []corev1.Container{{ | ||
Name: "container-0", | ||
}}, | ||
Containers: []corev1.Container{{ | ||
Name: "container-1", | ||
Command: []string{"foo", "bar"}, | ||
Args: []string{"--a=b"}, | ||
Env: []corev1.EnvVar{{Name: "foo", Value: "bar"}}, | ||
}}, | ||
NodeName: "nodeA", | ||
}, | ||
Status: corev1.PodStatus{ | ||
Conditions: []corev1.PodCondition{ | ||
{ | ||
Type: corev1.PodReady, | ||
Status: corev1.ConditionTrue, | ||
}, | ||
}, | ||
PodIP: "1.2.3.4", | ||
PodIPs: []corev1.PodIP{ | ||
{IP: "1.2.3.4"}, | ||
}, | ||
}, | ||
}, | ||
want: &corev1.Pod{ | ||
TypeMeta: metav1.TypeMeta{}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-pod", | ||
Namespace: "default", | ||
UID: "test-uid", | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{{ | ||
Name: "container-1", | ||
}}, | ||
NodeName: "nodeA", | ||
}, | ||
Status: corev1.PodStatus{ | ||
PodIP: "1.2.3.4", | ||
PodIPs: []corev1.PodIP{ | ||
{IP: "1.2.3.4"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := tt.trimmer(tt.obj) | ||
require.NoError(t, err) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} |