-
Notifications
You must be signed in to change notification settings - Fork 174
refactor(chore): Decouple v1 API from storage.Cluster protobuf #19824
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
Open
vjwilson
wants to merge
6
commits into
master
Choose a base branch
from
vjwilson/refactor-cluster-storage-proto
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f67dbe7
Decouple v1 API from storage.Cluster protobuf
vjwilson 294a23f
Add unit tests for cluster proto conversion functions
vjwilson 8afcfb8
Restore token_service.pb.go to upstream state
vjwilson 3fef02c
Fix tests/common.go to use v1.ClusterConfig return type
vjwilson ae1f3d0
Fix remaining consumers of storage.Cluster in tests and Groovy QA code
vjwilson 7a482b4
Centralize storage-to-API cluster conversion and fix remaining consumers
vjwilson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package service | ||
|
|
||
| import ( | ||
| v1 "github.com/stackrox/rox/generated/api/v1" | ||
| "github.com/stackrox/rox/generated/storage" | ||
| clusterutil "github.com/stackrox/rox/pkg/cluster" | ||
| ) | ||
|
|
||
| // convertStorageClusterToAPI delegates to the shared conversion in pkg/cluster. | ||
| var convertStorageClusterToAPI = clusterutil.StorageClusterToAPIClusterConfig | ||
|
|
||
| // convertStorageClustersToAPI delegates to the shared conversion in pkg/cluster. | ||
| var convertStorageClustersToAPI = clusterutil.StorageClustersToAPIClusterConfigs | ||
|
|
||
| // convertAPIClusterToStorage converts an API ClusterConfig to storage.Cluster for persistence. | ||
| // | ||
| // Server-managed fields (status, health_status, most_recent_sensor_id, | ||
| // sensor_capabilities, audit_log_state, helm_config, init_bundle_id) are | ||
| // intentionally NOT copied from the API request. If a client sends values | ||
| // for these fields, they are silently ignored. This preserves backward | ||
| // compatibility with older clients that may send the full object on update, | ||
| // while ensuring server-authoritative fields are never overwritten by clients. | ||
| // The server always populates these fields from its own state. | ||
| func convertAPIClusterToStorage(config *v1.ClusterConfig) *storage.Cluster { | ||
| if config == nil { | ||
| return nil | ||
| } | ||
| return &storage.Cluster{ | ||
| Id: config.GetId(), | ||
| Name: config.GetName(), | ||
| Type: config.GetType(), | ||
| Labels: config.GetLabels(), | ||
| MainImage: config.GetMainImage(), | ||
| CollectorImage: config.GetCollectorImage(), | ||
| CentralApiEndpoint: config.GetCentralApiEndpoint(), | ||
| CollectionMethod: config.GetCollectionMethod(), | ||
| AdmissionController: config.GetAdmissionController(), | ||
| AdmissionControllerUpdates: config.GetAdmissionControllerUpdates(), | ||
| AdmissionControllerEvents: config.GetAdmissionControllerEvents(), | ||
| AdmissionControllerFailOnError: config.GetAdmissionControllerFailOnError(), | ||
| DynamicConfig: config.GetDynamicConfig(), | ||
| TolerationsConfig: config.GetTolerationsConfig(), | ||
| SlimCollector: config.GetSlimCollector(), | ||
| Priority: config.GetPriority(), | ||
| ManagedBy: config.GetManagedBy(), | ||
| // Server-managed fields below are intentionally omitted. | ||
| // Values sent by clients for these fields are silently discarded: | ||
| // - Status | ||
| // - HealthStatus | ||
| // - HelmConfig | ||
| // - MostRecentSensorId | ||
| // - AuditLogState | ||
| // - InitBundleId | ||
| // - SensorCapabilities | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| package service | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| v1 "github.com/stackrox/rox/generated/api/v1" | ||
| "github.com/stackrox/rox/generated/storage" | ||
| "github.com/stackrox/rox/pkg/protoassert" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestConvertAPIClusterToStorage_Nil(t *testing.T) { | ||
| assert.Nil(t, convertAPIClusterToStorage(nil)) | ||
| } | ||
|
|
||
| func TestConvertAPIClusterToStorage_UserSettableFields(t *testing.T) { | ||
| config := &v1.ClusterConfig{ | ||
| Id: "test-id", | ||
| Name: "test-cluster", | ||
| Type: storage.ClusterType_KUBERNETES_CLUSTER, | ||
| Labels: map[string]string{"env": "dev"}, | ||
| MainImage: "quay.io/stackrox/main:latest", | ||
| CollectorImage: "quay.io/stackrox/collector:latest", | ||
| CentralApiEndpoint: "central.stackrox:443", | ||
| CollectionMethod: storage.CollectionMethod_CORE_BPF, | ||
| AdmissionController: true, | ||
| AdmissionControllerUpdates: true, | ||
| AdmissionControllerEvents: true, | ||
| AdmissionControllerFailOnError: true, | ||
| DynamicConfig: &storage.DynamicClusterConfig{ | ||
| DisableAuditLogs: true, | ||
| }, | ||
| TolerationsConfig: &storage.TolerationsConfig{ | ||
| Disabled: true, | ||
| }, | ||
| SlimCollector: true, | ||
| Priority: 5, | ||
| ManagedBy: storage.ManagerType_MANAGER_TYPE_HELM_CHART, | ||
| } | ||
|
|
||
| result := convertAPIClusterToStorage(config) | ||
|
|
||
| assert.Equal(t, config.GetId(), result.GetId()) | ||
| assert.Equal(t, config.GetName(), result.GetName()) | ||
| assert.Equal(t, config.GetType(), result.GetType()) | ||
| assert.Equal(t, config.GetLabels(), result.GetLabels()) | ||
| assert.Equal(t, config.GetMainImage(), result.GetMainImage()) | ||
| assert.Equal(t, config.GetCollectorImage(), result.GetCollectorImage()) | ||
| assert.Equal(t, config.GetCentralApiEndpoint(), result.GetCentralApiEndpoint()) | ||
| assert.Equal(t, config.GetCollectionMethod(), result.GetCollectionMethod()) | ||
| assert.Equal(t, config.GetAdmissionController(), result.GetAdmissionController()) | ||
| assert.Equal(t, config.GetAdmissionControllerUpdates(), result.GetAdmissionControllerUpdates()) | ||
| assert.Equal(t, config.GetAdmissionControllerEvents(), result.GetAdmissionControllerEvents()) | ||
| assert.Equal(t, config.GetAdmissionControllerFailOnError(), result.GetAdmissionControllerFailOnError()) | ||
| protoassert.Equal(t, config.GetDynamicConfig(), result.GetDynamicConfig()) | ||
| protoassert.Equal(t, config.GetTolerationsConfig(), result.GetTolerationsConfig()) | ||
| assert.Equal(t, config.GetSlimCollector(), result.GetSlimCollector()) | ||
| assert.Equal(t, config.GetPriority(), result.GetPriority()) | ||
| assert.Equal(t, config.GetManagedBy(), result.GetManagedBy()) | ||
| } | ||
|
|
||
| func TestConvertAPIClusterToStorage_ServerManagedFieldsIgnored(t *testing.T) { | ||
| config := &v1.ClusterConfig{ | ||
| Id: "test-id", | ||
| Name: "test-cluster", | ||
| // Server-managed fields that should be silently discarded | ||
| Status: &storage.ClusterStatus{ | ||
| SensorVersion: "3.0.0", | ||
| }, | ||
| HealthStatus: &storage.ClusterHealthStatus{ | ||
| SensorHealthStatus: storage.ClusterHealthStatus_HEALTHY, | ||
| }, | ||
| HelmConfig: &storage.CompleteClusterConfig{ | ||
| ClusterLabels: map[string]string{"helm": "true"}, | ||
| }, | ||
| MostRecentSensorId: &storage.SensorDeploymentIdentification{ | ||
| SystemNamespaceId: "ns-id", | ||
| }, | ||
| AuditLogState: map[string]*storage.AuditLogFileState{ | ||
| "node1": {CollectLogsSince: nil}, | ||
| }, | ||
| InitBundleId: "init-bundle-123", | ||
| SensorCapabilities: []string{"cap1", "cap2"}, | ||
| } | ||
|
|
||
| result := convertAPIClusterToStorage(config) | ||
|
|
||
| assert.Equal(t, "test-id", result.GetId()) | ||
| assert.Equal(t, "test-cluster", result.GetName()) | ||
| assert.Nil(t, result.GetStatus()) | ||
| assert.Nil(t, result.GetHealthStatus()) | ||
| assert.Nil(t, result.GetHelmConfig()) | ||
| assert.Nil(t, result.GetMostRecentSensorId()) | ||
| assert.Nil(t, result.GetAuditLogState()) | ||
| assert.Empty(t, result.GetInitBundleId()) | ||
| assert.Nil(t, result.GetSensorCapabilities()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
issue (complexity): Consider removing the package-level conversion vars and centralizing both directions of cluster conversion in pkg/cluster so this service just calls the shared helpers directly.
You can simplify this by:
pkg/clusteralongside the existing storage→API helpers, so we only have one implementation per direction.1. Inline the function variables
Instead of:
Call the shared helpers directly where used:
If you need test seams, define a small interface on the consumer side rather than package-level vars, e.g.:
and inject a
ClusterConverterin tests.2. Move
convertAPIClusterToStoragetopkg/clusterTo avoid having a third independent conversion implementation, move the inverse logic into
pkg/cluster(or extend the existing one there) and call it from this service.In
pkg/cluster/convert.go:Then in this service:
or even skip the local wrapper entirely and call
clusterutil.APIClusterConfigToStoragedirectly.This keeps all behavior (including the “server-managed fields are ignored” policy) in one place, reducing divergence risk and making the conversion story easier to understand.