github.com/verrazzano/verrazzano@v1.7.1/tools/psr/backend/config/config.go (about)

     1  // Copyright (c) 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package config
     5  
     6  import (
     7  	"strconv"
     8  	"time"
     9  
    10  	"github.com/verrazzano/verrazzano/pkg/log/vzlog"
    11  	"github.com/verrazzano/verrazzano/tools/psr/backend/osenv"
    12  )
    13  
    14  // Define common worker configuration params
    15  const (
    16  	// PsrWorkerType specifies a worker type
    17  	PsrWorkerType = "PSR_WORKER_TYPE"
    18  
    19  	// PsrDuration specified the duration of the test using a duration string ("4m or 2h")
    20  	// By default, the worker runs until the pod terminates
    21  	PsrDuration = "PSR_DURATION"
    22  
    23  	// PsrLoopSleep specified the sleep duration between loops
    24  	// of work actions using a duration string ("4m or 2h")
    25  	// For example, delay 1 second between logging
    26  	// By default, the worker does not delay
    27  	PsrLoopSleep = "PSR_LOOP_SLEEP"
    28  
    29  	// PsrNumLoops specifies the number of loops
    30  	// of work actions.  The default is -1 (forever)
    31  	// By default, the worker iterates forever
    32  	PsrNumLoops = "PSR_NUM_ITERATIONS"
    33  
    34  	// PsrWorkerThreadCount specifies the number of worker threads to run.
    35  	// By default, there is one thread per worker
    36  	PsrWorkerThreadCount = "PSR_WORKER_THREAD_COUNT"
    37  
    38  	// PsrWorkerNamespace is the namespace of the PSR release
    39  	PsrWorkerNamespace = "NAMESPACE"
    40  
    41  	// PsrWorkerReleaseName is the name of the PSR release
    42  	PsrWorkerReleaseName = "RELEASE_NAME"
    43  
    44  	// PsrWorkerTypeMetricsName is the metrics label key for the PSR worker type
    45  	PsrWorkerTypeMetricsName = "psr_worker_type"
    46  )
    47  
    48  // Define worker types
    49  const (
    50  	WorkerTypeExample       = "example"
    51  	WorkerTypeOpsWriteLogs  = "ops-writelogs"
    52  	WorkerTypeOpsGetLogs    = "ops-getlogs"
    53  	WorkerTypeOpsPostLogs   = "ops-postlogs"
    54  	WorkerTypeOpsScale      = "ops-scale"
    55  	WorkerTypeOpsRestart    = "ops-restart"
    56  	WorkerTypeHTTPGet       = "http-get"
    57  	WorkerTypeReceiveAlerts = "prom-alerts"
    58  	WorkerTypeWlsScale      = "wls-scale"
    59  )
    60  
    61  const (
    62  	UnlimitedWorkerLoops    = -1
    63  	UnlimitedWorkerDuration = -1 * time.Second
    64  )
    65  
    66  var PsrEnv = osenv.NewEnv()
    67  
    68  type CommonConfig struct {
    69  	WorkerType        string
    70  	PsrDuration       time.Duration
    71  	LoopSleepNanos    time.Duration
    72  	NumLoops          int64
    73  	WorkerThreadCount int
    74  	Namespace         string
    75  	ReleaseName       string
    76  }
    77  
    78  // GetCommonConfig loads the common config from env vars
    79  func GetCommonConfig(log vzlog.VerrazzanoLogger) (CommonConfig, error) {
    80  	dd := []osenv.EnvVarDesc{
    81  		{Key: PsrWorkerType, DefaultVal: "", Required: true},
    82  		{Key: PsrDuration, DefaultVal: "-1s", Required: false},
    83  		{Key: PsrLoopSleep, DefaultVal: "1s", Required: false},
    84  		{Key: PsrNumLoops, DefaultVal: "-1", Required: false},
    85  		{Key: PsrWorkerThreadCount, DefaultVal: "1", Required: false},
    86  		{Key: PsrWorkerNamespace, DefaultVal: "", Required: false},
    87  		{Key: PsrWorkerReleaseName, DefaultVal: "", Required: false},
    88  	}
    89  	if err := PsrEnv.LoadFromEnv(dd); err != nil {
    90  		return CommonConfig{}, err
    91  	}
    92  
    93  	duration, err := time.ParseDuration(PsrEnv.GetEnv(PsrDuration))
    94  	if err != nil {
    95  		return CommonConfig{}, log.ErrorfNewErr("Error parsing worker duration: %v", err)
    96  	}
    97  	// Negative values are not allowed, -1s represents no duration timeout
    98  	if duration < 0 {
    99  		duration = UnlimitedWorkerDuration
   100  	}
   101  
   102  	sleepDuration, err := time.ParseDuration(PsrEnv.GetEnv(PsrLoopSleep))
   103  	if err != nil {
   104  		return CommonConfig{}, log.ErrorfNewErr("Error parsing loop sleep duration: %v", err)
   105  	}
   106  	// Sleep at least 10 nanos
   107  	if sleepDuration < (10 * time.Nanosecond) {
   108  		sleepDuration = 10 * time.Nanosecond
   109  	}
   110  
   111  	threadCount, err := strconv.Atoi(PsrEnv.GetEnv(PsrWorkerThreadCount))
   112  	if err != nil {
   113  		return CommonConfig{}, log.ErrorfNewErr("Error parsing worker thread count: %v", err)
   114  	}
   115  	// Max thread count is 100
   116  	if threadCount > 100 {
   117  		threadCount = 100
   118  	}
   119  
   120  	numLoops, err := strconv.Atoi(PsrEnv.GetEnv(PsrNumLoops))
   121  	if err != nil {
   122  		return CommonConfig{}, log.ErrorfNewErr("Failed to convert ENV var %s to integer", PsrNumLoops)
   123  	}
   124  
   125  	return CommonConfig{
   126  		WorkerType:        PsrEnv.GetEnv(PsrWorkerType),
   127  		PsrDuration:       duration,
   128  		LoopSleepNanos:    sleepDuration,
   129  		NumLoops:          int64(numLoops),
   130  		WorkerThreadCount: threadCount,
   131  		Namespace:         PsrEnv.GetEnv(PsrWorkerNamespace),
   132  		ReleaseName:       PsrEnv.GetEnv(PsrWorkerReleaseName),
   133  	}, nil
   134  }