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

chore: cleanup duplicated filter logic #402

Closed
wants to merge 3 commits into from

Conversation

panpan0000
Copy link
Contributor

@panpan0000 panpan0000 commented May 12, 2023

Closes #

Clean up duplicated code of original https://github.com/k8sgpt-ai/k8sgpt/blob/main/pkg/analysis/analysis.go#L136 and https://github.com/k8sgpt-ai/k8sgpt/blob/main/pkg/analysis/analysis.go#L161

📑 Description

✅ Checks

  • My pull request adheres to the code style of this project
  • My code requires changes to the documentation
  • I have updated the documentation as required
  • All the tests have passed

1)I've run make test make fmt make lint make build
2)then run the generated binary with below command:
k8sgpt analyze --filter=VulnerabilityReport
k8sgpt analyze
the result outputs are the same with released binary from github.

ℹ Additional Information

@panpan0000 panpan0000 requested review from a team as code owners May 12, 2023 07:06
@panpan0000 panpan0000 force-pushed the cleanup branch 2 times, most recently from 865365b to 86684af Compare May 12, 2023 07:14
@panpan0000
Copy link
Contributor Author

@matthisholleville @AlexsJones can you pls review it ?

@panpan0000
Copy link
Contributor Author

Hi, Maintainers, when you have time, can you please kindly help to review it ?
since the code get changed very frequently , this PR conflicts from time to time T_T....

Copy link
Contributor

@matthisholleville matthisholleville left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution. Great work, left a comment.

Shouldn't add some test about filter logic ?

pkg/analysis/analysis.go Outdated Show resolved Hide resolved
@panpan0000
Copy link
Contributor Author

panpan0000 commented May 16, 2023

Thank you for your contribution. Great work, left a comment.

Shouldn't add some test about filter logic ?

T_T... @matthisholleville
I tried to add 2 of 3 UT, but the last one with active_filter requires run TrivyAnalyzer.Analyze()
I don't think I can fake a trivy client easily to finish this UT....
so I will have to comment the code for now...

@panpan0000 panpan0000 force-pushed the cleanup branch 2 times, most recently from 3fa134b to f6f77b0 Compare May 16, 2023 06:31
@matthisholleville
Copy link
Contributor

Active filters correspond to the filters added by the command k8sgpt filter add ... Why would we need Trivy?

@panpan0000
Copy link
Contributor Author

panpan0000 commented May 16, 2023

Active filters correspond to the filters added by the command k8sgpt filter add ... Why would we need Trivy?

@matthisholleville
Per your ask, I added UT for analysis.RunAnalysis(), so there're 3 cases:

  • 1 --filter specified
  • 2 there's activeFilter in viper(config)
  • 3 none of two above

For case 2, there's only Trivy plugin implemented. that's where my problem came from.

in analysis.RunAnalysis(), major code as below , the key is the map analyzerMap[]:

        for _, filter := range filtersCandidate {
                if analyzer, ok := analyzerMap[filter]; ok {
                                   analyzer.Analyze(..)
                }
         }

So I guess I should do below in UT:

  1. add a new filter type ( just viper.SetDefault("active_filters", "newFilter"))
  2. fake a dummy analyzer( maybe just return Result with kind == newFilter)
  3. hack into the analyzerMap, to inject the dummy analyzer as above. But the new PROBLEM is : the additionalAnalyzerMap map is private variable in pkg/analyzer/analyzer.go, I cannot access it in my UT.

should I add a new public function to modify that private variable ?

@matthisholleville
Copy link
Contributor

I don't quite understand point two. Active filters are the result of adding filters via the command k8sgpt filters add Ingress for example. This is not a feature only related to integrations. I think they are two different things.

For me there are 3 things to test:

  1. The user adds --filter in his analysis command
  2. We get the actives_filters (stored in the configuration file)
  3. Neither of them is specified and we use the core analyzers

Signed-off-by: Peter Pan <Peter.Pan@daocloud.io>
Signed-off-by: Peter Pan <Peter.Pan@daocloud.io>
@panpan0000
Copy link
Contributor Author

Thanks @matthisholleville . I thought the active_filter can only be added by integration and not knowing about k8sgpt filters add . Now it 's clear.

the PR has been updated. please kindly help to review again :-)

@matthisholleville
Copy link
Contributor

There seems to be an issue with the filters. I'm not sure if it's related to this change, but it would be worth fixing it.

I don't have the HPA filter enabled, yet it is being analyzed. Could you please take a look?

image

@matthisholleville
Copy link
Contributor

Fixed by : #431

@panpan0000
Copy link
Contributor Author

Fixed by : #431

Hi, @matthisholleville , I think you meant to reply to PR #413 (which I will close) instead of this one ?

this PR has been updated with unit-test added per your ask. when you have time, can you review this ?

@matthisholleville
Copy link
Contributor

We have just discussed your PR, and we believe that your modification adds more complexity than it resolves. Thank you very much for your time and contribution, but at the moment, we will not be proceeding with your PR.

@panpan0000
Copy link
Contributor Author

@matthisholleville , Thanks for your time and response

    1. Sorry, I don't catch that why the "duplicated logic" simplify will introduce complexity.
    1. Even when the code is unnecessary, is my Unit-Test part still worthy ?

@panpan0000
Copy link
Contributor Author

Mat explain that the filter part code is unstable now , it's better to keep it as it is.

I will create new PR for UT only.

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

Successfully merging this pull request may close these issues.

None yet

2 participants