Skip to content

Commit

Permalink
feat: implement values.schema.json for elastic-agent helm chart
Browse files Browse the repository at this point in the history
  • Loading branch information
pkoutsovasilis committed Sep 12, 2024
1 parent 80ad645 commit e8f34e4
Show file tree
Hide file tree
Showing 6 changed files with 1,178 additions and 105 deletions.
71 changes: 14 additions & 57 deletions deploy/helm/elastic-agent/templates/agent/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,64 +29,34 @@ Entrypoint for chart initialisation
{{- define "elasticagent.init" -}}
{{- if not (hasKey $.Values.agent "initialised") -}}
{{/* init order matters */}}
{{- include "elasticagent.init.engine" $ -}}
{{- include (printf "elasticagent.engine.%s.init" $.Values.agent.engine) $ -}}
{{- include "elasticagent.init.fleet" $ -}}
{{- include "elasticagent.init.outputs" $ -}}
{{- include "elasticagent.init.inputs" $ -}}
{{- include "elasticagent.init.presets" $ -}}
{{- $_ := set $.Values.agent "initialised" dict -}}
{{- end -}}
{{- end -}}

{{/*
Check the agent.engine and fallback to "k8s"
*/}}
{{- define "elasticagent.init.engine" -}}
{{- $ := . -}}
{{- if empty (dig "engine" ("") $.Values.agent) -}}
{{- $_ := set $.Values.agent "engine" "k8s" -}}
{{- end -}}
{{- include (printf "elasticagent.engine.%s.init" $.Values.agent.engine) $ -}}
{{- end -}}

{{/*
Validate fleet configuration
*/}}
{{- define "elasticagent.init.fleet" -}}
{{- $ := . -}}
{{/* check if fleet is enabled */}}
{{- if eq $.Values.agent.fleet.enabled true -}}
{{- $_ := required "url must be defined when fleet mode is enabled" $.Values.agent.fleet.url -}}
{{- if empty $.Values.agent.fleet.token -}}
{{- $_ := required "kibana host must be defined when fleet mode is enabled and a token is not supplied" $.Values.agent.fleet.kibanaHost -}}
{{- $_ := required "kibana username must be defined when fleet mode is enabled and a token is not supplied" $.Values.agent.fleet.kibanaUser -}}
{{- $_ := required "kibana password must be defined when fleet mode is enabled and a token is not supplied" $.Values.agent.fleet.kibanaPassword -}}
{{- end -}}
{{- $_ := required "preset must be defined when fleet mode is enabled" $.Values.agent.fleet.preset -}}
{{- if not (hasKey $.Values.agent.presets $.Values.agent.fleet.preset)}}
{{- fail (printf "specified preset \"%s\" under fleet is not found" $.Values.agent.fleet.preset) -}}
{{- end -}}
{{/* check if the preset exists */}}
{{- $fleetPresetName := $.Values.agent.fleet.preset -}}
{{- $fleetPresetVal := get $.Values.agent.presets $fleetPresetName -}}
{{- $_ := required (printf "preset with name \"%s\" of fleet not defined" $fleetPresetName) $fleetPresetVal -}}
{{/* disable all presets except the fleet one */}}
{{- range $presetName, $presetVal := $.Values.agent.presets}}
{{- if ne $presetName $.Values.agent.fleet.preset -}}
{{- if ne $presetName $fleetPresetName -}}
{{- $_ := unset $.Values.agent.presets $presetName}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Validate and initialise outputs
*/}}
{{- define "elasticagent.init.outputs" -}}
{{- $ := . -}}
{{- $supportOutputTypes := dict "ESPlainAuth" "" "ESSecretAuthBasic" "" "ESSecretAuthAPI" "" "ESECKRef" ""}}
{{- range $outputName, $outputVal := $.Values.outputs -}}
{{- if empty ($outputVal).type -}}
{{- $_ := set $outputVal "type" "ESPlainAuth" -}}
{{- end -}}
{{- include (printf "elasticagent.output.%s.validate" ($outputVal).type) (list $ $outputName $outputVal)}}
{{- end -}}
{{- end -}}

{{/*
Initialise input templates if we are not deploying as managed
*/}}
Expand All @@ -97,13 +67,9 @@ Initialise input templates if we are not deploying as managed
{{- include "elasticagent.kubernetes.init" $ -}}
{{- range $customInputName, $customInputVal := $.Values.extraIntegrations -}}
{{- $customInputPresetName := ($customInputVal).preset -}}
{{- $_ := required (printf "customInput \"%s\" is missing required preset field" $customInputName) $customInputPresetName -}}
{{- $presetVal := get $.Values.agent.presets $customInputPresetName -}}
{{- $_ := required (printf "preset with name \"%s\" of customInput \"%s\" not defined" $customInputPresetName $customInputName) $customInputVal -}}
{{- $customInputOuput := dig "use_output" (list) $customInputVal -}}
{{- if empty $customInputOuput -}}
{{- fail (printf "output not defined in custom integration \"%s\"" $customInputName) -}}
{{- end -}}
{{- $customInputOuput := ($customInputVal).use_output -}}
{{- include "elasticagent.preset.mutate.outputs.byname" (list $ $presetVal $customInputOuput) -}}
{{- include "elasticagent.preset.mutate.inputs" (list $ $presetVal (list $customInputVal)) -}}
{{- end -}}
Expand All @@ -118,15 +84,7 @@ Validate and initialise the defined agent presets
{{- range $presetName, $presetVal := $.Values.agent.presets -}}
{{- include "elasticagent.preset.mutate.unprivileged" (list $ $presetVal) -}}
{{- include "elasticagent.preset.mutate.fleet" (list $ $presetVal) -}}
{{- $presetMode := dig "mode" ("") $presetVal -}}
{{- if not $presetMode -}}
{{- fail (printf "mode is missing from preset \"%s\"" $presetName) -}}
{{- else if eq $presetMode "deployment" -}}
{{- else if eq $presetMode "statefulset" -}}
{{- else if eq $presetMode "daemonset" -}}
{{- else -}}
{{- fail (printf "invalid mode \"%s\" in preset \"%s\", must be one of deployment, statefulset, daemonset" $presetMode $presetName) -}}
{{- end -}}
{{- $presetMode := ($presetVal).mode -}}
{{- if eq $.Values.agent.fleet.enabled false -}}
{{- $presetInputs := dig "_inputs" (list) $presetVal -}}
{{- if empty $presetInputs -}}
Expand Down Expand Up @@ -384,13 +342,12 @@ app.kubernetes.io/version: {{ .Values.agent.version}}
{{- $ := index . 0 -}}
{{- $preset := index . 1 -}}
{{- $outputName := index . 2 -}}
{{- if not (hasKey $.Values.outputs $outputName) -}}
{{- fail (printf "output \"%s\" is not defined" $outputName) -}}
{{- end -}}
{{- $outputDict := deepCopy (get $.Values.outputs $outputName) -}}
{{- $ouputVal := get $.Values.outputs $outputName }}
{{- $_ := required (printf "output \"%s\" is not defined" $outputName) $ouputVal -}}
{{- $outputCopy := deepCopy $ouputVal -}}
{{- $presetOutputs := dig "outputs" (dict) $preset -}}
{{- if not (hasKey $presetOutputs $outputName) -}}
{{- $_ := set $presetOutputs $outputName $outputDict}}
{{- $_ := set $presetOutputs $outputName $outputCopy}}
{{- end -}}
{{- $_ := set $preset "outputs" $presetOutputs -}}
{{- end -}}
46 changes: 0 additions & 46 deletions deploy/helm/elastic-agent/templates/agent/_outputs.tpl
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
{{- define "elasticagent.output.ESPlainAuth.validate" -}}
{{- $ := index . 0 -}}
{{- $outputName := index . 1 -}}
{{- $outputVal := index . 2 -}}
{{- if empty ($outputVal).url -}}
{{- fail (printf "missing url in %s output" $outputName)}}
{{- end -}}
{{- if empty ($outputVal).api_key -}}
{{- if empty ($outputVal).username -}}
{{- fail (printf "missing user in %s output" $outputName)}}
{{- end -}}
{{- if empty ($outputVal).password -}}
{{- fail (printf "missing pass in %s output" $outputName)}}
{{- end -}}
{{- end -}}
{{- end -}}

{{- define "elasticagent.output.ESPlainAuth.preset.envvars" -}}
{{/* this is plain text so nothing to be added in the pod env vars */}}
{{- end -}}
Expand All @@ -34,15 +17,6 @@
{{- $outputVal | toYaml | nindent 2}}
{{- end -}}

{{- define "elasticagent.output.ESSecretAuthBasic.validate" -}}
{{- $ := index . 0 -}}
{{- $outputName := index . 1 -}}
{{- $outputVal := index . 2 -}}
{{- if empty ($outputVal).secretName -}}
{{- fail (printf "secret name missing in %s output" $outputName)}}
{{- end -}}
{{- end -}}

{{- define "elasticagent.output.ESSecretAuthBasic.preset.envvars" -}}
{{- $ := index . 0 -}}
{{- $outputName := index . 1 -}}
Expand Down Expand Up @@ -77,15 +51,6 @@
{{- $outputVal | toYaml | nindent 2}}
{{- end -}}

{{- define "elasticagent.output.ESSecretAuthAPI.validate" -}}
{{- $ := index . 0 -}}
{{- $outputName := index . 1 -}}
{{- $outputVal := index . 2 -}}
{{- if empty ($outputVal).secretName -}}
{{- fail (printf "secret name missing in %s output" $outputName)}}
{{- end -}}
{{- end -}}

{{- define "elasticagent.output.ESSecretAuthAPI.preset.config" -}}
{{- $ := index . 0 -}}
{{- $outputName := index . 1 -}}
Expand Down Expand Up @@ -114,17 +79,6 @@
key: api_key
{{- end -}}

{{- define "elasticagent.output.ESECKRef.validate" -}}
{{- $ := index . 0 -}}
{{- $outputName := index . 1 -}}
{{- $outputVal := index . 2 -}}
{{- if empty ($outputVal).name -}}
{{- if empty ($outputVal).secretName -}}
{{- fail (printf "missing secretName in %s output (ESECKRef type)" $outputName)}}
{{- end -}}
{{- end -}}
{{- end -}}

{{- define "elasticagent.output.ESECKRef.preset.config" -}}
{{- $ := index . 0 -}}
{{- $outputName := index . 1 -}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
{{- $agentName := index . 2 }}
spec:
dnsPolicy: ClusterFirstWithHostNet
{{- with ($presetVal).hostNetwork }}
hostNetwork: {{ . }}
{{- end }}
{{- with ($presetVal).hostPID }}
hostPID: {{ . }}
{{- end }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
{{- $agentName := index . 2 }}
spec:
dnsPolicy: ClusterFirstWithHostNet
{{- if eq $.Values.agent.fleet.enabled true }}
hostNetwork: true
{{- with ($presetVal).hostNetwork }}
hostNetwork: {{ . }}
{{- end }}
{{- with ($presetVal).hostPID }}
hostPID: {{ . }}
Expand Down
Loading

0 comments on commit e8f34e4

Please sign in to comment.