Skip to content

fix(terraform): normalize numeric inputs and prevent phantom memory flags - #450

Merged
mfielding merged 1 commit into
google:masterfrom
mfielding:fix-numeric-inputs
Sep 10, 2026
Merged

mfielding merged 1 commit into
google:masterfrom
mfielding:fix-numeric-inputs

Conversation

@mfielding

Copy link
Copy Markdown
Member

Summary

Fixes string comparison antipattern (var.X != "") on numeric Terraform variables in terraform/main.tf and terraform/variables.tf.

Problem Statement

  1. Phantom CLI Flags (0 != "" Coercion):
    In terraform/variables.tf, ora_pga_target_mb and ora_sga_target_mb default to 0 to signal that the toolkit should automatically compute memory (45% of host RAM for SGA, HugePages allocation, and baseline PGA). However, terraform/main.tf checks:
    var.ora_pga_target_mb != "" ? "--ora-pga-target-mb ${var.ora_pga_target_mb}" : "",
    var.ora_sga_target_mb != "" ? "--ora-sga-target-mb ${var.ora_sga_target_mb}" : "",
    In HCL, comparing a number to "" performs implicit string coercion: "0" != "" evaluates to true. This causes --ora-pga-target-mb 0 and --ora-sga-target-mb 0 to always be passed on standard deployments when unset by the caller.
  2. Runtime Errors on null:
    When callers pass null into module arguments, null != "" evaluates to true, and string template interpolation ("${var.ora_pga_target_mb}") aborts with:
    Invalid interpolation: null value cannot be formatted as a string.
  3. Redo Log Count Typing & Validation:
    ora_redo_log_count was previously typed as string with a regex check (^[0-9]+$) to work around the string idiom. Passing null fails validation because null == "" is false and regex fails can().
  4. Port Number Validation:
    ora_listener_port and tls_listener_port validation rules (var.port >= 1) crash if a caller passes null.

Solution

  1. Replace String Checks with try(var.X > 0, false):
    In local.common_flags, replace var.num != "" with try(var.num > 0, false) for ora_pga_target_mb, ora_sga_target_mb, ora_listener_port, tls_listener_port, and ora_redo_log_count.
  2. Null-Safe Validations:
    Update validation conditions across all numeric inputs in variables.tf to explicitly permit null.
  3. Flexible ora_redo_log_count:
    Change ora_redo_log_count type to any with validation allowing numbers, strings (e.g. "2" in existing tfvars templates), empty string, or null.
  4. Firewall Port Casting:
    Add tostring() to firewall rule port mapping to ensure strict schema compliance.

Test Results & Matrix

A verification test suite was executed across all 7 caller scenarios (defaults, explicit tuning, explicit zero, nulls, custom ports, string numbers, and TLS).

The full test matrix output is published in this GitHub Gist:
https://gist.github.com/mfielding/e7c3a4e9db5bd7594ff6d00309fe3a22

Comment thread terraform/main.tf Outdated
var.ora_release != "" ? "--ora-release ${var.ora_release}" : "",
var.ora_edition != "" ? "--ora-edition ${var.ora_edition}" : "",
var.ora_listener_port != "" ? "--ora-listener-port ${var.ora_listener_port}" : "",
try(var.ora_listener_port > 0, false) ? "--ora-listener-port ${var.ora_listener_port}" : "",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should we add tonumber() here as well?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added.

Comment thread terraform/main.tf Outdated
var.skip_database_config ? "--skip-database-config" : "",
var.ora_pga_target_mb != "" ? "--ora-pga-target-mb ${var.ora_pga_target_mb}" : "",
var.ora_sga_target_mb != "" ? "--ora-sga-target-mb ${var.ora_sga_target_mb}" : "",
try(var.ora_pga_target_mb > 0, false) ? "--ora-pga-target-mb ${var.ora_pga_target_mb}" : "",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

and here as well

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I actually added it to all the numerical checks.

@google-oss-prow

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: AlexBasinov, mfielding

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:
  • OWNERS [AlexBasinov,mfielding]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@mfielding

Copy link
Copy Markdown
Member Author

/retest

@mfielding

Copy link
Copy Markdown
Member Author

/test oracle-toolkit-install-26ai-data-guard-on-gcp
/test oracle-toolkit-install-data-guard-on-gcp

1 similar comment
@mfielding

Copy link
Copy Markdown
Member Author

/test oracle-toolkit-install-26ai-data-guard-on-gcp
/test oracle-toolkit-install-data-guard-on-gcp

…lags

Fix string comparison antipattern (var.X != "") on numeric Terraform
variables. When ora_pga_target_mb or ora_sga_target_mb was unset (default 0),
HCL string coercion evaluated "0" != "" to true, always passing
--ora-pga-target-mb 0 and --ora-sga-target-mb 0 to install-oracle.sh.

Furthermore, passing null to numeric variables caused string interpolation
runtime errors.

This change:
1. Updates local.common_flags to use try(tonumber(var.X) > 0, false) for all numeric inputs.
2. Adds null-safe validations for ora_pga_target_mb, ora_sga_target_mb,
   ora_listener_port, and tls_listener_port.
3. Allows ora_redo_log_count to accept numbers, strings, or null while
   preserving backward compatibility with existing tfvars templates.
4. Uses tostring() for firewall port mappings to ensure strict schema adherence.
@google-oss-prow

Copy link
Copy Markdown

New changes are detected. LGTM label has been removed.

@google-oss-prow

Copy link
Copy Markdown

@mfielding: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
oracle-toolkit-install-data-guard-on-gcp 226c506 link true /test oracle-toolkit-install-data-guard-on-gcp
oracle-toolkit-install-26ai-data-guard-on-gcp 226c506 link true /test oracle-toolkit-install-26ai-data-guard-on-gcp
Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@mfielding
mfielding merged commit 9e590f2 into google:master Sep 10, 2026
10 of 12 checks passed
@mfielding
mfielding deleted the fix-numeric-inputs branch September 10, 2026 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants