forked from kosmos-io/kosmos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: concurrent execution of change node tasks
Signed-off-by: baoyinghai_yewu <[email protected]>
- Loading branch information
Showing
9 changed files
with
278 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
#!/bin/bash | ||
|
||
filename="nodes.txt" | ||
readarray -t globalnodes < "$filename" | ||
|
||
function updateState() { | ||
local nodename="$1" | ||
local state="$2" | ||
kubectl patch globalnodes $nodename -p '{"spec": {"state": "'$state'"}}' --type=merge | ||
} | ||
|
||
function updateNodeState() { | ||
local nodename="$1" | ||
local state="$2" | ||
kubectl patch node $nodename -p '{"metadata": {"labels": {"kosmos-io/state": "'$state'"}}}' | ||
} | ||
|
||
function uncordon() { | ||
local nodename="$1" | ||
kubectl uncordon $nodename | ||
kubectl taint nodes $nodename node.kosmos.io/unschedulable- | ||
} | ||
|
||
|
||
# Update the state of the global nodes | ||
function free_globalnodes() { | ||
local globalnode="$1" | ||
updateState "$globalnode" "free" | ||
updateNodeState "$globalnode" "free" | ||
} | ||
|
||
|
||
|
||
# Update the state of the global nodes | ||
function reserved_globalnodes() { | ||
local globalnode="$1" | ||
updateState "$globalnode" "reserved" | ||
updateNodeState "$globalnode" "reserved" | ||
uncordon "$globalnode" | ||
} | ||
|
||
|
||
# Function to display progress bar | ||
show_progress() { | ||
local progress=$1 | ||
local total=$2 | ||
local width=$3 | ||
|
||
# Calculate percentage | ||
local percent=$((progress * 100 / total)) | ||
local num_hashes=$((percent * width / 100)) | ||
|
||
# Generate progress bar | ||
local bar="[" | ||
for ((i = 0; i < width; i++)); do | ||
if ((i < num_hashes)); then | ||
bar+="#" | ||
else | ||
bar+=" " | ||
fi | ||
done | ||
bar+="]" | ||
|
||
# Print progress bar with percentage | ||
printf "\rProgress: %s %d%%" "$bar" "$percent" | ||
} | ||
|
||
# Total steps for the task | ||
total_steps=${#globalnodes[@]} | ||
# Width of the progress bar | ||
bar_width=50 | ||
|
||
function free() { | ||
# Simulate a task by looping through steps | ||
for ((step = 1; step <= total_steps; step++)); do | ||
# Simulate work with sleep | ||
index=$((step - 1)) | ||
free_globalnodes ${globalnodes[index]} | ||
|
||
# Update progress bar | ||
show_progress $step $total_steps $bar_width | ||
done | ||
|
||
# Print a new line after the progress bar completes | ||
echo | ||
} | ||
|
||
function reserved() { | ||
# Simulate a task by looping through steps | ||
for ((step = 1; step <= total_steps; step++)); do | ||
# Simulate work with sleep | ||
index=$((step - 1)) | ||
reserved_globalnodes ${globalnodes[index]} | ||
|
||
# Update progress bar | ||
show_progress $step $total_steps $bar_width | ||
done | ||
|
||
# Print a new line after the progress bar completes | ||
echo | ||
} | ||
|
||
|
||
# See how we were called. | ||
case "$1" in | ||
free) | ||
free | ||
;; | ||
reserved) | ||
reserved | ||
;; | ||
*) | ||
echo $"usage: $0 free|reserved" | ||
exit 1 | ||
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
pkg/kubenest/controller/virtualcluster.node.controller/workflow/task/logger.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package task | ||
|
||
import ( | ||
"fmt" | ||
|
||
"k8s.io/klog/v2" | ||
) | ||
|
||
type PrefixedLogger struct { | ||
level klog.Verbose | ||
prefix string | ||
} | ||
|
||
func NewPrefixedLogger(level klog.Verbose, prefix string) *PrefixedLogger { | ||
return &PrefixedLogger{level: level, prefix: prefix} | ||
} | ||
|
||
func (p *PrefixedLogger) Info(args ...interface{}) { | ||
if p.level.Enabled() { | ||
klog.InfoDepth(1, append([]interface{}{p.prefix}, args...)...) | ||
} | ||
} | ||
|
||
func (p *PrefixedLogger) Infof(format string, args ...interface{}) { | ||
if p.level.Enabled() { | ||
klog.InfoDepth(1, fmt.Sprintf(p.prefix+format, args...)) | ||
} | ||
} | ||
|
||
func (p *PrefixedLogger) Error(args ...interface{}) { | ||
klog.ErrorDepth(1, append([]interface{}{p.prefix}, args...)...) | ||
} | ||
|
||
func (p *PrefixedLogger) Errorf(format string, args ...interface{}) { | ||
klog.ErrorDepth(1, fmt.Sprintf(p.prefix+format, args...)) | ||
} |
Oops, something went wrong.