Skip to content

Commit

Permalink
Reduce kube client memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
anjmao committed Mar 6, 2024
1 parent b13c332 commit 52badb0
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 757 deletions.
8 changes: 7 additions & 1 deletion charts/kvisor/templates/agent.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ spec:
prometheus.io/scrape: "true"
prometheus.io/port: "{{.Values.agent.httpListenPort}}"
{{- end }}
{{- if .Values.agent.phlare.enabled }}
{{- if .Values.pyroscope.enabled }}
phlare.grafana.com/scrape: "true"
phlare.grafana.com/port: "{{ .Values.agent.httpListenPort }}"
profiles.grafana.com/memory.scrape: "true"
profiles.grafana.com/memory.port: "{{ .Values.agent.httpListenPort }}"
profiles.grafana.com/cpu.scrape: "true"
profiles.grafana.com/cpu.port: "{{ .Values.agent.httpListenPort }}"
profiles.grafana.com/goroutine.scrape: "true"
profiles.grafana.com/goroutine.port: "{{ .Values.agent.httpListenPort }}"
{{- end }}
{{- with .Values.agent.podAnnotations }}
{{- toYaml . | nindent 8 }}
Expand Down
8 changes: 7 additions & 1 deletion charts/kvisor/templates/controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ spec:
prometheus.io/scrape: "true"
prometheus.io/port: "{{.Values.controller.httpListenPort}}"
{{- end }}
{{- if .Values.controller.phlare.enabled }}
{{- if .Values.pyroscope.enabled }}
phlare.grafana.com/scrape: "true"
phlare.grafana.com/port: "{{ .Values.controller.httpListenPort }}"
profiles.grafana.com/memory.scrape: "true"
profiles.grafana.com/memory.port: "{{ .Values.controller.httpListenPort }}"
profiles.grafana.com/cpu.scrape: "true"
profiles.grafana.com/cpu.port: "{{ .Values.controller.httpListenPort }}"
profiles.grafana.com/goroutine.scrape: "true"
profiles.grafana.com/goroutine.port: "{{ .Values.controller.httpListenPort }}"
{{- end }}
{{- with .Values.controller.podAnnotations }}
{{- toYaml . | nindent 8 }}
Expand Down
9 changes: 3 additions & 6 deletions charts/kvisor/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ image:
# Overrides the image tag whose default is the chart appVersion.
tag: ""

pyroscope:
enabled: false

agent:
enabled: false

Expand Down Expand Up @@ -116,9 +119,6 @@ agent:
debug:
ebpf: false

phlare:
enabled: false

controller:

replicas: 1
Expand Down Expand Up @@ -186,9 +186,6 @@ controller:
persistentVolumePostgres:
size: 20Gi

phlare:
enabled: false

eventGenerator:
enabled: false
image:
Expand Down
22 changes: 15 additions & 7 deletions cmd/agent/kube/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,34 @@ import (
corev1 "k8s.io/api/core/v1"
)

// addObjectMeta adds missing metadata since kubernetes client removes object kind and api version information.
func addObjectMeta(o kubernetesObject) {
func transformObject(obj kubernetesObject) {
obj.SetManagedFields(nil)
obj.SetAnnotations(nil)
obj.SetLabels(nil)

appsV1 := "apps/v1"
v1 := "v1"
switch o := o.(type) {
switch o := obj.(type) {
case *appsv1.ReplicaSet:
o.Kind = "ReplicaSet"
o.APIVersion = appsV1
o.Spec = appsv1.ReplicaSetSpec{}
o.Status = appsv1.ReplicaSetStatus{}
case *corev1.Pod:
o.Kind = "Pod"
o.APIVersion = v1
o.Spec = corev1.PodSpec{}
o.Status = corev1.PodStatus{}
case *batchv1.Job:
o.Kind = "Job"
o.APIVersion = "batch/v1"
o.Spec = batchv1.JobSpec{}
o.Status = batchv1.JobStatus{}
case *batchv1.CronJob:
o.Kind = "CronJob"
o.APIVersion = "batch/v1"
o.Spec = batchv1.CronJobSpec{}
o.Status = batchv1.CronJobStatus{}
}
}

Expand All @@ -34,10 +45,7 @@ func informerTransformer(i any) (any, error) {
return nil, errors.New("unsupported object")
}

// Add missing metadata which is removed by k8s.
addObjectMeta(obj)
// Remove managed fields since we don't need them. This should decrease memory usage.
obj.SetManagedFields(nil)
transformObject(obj)

return obj, nil
}
9 changes: 1 addition & 8 deletions cmd/controller/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,29 +103,22 @@ func (a *App) Run(ctx context.Context) error {

defer castaiClient.Close()

ipInventory := kube.NewIPInventory()

// Setup kubernetes client and watcher.
informersFactory := informers.NewSharedInformerFactory(clientset, 0)
k8sVersion, err := kube.GetVersion(clientset)
if err != nil {
return err
}
kubeClient := kube.NewClient(log, cfg.AgentDaemonSetName, cfg.PodNamespace, k8sVersion, clientset, ipInventory)
kubeClient := kube.NewClient(log, cfg.AgentDaemonSetName, cfg.PodNamespace, k8sVersion, clientset)
kubeClient.RegisterHandlers(informersFactory)

castaiCtrl := state.NewCastaiController(log, cfg.CastaiController, kubeClient, castaiClient)

ingestorCtrl := state.NewIngestorController(log, kubeClient, ipInventory, castaiCtrl)

// Run all components.
errg, ctx := errgroup.WithContext(ctx)
errg.Go(func() error {
return kubeClient.Run(ctx)
})
errg.Go(func() error {
return ingestorCtrl.Run(ctx, kubeClient.GetIssuesChan())
})
errg.Go(func() error {
return castaiCtrl.Run(ctx)
})
Expand Down
Loading

0 comments on commit 52badb0

Please sign in to comment.