Skip to content

Commit

Permalink
Merge pull request #77 from niqniqniqq/add_max_job_creation_time_option
Browse files Browse the repository at this point in the history
Fixed to be able to get xx_wait_time from environment variable
  • Loading branch information
niqniqniqq authored Mar 7, 2023
2 parents 8f1e109 + eaedb87 commit 55efbad
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
20 changes: 12 additions & 8 deletions controllers/gatling_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,9 @@ func (r *GatlingReconciler) gatlingRunnerReconcile(ctx context.Context, req ctrl
err := r.Get(ctx, client.ObjectKey{Name: gatling.Status.RunnerJobName, Namespace: req.Namespace}, foundJob)
if err != nil && apierr.IsNotFound(err) {
duration := utils.GetEpocTime() - gatling.Status.RunnerStartTime
if duration > maxJobCreationWaitTimeInSeconds {
msg := fmt.Sprintf("Runs out of time (%d sec) in creating the runner job", maxJobCreationWaitTimeInSeconds)
maxCreationWaitTIme := int32(utils.GetNumEnv("MAX_JOB_CREATION_WAIT_TIME", maxJobCreationWaitTimeInSeconds))
if duration > maxCreationWaitTIme {
msg := fmt.Sprintf("Runs out of time (%d sec) in creating the runner job", maxCreationWaitTIme)
log.Error(err, msg, "namespace", req.Namespace, "name", gatling.Status.RunnerJobName)
gatling.Status.Error = msg
if err := r.updateGatlingStatus(ctx, gatling); err != nil {
Expand All @@ -240,8 +241,9 @@ func (r *GatlingReconciler) gatlingRunnerReconcile(ctx context.Context, req ctrl

// Check if the job runs out of time in running the job
duration := utils.GetEpocTime() - gatling.Status.RunnerStartTime
if duration > maxJobRunWaitTimeInSeconds {
msg := fmt.Sprintf("Runs out of time (%d sec) in running the runner job", maxJobCreationWaitTimeInSeconds)
maxRunWaitTIme := int32(utils.GetNumEnv("MAX_JOB_RUN_WAIT_TIME", maxJobRunWaitTimeInSeconds))
if duration > maxRunWaitTIme {
msg := fmt.Sprintf("Runs out of time (%d sec) in running the runner job", maxRunWaitTIme)
log.Error(nil, msg, "namespace", req.Namespace, "name", gatling.Status.ReporterJobName)
gatling.Status.Error = msg
if err := r.updateGatlingStatus(ctx, gatling); err != nil {
Expand Down Expand Up @@ -317,8 +319,9 @@ func (r *GatlingReconciler) gatlingReporterReconcile(ctx context.Context, req ct
if err != nil && apierr.IsNotFound(err) {
// Check if the job runs out of time in creating the job
duration := utils.GetEpocTime() - gatling.Status.ReporterStartTime
if duration > maxJobCreationWaitTimeInSeconds {
msg := fmt.Sprintf("Runs out of time (%d sec) in creating the reporter job", maxJobCreationWaitTimeInSeconds)
maxCreationWaitTIme := int32(utils.GetNumEnv("MAX_JOB_CREATION_WAIT_TIME", maxJobCreationWaitTimeInSeconds))
if duration > maxCreationWaitTIme {
msg := fmt.Sprintf("Runs out of time (%d sec) in creating the reporter job", maxCreationWaitTIme)
log.Error(err, msg, "namespace", req.Namespace, "name", gatling.Status.ReporterJobName)
gatling.Status.Error = msg
if err := r.updateGatlingStatus(ctx, gatling); err != nil {
Expand All @@ -334,8 +337,9 @@ func (r *GatlingReconciler) gatlingReporterReconcile(ctx context.Context, req ct
}
// Check if the job runs out of time in running the job
duration := utils.GetEpocTime() - gatling.Status.ReporterStartTime
if duration > maxJobRunWaitTimeInSeconds {
msg := fmt.Sprintf("Runs out of time (%d sec) in running the reporter job, and no longer requeue", maxJobCreationWaitTimeInSeconds)
maxRunWaitTIme := int32(utils.GetNumEnv("MAX_JOB_RUN_WAIT_TIME", maxJobRunWaitTimeInSeconds))
if duration > maxRunWaitTIme {
msg := fmt.Sprintf("Runs out of time (%d sec) in running the reporter job, and no longer requeue", maxRunWaitTIme)
log.Error(nil, msg, "namespace", req.Namespace, "name", gatling.Status.ReporterJobName)
gatling.Status.Error = msg
if err := r.updateGatlingStatus(ctx, gatling); err != nil {
Expand Down
16 changes: 16 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package utils

import (
"fmt"
"hash/fnv"
"os"
"strconv"
"time"
)

Expand Down Expand Up @@ -36,3 +39,16 @@ func AddMapValue(key string, value string, dataMap map[string]string, overwrite
return dataMap

}

func GetNumEnv(key string, defaultValue int) int {
value := os.Getenv(key)
if value == "" {
return defaultValue
}
valueNum, err := strconv.Atoi(value)
if err != nil {
fmt.Fprintf(os.Stderr, "ENV '%s' is not valid\n", key)
return defaultValue
}
return valueNum
}

0 comments on commit 55efbad

Please sign in to comment.