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

cleanup: Fix various issues found by code inspection #1278

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

akrejcir
Copy link
Collaborator

What this PR does / why we need it:
Fixed:

  • Handle unhandled errors
  • Removed unused code
  • Stopped using deprecated methods
  • Code style issues

Release note:

None

@kubevirt-bot kubevirt-bot added release-note-none Denotes a PR that doesn't merit a release note. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. labels Feb 17, 2025
Comment on lines -14 to -16
func ListMetrics() []operatormetrics.Metric {
return operatormetrics.ListMetrics()
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@machadovilaca , I've found this unused function and removed it. Is this a bug, that it is not used anywhere?

Copy link
Member

Choose a reason for hiding this comment

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

In usually call these functions the tooling to generate the docs, but when setting up multiple metrics, in the registry we only need to list from one. That's why the ListMetrics from metrics/ssp-operator is used but not this one

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So, this was not a bug, that the function was not called?

Copy link
Member

Choose a reason for hiding this comment

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

No, we can remove it. On a future release of the operator-observability library, it will support different registries, and then we will improve the docs to setup and list the metrics separately.

@akrejcir
Copy link
Collaborator Author

/cc @0xFelix @jcanocan

res := GetOperatorVersion()
Expect(res).To(Equal("v0.0.1"), "OPERATOR_VERSION should equal")
os.Unsetenv(OperatorVersionKey)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe DeferCleanup that so the Env is always unset?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

@@ -130,7 +130,10 @@ func (app *App) Run() {

func registerReadinessProbe() {
http.HandleFunc("/readyz", func(resp http.ResponseWriter, req *http.Request) {
fmt.Fprintf(resp, "ok")
_, err := fmt.Fprintf(resp, "ok")
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Inline it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@@ -130,7 +130,10 @@ func (app *App) Run() {

func registerReadinessProbe() {
http.HandleFunc("/readyz", func(resp http.ResponseWriter, req *http.Request) {
fmt.Fprintf(resp, "ok")
_, err := fmt.Fprintf(resp, "ok")
Copy link
Member

Choose a reason for hiding this comment

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

resp.Write([]byte("ok")) would probably achieve the same without an additional dependency.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good idea, done.

@@ -204,7 +204,7 @@ func getCaCertificate() []byte {
func tryToAccessEndpoint(pod core.Pod, serviceName string, subpath string, port uint16, tlsConfig clientTLSOptions, insecure bool) (attemptedUrl string, err error) {
conn, err := portForwarder.Connect(&pod, port)
Expect(err).ToNot(HaveOccurred())
defer conn.Close()
defer Expect(conn.Close()).To(Succeed())
Copy link
Member

Choose a reason for hiding this comment

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

Will it run if another Expect fails?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it will work. A panic can happen during panicking. They will just wrap around each other.
https://go.dev/play/p/Eq5sdpNkDl

@@ -43,17 +44,19 @@ func (p *portForwarderImpl) Connect(pod *core.Pod, remotePort uint16) (net.Conn,
headers.Set(core.PortForwardRequestIDHeader, strconv.Itoa(int(requestId)))
errorStream, err := streamConnection.CreateStream(headers)
if err != nil {
streamConnection.Close()
return nil, err
closeErr := streamConnection.Close()
Copy link
Member

Choose a reason for hiding this comment

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

Inline?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done


headers.Set(core.StreamType, core.StreamTypeData)
dataStream, err := streamConnection.CreateStream(headers)
if err != nil {
streamConnection.Close()
return nil, err
closeErr := streamConnection.Close()
Copy link
Member

Choose a reason for hiding this comment

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

Same

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@@ -3,6 +3,7 @@ package tests
import (
"crypto/tls"
"crypto/x509"
goerrors "errors"
Copy link
Member

Choose a reason for hiding this comment

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

Import "k8s.io/apimachinery/pkg/api/errors" as k8serrors instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@akrejcir akrejcir force-pushed the code-inspection-cleanup branch 2 times, most recently from c3d1c4b to 45c1ccc Compare February 17, 2025 14:52
@akrejcir
Copy link
Collaborator Author

In multiple PRs we have problem with the golangci-lint timeout. I will look into it...

@akrejcir
Copy link
Collaborator Author

/retest

1 similar comment
@akrejcir
Copy link
Collaborator Author

/retest

Copy link
Member

@0xFelix 0xFelix left a comment

Choose a reason for hiding this comment

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

/approve

Thanks!

@kubevirt-bot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: 0xFelix

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubevirt-bot kubevirt-bot added approved Indicates a PR has been approved by an approver from all required OWNERS files. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Feb 18, 2025
They were found by Goland code inspection.

Signed-off-by: Andrej Krejcir <[email protected]>
Copy link
Member

@codingben codingben left a comment

Choose a reason for hiding this comment

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

/lgtm
/hold

@akrejcir Do you want to hold it until you get a response from @machadovilaca to your question about an unused function?

@kubevirt-bot kubevirt-bot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. lgtm Indicates that a PR is ready to be merged. labels Feb 19, 2025
The unhandled errors were found by Goland code inspection.

Signed-off-by: Andrej Krejcir <[email protected]>
@akrejcir akrejcir force-pushed the code-inspection-cleanup branch from 45c1ccc to c7b19d4 Compare February 19, 2025 09:27
@kubevirt-bot kubevirt-bot removed lgtm Indicates that a PR is ready to be merged. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Feb 19, 2025
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
3.8% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@akrejcir
Copy link
Collaborator Author

/unhold

@kubevirt-bot kubevirt-bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Feb 19, 2025
@akrejcir
Copy link
Collaborator Author

/retest

@jcanocan
Copy link
Contributor

/lgtm

@kubevirt-bot kubevirt-bot added the lgtm Indicates that a PR is ready to be merged. label Feb 20, 2025
Copy link

openshift-ci bot commented Feb 20, 2025

@akrejcir: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
ci/prow/e2e-upgrade-functests c7b19d4 link true /test e2e-upgrade-functests
ci/prow/e2e-functests c7b19d4 link true /test e2e-functests
ci/prow/e2e-single-node-functests c7b19d4 link true /test e2e-single-node-functests

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. lgtm Indicates that a PR is ready to be merged. release-note-none Denotes a PR that doesn't merit a release note. size/L
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants