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

backend: refactor: refreshAndCacheNewToken in headlamp.go #2816

Merged
merged 1 commit into from
Feb 6, 2025

Conversation

Faakhir30
Copy link
Contributor

@Faakhir30 Faakhir30 commented Jan 30, 2025

Fixes #1848
The refreshAndCacheNewToken function in backend/cmd/headlamp.go is currently too long and complex, requiring a //nolint:funlen directive to bypass linter warnings. This makes the code harder to maintain and understand.

Refactored the function by:

  • Breaking it down into smaller, more focused functions
  • Improving readability and maintainability
  • Removing the need for the funlen linter bypass

Testing

  • Ran make backend-test to ensure all tests pass
  • Verified with make backend-lint that code passes linting without exceptions

Sorry, something went wrong.

@Faakhir30 Faakhir30 marked this pull request as ready for review January 30, 2025 20:51
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Jan 30, 2025
Copy link
Contributor

@knrt10 knrt10 left a comment

Choose a reason for hiding this comment

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

Thanks you for the PR @Faakhir30. Some suggestions

@Faakhir30 Faakhir30 force-pushed the refactor_backend branch 3 times, most recently from 0a2c22a to 886e3c2 Compare February 4, 2025 11:41
@knrt10
Copy link
Contributor

knrt10 commented Feb 4, 2025

@Faakhir30 you can try running make backend-lint command to see the errors locally. Would help before pushing.

@Faakhir30
Copy link
Contributor Author

@Faakhir30 you can try running make backend-lint command to see the errors locally. Would help before pushing.

Yeah, did try that a lot, but for some reason it is not working for me locally, did asked in #headlamp CNCF slack channel also.
Did implement other changes, however lint failing.

couldnt find much about this error. most probably its some packages version conflict, but im not sure:
@knrt10 Is there any preffered go version? mine is go version go1.23.5 linux/amd64
also tried modifying versions of golang-lint in Makefile to [email protected]
and [email protected] , both giving same following output:

f@f:~/w/oss/headlamp$ make backend-lint 
GOBIN=`pwd`/backend/tools go install github.com/golangci/golangci-lint/cmd/[email protected]
cd backend && ./tools/golangci-lint run
WARN [runner] The linter 'golint' is deprecated (since v1.41.0) due to: The repository of the linter has been archived by the owner. Replaced by revive. 
WARN [runner] The linter 'interfacer' is deprecated (since v1.38.0) due to: The repository of the linter has been archived by the owner.  
ERRO [linters_context/goanalysis] buildir: panic during analysis: Cannot range over: func(yield func(K, V) bool), goroutine 10688 [running]:
runtime/debug.Stack()
        /snap/go/10818/src/runtime/debug/stack.go:26 +0x5e
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe.func1()
        /home/f/go/pkg/mod/github.com/golangci/[email protected]/pkg/golinters/goanalysis/runner_action.go:105 +0x5a
panic({0x160ef80?, 0xc00199bb90?})
        /snap/go/10818/src/runtime/panic.go:785 +0x132
honnef.co/go/tools/go/ir.(*builder).rangeStmt(0xc005425968, 0xc00547c500, 0xc00542c120, 0x0, {0x1a9b488, 0xc00542c120})
        /home/f/go/pkg/mod/honnef.co/go/[email protected]/go/ir/builder.go:2214 +0x894
honnef.co/go/tools/go/ir.(*builder).stmt(0xc005425968, 0xc00547c500, {0x1aa0570?, 0xc00542c120?})
        /home/f/go/pkg/mod/honnef.co/go/[email protected]/go/ir/builder.go:2427 +0x20a
.............
        /home/f/go/pkg/mod/github.com/golangci/[email protected]/pkg/golinters/goanalysis/runner_loadingpackage.go:75 +0x1e9 
WARN [runner] Can't run linter goanalysis_metalinter: goanalysis_metalinter: buildir: package "slices" (isInitialPkg: false, needAnalyzeSource: true): Cannot range over: func(yield func(E) bool) 
ERRO Running error: 1 error occurred:
        * can't run linter goanalysis_metalinter: goanalysis_metalinter: buildir: package "slices" (isInitialPkg: false, needAnalyzeSource: true): Cannot range over: func(yield func(E) bool) 
 
make: *** [Makefile:35: backend-lint] Error 3
f@f:~/w/oss/headlamp$ 

@knrt10
Copy link
Contributor

knrt10 commented Feb 5, 2025

You need Go version 1.22.* for current project setup. You can easily change it is gvm. Mine currently is

go version
go version go1.22.1 darwin/amd64

Also, you can always check our actions workfows for the passing CI

- uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
with:
go-version: '1.22.*'
- name: Install dependencies
run: |
cd backend
go mod download
- name: Start cluster
uses: medyagh/setup-minikube@d8c0eb871f6f455542491d86a574477bd3894533 # latest
- name: Check cluster status and enable headlamp addon
run: |
minikube status
minikube addons enable headlamp
kubectl wait deployment -n headlamp headlamp --for condition=Available=True --timeout=30s
- name: setup and run golangci-lint
uses: golangci/golangci-lint-action@d6238b002a20823d52840fda27e2d4891c5952dc # v4.0.1
with:
version: v1.54
working-directory: backend
skip-cache: true
args: --timeout 3m

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Feb 5, 2025
Signed-off-by: Faakhir30 <[email protected]>
@Faakhir30
Copy link
Contributor Author

Changes implemented, lint errors fixed, rebased with main, ready for review. @knrt10 thanks

Copy link
Contributor

@knrt10 knrt10 left a comment

Choose a reason for hiding this comment

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

Awesome. Thanks again for PR, appreciate it

Copy link
Collaborator

@illume illume left a comment

Choose a reason for hiding this comment

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

🎉 great work, thanks!

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Feb 6, 2025
@illume illume merged commit 3890eeb into headlamp-k8s:main Feb 6, 2025
10 checks passed
@Faakhir30 Faakhir30 deleted the refactor_backend branch February 6, 2025 09:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

backend: Refactor headlamp.go refreshAndCacheNewToken to be shorter
3 participants