cmd/violations: refactor violations to share clients
General MR
Summary
While checking the violations usage strings, I noticed that there may exist an unlikely but possible scenarios where a customer could be in a state where they had previously installed Gatekeeper and have lingering CRDs in their cluster, preventing us from checking Kyverno. The change I was proposing was to aggregate errors and present them all at the end, instead of short-circuiting the command and early returning. In the process of making that change, I noticed that in certain fail scenarios we would end up with a multitude of duplicate "failed to get Kubernetes client" errors. The way the code was structured, basically every function in the violations command took the cobra.Command
, the bbUtil.Factory
, and a streams
as an argument and was responsible for getting its own clients, loggers, and configurations.
This MR suggests an alternative structure to these larger, more complicated commands that introduces an internal structure to store common clients and configurations, and converts all the discrete functions into methods off this structure. Things like ConfigClient
s, loggers, streams, and Kubernetes clients can then be store on this struct once (through a helper constructor) and referenced in subsequent function calls without constructing new clients every time.
All unit tests have been updated to work and the duplicate tests have been removed. Coverage is 97.9% on violations.go
.
Relevant logs/screenshots
N/A
Linked Issue
N/A
Upgrade Notices
N/A