Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim images in Node objects to reduce memory footprint #6351

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/antrea-agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func run(o *Options) error {
}
k8s.OverrideKubeAPIServer(o.config.KubeAPIServerOverride)

informerFactory := informers.NewSharedInformerFactoryWithOptions(k8sClient, informerDefaultResync, informers.WithTransform(k8s.NewTrimmer()))
informerFactory := informers.NewSharedInformerFactoryWithOptions(k8sClient, informerDefaultResync, informers.WithTransform(k8s.NewTrimmer(k8s.TrimNode)))
crdInformerFactory := crdinformers.NewSharedInformerFactoryWithOptions(crdClient, informerDefaultResync, crdinformers.WithTransform(k8s.NewTrimmer()))
traceflowInformer := crdInformerFactory.Crd().V1beta1().Traceflows()
egressInformer := crdInformerFactory.Crd().V1beta1().Egresses()
Expand Down
11 changes: 11 additions & 0 deletions pkg/util/k8s/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,14 @@ func TrimPod(obj interface{}) (interface{}, error) {
pod.Status.ResourceClaimStatuses = nil
return pod, nil
}

// TrimNode clears unused fields from a Node that are not required by Antrea.
// It's safe to do so because Antrea only patches Node.
func TrimNode(obj interface{}) (interface{}, error) {
node, ok := obj.(*corev1.Node)
if !ok {
return obj, nil
}
node.Status.Images = nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is node.Status.NodeInfo safe to remove as well?
is it insignificant compared to node.Status.Images?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's safe to remove it. However, the field is not a pointer but a struct containing 10 strings, while each string is not too long. To trim them, the simplest code is to override it with a new struct, which could increase heap usage 160 bytes per object temporarily, whille overriding one field per line seems not very worth as each field is not long: the total data size in a typical nodeInfo is about 200 bytes.

  nodeInfo:
    architecture: amd64
    bootID: 56ae4e38-a8b1-4657-8b8a-86a75623926e
    containerRuntimeVersion: containerd://1.7.15
    kernelVersion: 5.4.0-169-generic
    kubeProxyVersion: v1.30.0
    kubeletVersion: v1.30.0
    machineID: d61012698a3649178b7f74d6f57fc965
    operatingSystem: linux
    osImage: Debian GNU/Linux 12 (bookworm)
    systemUUID: d43097b6-a686-420f-b033-fd5c15392e4d

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, thanks for the detailed answer

return node, nil
}
68 changes: 68 additions & 0 deletions pkg/util/k8s/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,74 @@ func TestTrimK8sObject(t *testing.T) {
},
},
},
{
name: "node",
trimmer: NewTrimmer(TrimNode),
obj: &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "test-node",
ManagedFields: []metav1.ManagedFieldsEntry{
{
APIVersion: "v1",
FieldsType: "FieldsV1",
},
},
},
Spec: corev1.NodeSpec{
PodCIDR: "10.0.0.0/24",
PodCIDRs: []string{
"10.0.0.0/24",
},
},
Status: corev1.NodeStatus{
Conditions: []corev1.NodeCondition{
{
Type: corev1.NodeReady,
Status: corev1.ConditionTrue,
Reason: "KubeletReady",
Message: "kubelet is posting ready status. AppArmor enabled",
},
},
Images: []corev1.ContainerImage{
{
Names: []string{
"registry.k8s.io/kube-proxy@sha256:a9f441a6b440c634ccfe62530ab1c7ff2ea7ed3f577f91f6a71c7e2f51256410",
"registry.k8s.io/kube-proxy:v1.26.15",
},
SizeBytes: 72051242,
},
{
Names: []string{
"registry.k8s.io/pause@sha256:3d380ca8864549e74af4b29c10f9cb0956236dfb01c40ca076fb6c37253234db",
"registry.k8s.io/pause:3.6",
},
SizeBytes: 682696,
},
},
},
},
want: &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "test-node",
},
Spec: corev1.NodeSpec{
PodCIDR: "10.0.0.0/24",
PodCIDRs: []string{
"10.0.0.0/24",
},
},
Status: corev1.NodeStatus{
Conditions: []corev1.NodeCondition{
{
Type: corev1.NodeReady,
Status: corev1.ConditionTrue,
Reason: "KubeletReady",
Message: "kubelet is posting ready status. AppArmor enabled",
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading