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

Using stackrox/storage/ResourceCollection #1622

Closed
wants to merge 11 commits into from

Conversation

JoukoVirtanen
Copy link
Contributor

@JoukoVirtanen JoukoVirtanen commented Mar 29, 2024

Description

Uses ResourceCollection storage object for filtering at the cluster and namespace levels.

Builds on #1620 by adding the use of the AND operator, checking cluster level rules, checking embedded collections, and more unit tests.

Checklist

  • Investigated and inspected CI test results
  • Updated documentation accordingly

Automated testing

  • Added unit tests
    - [ ] Added integration tests
    - [ ] Added regression tests

The changes are not currently wired to anything that would impact integration tests.

If any of these don't apply, please comment below.

Testing Performed

CI is sufficient

}
}
return false;
}
Copy link
Contributor Author

@JoukoVirtanen JoukoVirtanen Mar 30, 2024

Choose a reason for hiding this comment

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

Possible improvements to this function include checks for cycles and stopping at a maximum depth.

collector/lib/ResourceSelector.h Show resolved Hide resolved
collector/lib/ResourceSelector.cpp Outdated Show resolved Hide resolved
collector/lib/ResourceSelector.cpp Outdated Show resolved Hide resolved
collector/lib/ResourceSelector.cpp Outdated Show resolved Hide resolved
collector/lib/ResourceSelector.cpp Outdated Show resolved Hide resolved
collector/lib/ResourceSelector.cpp Outdated Show resolved Hide resolved
collector/lib/ResourceSelector.cpp Outdated Show resolved Hide resolved
collector/lib/ResourceSelector.cpp Outdated Show resolved Hide resolved
return false;
}

bool ResourceSelector::AreClusterAndNamespaceSelected(const storage::ResourceCollection& rc, const std::string& cluster, const std::string& ns) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

One alternative could be to have a function that strips out all of the ResourceSelectors where the cluster does not match collector's cluster.

collector/lib/ResourceSelector.cpp Outdated Show resolved Hide resolved
collector/lib/ResourceSelector.cpp Outdated Show resolved Hide resolved
collector/lib/ResourceSelector.cpp Outdated Show resolved Hide resolved
collector/lib/ResourceSelector.cpp Outdated Show resolved Hide resolved
collector/lib/ResourceSelector.cpp Outdated Show resolved Hide resolved
@JoukoVirtanen JoukoVirtanen requested a review from Molter73 April 3, 2024 15:37

namespace collector {

class ResourceSelector {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The use of a class here feels a little redundant since everything is static anyway. A namespace might be a better fit

Comment on lines +23 to +24
static bool IsRuleFollowed(const storage::SelectorRule& rule, const std::string& ns);
static bool IsRuleValueFollowed(const storage::RuleValue& value, const std::string& ns);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could also use overloading here and call both functions IsRuleFollowed (or IsFollowed to be type-agnostic)

Comment on lines +104 to +113
for (const auto& embeddedCollection : rc.embedded_collections()) {
auto embeddedRc = rcMap.find(embeddedCollection.id());
if (embeddedRc != rcMap.end()) {
bool inEmbeddedRc = AreClusterAndNamespaceSelected(embeddedRc->second, rcMap, cluster, ns);
if (inEmbeddedRc) {
return true;
}
}
}
return false;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can this be std::any_of as well?

Comment on lines +107 to +108
bool inEmbeddedRc = AreClusterAndNamespaceSelected(embeddedRc->second, rcMap, cluster, ns);
if (inEmbeddedRc) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
bool inEmbeddedRc = AreClusterAndNamespaceSelected(embeddedRc->second, rcMap, cluster, ns);
if (inEmbeddedRc) {
if (AreClusterAndNamespaceSelected(embeddedRc->second, rcMap, cluster, ns)) {

Comment on lines +25 to +27
static bool IsResourceInResourceSelector(const storage::ResourceSelector& rs, const std::string& resource_type, const std::string& resource_name);
static bool IsNamespaceInResourceSelector(const storage::ResourceSelector& rs, const std::string& ns);
static bool IsClusterInResourceSelector(const storage::ResourceSelector& rs, const std::string& cluster);
Copy link
Collaborator

Choose a reason for hiding this comment

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

[nit] these are pretty verbose and the type name suffix is unnecessary - perhaps ContainsResource ContainsNamespace ContainsCluster?


for (auto filteringRule : filteringRules) {
auto rc = rcMap.find(filteringRule.collectionId());
if (rc != rcMap.end()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Need to decide what to do if the key is not in the map.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants