Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions cmd/scheduler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var (
Short: "kubernetes vgpu scheduler",
RunE: func(cmd *cobra.Command, args []string) error {
flag.PrintPFlags(cmd.Flags())
return start()
return start(cmd.Flags().Changed("node-lock-timeout"))
},
}
)
Expand Down Expand Up @@ -109,9 +109,18 @@ func injectProfilingRoute(router *httprouter.Router) {
})
}

func start() error {
// Initialize node lock timeout from config
nodelock.NodeLockTimeout = config.NodeLockTimeout
// applyNodeLockTimeout applies the flag only when it was set explicitly, so it
// does not overwrite the value nodelock's init took from HAMI_NODELOCK_EXPIRE.
func applyNodeLockTimeout(flagSet bool) {
if flagSet {
nodelock.NodeLockTimeout = config.NodeLockTimeout
return
}

@moezdil moezdil Aug 19, 2026 •

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.

1 alternative: make the flag default nodelock.NodeLockTimeout in init, the env is already applied there by import order, then no bool threading is needed. also nothing else reads config.NodeLockTimeout, is the write back needed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good call on both. Done.

Flag default is now nodelock.NodeLockTimeout — main imports pkg/util/nodelock, so its init() (and setupNodeLockTimeout, which reads HAMI_NODELOCK_EXPIRE) completes before this package registers flags. The default already carries the env value, so start() goes back to the plain unconditional assignment and applyNodeLockTimeout, the bool param and the Changed(...) call are gone. The write-back went too — as you say, nothing outside cmd/scheduler reads config.NodeLockTimeout.

Side benefit: PrintPFlags runs before start(), so the startup log used to print FLAG: --node-lock-timeout="5m0s" while the effective value was 7s. They agree now.

Rebuilt at both revisions and compared startup logs — Set node lock timeout is unchanged in all four cases (env only 7s, env+flag 33s, neither 5m0s, bad env 5m0s).

One visible trade-off: --help now shows an env-dependent default (7s when the chart sets scheduler.nodeLockExpire: 7s). I think showing the real default is better, but happy to pin it back if you'd rather.

TestApplyNodeLockTimeout had no function left to cover, so it's now TestNodeLockTimeoutFlagDefault, asserting the registered default is nodelock.NodeLockTimeout — re-hardcoding a duration there is exactly how #2692 comes back. Env parsing is already covered in pkg/util/nodelock/nodelock_test.go.

config.NodeLockTimeout = nodelock.NodeLockTimeout
}

func start(nodeLockTimeoutFlagSet bool) error {
applyNodeLockTimeout(nodeLockTimeoutFlagSet)
klog.InfoS("Set node lock timeout", "timeout", nodelock.NodeLockTimeout)
client.InitGlobalClient(
client.WithBurst(config.Burst),
Expand Down
59 changes: 59 additions & 0 deletions cmd/scheduler/nodelock_timeout_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2024 The HAMi Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"testing"
"time"

"github.com/Project-HAMi/HAMi/pkg/scheduler/config"
"github.com/Project-HAMi/HAMi/pkg/util/nodelock"
)

func TestApplyNodeLockTimeout(t *testing.T) {
const defaultTimeout = 5 * time.Minute

tests := []struct {
name string
flagSet bool
flag time.Duration
env time.Duration
want time.Duration
}{
{name: "flag unset keeps env value", flagSet: false, flag: defaultTimeout, env: 5 * time.Second, want: 5 * time.Second},
{name: "flag set overrides env", flagSet: true, flag: 5 * time.Second, env: defaultTimeout, want: 5 * time.Second},
{name: "neither set keeps default", flagSet: false, flag: defaultTimeout, env: defaultTimeout, want: defaultTimeout},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
origFlag, origEnv := config.NodeLockTimeout, nodelock.NodeLockTimeout
t.Cleanup(func() {
config.NodeLockTimeout, nodelock.NodeLockTimeout = origFlag, origEnv
})
config.NodeLockTimeout, nodelock.NodeLockTimeout = tc.flag, tc.env

applyNodeLockTimeout(tc.flagSet)

if nodelock.NodeLockTimeout != tc.want {
t.Errorf("nodelock.NodeLockTimeout = %v, want %v", nodelock.NodeLockTimeout, tc.want)
}
if config.NodeLockTimeout != tc.want {
t.Errorf("config.NodeLockTimeout = %v, want %v", config.NodeLockTimeout, tc.want)
}
})
}
}
Loading