-
Notifications
You must be signed in to change notification settings - Fork 418
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
[Chore] Fix lint errors caused by casting int to int32 #2368
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"context" | ||
errstd "errors" | ||
"fmt" | ||
"math" | ||
"os" | ||
"reflect" | ||
"runtime" | ||
|
@@ -769,21 +768,16 @@ func (r *RayClusterReconciler) reconcilePods(ctx context.Context, instance *rayv | |
if worker.NumOfHosts <= 0 { | ||
worker.NumOfHosts = 1 | ||
} | ||
numExpectedPods := workerReplicas * worker.NumOfHosts | ||
|
||
if len(runningPods.Items) > math.MaxInt32 { | ||
return errstd.New("len(runningPods.Items) exceeds math.MaxInt32") | ||
} | ||
diff := numExpectedPods - int32(len(runningPods.Items)) //nolint:gosec // Already checked in the previous line. | ||
numExpectedPods := int(workerReplicas * worker.NumOfHosts) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Converting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code is doing the opposite, it converts to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yeah, it is a typo in my comment. |
||
diff := numExpectedPods - len(runningPods.Items) | ||
|
||
logger.Info("reconcilePods", "workerReplicas", workerReplicas, "NumOfHosts", worker.NumOfHosts, "runningPods", len(runningPods.Items), "diff", diff) | ||
|
||
if diff > 0 { | ||
// pods need to be added | ||
logger.Info("reconcilePods", "Number workers to add", diff, "Worker group", worker.GroupName) | ||
// create all workers of this group | ||
var i int32 | ||
for i = 0; i < diff; i++ { | ||
for i := 0; i < diff; i++ { | ||
logger.Info("reconcilePods", "creating worker for group", worker.GroupName, fmt.Sprintf("index %d", i), fmt.Sprintf("in total %d", diff)) | ||
if err := r.createWorkerPod(ctx, *instance, *worker.DeepCopy()); err != nil { | ||
return errstd.Join(utils.ErrFailedCreateWorkerPod, err) | ||
|
@@ -814,7 +808,7 @@ func (r *RayClusterReconciler) reconcilePods(ctx context.Context, instance *rayv | |
// diff < 0 means that we need to delete some Pods to meet the desired number of replicas. | ||
randomlyRemovedWorkers := -diff | ||
logger.Info("reconcilePods", "Number workers to delete randomly", randomlyRemovedWorkers, "Worker group", worker.GroupName) | ||
for i := 0; i < int(randomlyRemovedWorkers); i++ { | ||
for i := 0; i < randomlyRemovedWorkers; i++ { | ||
randomPodToDelete := runningPods.Items[i] | ||
logger.Info("Randomly deleting Pod", "progress", fmt.Sprintf("%d / %d", i+1, randomlyRemovedWorkers), "with name", randomPodToDelete.Name) | ||
if err := r.Delete(ctx, &randomPodToDelete); err != nil { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -251,7 +251,7 @@ func (r *RayServiceReconciler) calculateStatus(ctx context.Context, rayServiceIn | |
if numServeEndpoints > math.MaxInt32 { | ||
return errstd.New("numServeEndpoints exceeds math.MaxInt32") | ||
} | ||
rayServiceInstance.Status.NumServeEndpoints = int32(numServeEndpoints) //nolint:gosec // Already checked in the previous line. | ||
rayServiceInstance.Status.NumServeEndpoints = int32(numServeEndpoints) //nolint:gosec // This is a false positive from gosec. See https://github.com/securego/gosec/issues/1212 for more details. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is not a false positive. The type of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return nil | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The linter is not smart enough to determine whether casting
probeTimeout
(int
) toint32
will cause an overflow, but it can determine if an overflow will occur when castingutils.DefaultLivenessProbeTimeoutSeconds
orutils.DefaultHeadLivenessProbeTimeoutSeconds
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is not the fault of the linter. Because
utils.DefaultLivenessProbeTimeoutSeconds
is a constant so the linter can easily find it will not cause error if casted toint32
. But for theint32(probeTimeout)
case, the linter cannot determine if it will cause an overflow because it does not know whetherprobeTimeout
is assigned with some other values in between.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can check securego/gosec#1212. The
gosec
community seems to consider it as a false positive becauseprobeTimeout
should be eitherutils.DefaultLivenessProbeTimeoutSeconds
orutils.DefaultHeadLivenessProbeTimeoutSeconds
which is smaller thanMaxInt32
and should not cause overflow when casting.