Skip to content

Conversation

pkoutsovasilis
Copy link
Contributor

@pkoutsovasilis pkoutsovasilis commented Aug 11, 2025

What does this PR do?

This PR updates the Kubernetes hints variable substitution logic to:

  1. Restrict variable resolution to only Kubernetes-related metadata keys (e.g., ${kubernetes.pod.name}), ensuring that non-Kubernetes variables are ignored.
  2. Replace usage of strings.Replace with strings.ReplaceAll to simplify code and improve readability, addressing lint rule QF1004.

Specifically:

  • The regular expression used to detect variable placeholders is narrowed from \${([^{}]+)} to \${(kubernetes\.[^{}]+)}, preventing unintended resolution of non-Kubernetes placeholders such as env vars ${env_var}.
  • strings.Replace with -1 is replaced by strings.ReplaceAll.

Why is it important?

Because it allows users to follow the best-practices in our documentation and actually set the value of a pod annotation as an env var of the elastic-agent pod.

co.elastic.hints/password
The password to use for authentication. It is recommended to retrieve this sensitive information from an ENV variable and avoid placing passwords in plain text.

Checklist

  • I have read and understood the pull request guidelines of this project.
  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in ./changelog/fragments using the changelog tool
  • I have added an integration test or an E2E test

Disruptive User Impact

How to test this PR locally

  1. Provision a kind k8s cluster
  2. Install ECK operator
helm install elastic-operator elastic/eck-operator -n elastic-system --create-namespace
  1. Deploy an Elasticsearch cluster
cat <<EOF | kubectl apply -f - apiVersion: elasticsearch.k8s.elastic.co/v1 kind: Elasticsearch metadata: name: quickstart spec: http: tls: selfSignedCertificate: disabled: true version: 9.2.0-SNAPSHOT nodeSets: - name: default count: 1 config: node.store.allow_mmap: false podTemplate: metadata: annotations: co.elastic.hints/enabled: "true" co.elastic.hints/package: elasticsearch co.elastic.hints/host: "http://\${kubernetes.pod.ip}:9200" co.elastic.hints/username: "\${HINTS_ELASTICSEARCH_USERNAME}" co.elastic.hints/password: "\${HINTS_ELASTICSEARCH_PASSWORD}" EOF
  1. Compile the elastic-agent image of this PR and loaded in kind
USE_PACKAGE_VERSION="true" SNAPSHOT="true" EXTERNAL="true" PLATFORMS="linux/arm64" PACKAGES="docker" DOCKER_VARIANTS="basic" mage package kind load docker-image docker.elastic.co/elastic-agent/elastic-agent:9.2.0-SNAPSHOT
  1. Get the elasticsearch password (username is elastic)
kubectl get secret -n default quickstart-es-elastic-user -o jsonpath="{.data.elastic}" | base64 --decode
  1. Install elastic-agent using the helm chart with the values below
outputs: default: type: ESPlainAuthBasic url: "${YOUR_ES_HOST}" username: "${YOUR_ES_USERNAME}" password: "${YOUR_ES_PASSWORD}" kubernetes: hints: enabled: true state: enabled: false apiserver: enabled: false agent: presets: perNode: extraEnvs: - name: HINTS_ELASTICSEARCH_USERNAME value: "elastic" - name: HINTS_ELASTICSEARCH_PASSWORD value: "${THE_PASSWORD_FROM_PREVIOUS_STEP}" 
  1. exec inside the agent pod and grab a diagnostics and in the rendered beats config you will see the elasticsearch one being rendered correctly

Related issues

  • N/A
@pkoutsovasilis pkoutsovasilis self-assigned this Aug 11, 2025
@pkoutsovasilis pkoutsovasilis added bug Something isn't working skip-changelog backport-active-all Automated backport with mergify to all the active branches labels Aug 11, 2025
@pkoutsovasilis pkoutsovasilis added Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team and removed skip-changelog labels Aug 11, 2025
@elasticmachine
Copy link
Collaborator

💛 Build succeeded, but was flaky

Failed CI Steps

cc @pkoutsovasilis

@pkoutsovasilis pkoutsovasilis marked this pull request as ready for review August 12, 2025 07:11
@pkoutsovasilis pkoutsovasilis requested a review from a team as a code owner August 12, 2025 07:11
@elasticmachine
Copy link
Collaborator

Pinging @elastic/elastic-agent-control-plane (Team:Elastic-Agent-Control-Plane)

@pkoutsovasilis pkoutsovasilis merged commit 228e4fc into elastic:main Aug 18, 2025
21 checks passed
Copy link
Contributor

@Mergifyio backport 8.17 8.18 8.19 9.0 9.1

mergify bot pushed a commit that referenced this pull request Aug 18, 2025
…9307) * fix: validate only kubernetes-related vars from kubernetes metadata * fix: QF1004: could use strings.ReplaceAll instead * chore: add changelog fragment (cherry picked from commit 228e4fc)
mergify bot pushed a commit that referenced this pull request Aug 18, 2025
…9307) * fix: validate only kubernetes-related vars from kubernetes metadata * fix: QF1004: could use strings.ReplaceAll instead * chore: add changelog fragment (cherry picked from commit 228e4fc)
mergify bot pushed a commit that referenced this pull request Aug 18, 2025
…9307) * fix: validate only kubernetes-related vars from kubernetes metadata * fix: QF1004: could use strings.ReplaceAll instead * chore: add changelog fragment (cherry picked from commit 228e4fc)
mergify bot pushed a commit that referenced this pull request Aug 18, 2025
…9307) * fix: validate only kubernetes-related vars from kubernetes metadata * fix: QF1004: could use strings.ReplaceAll instead * chore: add changelog fragment (cherry picked from commit 228e4fc)
mergify bot pushed a commit that referenced this pull request Aug 18, 2025
…9307) * fix: validate only kubernetes-related vars from kubernetes metadata * fix: QF1004: could use strings.ReplaceAll instead * chore: add changelog fragment (cherry picked from commit 228e4fc)
pkoutsovasilis added a commit that referenced this pull request Aug 18, 2025
…9307) (#9419) * fix: validate only kubernetes-related vars from kubernetes metadata * fix: QF1004: could use strings.ReplaceAll instead * chore: add changelog fragment (cherry picked from commit 228e4fc) Co-authored-by: Panos Koutsovasilis <panos.koutsovasilis@elastic.co>
pkoutsovasilis added a commit that referenced this pull request Aug 18, 2025
…9307) (#9421) * fix: validate only kubernetes-related vars from kubernetes metadata * fix: QF1004: could use strings.ReplaceAll instead * chore: add changelog fragment (cherry picked from commit 228e4fc) Co-authored-by: Panos Koutsovasilis <panos.koutsovasilis@elastic.co>
pkoutsovasilis added a commit that referenced this pull request Aug 18, 2025
…9307) (#9418) * fix: validate only kubernetes-related vars from kubernetes metadata * fix: QF1004: could use strings.ReplaceAll instead * chore: add changelog fragment (cherry picked from commit 228e4fc) Co-authored-by: Panos Koutsovasilis <panos.koutsovasilis@elastic.co>
pkoutsovasilis added a commit that referenced this pull request Aug 18, 2025
…9307) (#9422) * fix: validate only kubernetes-related vars from kubernetes metadata * fix: QF1004: could use strings.ReplaceAll instead * chore: add changelog fragment (cherry picked from commit 228e4fc) Co-authored-by: Panos Koutsovasilis <panos.koutsovasilis@elastic.co>
pkoutsovasilis added a commit that referenced this pull request Aug 19, 2025
…9307) (#9420) * fix: validate only kubernetes-related vars from kubernetes metadata * fix: QF1004: could use strings.ReplaceAll instead * chore: add changelog fragment (cherry picked from commit 228e4fc) Co-authored-by: Panos Koutsovasilis <panos.koutsovasilis@elastic.co>
kaanyalti pushed a commit to kaanyalti/elastic-agent that referenced this pull request Sep 4, 2025
…lastic#9307) * fix: validate only kubernetes-related vars from kubernetes metadata * fix: QF1004: could use strings.ReplaceAll instead * chore: add changelog fragment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-active-all Automated backport with mergify to all the active branches bug Something isn't working Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team

3 participants