-
Notifications
You must be signed in to change notification settings - Fork 8
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
Adds in-mem PolicyReportStore
implementation (#107)
#150
Adds in-mem PolicyReportStore
implementation (#107)
#150
Conversation
df03f6f
to
48106e8
Compare
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.
I could not finish testing the PR yet. But I'll continue soon.
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.
Please, beside the comments, review the linter warning. I got some of them when building the PR locally.
internal/policies/fetcher.go
Outdated
@@ -194,7 +194,7 @@ func (f *Fetcher) getAdmissionPolicies(namespace string) ([]policiesv1.Admission | |||
return policies.Items, nil | |||
} | |||
|
|||
func newClient() (client.Client, error) { //nolint:ireturn | |||
func newClient() (client.Client, error) { //nolint:ireturn // no specific client type |
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.
Can you elaborate in the comment that you added? What do you mean exactly? ;-)
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.
to be fair, this was a quick and dirty "solution" to try to keep ireturn
lint happy. for some reason, it's a bit flaky as sometimes nolintlint
complains that that comment is not needed but it's not consistent :/
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.
Ah! I know that pain! Try remove this comment and just clean the linter cache:
./bin/golangci-lint cache clean && make lint
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.
thank you so much for your tip! It did the trick 🎉
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.
Hey, thanks for this PR and tackling this! ❤️
I would like to not add logic to cmd/root.go
, but keep it in internal/report/ and internal/scanner as it was.
Also, separate commits would indeed help for the review, better to have multiple trivial commits than one big one. I would like separate commits for moving a file without modifications (report/store.go -> report/store_kubernetes.go), or adding style changes.
0488f1a
to
599c2c0
Compare
Part of kubewarden#107 As specified in the original issue, this commit: - Create a store Go interface that satisfies the current store implementation at report/store.go - Refactor current report/store.go into report/store_kubernetes.go, as an implementation of the store interface - Adds a new memory store with 2 maps, one with `ClusterPolicyReports` and another one with `PolicyReports` - Provide an optional cli flag, --store TYPE, with possible types memory, kubernetes. Defaults to --store kubernetes for ease on first Kubewarden installations. Besides that, this commit also: - Changes `printJSON` to `outputScan` to make it more aligned with what the input params is - `outputScan` only outputs at the end of the flow, to prevent having a lot of logs whenever someone scans the entire cluster (we were outputting the entire store on each namespace loop) It also seems that running `make fmt` introduced a couple of unwanted changes that I'm happy to revert to keep this commit smaller. Finally, I also had to do a couple of tweaks to make golangci lint happy. Signed-off-by: Nuno Nelas <nuno.nelas@icloud.com>
- Removes `RetryOnConflict` since we will only need them for Kubernetes backend - Changes "enums" to lower-case (and updates README accordingly) Signed-off-by: Nuno Nelas <nuno.nelas@icloud.com>
- Removes Update methods from PolicyReportStore interface since there's no current usage for them Signed-off-by: Nuno Nelas <nuno.nelas@icloud.com>
- Removes `mutex` since there's no parallel processing - Changes logLevels for both `PolicyReportStore` to debug to prevent output of non-relevant information by default. Also, aligns what is logged between `PolicyReport` and `ClusterPolicyReport` and also `MemoryPolicyReportStore` and `KubernetesPolicyReportStore` Signed-off-by: Nuno Nelas <nuno.nelas@icloud.com>
- Removes `require` dependency from unit-tests. Instead, uses if-clauses to validate the returning errors - Given that I was changing go.mod, I also updated some of our dependencies - Still struggling with `ireturn`. Sometimes it works and build succeeds, sometimes it doesn't Signed-off-by: Nuno Nelas <nuno.nelas@icloud.com>
5af9b41
to
46b73b2
Compare
- Changes godocs since it doesn't make much sense to have an interface knowing about it's implementations Signed-off-by: Nuno Nelas <nuno.nelas@icloud.com>
- Simplifies cmd/root.go logic, since it should only read cmd flags and thus shouldn't have any business logic. There's still an `outputScan` that should also be moved after clarification of kubewarden#150 (comment) Signed-off-by: Nuno Nelas <nuno.nelas@icloud.com>
hallo @jvanz and @viccuad ! first of all, thank you so much for your suggestions/comments, really helpful to keep the codebase consistent and aligned with your standards ❤️ I think I addressed all your topics, except for #150 (comment) and #150 (comment) since I need further help/clarification for them |
- As discussed in kubewarden#150 (comment), this commit removes `outputScan` logic from `cmd` and moves it to `scanner.go` where it originally was. This ensures we don't add more business logic into a package that should only handle commands Signed-off-by: Nuno Nelas <nuno.nelas@icloud.com>
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.
internal/policies/fetcher_test.go
Outdated
var nsKubewarden = v1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "kubewarden", | ||
Labels: map[string]string{"kubernetes.io/metadata.name": "kubewarden"}, | ||
}, | ||
} | ||
|
||
func mockClient(initObjs ...k8sClient.Object) k8sClient.Client { //nolint:ireturn | ||
func mockClient(initObjs ...k8sClient.Object) k8sClient.Client { //nolint:ireturn // no specific client type |
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.
Again, try the trick about the linter that I've shared in another comment
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.
sorry for not replying to you on this thread! that suggestion you made worked so I removed the new comment from both methods
Furthermore, @nnelas can you rebase and fix the conflicts in the |
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.
Great contribution, thanks a lot @nnelas!
internal/report/store_memory.go
Outdated
cprCache map[string]ClusterPolicyReport | ||
} | ||
|
||
func NewMemoryPolicyReportStore() (*MemoryPolicyReportStore, error) { |
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.
Since it never fails, we could omit error
from the return type
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.
I had mixed feelings about this one, I was thinking of keeping all constructors for all PolicyReportStore
implementations coherent, but I'm now thinking that doesn't make sense because we'll have different dependencies for each store. I'll remove the error
…emory_policy-report-store_implementation Signed-off-by: Nuno Nelas <nuno.nelas@icloud.com>
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.
LGTM. @kubewarden/kubewarden-developers remember to squash the commits when merging this PR!
- Removes "dumb" comment to try to fix golangci. The solution that @jvanz gave [here](kubewarden#150 (comment)) worked like a charm! - Removes unused methods from `PolicyReportStore` interface Signed-off-by: Nuno Nelas <nuno.nelas@icloud.com>
- Simplifies `NewMemoryPolicyReportStore` method signature, by removing `error` which is never used Signed-off-by: Nuno Nelas <nuno.nelas@icloud.com>
…emory_policy-report-store_implementation Signed-off-by: Nuno Nelas <nuno.nelas@icloud.com>
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.
Looks good! Many thanks for dealing with the changes!
The only blocker to me is the bump on the sigs.k8s.io/wg-policy-prototypes
library, I would like to postpone that.
- Reverts `wg-policy-prototypes` upgrade since this library is not correctly tagged nor has proper releases, so it's best to separate the upgrade to another PR that we can properly test Signed-off-by: Nuno Nelas <nuno.nelas@icloud.com>
hallo @viccuad ! thank you so much for your review 🙇 I've reverted that upgrade under 10c4eee so I think I've addressed all your suggestions. in the meantime, I'll create a PR in https://github.com/kubewarden/helm-charts to use this new |
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.
LGTM, thanks!
golangci-lint was again cryptically failing, and passing locally. Ended up deleting its cache under https://github.com/kubewarden/audit-scanner/actions/caches. GH says merging is blocked because all commits must be signed, yet DCO reports all are signed Oo. Maybe it got confused with unsigned merge commits from gh. @flavio could you merge this please? |
Squashed and merged. Thanks for your contribution @nnelas! |
Description
Fix #107
As specified in the original issue, this commit:
report/store.go
report/store.go
intoreport/store_kubernetes.go
, as an implementation of the store interfaceClusterPolicyReports
and another one withPolicyReports
--store TYPE
, with possible types memory, kubernetes. Defaults to --store kubernetes for ease on first Kubewarden installations.Besides that, this commit also:
printJSON
tooutputScan
to make it more aligned with what the input params isoutputScan
only outputs at the end of the flow, to prevent having a lot of logs whenever someone scans the entire cluster (we were outputting the entire store on each namespace loop)It also seems that running
make fmt
introduced a couple of unwanted changes that I'm happy to revert to keep this commit smaller. Finally, I also had to do a couple of tweaks to make golangci lint happy.Test
To test this pull request, you can run the following commands:
Additional Information
Tradeoff
Potential improvement
One thing that we can also do in the future is to parse (in some way) the
toJSON()
result and instead of logging the entire report in a single log block, we could have a log line per violation. What do you think?Also, I'll be out-of-office for the rest of the week, but I'll return next Monday. Looking forward to seeing your comments! ❤️