utils

package
v0.0.0-...-eed12a1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

pkg/utils/utils.go

Index

Constants

This section is empty.

Variables

View Source
var (
	ExitFunc                 = os.Exit              // Function for exiting the application
	WaitForKubernetesAPIFunc = WaitForKubernetesAPI // Function for mocking
	RetryInterval            = 2 * time.Second      // Default values for retry logic, can be overridden externally

	// GCP Metadata Server URL
	MetadataServerBaseURL = "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity"

	// ListenAndServeWrapper wraps the http.ListenAndServe function for testing
	ListenAndServeWrapper = func(addr string, handler http.Handler) error {
		server := &http.Server{
			Addr:    addr,
			Handler: handler,
		}

		listener, err := net.Listen("tcp", addr)
		if err != nil {
			logging.Logger().Errorw(
				"Port already in use or cannot bind",
				"address", addr,
				"error", err,
			)
			return err
		}

		logging.Logger().Infow(
			"Server started successfully",
			"address", addr,
		)
		return server.Serve(listener)
	}
)

Functions

func BuildUserAgent

func BuildUserAgent(name, dependency, platform, version string) string

Build User-Agent string

func FetchGCPIdentityToken

func FetchGCPIdentityToken(client *http.Client, audience string) (string, error)

Retrieve GCP Service Account JWT

func GetAvailablePort

func GetAvailablePort() (int, error)

Dynamically find an unused TCP port

func GetDependencyVersion

func GetDependencyVersion(dependency string) string

Get the Dependency Version

func GetDynamicMetricsAndProbePorts

func GetDynamicMetricsAndProbePorts(defaultMetricsPort, defaultProbePort int) (int, int)

Dynamically find ports for the metrics and probe servers (use for avoiding port conflicts during tests)

func GetEnvOrFatal

func GetEnvOrFatal(env string) string

Retrieves an environment variable or exits if not set

func GetEnvValueOrDefault

func GetEnvValueOrDefault(variableName, defaultValue string, enableLogging bool) string

Retrieves a variable or if not set, returns the provided default

func ParseTimeInterval

func ParseTimeInterval(interval, defaultInterval string) time.Duration

Process time duration

func WaitForKubernetesAPI

func WaitForKubernetesAPI(clientset kubernetes.Interface, namespace string, timeout time.Duration)

WaitForKubernetesAPI waits for the Kubernetes API to be ready within a timeout period

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL