Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OSD-26117] Add command to start ssm session for nodes of HCP clusters #585

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Dee-6777
Copy link
Contributor

@Dee-6777 Dee-6777 commented Jan 5, 2025

What type of PR is this?

feature

What this PR does / Why we need it?

This PR will add a subcommand named "ssm" to start a ssm session for HCP with a given node name in it's management cluster
Example command: ocm backplane cloud ssm --node ${NODE_NAME}

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

Resolves OSD-26117

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 (OSD-27393)
  • 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 openshift-ci bot requested review from Tessg22 and xiaoyu74 January 5, 2025 22:21
Copy link
Contributor

openshift-ci bot commented Jan 5, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Dee-6777

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

The pull request process is described here

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 Jan 5, 2025
@codecov-commenter
Copy link

codecov-commenter commented Jan 5, 2025

Codecov Report

Attention: Patch coverage is 4.22535% with 68 lines in your changes missing coverage. Please review.

Project coverage is 45.24%. Comparing base (c090c68) to head (f9bae22).
Report is 2 commits behind head on main.

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

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #585      +/-   ##
==========================================
- Coverage   45.39%   45.24%   -0.15%     
==========================================
  Files          87       88       +1     
  Lines        6759     6852      +93     
==========================================
+ Hits         3068     3100      +32     
- Misses       3311     3367      +56     
- Partials      380      385       +5     
Files with missing lines Coverage Δ
cmd/ocm-backplane/cloud/cloud.go 66.66% <100.00%> (+6.66%) ⬆️
cmd/ocm-backplane/cloud/ssm.go 2.85% <2.85%> (ø)

... and 1 file with indirect coverage changes

Copy link
Contributor

openshift-ci bot commented Jan 6, 2025

@Dee-6777: all tests passed!

Full PR test history. Your PR dashboard.

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.

@feichashao
Copy link
Contributor

Follow up card to add test cases https://issues.redhat.com/browse/OSD-27393

// getInstanceID fetches the instance id of the given node
func getInstanceID(Node string) (string, error) {
logger.Infof("Fetching instance ID for node: %s", Node)
command := fmt.Sprintf("oc get node %s -o jsonpath='{.spec.providerID}' | awk -F/ '{print $NF}'", Node)
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's try using client-go whenever possible instead of calling the oc command directly.
Could you have a try to see if client-go can work for this?
https://pkg.go.dev/k8s.io/client-go/kubernetes#Clientset.CoreV1
https://pkg.go.dev/k8s.io/[email protected]/kubernetes/typed/core/v1#CoreV1Client.Nodes

Examples:

deploymentsClient := clientSet.AppsV1().Deployments(ConsoleNS)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can try to use this

@feichashao
Copy link
Contributor

@Dee-6777 Thank you for the well-struct code and a detailed README!
I left some comments, could you check them?

Comment on lines +136 to +147
command := fmt.Sprintf("aws ssm start-session --target %s", instanceID)
logger.Infof("Executing command: %s", command)

cmdExec := exec.Command("sh", "-c", command)
cmdExec.Stdout = os.Stdout
cmdExec.Stderr = os.Stderr

if err := cmdExec.Run(); err != nil {
logger.Errorf("Failed to start SSM session: %v", err)
fmt.Printf("To manually start the session, run: %s\n", command)
return err
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is acting as a wrapper around the CLI. We should be looking to use native Go libraries itself to run the command

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks pratik, I can try to implement this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/ssm - those have all the required APIs but we also need to implement the ssm plugin

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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants