Skip to content

Add support for operator environment variables#215

Merged
mclasmeier merged 4 commits into
mainfrom
operator-env-vars
Jun 25, 2026
Merged

Add support for operator environment variables#215
mclasmeier merged 4 commits into
mainfrom
operator-env-vars

Conversation

@porridge

@porridge porridge commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add --operator-env KEY=VALUE CLI flag for setting env vars on the operator deployment
  • Support via YAML config file under operator.envVars map
  • Non-OLM path: injects env vars into the manager container of the CSV-derived deployment
  • OLM path: sets spec.config.env on the Subscription so OLM propagates to the deployment
  • Useful for overriding RELATED_IMAGE_* variables to test with custom-built component images, in particular to support custom images in stackrox "infra" service.

Usage

# CLI flag (repeatable)
roxie deploy --operator-env RELATED_IMAGE_MAIN=quay.io/rhacs-eng/main:4.7.0
roxie deploy --operator-env RELATED_IMAGE_MAIN=... --operator-env RELATED_IMAGE_SCANNER=...

# YAML config file
operator:
  envVars:
    RELATED_IMAGE_MAIN: quay.io/rhacs-eng/main:4.7.0
    RELATED_IMAGE_SCANNER: quay.io/rhacs-eng/scanner:4.7.0

Test plan

  • Unit tests for ParseOperatorEnvVar — valid pairs, values with =/commas/spaces, missing =, empty key
  • Unit test for envVarsToSortedList — sort order
  • CLI flag tests — single and multiple flags
  • Config file test — YAML operator.envVars map
  • go build ./..., go test ./..., go vet ./... all pass

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added support for configuring operator environment variables via --operator-env (single KEY=VALUE, comma-separated lists, and repeated flags).
    • Added support for setting operator environment variables in configuration files under operator.envVars.
  • Enhancements
    • Injects configured environment variables into the operator deployment (manager container) and into OLM subscription config when applicable.
    • Adds more detailed logging of the deployment plan and configured operator environment variables.
  • Bug Fixes
    • Improved validation and error messages for malformed --operator-env expressions.
  • Tests
    • Expanded CLI and config-file test coverage, plus added unit tests for env var parsing and sorting.

Allow overriding environment variables on the operator deployment,
useful for setting RELATED_IMAGE_* vars to test with custom-built
component images. Supports both non-OLM (direct injection into the
manager container) and OLM (via Subscription spec.config.env) paths.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Enterprise

Run ID: d8655f23-fdb9-43a9-8523-3c5822573d70

📥 Commits

Reviewing files that changed from the base of the PR and between d59ebd5 and 1021809.

📒 Files selected for processing (3)
  • cmd/deploy_test.go
  • internal/deployer/operator_env.go
  • internal/deployer/operator_env_test.go
🚧 Files skipped from review as they are similar to previous changes (3)
  • cmd/deploy_test.go
  • internal/deployer/operator_env.go
  • internal/deployer/operator_env_test.go

📝 Walkthrough

Walkthrough

A new --operator-env flag is added, backed by OperatorConfig.EnvVars and parsing helpers. The configured env vars are loaded from CLI or YAML, then injected into the non-OLM operator deployment and the OLM Subscription spec.

Changes

Operator Env Var Injection

Layer / File(s) Summary
Config field and env helpers
internal/deployer/config.go, internal/deployer/operator_env.go, internal/deployer/operator_env_test.go
OperatorConfig gains EnvVars map[string]string. ParseOperatorEnvVar parses KEY=VALUE strings using the first =, and envVarsToSortedList returns sorted {name, value} entries. Tests cover parsing edge cases and ordering.
CLI flag wiring
cmd/deploy.go, cmd/deploy_test.go
Registers --operator-env on the deploy command, parses values into config.Operator.EnvVars, and verifies CLI and YAML config loading cases.
Non-OLM deployment injection
internal/deployer/operator.go
Logs configured env vars, injects them into the manager container in createDeploymentFromCSV, and merges by env var name.
OLM subscription injection
internal/deployer/operator_olm.go
Logs configured env vars and adds them to Subscription spec.config.env through subscriptionSpec.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 Hop hop, a flag takes flight,
KEY=VALUE envs now land just right.
The manager hops, the Subscription too,
With sorted vars in a tidy queue.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and accurately summarizes the main change: adding support for operator environment variables.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch operator-env-vars

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

@vladbologa

Copy link
Copy Markdown
Collaborator

@coderabitai review

Comment thread internal/deployer/operator_env.go Outdated
Comment thread internal/deployer/operator_env.go Outdated
Comment thread internal/deployer/operator.go Outdated
Comment thread internal/deployer/operator.go Outdated
Comment thread internal/deployer/operator_env.go Outdated
Comment thread internal/deployer/operator_env_test.go Outdated
Comment thread internal/deployer/operator_env_test.go Outdated
Comment thread internal/deployer/operator_env_test.go Outdated
- Rename operatorEnvVarsToSortedList to envVarsToSortedList
- Simplify ParseOperatorEnvVars to ParseOperatorEnvVar taking a single
  KEY=VALUE string (no comma splitting or space trimming)
- Use Dimf instead of Dim(fmt.Sprintf(...))
- Switch tests to use testify require/assert

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

@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: 3

🧹 Nitpick comments (1)
internal/deployer/operator.go (1)

541-545: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Surface when env injection is skipped due to missing manager container.

When env vars are configured but no name: manager container exists, injection silently no-ops. Emit a signal (bool/error + log) so misconfiguration is visible.

🔧 Suggested fix
-				if containers, ok := podSpec["containers"].([]interface{}); ok {
-					d.injectEnvVarsIntoManagerContainer(containers)
+				if containers, ok := podSpec["containers"].([]interface{}); ok {
+					if !d.injectEnvVarsIntoManagerContainer(containers) {
+						d.logger.Infof("Custom operator env vars configured, but container %q was not found; skipping injection", managerContainerName)
+					}
 				}
 			}
 		}
 	}
@@
-func (d *Deployer) injectEnvVarsIntoManagerContainer(containers []interface{}) {
+func (d *Deployer) injectEnvVarsIntoManagerContainer(containers []interface{}) bool {
 	for _, c := range containers {
 		container, ok := c.(map[string]interface{})
 		if !ok {
 			continue
 		}
@@
 		container["env"] = envList
-		return
+		return true
 	}
+	return false
 }

Also applies to: 568-600

🤖 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 `@internal/deployer/operator.go` around lines 541 - 545, The
injectEnvVarsIntoManagerContainer method silently skips injection if the manager
container is not found, which masks misconfiguration. Modify the
injectEnvVarsIntoManagerContainer method to return a bool or error indicating
whether the manager container was successfully found and updated. In the code
block where injectEnvVarsIntoManagerContainer is called (around line 541), check
the return value and log a warning if the manager container was not found,
ensuring that misconfigured setups become visible rather than silently no-oping.
Apply the same fix to all other locations where
injectEnvVarsIntoManagerContainer is called as mentioned in the comment range
568-600.
🤖 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 `@cmd/deploy_test.go`:
- Around line 156-162: The test case "operator-env comma separated" expects
comma-separated environment variables to be parsed into separate map entries,
but the ParseOperatorEnvVar function only splits on the first equals sign and
does not implement comma-splitting logic. Either remove this test case entirely
if comma-separated values are not a supported feature, or implement
comma-splitting logic in the apply function that handles the --operator-env flag
to split the value on commas before individually parsing each key-value pair
into the EnvVars map.

In `@internal/deployer/operator_env_test.go`:
- Around line 74-75: The TestEnvVarsToSortedList function has an extra blank
line before it, causing a gofmt formatting violation. Remove the extra blank
line that appears immediately before the TestEnvVarsToSortedList function
declaration to comply with Go formatting standards and resolve the CI formatting
drift.

In `@internal/deployer/operator.go`:
- Around line 327-332: The logging of operator environment variables in
operator.go is exposing raw values which can leak sensitive secrets or tokens
into deploy logs. In the loop that iterates over
envVarsToSortedList(d.config.Operator.EnvVars), modify the logger.Dimf call to
only log the environment variable names by removing the value part from the
format string and arguments. Apply the identical fix to operator_olm.go at lines
36-41 where the same pattern exists.

---

Nitpick comments:
In `@internal/deployer/operator.go`:
- Around line 541-545: The injectEnvVarsIntoManagerContainer method silently
skips injection if the manager container is not found, which masks
misconfiguration. Modify the injectEnvVarsIntoManagerContainer method to return
a bool or error indicating whether the manager container was successfully found
and updated. In the code block where injectEnvVarsIntoManagerContainer is called
(around line 541), check the return value and log a warning if the manager
container was not found, ensuring that misconfigured setups become visible
rather than silently no-oping. Apply the same fix to all other locations where
injectEnvVarsIntoManagerContainer is called as mentioned in the comment range
568-600.
🪄 Autofix (Beta)

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: Path: .coderabbit.yml

Review profile: CHILL

Plan: Enterprise

Run ID: af74d7fd-907a-4995-a9bb-e8be86dbfb67

📥 Commits

Reviewing files that changed from the base of the PR and between b91d393 and dfbad9a.

📒 Files selected for processing (7)
  • cmd/deploy.go
  • cmd/deploy_test.go
  • internal/deployer/config.go
  • internal/deployer/operator.go
  • internal/deployer/operator_env.go
  • internal/deployer/operator_env_test.go
  • internal/deployer/operator_olm.go

Comment thread cmd/deploy_test.go Outdated
Comment thread internal/deployer/operator_env_test.go Outdated
Comment thread internal/deployer/operator.go
Make env var injection fail loudly instead of silently succeeding
when the manager container is not found in the deployment spec.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

@porridge porridge left a comment

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.

Feedback addressed, I also added a change to complain loudly if injection was requested and did not happen (in case the manager deployment structure evolved behind our back).
PTAL.

Comment thread internal/deployer/operator_env.go Outdated
Comment thread internal/deployer/operator_env.go Outdated
@porridge

Copy link
Copy Markdown
Contributor Author

PTAL @mclasmeier

@porridge porridge requested a review from mclasmeier June 25, 2026 05:38
@mclasmeier mclasmeier merged commit 189e745 into main Jun 25, 2026
12 checks passed
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.

3 participants