Skip to content

Commit

Permalink
lower memory usage
Browse files Browse the repository at this point in the history
We keep track of of all Pods in the cluster, while only caring about a small
subset of them. Since we only need very basic information from the Pod, we
can lower memory usage by ensuring we only store information we actually
care about.

Signed-off-by: Moritz Wanzenböck <[email protected]>
  • Loading branch information
WanzenBug committed Oct 16, 2024
1 parent 6284a82 commit 25274b2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Filter Pod objects before storing in cache. This lowers memory usage of the HA Controller Pods.

## [1.2.1] - 2024-03-28

### Fixed
Expand Down
8 changes: 8 additions & 0 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"k8s.io/client-go/tools/events"
"k8s.io/klog/v2"

"github.com/piraeusdatastore/piraeus-ha-controller/pkg/cleaners"
"github.com/piraeusdatastore/piraeus-ha-controller/pkg/indexers"
"github.com/piraeusdatastore/piraeus-ha-controller/pkg/metadata"
)
Expand Down Expand Up @@ -135,7 +136,14 @@ func NewAgent(opt *Options) (*agent, error) {

klog.V(2).Info("setting up Pod informer")

requirements, _ := opt.SatellitePodSelector.Requirements()
podInformer := informerFactory.Core().V1().Pods().Informer()
err = podInformer.SetTransform(func(o any) (any, error) {
return cleaners.CleanPod(o.(*corev1.Pod), requirements), nil
})
if err != nil {
return nil, err
}

err = podInformer.AddIndexers(map[string]cache.IndexFunc{
PodByPersistentVolumeClaimIndex: indexers.Gen(func(obj *corev1.Pod) ([]string, error) {
Expand Down
48 changes: 48 additions & 0 deletions pkg/cleaners/pods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cleaners

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"

"github.com/piraeusdatastore/piraeus-ha-controller/pkg/metadata"
)

func CleanPod(pod *corev1.Pod, labelRequirements labels.Requirements) *corev1.Pod {
neededLabels := map[string]string{}
for i := range labelRequirements {
k := labelRequirements[i].Key()
if v, ok := pod.Labels[k]; ok {
neededLabels[k] = v
}
}

neededAnnotations := map[string]string{}
if v, ok := pod.Annotations[metadata.AnnotationIgnoreFailOver]; ok {
neededAnnotations[metadata.AnnotationIgnoreFailOver] = v
}

return &corev1.Pod{
TypeMeta: pod.TypeMeta,
ObjectMeta: metav1.ObjectMeta{
Name: pod.Name,
Namespace: pod.Namespace,
UID: pod.UID,
ResourceVersion: pod.ResourceVersion,
Labels: neededLabels,
Annotations: neededAnnotations,
OwnerReferences: pod.OwnerReferences,
DeletionTimestamp: pod.DeletionTimestamp,
},
Spec: corev1.PodSpec{
// Used in "agent", "failover", "force io error pods" and "suspeded pod termination".
NodeName: pod.Spec.NodeName,
// Used in "agent" and "failover".
Volumes: pod.Spec.Volumes,
},
Status: corev1.PodStatus{
// Used in "failover" and "suspended pod termination".
Phase: pod.Status.Phase,
},
}
}

0 comments on commit 25274b2

Please sign in to comment.