From 1eefbe0c7b5515b45e2dc71cd543ed2c0a83c476 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paolo=20Chil=C3=A0?= Date: Tue, 18 Feb 2025 17:08:17 +0100 Subject: [PATCH] Add statePersistence value to helm chart presets (#6803) * Add statePersistence value to helm chart presets --- .../examples/eck/rendered/manifest.yaml | 5 +- .../netflow-service/rendered/manifest.yaml | 4 +- .../rendered/manifest.yaml | 4 +- .../user-cluster-role/rendered/manifest.yaml | 4 +- .../templates/agent/_helpers.tpl | 69 +++++++++++++++++++ .../templates/agent/eck/_pod_template.yaml | 9 +-- .../templates/agent/k8s/_pod_template.yaml | 39 ++--------- deploy/helm/elastic-agent/values.schema.json | 9 +++ deploy/helm/elastic-agent/values.yaml | 8 +-- 9 files changed, 98 insertions(+), 53 deletions(-) diff --git a/deploy/helm/elastic-agent/examples/eck/rendered/manifest.yaml b/deploy/helm/elastic-agent/examples/eck/rendered/manifest.yaml index d033d5fb094..d753d27c45c 100644 --- a/deploy/helm/elastic-agent/examples/eck/rendered/manifest.yaml +++ b/deploy/helm/elastic-agent/examples/eck/rendered/manifest.yaml @@ -1033,6 +1033,10 @@ spec: - hostPath: path: /var/log name: varlog + - hostPath: + path: /etc/elastic-agent/default/agent-pernode-example/state + type: DirectoryOrCreate + name: agent-data --- # Source: elastic-agent/templates/agent/eck/deployment.yaml apiVersion: agent.k8s.elastic.co/v1alpha1 @@ -1093,7 +1097,6 @@ spec: privileged: false runAsGroup: 1000 runAsUser: 1000 - volumeMounts: null dnsPolicy: ClusterFirstWithHostNet nodeSelector: kubernetes.io/os: linux diff --git a/deploy/helm/elastic-agent/examples/netflow-service/rendered/manifest.yaml b/deploy/helm/elastic-agent/examples/netflow-service/rendered/manifest.yaml index d324b371dae..8ed9f874e48 100644 --- a/deploy/helm/elastic-agent/examples/netflow-service/rendered/manifest.yaml +++ b/deploy/helm/elastic-agent/examples/netflow-service/rendered/manifest.yaml @@ -158,9 +158,7 @@ spec: subPath: agent.yml dnsPolicy: ClusterFirstWithHostNet volumes: - - hostPath: - path: /etc/elastic-agent/default/agent-netflow-example/state - type: DirectoryOrCreate + - emptyDir: {} name: agent-data - name: config secret: diff --git a/deploy/helm/elastic-agent/examples/nginx-custom-integration/rendered/manifest.yaml b/deploy/helm/elastic-agent/examples/nginx-custom-integration/rendered/manifest.yaml index 5c445c5e5be..88a00759d5e 100644 --- a/deploy/helm/elastic-agent/examples/nginx-custom-integration/rendered/manifest.yaml +++ b/deploy/helm/elastic-agent/examples/nginx-custom-integration/rendered/manifest.yaml @@ -107,9 +107,7 @@ spec: subPath: agent.yml dnsPolicy: ClusterFirstWithHostNet volumes: - - hostPath: - path: /etc/elastic-agent/default/agent-nginx-example/state - type: DirectoryOrCreate + - emptyDir: {} name: agent-data - name: config secret: diff --git a/deploy/helm/elastic-agent/examples/user-cluster-role/rendered/manifest.yaml b/deploy/helm/elastic-agent/examples/user-cluster-role/rendered/manifest.yaml index 6aef8be15d9..e559b52b54a 100644 --- a/deploy/helm/elastic-agent/examples/user-cluster-role/rendered/manifest.yaml +++ b/deploy/helm/elastic-agent/examples/user-cluster-role/rendered/manifest.yaml @@ -147,9 +147,7 @@ spec: dnsPolicy: ClusterFirstWithHostNet serviceAccountName: agent-nginx-example volumes: - - hostPath: - path: /etc/elastic-agent/default/agent-nginx-example/state - type: DirectoryOrCreate + - emptyDir: {} name: agent-data - name: config secret: diff --git a/deploy/helm/elastic-agent/templates/agent/_helpers.tpl b/deploy/helm/elastic-agent/templates/agent/_helpers.tpl index 563664f8866..c45971475c1 100644 --- a/deploy/helm/elastic-agent/templates/agent/_helpers.tpl +++ b/deploy/helm/elastic-agent/templates/agent/_helpers.tpl @@ -120,6 +120,18 @@ Validate and initialise the defined agent presets {{/* merge the default leader election with the leader election from the preset giving priority to the one from the preset */}} {{- $presetLeaderElection := mergeOverwrite dict $defaultLeaderElection ($presetProviders).kubernetes_leaderelection -}} {{- $_ := set $presetProviders "kubernetes_leaderelection" $presetLeaderElection -}} +{{/* set a sensible default to preset.statePersistence if no value is already present in the preset */}} +{{- if empty ($presetVal).statePersistence -}} +{{- if eq ($presetMode) "daemonset" -}} +{{- $_ := set $presetVal "statePersistence" "HostPath" -}} +{{- else if eq ($presetMode) "deployment" -}} +{{- $_ := set $presetVal "statePersistence" "EmptyDir" -}} +{{- else if eq ($presetMode) "statefulset" -}} +{{- $_ := set $presetVal "statePersistence" "EmptyDir" -}} +{{- else -}} +fail printf "Unsupported mode %v for preset %v" ($presetMode) $($presetName) +{{- end -}} +{{- end -}} {{- end -}} {{- end -}} @@ -323,3 +335,60 @@ app.kubernetes.io/version: {{ .Values.agent.version}} {{- end -}} {{- $_ := set $preset "outputs" $presetOutputs -}} {{- end -}} + +{{/* +Render a yaml with volumes and volumeMounts keys, rendering state volumes and extra volumes and their respective mounts +*/}} +{{- define "elasticagent.preset.render.volumes" -}} +{{- $ := index . 0 -}} +{{- $presetVal := index . 1 -}} +{{- $agentName := index . 2 -}} +volumes: +{{- $definedAgentStateVolume := false -}} + {{- with ($presetVal).extraVolumes }} + {{- . | toYaml | nindent 2 }} + {{- range $idx, $volume := . -}} + {{- if eq $definedAgentStateVolume false -}} + {{- if eq ($volume).name "agent-data" -}} + {{- $definedAgentStateVolume = true}} + {{- end -}} + {{- end -}} + {{- end -}} + {{- end }} + {{- if ne ($presetVal).statePersistence "None" }} + {{- if eq $definedAgentStateVolume false }} + - name: agent-data + {{- if eq ($presetVal).statePersistence "HostPath" }} + hostPath: + {{- if eq $.Values.agent.fleet.enabled true }} + {{/* different state hostPath for managed agents */}} + path: /etc/elastic-agent/{{$.Release.Namespace}}/{{$agentName}}-managed/state + {{- else }} + {{/* different state hostPath for standalone agents */}} + path: /etc/elastic-agent/{{$.Release.Namespace}}/{{$agentName}}/state + {{- end }} + type: DirectoryOrCreate + {{- else if eq ($presetVal).statePersistence "EmptyDir" }} + emptyDir: {} + {{- end }} + {{- end }} + {{- end }} +volumeMounts: + {{- $definedAgentStateVolumeMount := false -}} + {{- with ($presetVal).extraVolumeMounts }} + {{- . | toYaml | nindent 2}} + {{- range $idx, $volumeMount := . -}} + {{- if eq $definedAgentStateVolumeMount false -}} + {{- if eq ($volumeMount).name "agent-data" -}} + {{- $definedAgentStateVolumeMount = true}} + {{- end -}} + {{- end -}} + {{- end -}} + {{- end }} + {{- if ne ($presetVal).statePersistence "None" }} + {{- if eq $definedAgentStateVolumeMount false }} + - name: agent-data + mountPath: /usr/share/elastic-agent/state + {{- end }} + {{- end }} +{{- end -}} \ No newline at end of file diff --git a/deploy/helm/elastic-agent/templates/agent/eck/_pod_template.yaml b/deploy/helm/elastic-agent/templates/agent/eck/_pod_template.yaml index 4b7e4b8b295..0848686f059 100644 --- a/deploy/helm/elastic-agent/templates/agent/eck/_pod_template.yaml +++ b/deploy/helm/elastic-agent/templates/agent/eck/_pod_template.yaml @@ -2,6 +2,7 @@ {{- $ := index . 0 -}} {{- $presetVal := index . 1 -}} {{- $agentName := index . 2 }} +{{- $agentVolumes := (include "elasticagent.preset.render.volumes" (list $ $presetVal $agentName) | fromYaml) -}} apiVersion: v1 kind: PodTemplate template: @@ -39,10 +40,10 @@ template: topologySpreadConstraints: {{- . | toYaml | nindent 6 }} {{- end }} + {{- with ($agentVolumes).volumes }} volumes: - {{- with ($presetVal).extraVolumes }} {{- . | toYaml | nindent 6 }} - {{- end }} + {{- end }} {{- with ($presetVal).initContainers }} initContainers: {{- . | toYaml | nindent 6 }} @@ -90,10 +91,10 @@ template: {{- end }} {{- end }} {{- end }} + {{- with ($presetVal).extraVolumeMounts }} volumeMounts: - {{- with ($presetVal).extraVolumeMounts }} {{- . | toYaml | nindent 10 }} - {{- end }} + {{- end }} env: - name: NODE_NAME valueFrom: diff --git a/deploy/helm/elastic-agent/templates/agent/k8s/_pod_template.yaml b/deploy/helm/elastic-agent/templates/agent/k8s/_pod_template.yaml index afc8c5ab05d..6b74e6323a2 100644 --- a/deploy/helm/elastic-agent/templates/agent/k8s/_pod_template.yaml +++ b/deploy/helm/elastic-agent/templates/agent/k8s/_pod_template.yaml @@ -2,6 +2,7 @@ {{- $ := index . 0 -}} {{- $presetVal := index . 1 -}} {{- $agentName := index . 2 }} +{{- $agentVolumes := (include "elasticagent.preset.render.volumes" (list $ $presetVal $agentName) | fromYaml) -}} apiVersion: v1 kind: PodTemplate template: @@ -40,28 +41,8 @@ template: {{- . | toYaml | nindent 6 }} {{- end }} volumes: - {{- $definedAgentStateVolume := false -}} - {{- with ($presetVal).extraVolumes }} + {{- with ($agentVolumes).volumes }} {{- . | toYaml | nindent 6 }} - {{- range $idx, $volume := . -}} - {{- if eq $definedAgentStateVolume false -}} - {{- if eq ($volume).name "agent-data" -}} - {{- $definedAgentStateVolume = true}} - {{- end -}} - {{- end -}} - {{- end -}} - {{- end }} - {{- if eq $definedAgentStateVolume false }} - - name: agent-data - hostPath: - {{- if eq $.Values.agent.fleet.enabled true }} - {{/* different state hostPath for managed agents */}} - path: /etc/elastic-agent/{{$.Release.Namespace}}/{{$agentName}}-managed/state - {{- else }} - {{/* different state hostPath for standalone agents */}} - path: /etc/elastic-agent/{{$.Release.Namespace}}/{{$agentName}}/state - {{- end }} - type: DirectoryOrCreate {{- end }} {{/* standalone mode so config is static */}} - name: config @@ -117,20 +98,8 @@ template: {{- end }} {{- end }} volumeMounts: - {{- $definedAgentStateVolumeMount := false -}} - {{- with ($presetVal).extraVolumeMounts }} - {{- . | toYaml | nindent 10}} - {{- range $idx, $volumeMount := . -}} - {{- if eq $definedAgentStateVolumeMount false -}} - {{- if eq ($volumeMount).name "agent-data" -}} - {{- $definedAgentStateVolumeMount = true}} - {{- end -}} - {{- end -}} - {{- end -}} - {{- end }} - {{- if eq $definedAgentStateVolumeMount false }} - - name: agent-data - mountPath: /usr/share/elastic-agent/state + {{- with ($agentVolumes).volumeMounts }} + {{- . | toYaml | nindent 10 }} {{- end }} - name: config mountPath: /etc/elastic-agent/agent.yml diff --git a/deploy/helm/elastic-agent/values.schema.json b/deploy/helm/elastic-agent/values.schema.json index 509f2812227..a55a699bfe0 100644 --- a/deploy/helm/elastic-agent/values.schema.json +++ b/deploy/helm/elastic-agent/values.schema.json @@ -1356,6 +1356,15 @@ ] ] }, + "statePersistence": { + "type": "string", + "enum": [ + "HostPath", + "EmptyDir", + "None" + ], + "description": "Volume type used for Agent state persistence." + }, "extraVolumes": { "type": "array", "items": { diff --git a/deploy/helm/elastic-agent/values.yaml b/deploy/helm/elastic-agent/values.yaml index dbe837d9bc9..fd4576aa84c 100644 --- a/deploy/helm/elastic-agent/values.yaml +++ b/deploy/helm/elastic-agent/values.yaml @@ -396,6 +396,7 @@ agent: # tolerations: [] # topologySpreadConstraints: [] # extraEnv: [] + # statePersistence: [HostPath, EmptyDir, None] # extraVolumes: [] # extraVolumeMounts: [] # https://github.com/elastic/elastic-agent/blob/main/_meta/elastic-agent.yml @@ -418,10 +419,8 @@ agent: memory: 400Mi nodeSelector: kubernetes.io/os: linux - extraVolumes: - # override the default agent-data volume and make it an emptyDir - - name: agent-data - emptyDir: {} + statePersistence: EmptyDir + extraVolumes: [] extraEnvs: - name: ELASTIC_NETINFO value: "false" @@ -454,6 +453,7 @@ agent: memory: 400Mi nodeSelector: kubernetes.io/os: linux + statePersistence: HostPath extraEnvs: - name: ELASTIC_NETINFO value: "false"