Skip to content

SREP-711: Add non-interactive command input to ssm command#716

Merged
openshift-merge-bot[bot] merged 2 commits intoopenshift:mainfrom
MateSaary:srep-711-ssm-non-interactive
Jul 9, 2025
Merged

SREP-711: Add non-interactive command input to ssm command#716
openshift-merge-bot[bot] merged 2 commits intoopenshift:mainfrom
MateSaary:srep-711-ssm-non-interactive

Conversation

@MateSaary
Copy link
Member

@MateSaary MateSaary commented Jun 25, 2025

What type of PR is this?

  • Bug
  • Feature
  • Documentation
  • Test Coverage
  • Clean Up
  • Others

What this PR does / Why we need it?

This PR improves upon the cloud ssm command by allowing non-interactive command execution within the SSM session through the -- separator.

$ ocm backplane cloud ssm --node=some-node -- free -m
# Here, 'free -m' would be executed within the SSM session and the result returned.

The PR also removes some minor redundant unit test code variables.

Which Jira/Github issue(s) does this PR fix?

Special notes for your reviewer

Unit Test Coverage

Guidelines

  • If it's a new sub-command or new function to an existing sub-command, please cover at least 50% of the code
  • If it's a bug fix for an existing sub-command, please cover 70% of the code

Test coverage checks

  • Added unit tests
  • Created jira card to add unit test
  • This PR may not need unit tests

Pre-checks (if applicable)

  • Ran unit tests locally
  • Validated the changes in a cluster
  • Included documentation changes with PR

@openshift-ci-robot
Copy link

openshift-ci-robot commented Jun 25, 2025

@MateSaary: This pull request references SREP-711 which is a valid jira issue.

Warning: The referenced jira issue has an invalid target version for the target branch this PR targets: expected the story to target the "4.20.0" version, but no target version was set.

Details

In response to this:

What type of PR is this?

  • Bug
  • Feature
  • Documentation
  • Test Coverage
  • Clean Up
  • Others

What this PR does / Why we need it?

This PR improves upon the cloud ssm command by allowing non-interactive command execution within the SSM session through the -- separator.

The PR also removes some minor redundant unit test code variables.

Which Jira/Github issue(s) does this PR fix?

Special notes for your reviewer

Unit Test Coverage

Guidelines

  • If it's a new sub-command or new function to an existing sub-command, please cover at least 50% of the code
  • If it's a bug fix for an existing sub-command, please cover 70% of the code

Test coverage checks

  • Added unit tests
  • Created jira card to add unit test
  • This PR may not need unit tests

Pre-checks (if applicable)

  • Ran unit tests locally
  • Validated the changes in a cluster
  • Included documentation changes with PR

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-robot openshift-ci-robot added the jira/valid-reference Indicates that this PR references a valid Jira ticket of any type. label Jun 25, 2025
@openshift-ci openshift-ci bot requested review from a7vicky and bmeng June 25, 2025 10:20
@MateSaary MateSaary force-pushed the srep-711-ssm-non-interactive branch from 6d8dfaa to 863a364 Compare June 25, 2025 13:44
@codecov-commenter
Copy link

codecov-commenter commented Jun 25, 2025

Codecov Report

Attention: Patch coverage is 76.92308% with 6 lines in your changes missing coverage. Please review.

Project coverage is 50.80%. Comparing base (b19177a) to head (197cb49).

Files with missing lines Patch % Lines
cmd/ocm-backplane/cloud/ssm.go 76.92% 6 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #716      +/-   ##
==========================================
+ Coverage   50.71%   50.80%   +0.09%     
==========================================
  Files          76       76              
  Lines        5843     5862      +19     
==========================================
+ Hits         2963     2978      +15     
- Misses       2475     2479       +4     
  Partials      405      405              
Files with missing lines Coverage Δ
cmd/ocm-backplane/cloud/ssm.go 55.70% <76.92%> (+3.39%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@openshift-ci-robot
Copy link

openshift-ci-robot commented Jun 26, 2025

@MateSaary: This pull request references SREP-711 which is a valid jira issue.

Warning: The referenced jira issue has an invalid target version for the target branch this PR targets: expected the story to target the "4.20.0" version, but no target version was set.

Details

In response to this:

What type of PR is this?

  • Bug
  • Feature
  • Documentation
  • Test Coverage
  • Clean Up
  • Others

What this PR does / Why we need it?

This PR improves upon the cloud ssm command by allowing non-interactive command execution within the SSM session through the -- separator.

$ ocm backplane cloud ssm --node a-node -- free -m

The PR also removes some minor redundant unit test code variables.

Which Jira/Github issue(s) does this PR fix?

Special notes for your reviewer

Unit Test Coverage

Guidelines

  • If it's a new sub-command or new function to an existing sub-command, please cover at least 50% of the code
  • If it's a bug fix for an existing sub-command, please cover 70% of the code

Test coverage checks

  • Added unit tests
  • Created jira card to add unit test
  • This PR may not need unit tests

Pre-checks (if applicable)

  • Ran unit tests locally
  • Validated the changes in a cluster
  • Included documentation changes with PR

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-robot
Copy link

openshift-ci-robot commented Jun 26, 2025

@MateSaary: This pull request references SREP-711 which is a valid jira issue.

Warning: The referenced jira issue has an invalid target version for the target branch this PR targets: expected the story to target the "4.20.0" version, but no target version was set.

Details

In response to this:

What type of PR is this?

  • Bug
  • Feature
  • Documentation
  • Test Coverage
  • Clean Up
  • Others

What this PR does / Why we need it?

This PR improves upon the cloud ssm command by allowing non-interactive command execution within the SSM session through the -- separator.

$ ocm backplane cloud ssm --node=some-node -- free -m

The PR also removes some minor redundant unit test code variables.

Which Jira/Github issue(s) does this PR fix?

Special notes for your reviewer

Unit Test Coverage

Guidelines

  • If it's a new sub-command or new function to an existing sub-command, please cover at least 50% of the code
  • If it's a bug fix for an existing sub-command, please cover 70% of the code

Test coverage checks

  • Added unit tests
  • Created jira card to add unit test
  • This PR may not need unit tests

Pre-checks (if applicable)

  • Ran unit tests locally
  • Validated the changes in a cluster
  • Included documentation changes with PR

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-robot
Copy link

openshift-ci-robot commented Jun 26, 2025

@MateSaary: This pull request references SREP-711 which is a valid jira issue.

Warning: The referenced jira issue has an invalid target version for the target branch this PR targets: expected the story to target the "4.20.0" version, but no target version was set.

Details

In response to this:

What type of PR is this?

  • Bug
  • Feature
  • Documentation
  • Test Coverage
  • Clean Up
  • Others

What this PR does / Why we need it?

This PR improves upon the cloud ssm command by allowing non-interactive command execution within the SSM session through the -- separator.

$ ocm backplane cloud ssm --node=some-node -- free -m
# Here, 'free -m' would be executed within the SSM session and the result returned.

The PR also removes some minor redundant unit test code variables.

Which Jira/Github issue(s) does this PR fix?

Special notes for your reviewer

Unit Test Coverage

Guidelines

  • If it's a new sub-command or new function to an existing sub-command, please cover at least 50% of the code
  • If it's a bug fix for an existing sub-command, please cover 70% of the code

Test coverage checks

  • Added unit tests
  • Created jira card to add unit test
  • This PR may not need unit tests

Pre-checks (if applicable)

  • Ran unit tests locally
  • Validated the changes in a cluster
  • Included documentation changes with PR

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.

for i, arg := range args {
if arg == "--" {
if i+1 < len(args) {
return args[i+1:]
Copy link
Contributor

Choose a reason for hiding this comment

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

We might not need to do this by our own.

https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02

Guideline 10:
    The first -- argument that is not an option-argument should be accepted as a delimiter indicating the end of options. Any following arguments should be treated as operands, even if they begin with the '-' character.

The backplane elevate command is doing something similar, can use it as reference:

func runElevate(cmd *cobra.Command, argv []string) error {

An example given by GenAI:

// main.go
package main

import (
	"fmt"
	"os"
	"os/exec"
	"strings"

	"github.com/spf13/cobra"
)

func main() {
	rootCmd := &cobra.Command{Use: "mycli"}
	rootCmd.AddCommand(echoCmd())
	if err := rootCmd.Execute(); err != nil {
		os.Exit(1)
	}
}

func echoCmd() *cobra.Command {
	var shout bool

	cmd := &cobra.Command{
		Use:   "echo",
		Short: "Pass arguments to the echo command",
		Run: func(cmd *cobra.Command, args []string) {
			fmt.Println("Flag --shout:", shout)
			fmt.Println("Args to echo:", args)

			if shout {
				for i, v := range args {
					args[i] = strings.ToUpper(v)
				}
			}

			// Run echo with args
			out, err := exec.Command("echo", args...).CombinedOutput()
			if err != nil {
				fmt.Println("Error:", err)
				return
			}
			fmt.Println("Output:", string(out))
		},
	}

	cmd.Flags().BoolVar(&shout, "shout", false, "Uppercase all args")
	return cmd
}

To run it:

go mod init mycli
go mod tidy
go run main.go echo --shout -- hello world

Flag --shout: true
Args to echo: [hello world]
Output: HELLO WORLD

Hope this helps. Let me know if I miss anything. Thanks!

Copy link
Member Author

Choose a reason for hiding this comment

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

Great point, thanks! Did want to bring one thing to attention though before refactoring as such (might simply be me misusing/misunderstanding something)...

I refactored the code to take the above into account, and re-encountered the same issue I had previously not paid much attention to, and was the reason behind the parseArgsWithDoubleDash() function in the first place:

$ ocm-backplane cloud ssm -v debug --node <some-node> -- free -m
...
INFO[0011] Non-interactive command to be executed: debug free -m
...

It seems to cause issues if we add a parameter to the command prior, and this seems to be repeated in the elevate command:

$ ocmbp elevate -v debug "REASON" -- get pods
error: unknown command "REASON" for "oc"
ERRO[0000] exit status 1

Of course, in both cases, if we remove the preceding -v debug, the command works as normal. Is this expected behaviour or a flaw in this approach?

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for pointing out!

I think this behavior is caused by

logLevelFlag.NoOptDefVal = log.InfoLevel.String()

The -v flag has a default value info, so it doesn't take the following argument as its value. We will need an equal sign to pass a verbose level:

ocm backplane elevate -v=debug "test" -- get nodes

This behavior is from #139 . Probably we will need to revisit the -v behavior. But in this PR, let's assume the users will use -v=debug, so the debug won't be taken as a normal argument.

@feichashao
Copy link
Contributor

Hi @MateSaary , thank you very much for contributing to backplane-cli ! And thank you for keeping unit test in mind!

I left some comments in line, could you help a look and share your thoughts?

@MateSaary MateSaary requested a review from feichashao July 8, 2025 01:58
@MateSaary MateSaary force-pushed the srep-711-ssm-non-interactive branch from 863a364 to 1f3067b Compare July 8, 2025 14:57
@openshift-merge-robot openshift-merge-robot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Jul 8, 2025
@MateSaary MateSaary force-pushed the srep-711-ssm-non-interactive branch from 738efd1 to 197cb49 Compare July 8, 2025 15:09
@openshift-ci
Copy link
Contributor

openshift-ci bot commented Jul 8, 2025

@MateSaary: 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.

@MateSaary
Copy link
Member Author

Hi @feichashao thank you (again!) for your input, PTAL at the changes made now to simplify the additions significantly (in terms of maintainability), let me know if there's anything further to tweak 😄

@feichashao
Copy link
Contributor

/label tide/merge-method-squash
/lgtm
/approve

@openshift-ci openshift-ci bot added the tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. label Jul 9, 2025
@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Jul 9, 2025
@openshift-ci
Copy link
Contributor

openshift-ci bot commented Jul 9, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: feichashao, MateSaary

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:

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

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 9, 2025
@openshift-merge-bot openshift-merge-bot bot merged commit fc217b1 into openshift:main Jul 9, 2025
7 checks passed
@MateSaary MateSaary deleted the srep-711-ssm-non-interactive branch July 16, 2025 02:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. jira/valid-reference Indicates that this PR references a valid Jira ticket of any type. lgtm Indicates that a PR is ready to be merged. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants