Skip to content

Commit

Permalink
Fix Golangci Lint v1.61
Browse files Browse the repository at this point in the history
  • Loading branch information
mpass99 committed Sep 11, 2024
1 parent ca655a8 commit 6d9f93e
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 20 deletions.
31 changes: 22 additions & 9 deletions internal/environment/nomad_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ func (n *NomadEnvironment) PrewarmingPoolSize() uint {
count, err := strconv.Atoi(configTaskGroup.Meta[nomad.ConfigMetaPoolSizeKey])
if err != nil {
log.WithError(err).Error("Prewarming pool size can not be parsed from Job")
return 0

Check warning on line 98 in internal/environment/nomad_environment.go

View check run for this annotation

Codecov / codecov/patch

internal/environment/nomad_environment.go#L98

Added line #L98 was not covered by tests
} else if count < 0 {
log.WithError(util.ErrOverflow).WithField("size", count).Warning("Not a valid Prewarming pool size")
return 0

Check warning on line 101 in internal/environment/nomad_environment.go

View check run for this annotation

Codecov / codecov/patch

internal/environment/nomad_environment.go#L100-L101

Added lines #L100 - L101 were not covered by tests
}
return uint(count)
}
Expand All @@ -112,41 +116,50 @@ func (n *NomadEnvironment) SetPrewarmingPoolSize(count uint) {
func (n *NomadEnvironment) CPULimit() uint {
defaultTaskGroup := nomad.FindAndValidateDefaultTaskGroup(n.job)
defaultTask := nomad.FindAndValidateDefaultTask(defaultTaskGroup)
return uint(*defaultTask.Resources.CPU)
cpuLimit := *defaultTask.Resources.CPU
if cpuLimit < 0 {
log.WithError(util.ErrOverflow).WithField("limit", cpuLimit).Warning("not a valid CPU limit")
return 0

Check warning on line 122 in internal/environment/nomad_environment.go

View check run for this annotation

Codecov / codecov/patch

internal/environment/nomad_environment.go#L121-L122

Added lines #L121 - L122 were not covered by tests
}
return uint(cpuLimit)
}

func (n *NomadEnvironment) SetCPULimit(limit uint) error {
if limit > math.MaxInt32 {
return fmt.Errorf("limit too high: %w", util.ErrMaxNumberExceeded)
return fmt.Errorf("limit too high: %w", util.ErrOverflow)

Check warning on line 129 in internal/environment/nomad_environment.go

View check run for this annotation

Codecov / codecov/patch

internal/environment/nomad_environment.go#L129

Added line #L129 was not covered by tests
}

defaultTaskGroup := nomad.FindAndValidateDefaultTaskGroup(n.job)
defaultTask := nomad.FindAndValidateDefaultTask(defaultTaskGroup)

integerCPULimit := int(limit) //nolint:gosec // We check for an integer overflow right above.
integerCPULimit := int(limit)
defaultTask.Resources.CPU = &integerCPULimit
return nil
}

func (n *NomadEnvironment) MemoryLimit() uint {
defaultTaskGroup := nomad.FindAndValidateDefaultTaskGroup(n.job)
defaultTask := nomad.FindAndValidateDefaultTask(defaultTaskGroup)
maxMemoryLimit := defaultTask.Resources.MemoryMaxMB
if maxMemoryLimit != nil {
return uint(*maxMemoryLimit)
if defaultTask.Resources.MemoryMaxMB == nil {
return 0
}
maxMemoryLimit := *defaultTask.Resources.MemoryMaxMB
if maxMemoryLimit < 0 {
log.WithError(util.ErrOverflow).WithField("limit", maxMemoryLimit).Warning("not a valid memory limit")
return 0

Check warning on line 149 in internal/environment/nomad_environment.go

View check run for this annotation

Codecov / codecov/patch

internal/environment/nomad_environment.go#L148-L149

Added lines #L148 - L149 were not covered by tests
}
return 0
return uint(maxMemoryLimit)
}

func (n *NomadEnvironment) SetMemoryLimit(limit uint) error {
if limit > math.MaxInt32 {
return fmt.Errorf("limit too high: %w", util.ErrMaxNumberExceeded)
return fmt.Errorf("limit too high: %w", util.ErrOverflow)

Check warning on line 156 in internal/environment/nomad_environment.go

View check run for this annotation

Codecov / codecov/patch

internal/environment/nomad_environment.go#L156

Added line #L156 was not covered by tests
}

defaultTaskGroup := nomad.FindAndValidateDefaultTaskGroup(n.job)
defaultTask := nomad.FindAndValidateDefaultTask(defaultTaskGroup)

integerMemoryMaxLimit := int(limit) //nolint:gosec // We check for an integer overflow right above.
integerMemoryMaxLimit := int(limit)
defaultTask.Resources.MemoryMaxMB = &integerMemoryMaxLimit
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions internal/nomad/command_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ func injectStartDebugMessage(command string, start uint, end int) string {
if start < uint(len(commandFields)) {
commandFields = commandFields[start:]

if start > uint(math.MaxInt32)-uint(end) {
if (end < 0 && start > uint(math.MaxInt32)) || (end > 0 && start > uint(math.MaxInt32)-uint(end)) {
log.WithField("start", start).Error("passed start too big")
}
end -= int(start) //nolint:gosec // We check for an integer overflow right above.
end -= int(start)
}
if end >= 0 && end < len(commandFields) {
commandFields = commandFields[:end]
Expand Down
4 changes: 2 additions & 2 deletions internal/runner/aws_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (s *MainTestSuite) TestAWSFunctionWorkload_ExecuteInteractively() {
s.Run("establishes WebSocket connection to AWS endpoint", func() {
// Convert http://127.0.0.1 to ws://127.0.0.1
config.Config.AWS.Endpoint = "ws" + strings.TrimPrefix(sv.URL, "http")
awsMock.ctx, cancel = context.WithCancel(context.Background())
awsMock.ctx, cancel = context.WithCancel(context.Background()) //nolint:fatcontext // We are resetting the context not making it bigger.
cancel()

runnerWorkload.StoreExecution(tests.DefaultEnvironmentIDAsString, &dto.ExecutionRequest{})
Expand All @@ -88,7 +88,7 @@ func (s *MainTestSuite) TestAWSFunctionWorkload_ExecuteInteractively() {

s.Run("sends execution request", func() {
s.T().Skip("The AWS runner ignores its context for executions and waits infinitely for the exit message.")
awsMock.ctx, cancel = context.WithTimeout(context.Background(), tests.ShortTimeout)
awsMock.ctx, cancel = context.WithTimeout(context.Background(), tests.ShortTimeout) //nolint:fatcontext // We are not making the context bigger.
defer cancel()
command := "sl"
request := &dto.ExecutionRequest{Command: command}
Expand Down
2 changes: 1 addition & 1 deletion internal/runner/nomad_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (m *NomadRunnerManager) checkPrewarmingPoolAlert(ctx context.Context, envir
if reloadTimeout > uint(math.MaxInt64)/uint(time.Second) {
log.WithField("timeout", reloadTimeout).Error("configured reload timeout too big")
}
reloadTimeoutDuration := time.Duration(reloadTimeout) * time.Second //nolint:gosec // We check for an integer overflow right above.
reloadTimeoutDuration := time.Duration(reloadTimeout) * time.Second

if reloadTimeout == 0 || float64(environment.IdleRunnerCount())/float64(environment.PrewarmingPoolSize()) >= prewarmingPoolThreshold {
return
Expand Down
2 changes: 1 addition & 1 deletion internal/runner/nomad_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ func (s *MainTestSuite) TestNomadRunnerManager_Load() {
}

func (s *MainTestSuite) TestNomadRunnerManager_checkPrewarmingPoolAlert() {
timeout := 1
const timeout = 1
config.Config.Server.Alert.PrewarmingPoolReloadTimeout = uint(timeout)
config.Config.Server.Alert.PrewarmingPoolThreshold = 0.5
environment := &ExecutionEnvironmentMock{}
Expand Down
7 changes: 5 additions & 2 deletions internal/runner/nomad_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,11 @@ func (r *NomadJob) Environment() dto.EnvironmentID {
func (r *NomadJob) MappedPorts() []*dto.MappedPort {
ports := make([]*dto.MappedPort, 0, len(r.portMappings))
for _, portMapping := range r.portMappings {
if portMapping.To < 0 {
log.WithError(util.ErrOverflow).WithField("mapping", portMapping.To).Warn("not a valid port")

Check warning on line 114 in internal/runner/nomad_runner.go

View check run for this annotation

Codecov / codecov/patch

internal/runner/nomad_runner.go#L114

Added line #L114 was not covered by tests
}
ports = append(ports, &dto.MappedPort{
ExposedPort: uint(portMapping.To),
ExposedPort: uint(portMapping.To), //nolint:gosec // We check for an overflow right above.
HostAddress: fmt.Sprintf("%s:%d", portMapping.HostIP, portMapping.Value),
})
}
Expand All @@ -132,7 +135,7 @@ func (r *NomadJob) UpdateMappedPorts(ports []*dto.MappedPort) error {
return fmt.Errorf("failed parsing the port: %w", err)
}
if portMapping.ExposedPort > math.MaxInt32 {
return util.ErrMaxNumberExceeded
return util.ErrOverflow

Check warning on line 138 in internal/runner/nomad_runner.go

View check run for this annotation

Codecov / codecov/patch

internal/runner/nomad_runner.go#L138

Added line #L138 was not covered by tests
}

mapping = append(mapping, nomadApi.PortMapping{
Expand Down
4 changes: 2 additions & 2 deletions pkg/monitoring/influxdb2_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func InitializeInfluxDB(influxConfiguration *config.InfluxDB) (cancel func()) {

// Set options for retrying with the influx client.
options := influxdb2.DefaultOptions()
options.SetRetryInterval(uint(retryInterval.Milliseconds()))
options.SetRetryInterval(uint(retryInterval.Milliseconds())) //nolint:gosec // The constant 5_000 do not overflow uint.

Check warning on line 74 in pkg/monitoring/influxdb2_middleware.go

View check run for this annotation

Codecov / codecov/patch

pkg/monitoring/influxdb2_middleware.go#L74

Added line #L74 was not covered by tests
options.SetMaxRetries(maxRetries)
options.SetMaxRetryTime(uint(retryExpire.Milliseconds()))
options.SetMaxRetryTime(uint(retryExpire.Milliseconds())) //nolint:gosec // The constant 600_000 do not overflow uint.

Check warning on line 76 in pkg/monitoring/influxdb2_middleware.go

View check run for this annotation

Codecov / codecov/patch

pkg/monitoring/influxdb2_middleware.go#L76

Added line #L76 was not covered by tests
options.SetRetryBufferLimit(retryBufferLimit)

// Create a new influx client.
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var (
// InitialWaitingDuration is the default initial duration of waiting after a failed time.
InitialWaitingDuration = time.Second
ErrRetryContextDone = errors.New("the retry context is done")
ErrMaxNumberExceeded = errors.New("the passed number is too big")
ErrOverflow = errors.New("the passed number is too small or too big")
)

func retryExponential(ctx context.Context, sleep time.Duration, f func() error) func() error {
Expand Down

0 comments on commit 6d9f93e

Please sign in to comment.