-
Notifications
You must be signed in to change notification settings - Fork 138
add srelib poc #922
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
base: master
Are you sure you want to change the base?
add srelib poc #922
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,55 @@ | ||||||||||||||||
| package srelib | ||||||||||||||||
|
|
||||||||||||||||
| import ( | ||||||||||||||||
| "fmt" | ||||||||||||||||
| "os/exec" | ||||||||||||||||
|
|
||||||||||||||||
| "github.com/hashicorp/go-hclog" | ||||||||||||||||
| "github.com/hashicorp/go-plugin" | ||||||||||||||||
| cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" | ||||||||||||||||
|
|
||||||||||||||||
| "github.com/petrkotas/srelib/sdk" | ||||||||||||||||
| v1 "github.com/petrkotas/srelib/sdk/v1" | ||||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
| type Client struct { | ||||||||||||||||
| inner v1.Client | ||||||||||||||||
| killer *plugin.Client | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func NewClient(pluginPath string) (*Client, error) { | ||||||||||||||||
| pc := plugin.NewClient(&plugin.ClientConfig{ | ||||||||||||||||
| HandshakeConfig: sdk.HandshakeConfig, | ||||||||||||||||
| VersionedPlugins: map[int]plugin.PluginSet{ | ||||||||||||||||
| 1: {"srelib": &v1.Plugin{}}, | ||||||||||||||||
| }, | ||||||||||||||||
| Cmd: exec.Command(pluginPath), | ||||||||||||||||
| Logger: hclog.New(&hclog.LoggerOptions{Name: "srelib", Level: hclog.Error}), | ||||||||||||||||
| }) | ||||||||||||||||
|
|
||||||||||||||||
| rpcClient, err := pc.Client() | ||||||||||||||||
| if err != nil { | ||||||||||||||||
| pc.Kill() | ||||||||||||||||
| return nil, fmt.Errorf("srelib: connect to plugin: %w", err) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| raw, err := rpcClient.Dispense("srelib") | ||||||||||||||||
| if err != nil { | ||||||||||||||||
| pc.Kill() | ||||||||||||||||
| return nil, fmt.Errorf("srelib: dispense plugin: %w", err) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| return &Client{inner: raw.(v1.Client), killer: pc}, nil | ||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: find . -type f -name "client.go" | grep srelibRepository: openshift/osdctl Length of output: 83 🏁 Script executed: head -60 pkg/srelib/client.go | cat -nRepository: openshift/osdctl Length of output: 1697 Unguarded type assertion from plugin dispense can panic.
Proposed fix- return &Client{inner: raw.(v1.Client), killer: pc}, nil
+ inner, ok := raw.(v1.Client)
+ if !ok {
+ pc.Kill()
+ return nil, fmt.Errorf("srelib: unexpected plugin client type %T", raw)
+ }
+ return &Client{inner: inner, killer: pc}, nil📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func (c *Client) GetClusters(ids []string) ([]*cmv1.Cluster, error) { | ||||||||||||||||
| return c.inner.GetClusters(ids) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func (c *Client) GetClusterAnyStatus(id string) (*cmv1.Cluster, error) { | ||||||||||||||||
| return c.inner.GetClusterAnyStatus(id) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func (c *Client) Close() { | ||||||||||||||||
| c.killer.Kill() | ||||||||||||||||
| } | ||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,8 +20,13 @@ import ( | |
|
|
||
| ocmConfig "github.com/openshift-online/ocm-common/pkg/ocm/config" | ||
| ocmConnBuilder "github.com/openshift-online/ocm-common/pkg/ocm/connection-builder" | ||
| srelibpkg "github.com/openshift/osdctl/pkg/srelib" | ||
| ) | ||
|
|
||
| var activeSrelibClient *srelibpkg.Client | ||
|
|
||
| func SetSrelibClient(c *srelibpkg.Client) { activeSrelibClient = c } | ||
|
|
||
| const ClusterServiceClusterSearch = "id = '%s' or name = '%s' or external_id = '%s'" | ||
|
|
||
| const ( | ||
|
|
@@ -59,36 +64,17 @@ var urlAliases = map[string]string{ | |
| stagingGovURL: stagingGovURL, | ||
| } | ||
|
|
||
| // GetClusterAnyStatus returns an OCM cluster object given an OCM connection and cluster id | ||
| // GetClusterAnyStatus returns an OCM cluster object given a cluster id | ||
| // (internal id, external id, and name all supported). | ||
| func GetClusterAnyStatus(conn *sdk.Connection, clusterId string) (*cmv1.Cluster, error) { | ||
| // identifier in the accounts management service. To find those clusters we need to check | ||
| // directly in the clusters management service. | ||
| clustersSearch := fmt.Sprintf(ClusterServiceClusterSearch, clusterId, clusterId, clusterId) | ||
| clustersListResponse, err := conn.ClustersMgmt().V1().Clusters().List().Search(clustersSearch).Size(1).Send() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("can't retrieve clusters for clusterId '%s': %w", clusterId, err) | ||
| } | ||
|
|
||
| // If there is exactly one cluster matching then return it: | ||
| clustersTotal := clustersListResponse.Total() | ||
| if clustersTotal == 1 { | ||
| return clustersListResponse.Items().Slice()[0], nil | ||
| } | ||
|
|
||
| return nil, fmt.Errorf("there are %d clusters with identifier or name '%s', expected 1", clustersTotal, clusterId) | ||
| return activeSrelibClient.GetClusterAnyStatus(clusterId) | ||
| } | ||
|
|
||
| func GetClusters(ocmClient *sdk.Connection, clusterIds []string) []*cmv1.Cluster { | ||
| for i, id := range clusterIds { | ||
| clusterIds[i] = GenerateQuery(id) | ||
| } | ||
|
|
||
| clusters, err := ApplyFilters(ocmClient, []string{strings.Join(clusterIds, " or ")}) | ||
| clusters, err := activeSrelibClient.GetClusters(clusterIds) | ||
| if err != nil { | ||
| log.Fatalf("error while retrieving cluster(s) from ocm: %[1]s", err) | ||
| log.Fatalf("error retrieving cluster(s) via srelib: %s", err) | ||
| } | ||
|
Comment on lines
69
to
77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: cat -n pkg/utils/ocm.go | head -100Repository: openshift/osdctl Length of output: 4078 🏁 Script executed: # Search for activeSrelibClient initialization and usage
rg "activeSrelibClient" -B 2 -A 2Repository: openshift/osdctl Length of output: 966 🏁 Script executed: # Check for SetSrelibClient function definition
rg "SetSrelibClient" -B 3 -A 3Repository: openshift/osdctl Length of output: 661 🏁 Script executed: # Look for all calls to GetClusters to understand usage patterns
rg "GetClusters" -B 2 -A 2Repository: openshift/osdctl Length of output: 2532 🏁 Script executed: # Check for error handling in callers of GetClusters
rg "GetClusters|GetClusterAnyStatus" --type go -A 5 | head -50Repository: openshift/osdctl Length of output: 2319 🏁 Script executed: # Find test files
fd -e _test.go | head -20Repository: openshift/osdctl Length of output: 42 🏁 Script executed: # Search for calls to GetClusters and GetClusterAnyStatus in tests
rg "GetClusters|GetClusterAnyStatus" --type go -B 3 -A 3 | grep -E "(_test\.go|test_|Test)" -A 5 | head -40Repository: openshift/osdctl Length of output: 42 🏁 Script executed: # Check if tests initialize the client
rg "SetSrelibClient" --type goRepository: openshift/osdctl Length of output: 191 🏁 Script executed: # Look for alternative entrypoints or main functions
rg "func main" --type goRepository: openshift/osdctl Length of output: 178 🏁 Script executed: # Check docgen/main.go for potential usage
cat -n utils/docgen/main.go | head -80Repository: openshift/osdctl Length of output: 222 🏁 Script executed: # Check for any other imports or usage of GetClusters/GetClusterAnyStatus in tools
rg "GetClusters|GetClusterAnyStatus" utils/docgen/ 2>/dev/null || echo "No usage in docgen"Repository: openshift/osdctl Length of output: 79 🏁 Script executed: # Check if there are any test directories at all
find . -type d -name "*test*" 2>/dev/null | head -10Repository: openshift/osdctl Length of output: 131 🏁 Script executed: # Look for go.sum or go.mod to understand if there's a testing framework
ls -la | grep -E "go\.(mod|sum)|Makefile|\.github"Repository: openshift/osdctl Length of output: 295 🏁 Script executed: # Check callers of GetClusters and GetClusterAnyStatus to understand usage patterns
rg "GetClusters|GetClusterAnyStatus" --type go -B 2 -A 5 | grep -E "(cmd|pkg)/[^/]+\.(go|ts)" | head -50Repository: openshift/osdctl Length of output: 42 🏁 Script executed: # Verify the exact error handling expectations from callers
rg "GetClusters\(" -A 3 --type go | head -50Repository: openshift/osdctl Length of output: 2191 Guard global client access and stop terminating process from utility helpers. These helpers directly dereference the uninitialized Suggested fix func GetClusterAnyStatus(conn *sdk.Connection, clusterId string) (*cmv1.Cluster, error) {
- return activeSrelibClient.GetClusterAnyStatus(clusterId)
+ if activeSrelibClient == nil {
+ return nil, fmt.Errorf("srelib client is not initialized")
+ }
+ return activeSrelibClient.GetClusterAnyStatus(clusterId)
}
-func GetClusters(ocmClient *sdk.Connection, clusterIds []string) []*cmv1.Cluster {
- clusters, err := activeSrelibClient.GetClusters(clusterIds)
- if err != nil {
- log.Fatalf("error retrieving cluster(s) via srelib: %s", err)
- }
- return clusters
+func GetClusters(ocmClient *sdk.Connection, clusterIds []string) ([]*cmv1.Cluster, error) {
+ if activeSrelibClient == nil {
+ return nil, fmt.Errorf("srelib client is not initialized")
+ }
+ return activeSrelibClient.GetClusters(clusterIds)
}🤖 Prompt for AI Agents |
||
|
|
||
| return clusters | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: openshift/osdctl
Length of output: 425
Remove local
replacedirective; it blocks builds in any clean environment.replace github.com/petrkotas/srelib => ../srelibwill fail immediately on CI systems and consumer builds since../srelibis absent from the repository. This is a build blocker, not just a reproducibility issue. Either remove the replace directive or reference a resolvable module version (published release, commit-based pseudo-version, or git branch).Per supply chain security guidelines, all dependency references must be reproducible and pinnable. Local filesystem paths violate this requirement and must not reach production.
🤖 Prompt for AI Agents
Source: Coding guidelines