OSD-28241: Allow building backplane config, login with provided OCM connections by nephomaniac · Pull Request #657 · openshift/backplane-cli

What type of PR is this?

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

What this PR does / Why we need it?

This PR allows an OCM connection to be provided to the config building and login functions. ie adding new '*WithConn()' function variants allowing the caller to provide the OCM connection. This PR attempts to allow utils sharing backplane code to interact with clusters which reside in different OCM envs within the same executable. The most relevant use case is testing/developing within pre-production STAGE and INTEGRATION OCM envs, that are serviced by Hive clusters which exist in OCM Production environment(s)

  • Today as part of the client connection setup/code-path, it is not possible to create a clean separation of OCM config to be provided to multiple backplane connections within a single executable. It appears that through a chain of functions, the use of env vars prevents this from working as desired.

  • These changes also help avoid rebuilding/tearing down multiple OCM connections during the config + login phases.

  • This may make the build/login path more consistent with regard to the OCM attributes they use. Rebuilding/refetching OCM attributes may introduce (small) windows for race conditions.

This PR is an attempt at the least intrusive method of allowing a consumer to build and persist an OCM config/connection throughout the lifecycle of a BP-client connection.

Example:

`//Example using new '*withConn' functions from this PR...
func CreateBPClient(ocmConnection *ocmsdk.Connection, clusterID string) (*k8sClient, error) {
bpcfg, err := bpconfig.GetBackplaneConfigurationWithConn(prodOcmConn)
rstcfg, err := bplogin.GetRestConfigWithConn(bp, hiveOcmConn, clusterID)
return k8sclient.New(cfg, options)
}

// Fake function to create ocm sdk connections using provided ocmConfig.
// The author can decide how the config is built, this PR allows this config to persist through the BP clients later.
func RandomOCMSDKConnectionBuilder(*ocmConfig.Config) (*sdk.Connection, error) { ..some code...}

// Create an OCM connection for each OCM environment...
prodOcmConn := RandomOCMSDKConnectionBuilder( prodOCMConfig)
stageOcmConn := RandomOCMSDKConnectionBuilder( stageOCMConfig)
intOcmConn := RandomOCMSDKConnectionBuilder( intOCMConfig)

// Create the needed Hive BP clients using prod...
hiveBPClientA := CreateBPClient(prodOcmConn, hiveClusterIdA)
hiveBPClientB := CreateBPClient(prodOcmConn, hiveClusterIdB)

// Create the target cluster BP clients using prod, stage, and int...
prodClusterBPClient := CreateBPClient(prodOcmConn, prodClusterIdB)
stagingClusterBPClient := CreateBPClient(stageOcmConn, stageClusterId)
intClusterBPClient := CreateBPClient(intOcmConn, intClusterId)

// Sample use cases with the artifacts created above...
// Today ALL osdctl utils that work with both and target cluster connections can only be used in prod.
// With this PR these utils no longer have to share the same production OCM environment.
// Some example/sudo use case we have today...

func doHiveClusterSyncAndCheckCluster(hiveClient *k8sClient, clusterClient *k8sClient, clusterID string) {
doClusterSync(hiveClient, clusterID)
checkStuff(clusterClient)
}

func doFetchCloudStuffForCluster(hiveClient *k8sClient, clusterClient *k8sClient, clusterID string){
// Requires cluster access...
clusterResource := fetchClusterResourceThing(clusterClient)
// Requires Hive access...
cloudAPI := getCloudAPIConnection(hiveClient, clusterID)

fetchCloudResourceBackingClusterResource(cloudAPI, clusterResource)
}
`

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

Special notes for your reviewer

After initial draft review, additional unit tests should be added.

Unit Test Coverage

Waiting for initial draft review, before adding remaining unit tests.
I've manually tested this branch against OSDCTL using both legacy backplane functions, and new OCM sdk connection fed backplane functions. I've connected to clusters using each path, as well as mixing these within a single util. I've also tested basic ocm-backplane login cli connections in the terminal.

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.