Skip to content

Commit

Permalink
Add statePersistence value to helm chart presets (#6803)
Browse files Browse the repository at this point in the history
* Add statePersistence value to helm chart presets
  • Loading branch information
pchila authored Feb 18, 2025
1 parent 0ac7fd0 commit 1eefbe0
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1093,7 +1097,6 @@ spec:
privileged: false
runAsGroup: 1000
runAsUser: 1000
volumeMounts: null
dnsPolicy: ClusterFirstWithHostNet
nodeSelector:
kubernetes.io/os: linux
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
69 changes: 69 additions & 0 deletions deploy/helm/elastic-agent/templates/agent/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}

Expand Down Expand Up @@ -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 -}}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 }}
Expand Down Expand Up @@ -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:
Expand Down
39 changes: 4 additions & 35 deletions deploy/helm/elastic-agent/templates/agent/k8s/_pod_template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions deploy/helm/elastic-agent/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,15 @@
]
]
},
"statePersistence": {
"type": "string",
"enum": [
"HostPath",
"EmptyDir",
"None"
],
"description": "Volume type used for Agent state persistence."
},
"extraVolumes": {
"type": "array",
"items": {
Expand Down
8 changes: 4 additions & 4 deletions deploy/helm/elastic-agent/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -454,6 +453,7 @@ agent:
memory: 400Mi
nodeSelector:
kubernetes.io/os: linux
statePersistence: HostPath
extraEnvs:
- name: ELASTIC_NETINFO
value: "false"
Expand Down

0 comments on commit 1eefbe0

Please # to comment.