Skip to content

refactor(scheduler): replace goto in Bind with fail closure#2089

Merged
hami-robot[bot] merged 2 commits into
Project-HAMi:masterfrom
yxxhero:refactor/scheduler-bind-fail-closure
Jul 20, 2026
Merged

refactor(scheduler): replace goto in Bind with fail closure#2089
hami-robot[bot] merged 2 commits into
Project-HAMi:masterfrom
yxxhero:refactor/scheduler-bind-fail-closure

Conversation

@yxxhero

@yxxhero yxxhero commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Summary

Replace 3 goto ReleaseNodeLocks jumps and the trailing labeled cleanup block in Scheduler.Bind() with an inline fail closure. The closure preserves the exact cleanup sequence and return contract.

Motivation

goto across nested select/switch is flagged as a Go code-review anti-pattern. A named closure:

  • Makes the failure path an explicit, reusable concept (return fail(err))
  • Keeps the early-exit branches (pod/node lookup) untouched — they must not invoke releaseAllDevices (no lock acquired yet)
  • Future error branches can reuse the same pattern

Changes

pkg/scheduler/scheduler.goBind():

  • Extract the labeled cleanup block into a fail func(e error) (*extenderv1.ExtenderBindingResult, error) closure
  • Replace 3 goto ReleaseNodeLocks with return fail(err)
  • Inline err = ... + if err != nil into if err = ...; err != nil (idiomatic Go)

Behavioral equivalence

Path Before After
acquireNodeLocks fail goto → release + record + return (res, nil) return fail(err) → same
PatchPodAnnotations fail same goto path same closure
kubeClient.Bind fail same goto path same closure
Success path untouched untouched
Early lookup errors untouched (no lock yet) untouched

The extender contract (return (res, nil) for bind failures vs. scheduler-level errors) is preserved — fail always returns nil as the second value.

AI Assistance Disclosure

This PR was authored with AI assistance (Claude Code) and is disclosed per CONTRIBUTING.md.

Test Plan

  • go build ./pkg/scheduler/...
  • go vet ./pkg/scheduler/...
  • go test -run Bind -short --race -count=1 ./pkg/scheduler/... — all pass

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of pod binding failures to ensure consistent cleanup when binding cannot be completed.
    • Binding failures now return clearer failure details alongside corresponding failure events.
    • Successful pod bindings continue to behave the same, including success event reporting.
    • Eliminated inconsistent control flow that could leave locks unreleased after failed binding attempts.

Replace 3 'goto ReleaseNodeLocks' jumps and the trailing labeled block
in Bind() with an inline 'fail' closure. The closure preserves the
exact cleanup sequence (releaseAllDevices + recordScheduleBindingResult
event) and the (result, nil) return contract used by the kube-scheduler
extender protocol.

Why:
- goto across nested select/switch is a Go code-review anti-pattern;
  a named closure makes the failure path an explicit, reusable concept.
- Keeps the early-exit branches (pod/node lookup) untouched, since they
  must not invoke releaseAllDevices (no lock acquired yet).
- Future error branches can reuse 'return fail(err)'.

Verified: go build, go vet, and 'go test -run Bind --race ./pkg/scheduler/...'
all pass.

Signed-off-by: yxxhero <aiopsclub@163.com>
@hami-robot
hami-robot Bot requested review from archlitchi and lengrongfu July 18, 2026 09:17
@github-actions github-actions Bot added the kind/enhancement New feature or request label Jul 18, 2026
@hami-robot hami-robot Bot added the size/S label Jul 18, 2026
@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: b8a71fe6-8c14-4a24-825f-53f14ad6f574

📥 Commits

Reviewing files that changed from the base of the PR and between 600c836 and 3ae410c.

📒 Files selected for processing (1)
  • pkg/scheduler/scheduler.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/scheduler/scheduler.go

📝 Walkthrough

Walkthrough

Scheduler.Bind replaces its goto-based failure path with a closure that releases locks, records a failure event, and returns an ExtenderBindingResult while preserving the success path.

Changes

Scheduler binding flow

Layer / File(s) Summary
Centralized Bind failure handling
pkg/scheduler/scheduler.go
Scheduler.Bind routes lock, annotation patch, and Kubernetes bind failures through shared cleanup and failure-event handling while preserving the existing success behavior.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested labels: enhancement

Suggested reviewers: lengrongfu, archlitchi

Poem

I’m a rabbit who hops through the bind,
Leaving old goto trails behind.
Locks release, events appear,
Errors return clean and clear.
Success still warms my burrow bright—
A tidy scheduler tonight.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: refactoring Scheduler.Bind to replace goto cleanup with a fail closure.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the Bind method in pkg/scheduler/scheduler.go by replacing goto statements with a local helper function fail to handle binding failures. The review feedback suggests adding a nil check for the error parameter within this helper function to prevent a potential nil pointer dereference when calling e.Error().

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread pkg/scheduler/scheduler.go
Defensive nil check for the 'e' parameter in the Bind() fail closure
to prevent potential nil-pointer dereference when calling e.Error().
All current call sites guarantee non-nil err (inside if-err-fail
branches), but the closure is a reusable unit and the guard keeps it
self-contained for future call sites.

Addresses PR Project-HAMi#2089 review comment from gemini-code-assist.

Signed-off-by: yxxhero <aiopsclub@163.com>
@codecov

codecov Bot commented Jul 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.61538% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/scheduler/scheduler.go 84.61% 1 Missing and 1 partial ⚠️
Flag Coverage Δ
unittests 60.46% <84.61%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
pkg/scheduler/scheduler.go 66.86% <84.61%> (+0.09%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@DSFans2014 DSFans2014 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

/lgtm

@archlitchi archlitchi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

/lgtm

@hami-robot

hami-robot Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: archlitchi, yxxhero

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

The pull request process is described here

Details 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

@hami-robot hami-robot Bot added the approved label Jul 20, 2026
@hami-robot
hami-robot Bot merged commit 53da824 into Project-HAMi:master Jul 20, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants