Skip to content

fix(valkey-operator): align ServiceMonitor with metrics.secure (skip-verify opt-in) - #231

Open
daanvinken wants to merge 4 commits into
valkey-io:mainfrom
daanvinken:fix/servicemonitor-metrics-secure-port
Open

fix(valkey-operator): align ServiceMonitor with metrics.secure (skip-verify opt-in)#231
daanvinken wants to merge 4 commits into
valkey-io:mainfrom
daanvinken:fix/servicemonitor-metrics-secure-port

Conversation

@daanvinken

Copy link
Copy Markdown
Contributor

Summary

Fixes ServiceMonitor scrapes when metrics.secure is true, and makes TLS skip-verify opt-in rather than a silent default.

Bug

With metrics.secure: true, the metrics Service exposes port https, but the ServiceMonitor always targeted port http, so scrapes never hit the right endpoint.

What we change

  1. Port / scheme always follow metrics.secure (https + default scheme: https when secure and serviceMonitor.scheme is unset).
  2. metrics.serviceMonitor.insecureSkipVerify (default false). Only when this is true (and tlsConfig is empty) do we inject tlsConfig.insecureSkipVerify: true for the operator's self-signed metrics cert. Explicit serviceMonitor.tlsConfig always wins.

Design question for reviewers

Should skip-verify stay disabled by default?

Default Behavior
false (this PR) Secure scrapes need either insecureSkipVerify: true or a real tlsConfig (CA). No silent MITM-friendly default.
true Secure + ServiceMonitor works out of the box with self-signed certs, but enabling secure metrics also disables cert verification unless the user overrides.

We prefer default false: "secure" should not imply "do not verify." The operator cert is self-signed, so skip-verify is still available as an explicit one-liner for the common case.

metrics:
  secure: true
  serviceMonitor:
    enabled: true
    insecureSkipVerify: true  # opt-in for self-signed metrics cert

If maintainers prefer out-of-the-box scrapes, flipping the default to true is a one-line change; the important part is that it is named and documented, not hard-coded only in the template.

Docs

README (Metrics / ServiceMonitor), values comments, and CHANGELOG 0.4.1 describe port/scheme alignment and the opt-in flag.

Not in this PR

Bearer token for secure metrics auth (WithAuthenticationAndAuthorization) is a separate follow-up so scrapes do not get 401 after TLS is fixed.

Testing

# insecure: port http, no scheme/tls
helm template t valkey-operator \
  --set metrics.serviceMonitor.enabled=true

# secure, default: port https, scheme https, no tlsConfig
helm template t valkey-operator \
  --set metrics.secure=true \
  --set metrics.serviceMonitor.enabled=true

# secure + opt-in skip-verify
helm template t valkey-operator \
  --set metrics.secure=true \
  --set metrics.serviceMonitor.enabled=true \
  --set metrics.serviceMonitor.insecureSkipVerify=true

@Bloodraven21
Bloodraven21 requested a review from jdheyburn July 27, 2026 17:46
@Bloodraven21

Copy link
Copy Markdown
Collaborator

@daanvinken please rebase the branch

When metrics.secure is true the metrics Service exposes port https, but
the ServiceMonitor always targeted port http so scrapes failed. Use the
matching port name, default scheme https, and insecureSkipVerify for the
operator self-signed cert unless serviceMonitor overrides are set.

Signed-off-by: daanvinken <daanvinken@tythus.com>
…itor

Keep port/scheme aligned with metrics.secure. Do not default skip-verify;
add metrics.serviceMonitor.insecureSkipVerify (false by default). Explicit
tlsConfig still wins. Document secure scrape setup in the README.

Signed-off-by: daanvinken <daanvinken@tythus.com>
0.4.1 already shipped on main with the log level description change.
Move the ServiceMonitor secure-metrics fix notes to 0.4.2.

Signed-off-by: daanvinken <daanvinken@tythus.com>
@daanvinken
daanvinken force-pushed the fix/servicemonitor-metrics-secure-port branch from 2db45ce to d3dd681 Compare July 30, 2026 19:44
@daanvinken

Copy link
Copy Markdown
Contributor Author

Check

@rlunar

rlunar commented Jul 31, 2026

Copy link
Copy Markdown
Member

@greptileai

@greptile-apps

greptile-apps Bot commented Jul 31, 2026

Copy link
Copy Markdown

Greptile Summary

This change aligns the ServiceMonitor endpoint port and default scheme with secure metrics, adds configurable bearer-token and authorization settings, and documents TLS options. Helm v3.19.4 rendering confirmed that secure metrics render an HTTPS endpoint with a service-account token, and all 71 Helm unit tests passed. However, the default secure ServiceMonitor does not render TLS trust settings for the chart-documented self-signed metrics certificate, so Prometheus cannot scrape the secure endpoint without additional user configuration.

Confidence Score: 4/5

Do not merge until the secure ServiceMonitor default is made scrape-ready or the chart requires an explicit TLS trust configuration.

Helm v3.19.4 rendered the secure configuration and its TLS and authentication overrides, and the full Helm unit suite passed. The rendered default nevertheless omits TLS trust configuration despite the documented self-signed certificate, which breaks the primary secure monitoring path.

Files Needing Attention: valkey-operator/templates/servicemonitor.yaml and valkey-operator/values.yaml need attention to provide a trusted CA, enable explicit opt-in behavior as a required configuration, or otherwise make the secure ServiceMonitor default usable.

T-Rex T-Rex Logs

What T-Rex did

  • T-Rex produced a proof for a posted P1 finding about the Secure ServiceMonitor Helm render harness.
  • The baseline and PR render outputs, along with CI-pinned Helm unit tests, were reviewed to validate the rendering changes described in the finding.
  • T-Rex produced a second proof for another posted P1 finding.
  • Contract validation showed how render behavior changed from before to after, including HTTPS defaults and how explicit overrides affect tlsConfig and authorization handling.

View all artifacts

T-Rex Ran code and verified through T-Rex

Comments Outside Diff (1)

  1. General comment

    P1 Secure ServiceMonitor defaults cannot verify the operator's self-signed metrics certificate

    • Bug
      • With metrics.secure=true and metrics.serviceMonitor.enabled=true, the rendered endpoint uses HTTPS but has no tlsConfig. The chart values and template explicitly document that the operator certificate is self-signed, while insecureSkipVerify defaults to false. Prometheus will therefore reject the default TLS connection unless the user additionally supplies a CA or opts into skip verification. The default ServiceMonitor is not able to scrape the documented operator defaults.
    • Cause
      • tlsConfig is emitted only when the user sets metrics.serviceMonitor.tlsConfig or explicitly sets metrics.serviceMonitor.insecureSkipVerify=true; neither is enabled by the secure ServiceMonitor default.
    • Fix
      • Make the secure ServiceMonitor default scrape-ready by defaulting insecureSkipVerify to true for the generated self-signed certificate, or provision and reference a trusted CA by default. If retaining secure-by-default TLS verification is intentional, do not present enabling only metrics.secure and serviceMonitor.enabled as a usable monitor configuration; require an explicit trust configuration and consider failing template rendering when it is absent.

    T-Rex Ran code and verified through T-Rex

Reviews (2): Last reviewed commit: "fix(valkey-operator): secure ServiceMoni..." | Re-trigger Greptile

Comment thread valkey-operator/templates/servicemonitor.yaml
Comment thread valkey-operator/templates/servicemonitor.yaml
When metrics.secure is true, send the scraper ServiceAccount token
(bearerTokenFile) so protected /metrics does not 401. Allow authorization
or bearerTokenSecret overrides. Add helm unittest coverage for secure
port, scheme, TLS precedence, skip-verify, and auth.

Testing
- helm lint ./valkey-operator
- helm unittest ./valkey-operator

Signed-off-by: daanvinken <daanvinken@tythus.com>
@daanvinken

Copy link
Copy Markdown
Contributor Author

@greptileai

@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The chart now supports secure ServiceMonitor endpoints with HTTPS defaults, configurable authentication, TLS verification controls, precedence rules, Helm tests, documentation, and version 0.4.2 metadata.

Changes

Secure ServiceMonitor behavior

Layer / File(s) Summary
ServiceMonitor configuration and rendering
valkey-operator/values.yaml, valkey-operator/templates/servicemonitor.yaml
ServiceMonitor settings now support secure ports, HTTPS defaults, authentication overrides, bearer-token options, and TLS verification controls.
Secure and non-secure behavior validation
valkey-operator/tests/servicemonitor_test.yaml
Helm tests cover scheme selection, TLS precedence, authorization precedence, and omission of secure settings for non-secure metrics.
Release metadata and usage documentation
valkey-operator/Chart.yaml, valkey-operator/CHANGELOG.md, valkey-operator/README.md
The chart version is 0.4.2. The changelog and documentation describe secure ServiceMonitor configuration and examples.

Sequence Diagram(s)

sequenceDiagram
  participant Values as ServiceMonitor values
  participant Template as ServiceMonitor template
  participant Prometheus
  Values->>Template: Provide scheme, authentication, and TLS settings
  Template->>Prometheus: Render endpoint configuration
  Prometheus->>Template: Scrape the configured metrics port
Loading

Suggested reviewers: jdheyburn, somaz94

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the ServiceMonitor alignment with secure metrics and the opt-in TLS verification behavior.
Description check ✅ Passed The description directly explains the ServiceMonitor fixes, configuration behavior, documentation updates, and testing coverage.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
valkey-operator/README.md (1)

216-228: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

State the reader-binding prerequisite in the custom-CA example.

When this block is copied without an existing binding, metrics.reader.binding.create remains false. The ServiceMonitor still sends a token, but the Prometheus ServiceAccount is not bound to metrics-reader, so scrapes return 401. Add the binding block or state that this example assumes a pre-existing binding.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@valkey-operator/README.md` around lines 216 - 228, Update the custom-CA
configuration example near the metrics ServiceMonitor settings to include the
required metrics.reader.binding.create configuration, or explicitly state that
it assumes a pre-existing binding. Ensure copied configuration binds the
Prometheus ServiceAccount to metrics-reader so authenticated scrapes do not
return 401.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@valkey-operator/README.md`:
- Around line 76-79: The ServiceMonitor configuration must prevent credentials
from being sent over HTTP when metrics.secure is true. Update the scheme
validation or credential-rendering logic around metrics.serviceMonitor.scheme,
bearerTokenFile, bearerTokenSecret, and authorization so an explicit http scheme
either fails validation or omits all credential fields in secure mode; preserve
HTTPS credential behavior. Add a Helm test covering this case and document the
restriction in the README.

---

Nitpick comments:
In `@valkey-operator/README.md`:
- Around line 216-228: Update the custom-CA configuration example near the
metrics ServiceMonitor settings to include the required
metrics.reader.binding.create configuration, or explicitly state that it assumes
a pre-existing binding. Ensure copied configuration binds the Prometheus
ServiceAccount to metrics-reader so authenticated scrapes do not return 401.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 0244a443-b5db-485b-b733-573507f9a195

📥 Commits

Reviewing files that changed from the base of the PR and between 01b4fa4 and a10b1f9.

📒 Files selected for processing (6)
  • valkey-operator/CHANGELOG.md
  • valkey-operator/Chart.yaml
  • valkey-operator/README.md
  • valkey-operator/templates/servicemonitor.yaml
  • valkey-operator/tests/servicemonitor_test.yaml
  • valkey-operator/values.yaml

Comment thread valkey-operator/README.md
tlsConfig:
{{- toYaml . | nindent 6 }}
{{- toYaml .Values.metrics.serviceMonitor.tlsConfig | nindent 6 }}
{{- else if and .Values.metrics.secure .Values.metrics.serviceMonitor.insecureSkipVerify }}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Secure ServiceMonitor lacks TLS trust configuration

When metrics.secure=true and the ServiceMonitor is enabled, this renders an HTTPS endpoint without tlsConfig. The chart documents that the operator uses a self-signed metrics certificate, while insecureSkipVerify defaults to false, so Prometheus rejects the TLS handshake unless the user separately supplies a CA or enables skip verification. Make the generated configuration trust the certificate by default, or require and document an explicit CA-backed TLS configuration as part of enabling the secure ServiceMonitor.

Artifacts

Secure ServiceMonitor Helm render harness source

  • Exact executable harness used to render the baseline and PR charts with Helm 3.19.4, inspect endpoints, and assert override precedence; it provides the reproducible check.

Baseline secure ServiceMonitor render output

  • Helm 3.19.4 output for the parent chart at commit 01b4fa4 with secure metrics and the ServiceMonitor enabled; it shows the pre-PR monitor did not target HTTPS.

PR secure ServiceMonitor render output

  • Helm 3.19.4 output for the PR chart with secure defaults and explicit override cases; it shows default TLS trust is absent while requested overrides render correctly.

CI-pinned Helm unit test output

  • Observed output from the CI-pinned Helm 3.19.4 and helm-unittest 1.0.2 run; all 71 chart tests passed, including ServiceMonitor template tests.

View artifacts

T-Rex Ran code and verified through T-Rex

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intentional.

metrics.secure means the operator serves metrics over HTTPS with authn/authz. It should not also mean "Prometheus, skip certificate verification." That would mix "encrypted channel" with "do not check the peer," which is easy to miss in production.

The operator's stock metrics cert is self-signed, so a scrape that only sets secure + serviceMonitor.enabled is not fully turnkey until you either:

  • set metrics.serviceMonitor.insecureSkipVerify: true (common for the self-signed case), or
  • set a real tlsConfig (CA / cert), or
  • replace the metrics serving cert with one Prometheus already trusts.

That is documented in the README example (secure + reader binding + skip-verify). We prefer an explicit one-liner over baking skip-verify into the default secure path.

@greptile-apps

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood — that distinction is exactly right. Enabling HTTPS transport and disabling cert verification are separate concerns, and conflating them silently would be the worse default. The explicit opt-in via insecureSkipVerify: true is the correct model here.

Withdrawing the comment.

Tip: You can customize Greptile's behavior for this repo with .greptile/rules.md and .greptile/config.json.

Comment on lines +27 to 31
{{- if .Values.metrics.serviceMonitor.scheme }}
scheme: {{ .Values.metrics.serviceMonitor.scheme }}
{{- else if .Values.metrics.secure }}
scheme: https
{{- end }}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a nit; would this pattern be better to match what you're doing on line 19?

Suggested change
{{- if .Values.metrics.serviceMonitor.scheme }}
scheme: {{ .Values.metrics.serviceMonitor.scheme }}
{{- else if .Values.metrics.secure }}
scheme: https
{{- end }}
{{- if .Values.metrics.serviceMonitor.scheme }}
scheme: {{ .Values.metrics.serviceMonitor.scheme }}
{{- else }}
scheme: {{ if .Values.metrics.secure }}https{{ else }}http{{ end }}
{{- end }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants