Skip to content

Add support for operator environment variables#215

Open
porridge wants to merge 3 commits into
mainfrom
operator-env-vars
Open

Add support for operator environment variables#215
porridge wants to merge 3 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, including single KEY=VALUE, comma-separated lists, and repeated flags.
    • Added the ability to set operator environment variables in configuration files under operator.envVars.
  • Enhancements
    • Automatically injects configured environment variables into the operator deployment (manager container).
    • Also injects them into the OLM subscription configuration when applicable.
  • Bug Fixes
    • Improved validation and parsing errors for malformed --operator-env expressions.
  • Tests
    • Expanded coverage for CLI flag parsing and config-file loading.

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: 85081f3a-8aea-4bdb-8104-bcee08c7a631

📥 Commits

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

📒 Files selected for processing (1)
  • internal/deployer/operator.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • internal/deployer/operator.go

📝 Walkthrough

Walkthrough

A new --operator-env CLI flag is added to the deploy command. It parses KEY=VALUE expressions via ParseOperatorEnvVar and stores them in config.Operator.EnvVars (a new map[string]string field on OperatorConfig). These env vars are then injected into operator deployments both in the non-OLM path (into the manager container) and the OLM path (into Subscription spec.config.env).

Changes

Operator Env Var Injection

Layer / File(s) Summary
OperatorConfig.EnvVars field and parse/sort helpers
internal/deployer/config.go, internal/deployer/operator_env.go, internal/deployer/operator_env_test.go
OperatorConfig gains EnvVars map[string]string. ParseOperatorEnvVar splits on the first =, validates key presence. envVarsToSortedList converts the map to a lexicographically sorted []interface{} of {name, value} entries. Tests cover standard and edge-case inputs for both helpers.
--operator-env CLI flag wiring
cmd/deploy.go, cmd/deploy_test.go
Registers --operator-env flag on newDeployCmd, calls ParseOperatorEnvVar, allocates config.Operator.EnvVars if nil, and stores the result. Tests verify single, comma-separated, repeated flags, and YAML config-file sourced env vars.
Non-OLM env injection into manager container
internal/deployer/operator.go
deployOperatorFromCSV logs configured env vars when present. createDeploymentFromCSV calls new injectEnvVarsIntoManagerContainer when EnvVars is set; the helper locates the manager container, indexes existing env entries by name, and overwrites or appends configured values.
OLM env injection into Subscription spec.config.env
internal/deployer/operator_olm.go
deployOperatorViaOLM conditionally logs configured env vars. createSubscription builds subscriptionSpec separately and, when EnvVars is non-empty, populates spec.config.env using envVarsToSortedList.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 Hop hop, the env vars flow,
From KEY=VALUE strings in a row,
The manager container gets its fill,
OLM subscriptions fit the bill.
Sorted neatly, merged with care —
A bunny's deploy beyond compare! 🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.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 PR title 'Add support for operator environment variables' directly and clearly summarizes the main change—adding a feature for configuring operator environment variables via CLI flags and YAML config across both OLM and non-OLM deployment paths.
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

// operatorEnvVarsToSortedList converts a map of env vars to a sorted list of
// {name, value} maps suitable for use in Kubernetes container or OLM Subscription specs.
func operatorEnvVarsToSortedList(envVars map[string]string) []interface{} {

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.

This is actually not "operator env vars" specific, right?
Currently it receives d.config.Operator.EnvVars, so it could just be named envVarsToSortedList or something?

Comment thread internal/deployer/operator_env.go Outdated
// ParseOperatorEnvVars parses a slice of operator environment variable strings.
// Each string can contain one or more comma-separated KEY=VALUE pairs.
// Values may contain '=' characters (only the first '=' is used as the separator).
func ParseOperatorEnvVars(envExprs []string) (map[string]string, error) {

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.

Noticed that we are only using len==1 slices in non-testing code.
🤷

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.

Good point, simplified.

Comment thread internal/deployer/operator.go Outdated
d.logger.Dim(fmt.Sprintf(" • ServiceAccount: %s", serviceAccountName))
d.logger.Dim(fmt.Sprintf(" • Setting up pull secrets: %v", d.useOperatorPullSecrets))
if len(d.config.Operator.EnvVars) > 0 {
d.logger.Dim(fmt.Sprintf(" • Custom operator env vars: %d", len(d.config.Operator.EnvVars)))

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.

There's also Dimf.

Comment thread internal/deployer/operator.go Outdated
d.logger.Dim(fmt.Sprintf(" • Custom operator env vars: %d", len(d.config.Operator.EnvVars)))
for _, envVar := range operatorEnvVarsToSortedList(d.config.Operator.EnvVars) {
ev := envVar.(map[string]interface{})
d.logger.Dim(fmt.Sprintf(" %s=%s", ev["name"], ev["value"]))

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.

Same, Dimf.

Comment thread internal/deployer/operator_env.go Outdated
if !found {
return nil, fmt.Errorf("invalid operator env var %q: expected KEY=VALUE format", part)
}
key = strings.TrimSpace(key)

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.

Q: Doing deliberately no space-trimming for the value?

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.

Ater thinking about it, dropping the trimming and comma-splitting as they would make it hard to impossible to use them in the value on purpose.

Comment thread internal/deployer/operator_env_test.go Outdated
}
return
}
if err != nil {

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.

I kind of like the conciseness of something like

require.NotNil(t, err, "...")

What's the advantage of the more verbose

if err != nil {
  t.Fatalf(...)
}

?

Comment thread internal/deployer/operator_env_test.go Outdated
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !reflect.DeepEqual(result, tt.expected) {

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.

Similarly, could do

assert.True(reflect.DeepEqual(...), ...)

Maybe (haven't checked) we could even use

assert.Equal(....)

directly.

Comment thread internal/deployer/operator_env_test.go Outdated
t.Run(tt.name, func(t *testing.T) {
result, err := ParseOperatorEnvVars(tt.input)
if tt.expectError {
if err == nil {

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.

require.Nil(...)

for conciseness?

- 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
Comment on lines +156 to +162
name: "operator-env comma separated",
args: []string{"--operator-env", "FOO=bar,BAZ=qux"},
assert: func(t *testing.T, cfg deployer.Config) {
require.NotNil(t, cfg.Operator.EnvVars, "Operator.EnvVars should be set")
assert.Equal(t, "bar", cfg.Operator.EnvVars["FOO"])
assert.Equal(t, "qux", cfg.Operator.EnvVars["BAZ"])
},

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Locate the flag plumbing implementation and confirm comma tokenization behavior.
rg -n --type=go 'func registerFlag|func withApplyFn|StringSlice|StringArray|pflag\.' cmd internal

# Show operator-env usage and related tests.
rg -n --type=go 'operator-env|ParseOperatorEnvVar|comma separated' cmd/deploy.go cmd/deploy_test.go internal/deployer/operator_env.go

Repository: stackrox/roxie

Length of output: 1705


🏁 Script executed:

# Read the operator-env flag registration
sed -n '195,215p' cmd/deploy.go

# Read the withApplyFn implementation
sed -n '48,65p' cmd/flags.go

# Read the ParseOperatorEnvVar implementation
cat -n internal/deployer/operator_env.go

Repository: stackrox/roxie

Length of output: 2960


🏁 Script executed:

# Read the full registerFlag implementation
sed -n '83,120p' cmd/flags.go

Repository: stackrox/roxie

Length of output: 565


🏁 Script executed:

# Read the cliFlag type definition and its Set/String/Type methods
sed -n '1,80p' cmd/flags.go

Repository: stackrox/roxie

Length of output: 2021


Remove or fix the comma-separated test—comma-splitting is not implemented.

The flag plumbing does not split comma-separated values before invoking the apply function. When --operator-env "FOO=bar,BAZ=qux" is parsed:

  1. Set() is called once with the full string "FOO=bar,BAZ=qux"
  2. ParseOperatorEnvVar() splits only on the first =, returning key="FOO", value="bar,BAZ=qux"
  3. The map receives EnvVars["FOO"] = "bar,BAZ=qux", not separate FOO and BAZ entries

The test assertion assert.Equal(t, "qux", cfg.Operator.EnvVars["BAZ"]) will fail because the BAZ key is never created. Either remove this test case or add comma-splitting logic to the apply function if that behavior is intended.

🤖 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 `@cmd/deploy_test.go` around lines 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.

Comment on lines +74 to +75

func TestEnvVarsToSortedList(t *testing.T) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Fix gofmt violation before merge.

CI reports formatting drift at Line 71 (extra blank line before TestEnvVarsToSortedList).

Proposed fix
 }
-
 
 func TestEnvVarsToSortedList(t *testing.T) {
🤖 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_env_test.go` around lines 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.

Source: Pipeline failures

Comment on lines +327 to +332
if len(d.config.Operator.EnvVars) > 0 {
d.logger.Dimf(" • Custom operator env vars: %d", len(d.config.Operator.EnvVars))
for _, envVar := range envVarsToSortedList(d.config.Operator.EnvVars) {
ev := envVar.(map[string]interface{})
d.logger.Dimf(" %s=%s", ev["name"], ev["value"])
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔒 Security & Privacy | 🟠 Major | ⚡ Quick win

Avoid logging raw operator env var values.

This prints user-supplied env values directly, which can leak secrets/tokens into deploy logs. Log names only (or redact values). The same pattern should be fixed in internal/deployer/operator_olm.go Line 36-41.

🔧 Suggested fix
-		for _, envVar := range envVarsToSortedList(d.config.Operator.EnvVars) {
-			ev := envVar.(map[string]interface{})
-			d.logger.Dimf("    %s=%s", ev["name"], ev["value"])
-		}
+		for _, envVar := range envVarsToSortedList(d.config.Operator.EnvVars) {
+			ev := envVar.(map[string]interface{})
+			d.logger.Dimf("    %s=<redacted>", ev["name"])
+		}
🤖 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 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.

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
if !found {
return nil, fmt.Errorf("invalid operator env var %q: expected KEY=VALUE format", part)
}
key = strings.TrimSpace(key)

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.

Ater thinking about it, dropping the trimming and comma-splitting as they would make it hard to impossible to use them in the value on purpose.

Comment thread internal/deployer/operator_env.go Outdated
// ParseOperatorEnvVars parses a slice of operator environment variable strings.
// Each string can contain one or more comma-separated KEY=VALUE pairs.
// Values may contain '=' characters (only the first '=' is used as the separator).
func ParseOperatorEnvVars(envExprs []string) (map[string]string, error) {

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.

Good point, simplified.

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