github.com/kubeshop/testkube@v1.17.23/pkg/envs/variables.go (about)

     1  package envs
     2  
     3  import (
     4  	"github.com/kelseyhightower/envconfig"
     5  	"github.com/pkg/errors"
     6  
     7  	"github.com/kubeshop/testkube/pkg/executor/output"
     8  	"github.com/kubeshop/testkube/pkg/ui"
     9  )
    10  
    11  // Params are the environment variables provided by the Testkube api-server
    12  type Params struct {
    13  	Endpoint                  string // RUNNER_ENDPOINT
    14  	AccessKeyID               string // RUNNER_ACCESSKEYID
    15  	SecretAccessKey           string // RUNNER_SECRETACCESSKEY
    16  	Region                    string // RUNNER_REGION
    17  	Token                     string // RUNNER_TOKEN
    18  	Bucket                    string // RUNNER_BUCKET
    19  	Ssl                       bool   // RUNNER_SSL
    20  	SkipVerify                bool   `envconfig:"RUNNER_SKIP_VERIFY" default:"false"` // RUNNER_SKIP_VERIFY
    21  	CertFile                  string `envconfig:"RUNNER_CERT_FILE"`                   // RUNNER_CERT_FILE
    22  	KeyFile                   string `envconfig:"RUNNER_KEY_FILE"`                    // RUNNER_KEY_FILE
    23  	CAFile                    string `envconfig:"RUNNER_CA_FILE"`
    24  	ScrapperEnabled           bool   // RUNNER_SCRAPPERENABLED
    25  	DataDir                   string // RUNNER_DATADIR
    26  	GitUsername               string // RUNNER_GITUSERNAME
    27  	GitToken                  string // RUNNER_GITTOKEN
    28  	CompressArtifacts         bool   // RUNNER_COMPRESSARTIFACTS
    29  	WorkingDir                string // RUNNER_WORKINGDIR
    30  	ExecutionID               string // RUNNER_EXECUTIONID
    31  	TestName                  string // RUNNER_TESTNAME
    32  	ExecutionNumber           int32  // RUNNER_EXECUTIONNUMBER
    33  	ContextType               string // RUNNER_CONTEXTTYPE
    34  	ContextData               string // RUNNER_CONTEXTDATA
    35  	APIURI                    string // RUNNER_APIURI
    36  	ClusterID                 string `envconfig:"RUNNER_CLUSTERID"`                             // RUNNER_CLUSTERID
    37  	CDEventsTarget            string `envconfig:"RUNNER_CDEVENTS_TARGET"`                       // RUNNER_CDEVENTS_TARGET
    38  	DashboardURI              string `envconfig:"RUNNER_DASHBOARD_URI"`                         // RUNNER_DASHBOARD_URI
    39  	CloudMode                 bool   `envconfig:"RUNNER_CLOUD_MODE"`                            // RUNNER_CLOUD_MODE
    40  	CloudAPIKey               string `envconfig:"RUNNER_CLOUD_API_KEY"`                         // RUNNER_CLOUD_API_KEY
    41  	CloudAPITLSInsecure       bool   `envconfig:"RUNNER_CLOUD_API_TLS_INSECURE"`                // RUNNER_CLOUD_API_TLS_INSECURE
    42  	CloudAPIURL               string `envconfig:"RUNNER_CLOUD_API_URL"`                         // RUNNER_CLOUD_API_URL
    43  	CloudConnectionTimeoutSec int    `envconfig:"RUNNER_CLOUD_CONNECTION_TIMEOUT" default:"10"` // RUNNER_CLOUD_CONNECTION_TIMEOUT
    44  	CloudAPISkipVerify        bool   `envconfig:"RUNNER_CLOUD_API_SKIP_VERIFY" default:"false"` // RUNNER_CLOUD_API_SKIP_VERIFY
    45  	ProMode                   bool   `envconfig:"RUNNER_PRO_MODE"`                              // RUNNER_PRO_MODE
    46  	ProAPIKey                 string `envconfig:"RUNNER_PRO_API_KEY"`                           // RUNNER_PRO_API_KEY
    47  	ProAPITLSInsecure         bool   `envconfig:"RUNNER_PRO_API_TLS_INSECURE"`                  // RUNNER_PRO_API_TLS_INSECURE
    48  	ProAPIURL                 string `envconfig:"RUNNER_PRO_API_URL"`                           // RUNNER_PRO_API_URL
    49  	ProConnectionTimeoutSec   int    `envconfig:"RUNNER_PRO_CONNECTION_TIMEOUT" default:"10"`   // RUNNER_PRO_CONNECTION_TIMEOUT
    50  	ProAPISkipVerify          bool   `envconfig:"RUNNER_PRO_API_SKIP_VERIFY" default:"false"`   // RUNNER_PRO_API_SKIP_VERIFY
    51  	ProAPICertFile            string `envconfig:"RUNNER_PRO_API_CERT_FILE"`                     // RUNNER_PRO_API_CERT_FILE
    52  	ProAPIKeyFile             string `envconfig:"RUNNER_PRO_API_KEY_FILE"`                      // RUNNER_PRO_API_KEY_FILE
    53  	ProAPICAFile              string `envconfig:"RUNNER_PRO_API_CA_FILE"`                       // RUNNER_PRO_API_CA_FILE
    54  	SlavesConfigs             string `envconfig:"RUNNER_SLAVES_CONFIGS"`                        // RUNNER_SLAVES_CONFIGS
    55  }
    56  
    57  // LoadTestkubeVariables loads the parameters provided as environment variables in the Test CRD
    58  func LoadTestkubeVariables() (Params, error) {
    59  	var params Params
    60  	err := envconfig.Process("runner", &params)
    61  	if err != nil {
    62  		return params, errors.Errorf("failed to read environment variables: %v", err)
    63  	}
    64  	cleanDeprecatedParams(&params)
    65  	return params, nil
    66  }
    67  
    68  // PrintParams shows the read parameters in logs
    69  func PrintParams(params Params) {
    70  	output.PrintLogf("%s Environment variables read successfully", ui.IconCheckMark)
    71  	output.PrintLogf("RUNNER_ENDPOINT=\"%s\"", params.Endpoint)
    72  	printSensitiveParam("RUNNER_ACCESSKEYID", params.AccessKeyID)
    73  	printSensitiveParam("RUNNER_SECRETACCESSKEY", params.SecretAccessKey)
    74  	output.PrintLogf("RUNNER_REGION=\"%s\"", params.Region)
    75  	printSensitiveParam("RUNNER_TOKEN", params.Token)
    76  	output.PrintLogf("RUNNER_BUCKET=\"%s\"", params.Bucket)
    77  	output.PrintLogf("RUNNER_SSL=%t", params.Ssl)
    78  	output.PrintLogf("RUNNER_SCRAPPERENABLED=\"%t\"", params.ScrapperEnabled)
    79  	output.PrintLogf("RUNNER_GITUSERNAME=\"%s\"", params.GitUsername)
    80  	printSensitiveParam("RUNNER_GITTOKEN", params.GitToken)
    81  	output.PrintLogf("RUNNER_DATADIR=\"%s\"", params.DataDir)
    82  	output.PrintLogf("RUNNER_COMPRESSARTIFACTS=\"%t\"", params.CompressArtifacts)
    83  	output.PrintLogf("RUNNER_WORKINGDIR=\"%s\"", params.WorkingDir)
    84  	output.PrintLogf("RUNNER_EXECUTIONID=\"%s\"", params.ExecutionID)
    85  	output.PrintLogf("RUNNER_TESTNAME=\"%s\"", params.TestName)
    86  	output.PrintLogf("RUNNER_EXECUTIONNUMBER=\"%d\"", params.ExecutionNumber)
    87  	output.PrintLogf("RUNNER_CONTEXTTYPE=\"%s\"", params.ContextType)
    88  	output.PrintLogf("RUNNER_CONTEXTDATA=\"%s\"", params.ContextData)
    89  	output.PrintLogf("RUNNER_APIURI=\"%s\"", params.APIURI)
    90  	output.PrintLogf("RUNNER_CLUSTERID=\"%s\"", params.ClusterID)
    91  	output.PrintLogf("RUNNER_CDEVENTS_TARGET=\"%s\"", params.CDEventsTarget)
    92  	output.PrintLogf("RUNNER_DASHBOARD_URI=\"%s\"", params.DashboardURI)
    93  	output.PrintLogf("RUNNER_CLOUD_MODE=\"%t\" - DEPRECATED: please use RUNNER_PRO_MODE instead", params.CloudMode)
    94  	output.PrintLogf("RUNNER_CLOUD_API_TLS_INSECURE=\"%t\" - DEPRECATED: please use RUNNER_PRO_API_TLS_INSECURE instead", params.CloudAPITLSInsecure)
    95  	output.PrintLogf("RUNNER_CLOUD_API_URL=\"%s\" - DEPRECATED: please use RUNNER_PRO_API_URL instead", params.CloudAPIURL)
    96  	printSensitiveDeprecatedParam("RUNNER_CLOUD_API_KEY", params.CloudAPIKey, "RUNNER_PRO_API_KEY")
    97  	output.PrintLogf("RUNNER_CLOUD_CONNECTION_TIMEOUT=%d - DEPRECATED: please use RUNNER_PRO_CONNECTION_TIMEOUT instead", params.CloudConnectionTimeoutSec)
    98  	output.PrintLogf("RUNNER_CLOUD_API_SKIP_VERIFY=\"%t\" - DEPRECATED: please use RUNNER_PRO_API_SKIP_VERIFY instead", params.CloudAPISkipVerify)
    99  	output.PrintLogf("RUNNER_PRO_MODE=\"%t\"", params.ProMode)
   100  	output.PrintLogf("RUNNER_PRO_API_TLS_INSECURE=\"%t\"", params.ProAPITLSInsecure)
   101  	output.PrintLogf("RUNNER_PRO_API_URL=\"%s\"", params.ProAPIURL)
   102  	printSensitiveParam("RUNNER_PRO_API_KEY", params.ProAPIKey)
   103  	output.PrintLogf("RUNNER_PRO_CONNECTION_TIMEOUT=%d", params.ProConnectionTimeoutSec)
   104  	output.PrintLogf("RUNNER_PRO_API_SKIP_VERIFY=\"%t\"", params.ProAPISkipVerify)
   105  
   106  }
   107  
   108  // printSensitiveParam shows in logs if a parameter is set or not
   109  func printSensitiveParam(name string, value string) {
   110  	if len(value) == 0 {
   111  		output.PrintLogf("%s=\"\"", name)
   112  	} else {
   113  		output.PrintLogf("%s=\"********\"", name)
   114  	}
   115  }
   116  
   117  // printSensitiveDeprecatedParam shows in logs if a parameter is set or not
   118  func printSensitiveDeprecatedParam(name string, value string, newName string) {
   119  	if len(value) == 0 {
   120  		output.PrintLogf("%s=\"\" - DEPRECATED: please use %s instead", name, newName)
   121  	} else {
   122  		output.PrintLogf("%s=\"********\" - DEPRECATED: please use %s instead", name, newName)
   123  	}
   124  }
   125  
   126  // cleanDeprecatedParams makes sure deprecated parameter values are set in replacements
   127  func cleanDeprecatedParams(params *Params) {
   128  	if !params.ProMode && params.CloudMode {
   129  		params.ProMode = params.CloudMode
   130  	}
   131  
   132  	if params.ProAPIKey == "" && params.CloudAPIKey != "" {
   133  		params.ProAPIKey = params.CloudAPIKey
   134  	}
   135  
   136  	if !params.ProAPITLSInsecure && params.CloudAPITLSInsecure {
   137  		params.ProAPITLSInsecure = params.CloudAPITLSInsecure
   138  	}
   139  
   140  	if params.ProAPIURL == "" && params.CloudAPIURL != "" {
   141  		params.ProAPIURL = params.CloudAPIURL
   142  	}
   143  
   144  	if params.ProConnectionTimeoutSec == 0 && params.CloudConnectionTimeoutSec != 0 {
   145  		params.ProConnectionTimeoutSec = params.CloudConnectionTimeoutSec
   146  	}
   147  
   148  	if !params.ProAPISkipVerify && params.CloudAPISkipVerify {
   149  		params.ProAPISkipVerify = params.CloudAPISkipVerify
   150  	}
   151  }