Skip to content

SREP-3937: Add --hive-ocm-url flag to cluster resize infra for multi-env OCM support#868

Open
nephomaniac wants to merge 1 commit intoopenshift:masterfrom
nephomaniac:SREP-3937-resize-infra-multi-env-ocm
Open

SREP-3937: Add --hive-ocm-url flag to cluster resize infra for multi-env OCM support#868
nephomaniac wants to merge 1 commit intoopenshift:masterfrom
nephomaniac:SREP-3937-resize-infra-multi-env-ocm

Conversation

@nephomaniac
Copy link
Contributor

Summary

Adds --hive-ocm-url flag to osdctl cluster resize infra command to enable multi-environment OCM support for staging/integration testing.

Changes

  • Added --hive-ocm-url optional flag to cluster resize infra command
  • Implements conditional logic to use separate OCM connections for target cluster and hive when flag is provided
  • Added early validation in New() method to fail fast on invalid OCM URLs
  • Preserves backward compatibility - original code path unchanged when flag not provided
  • Supports OCM URL aliases: 'production', 'staging', 'integration'
  • Updated unit tests in infra_node_test.go to verify early validation behavior

Implementation Details

When --hive-ocm-url is specified:

  1. Creates separate OCM connection for hive using utils.CreateConnectionWithUrl()
  2. Uses utils.GetHiveClusterWithConn() to query hive from different OCM environment
  3. Creates hive k8s client with k8s.NewWithConn()
  4. Validates OCM URL early using utils.ValidateAndResolveOcmUrl()

Testing

  • ✅ Tested in staging environment with valid --hive-ocm-url values
  • ✅ Tested in production environment with valid --hive-ocm-url values
  • ✅ Validated early failure with invalid --hive-ocm-url values
  • ✅ Backward compatibility verified (command works without --hive-ocm-url flag)
  • ✅ Unit tests passing (including new tests for validation logic)

Related

Note

This PR is part of a series adding multi-environment OCM support to osdctl commands. Will place on hold until the common implementation pattern can be reviewed across all related PRs.

Co-Authored-By: Claude Sonnet 4.5 noreply@anthropic.com

@nephomaniac
Copy link
Contributor Author

Placing on hold until all the similar commands/PRs adding multiple OCM env support can have the common impl pattern reviewed. Will unhold after the other PRs, and general pattern get a chance for review.

/hold

@openshift-ci openshift-ci bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Mar 14, 2026
@coderabbitai
Copy link

coderabbitai bot commented Mar 14, 2026

Walkthrough

Added --hive-ocm-url CLI flag to the cluster resize infra command, enabling users to specify a separate OCM environment for Hive operations. Implementation includes validation logic, multi-environment OCM connection handling, and maintains backward compatibility when the flag is not provided.

Changes

Cohort / File(s) Summary
Core Implementation
cmd/cluster/resize/infra_node.go
Added hiveOcmUrl string field to Infra struct with new --hive-ocm-url CLI flag. Implemented multi-environment OCM path when flag is set, including target/hive OCM connections and Kubernetes client setup. Maintained backward-compatible original path when flag is empty. Updated control flow and logging.
Unit Tests
cmd/cluster/resize/infra_node_test.go
Added TestHiveOcmUrlValidation with table-driven test cases for production/staging/integration URLs, full URL, invalid URL, and empty URL scenarios. Uses utils.ValidateAndResolveOcmUrl for validation with error assertion checks.
Documentation
docs/README.md, docs/osdctl_cluster_resize_infra.md
Added flag documentation for --hive-ocm-url string with description, aliases (production, staging, integration), and default behavior when not specified.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 Coding Plan
  • Generate coding plan for human review comments

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 and usage tips.

@openshift-ci-robot
Copy link

@nephomaniac: An error was encountered searching for bug SREP-3937 on the Jira server at https://issues.redhat.com. No known errors were detected, please see the full error message for details.

Full error message. No response returned: Get "https://issues.redhat.com/rest/api/2/issue/SREP-3937": GET https://issues.redhat.com/rest/api/2/issue/SREP-3937 giving up after 5 attempt(s)

Please contact an administrator to resolve this issue, then request a bug refresh with /jira refresh.

Details

In response to this:

Summary

Adds --hive-ocm-url flag to osdctl cluster resize infra command to enable multi-environment OCM support for staging/integration testing.

Changes

  • Added --hive-ocm-url optional flag to cluster resize infra command
  • Implements conditional logic to use separate OCM connections for target cluster and hive when flag is provided
  • Added early validation in New() method to fail fast on invalid OCM URLs
  • Preserves backward compatibility - original code path unchanged when flag not provided
  • Supports OCM URL aliases: 'production', 'staging', 'integration'
  • Updated unit tests in infra_node_test.go to verify early validation behavior

Implementation Details

When --hive-ocm-url is specified:

  1. Creates separate OCM connection for hive using utils.CreateConnectionWithUrl()
  2. Uses utils.GetHiveClusterWithConn() to query hive from different OCM environment
  3. Creates hive k8s client with k8s.NewWithConn()
  4. Validates OCM URL early using utils.ValidateAndResolveOcmUrl()

Testing

  • ✅ Tested in staging environment with valid --hive-ocm-url values
  • ✅ Tested in production environment with valid --hive-ocm-url values
  • ✅ Validated early failure with invalid --hive-ocm-url values
  • ✅ Backward compatibility verified (command works without --hive-ocm-url flag)
  • ✅ Unit tests passing (including new tests for validation logic)

Related

Note

This PR is part of a series adding multi-environment OCM support to osdctl commands. Will place on hold until the common implementation pattern can be reviewed across all related PRs.

Co-Authored-By: Claude Sonnet 4.5 noreply@anthropic.com

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 openshift-eng/jira-lifecycle-plugin repository.

@openshift-ci openshift-ci bot requested review from MateSaary and joshbranham March 14, 2026 01:36
@openshift-ci
Copy link
Contributor

openshift-ci bot commented Mar 14, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: nephomaniac
Once this PR has been reviewed and has the lgtm label, please assign raphaelbut for approval. For more information see the Code Review Process.

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

Details Needs approval from an approver in each of these files:

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

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Mar 14, 2026

@nephomaniac: all tests passed!

Full PR test history. Your PR dashboard.

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-sigs/prow repository. I understand the commands that are listed here.

Copy link

@coderabbitai coderabbitai bot left a comment

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 (2)
cmd/cluster/resize/infra_node.go (2)

118-124: Use the resolved OCM URL for connection creation.

Line 120 resolves the input, but the resolved value is discarded and Line 149 still uses the raw flag string. Persisting the resolved value keeps validation and connection behavior strictly aligned.

Suggested change
 	// Validate --hive-ocm-url if provided
 	if r.hiveOcmUrl != "" {
-		_, err := utils.ValidateAndResolveOcmUrl(r.hiveOcmUrl)
+		resolvedHiveOcmURL, err := utils.ValidateAndResolveOcmUrl(r.hiveOcmUrl)
 		if err != nil {
 			return fmt.Errorf("invalid --hive-ocm-url: %w", err)
 		}
+		r.hiveOcmUrl = resolvedHiveOcmURL
 	}

Also applies to: 149-151

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@cmd/cluster/resize/infra_node.go` around lines 118 - 124, The validation
discards the resolved OCM URL returned by utils.ValidateAndResolveOcmUrl and
later code still uses the raw r.hiveOcmUrl when creating the connection; change
the validation call in the infra node flow to capture the resolved value (e.g.,
resolvedHiveOcmUrl, or overwrite r.hiveOcmUrl) and then use that resolved value
when constructing the connection (the code that currently references
r.hiveOcmUrl for connection creation). Ensure you call
utils.ValidateAndResolveOcmUrl only once, persist its returned URL, and pass
that persisted resolved URL into the connection creation code paths that
currently read r.hiveOcmUrl.

141-221: Reduce duplicated client/bootstrap logic across both paths.

The two branches repeat most setup steps (cluster lookup, r.clusterId assignment, client creation, admin client creation). Extracting shared setup into helpers will lower drift risk and make future changes safer.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@cmd/cluster/resize/infra_node.go` around lines 141 - 221, The two branches
for r.hiveOcmUrl share cluster lookup, r.clusterId assignment and k8s client
creation; extract that shared setup into helper(s). Create a helper like
prepareClusterAndClients(targetConn utils.OCMConnection, hiveConn
utils.OCMConnection, hiveOcmUrl string) (or two helpers: one to resolve cluster
via utils.GetClusterAnyStatus and set r.cluster/r.clusterId, and one to create
k8s clients) that calls utils.GetClusterAnyStatus, assigns r.cluster and
r.clusterId, and returns c, hc, hac (or returns error). In the hive-path call
utils.CreateConnection and utils.CreateConnectionWithUrl and pass both conns and
hiveOcmUrl into the helper so it can choose k8s.NewWithConn /
k8s.NewAsBackplaneClusterAdminWithConn (preserving error wrapping with the
hiveOcmUrl); in the non-hive path call utils.CreateConnection and pass nil
hiveConn so the helper uses k8s.New / k8s.NewAsBackplaneClusterAdmin. Ensure all
original error messages that referenced r.hiveOcmUrl remain when hiveOcmUrl is
non-empty.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@cmd/cluster/resize/infra_node_test.go`:
- Around line 507-572: Test Hive OCM URL validation is calling
utils.ValidateAndResolveOcmUrl directly and expects empty input to error, but
Infra.New() treats an empty --hive-ocm-url as optional; update
TestHiveOcmUrlValidation to exercise the same path as the command by invoking
Infra.New() (or a thin wrapper that mirrors its preflight behavior) instead of
calling utils.ValidateAndResolveOcmUrl directly, adjust the test cases so empty
hiveOcmUrl expects no error, and remove the redundant conditional that calls
ValidateAndResolveOcmUrl twice; reference TestHiveOcmUrlValidation, Infra.New,
and utils.ValidateAndResolveOcmUrl when making the change.

---

Nitpick comments:
In `@cmd/cluster/resize/infra_node.go`:
- Around line 118-124: The validation discards the resolved OCM URL returned by
utils.ValidateAndResolveOcmUrl and later code still uses the raw r.hiveOcmUrl
when creating the connection; change the validation call in the infra node flow
to capture the resolved value (e.g., resolvedHiveOcmUrl, or overwrite
r.hiveOcmUrl) and then use that resolved value when constructing the connection
(the code that currently references r.hiveOcmUrl for connection creation).
Ensure you call utils.ValidateAndResolveOcmUrl only once, persist its returned
URL, and pass that persisted resolved URL into the connection creation code
paths that currently read r.hiveOcmUrl.
- Around line 141-221: The two branches for r.hiveOcmUrl share cluster lookup,
r.clusterId assignment and k8s client creation; extract that shared setup into
helper(s). Create a helper like prepareClusterAndClients(targetConn
utils.OCMConnection, hiveConn utils.OCMConnection, hiveOcmUrl string) (or two
helpers: one to resolve cluster via utils.GetClusterAnyStatus and set
r.cluster/r.clusterId, and one to create k8s clients) that calls
utils.GetClusterAnyStatus, assigns r.cluster and r.clusterId, and returns c, hc,
hac (or returns error). In the hive-path call utils.CreateConnection and
utils.CreateConnectionWithUrl and pass both conns and hiveOcmUrl into the helper
so it can choose k8s.NewWithConn / k8s.NewAsBackplaneClusterAdminWithConn
(preserving error wrapping with the hiveOcmUrl); in the non-hive path call
utils.CreateConnection and pass nil hiveConn so the helper uses k8s.New /
k8s.NewAsBackplaneClusterAdmin. Ensure all original error messages that
referenced r.hiveOcmUrl remain when hiveOcmUrl is non-empty.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: b56f40e0-9f36-488b-af3b-8e0dc3a6f6e6

📥 Commits

Reviewing files that changed from the base of the PR and between 71e44ff and 5485f25.

📒 Files selected for processing (4)
  • cmd/cluster/resize/infra_node.go
  • cmd/cluster/resize/infra_node_test.go
  • docs/README.md
  • docs/osdctl_cluster_resize_infra.md

Comment on lines +507 to +572
// TestHiveOcmUrlValidation tests the early validation of --hive-ocm-url flag in the infra resize command
func TestHiveOcmUrlValidation(t *testing.T) {
tests := []struct {
name string
hiveOcmUrl string
expectErr bool
errContains string
}{
{
name: "Valid hive-ocm-url (production)",
hiveOcmUrl: "production",
expectErr: false,
},
{
name: "Valid hive-ocm-url (staging)",
hiveOcmUrl: "staging",
expectErr: false,
},
{
name: "Valid hive-ocm-url (integration)",
hiveOcmUrl: "integration",
expectErr: false,
},
{
name: "Valid hive-ocm-url (full URL)",
hiveOcmUrl: "https://api.openshift.com",
expectErr: false,
},
{
name: "Invalid hive-ocm-url",
hiveOcmUrl: "invalid-environment",
expectErr: true,
errContains: "invalid OCM_URL",
},
{
name: "Empty hive-ocm-url",
hiveOcmUrl: "",
expectErr: true,
errContains: "empty OCM URL",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// This simulates the validation that occurs in the Infra.New() method
var err error
if tt.hiveOcmUrl != "" {
_, err = utils.ValidateAndResolveOcmUrl(tt.hiveOcmUrl)
} else {
_, err = utils.ValidateAndResolveOcmUrl(tt.hiveOcmUrl)
}

if tt.expectErr {
if err == nil {
t.Errorf("Expected error containing '%s', but got nil", tt.errContains)
} else if !strings.Contains(err.Error(), tt.errContains) {
t.Errorf("Expected error containing '%s', but got: %v", tt.errContains, err)
}
} else {
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
}
})
}
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Test assertions do not match Infra.New() behavior.

This test claims to cover early validation in Infra.New(), but it calls utils.ValidateAndResolveOcmUrl directly. It also expects empty input to error, while Infra.New() skips validation when the flag is empty (optional flag behavior). That makes the test pass while not protecting the actual command path.

Suggested correction (align test semantics with command behavior)
 		{
 			name:        "Empty hive-ocm-url",
 			hiveOcmUrl:  "",
-			expectErr:   true,
-			errContains: "empty OCM URL",
+			expectErr:   false,
 		},
 	}
 
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
-			// This simulates the validation that occurs in the Infra.New() method
+			// Simulate Infra.New() behavior: validate only when value is provided
 			var err error
 			if tt.hiveOcmUrl != "" {
 				_, err = utils.ValidateAndResolveOcmUrl(tt.hiveOcmUrl)
-			} else {
-				_, err = utils.ValidateAndResolveOcmUrl(tt.hiveOcmUrl)
 			}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@cmd/cluster/resize/infra_node_test.go` around lines 507 - 572, Test Hive OCM
URL validation is calling utils.ValidateAndResolveOcmUrl directly and expects
empty input to error, but Infra.New() treats an empty --hive-ocm-url as
optional; update TestHiveOcmUrlValidation to exercise the same path as the
command by invoking Infra.New() (or a thin wrapper that mirrors its preflight
behavior) instead of calling utils.ValidateAndResolveOcmUrl directly, adjust the
test cases so empty hiveOcmUrl expects no error, and remove the redundant
conditional that calls ValidateAndResolveOcmUrl twice; reference
TestHiveOcmUrlValidation, Infra.New, and utils.ValidateAndResolveOcmUrl when
making the change.

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

Labels

do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants