common

package
v0.0.0-...-0c4b5fb Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: Apache-2.0 Imports: 48 Imported by: 15

Documentation

Index

Constants

View Source
const (
	AutoPlatformName  = "auto"
	KubePlatformName  = "kube"
	LocalPlatformName = "local"
)
View Source
const (
	ReferencePrefix         = "$ref:"
	ReferenceToEnvVarPrefix = "NUCLIO_B64_"
	SecretContentKey        = "content"
)
View Source
const (
	HTTPPrefix      = "http://"
	HTTPSPrefix     = "https://"
	LocalFilePrefix = "file://"
)
View Source
const DefaultIngressHostTemplate = "@nuclio.fromDefault"
View Source
const FunctionConfigFileName = "function.yaml"
View Source
const KubernetesDomainLevelMaxLength = 63

KubernetesDomainLevelMaxLength DNS domain level limitation is 63 chars https://en.wikipedia.org/wiki/Subdomain#Overview

View Source
const NuclioLabelKeyApp = "nuclio.io/app"
View Source
const NuclioLabelKeyClass = "nuclio.io/class"
View Source
const NuclioLabelKeyComponent = "nuclio.io/component"
View Source
const NuclioLabelKeyFunctionCronJobPod = "nuclio.io/function-cron-job-pod"
View Source
const NuclioLabelKeyFunctionCronTriggerName = "nuclio.io/function-cron-trigger-name"
View Source
const NuclioLabelKeyFunctionVersion = "nuclio.io/function-version"
View Source
const NuclioResourceLabelKeyApiGatewayName = "nuclio.io/apigateway-name"
View Source
const NuclioResourceLabelKeyFunctionName = "nuclio.io/function-name"
View Source
const NuclioResourceLabelKeyProjectName = "nuclio.io/project-name"
View Source
const NuclioResourceLabelKeyVolumeName = "nuclio.io/volume-name"
View Source
const RestoreConfigFromSecretEnvVar = "NUCLIO_RESTORE_FUNCTION_CONFIG_FROM_SECRET"

Variables

View Source
var LettersAndNumbers = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
View Source
var SmallLettersAndNumbers = []rune("abcdefghijklmnopqrstuvwxyz1234567890")

Functions

func AnyPositiveInSliceInt64

func AnyPositiveInSliceInt64(numbers []int64) bool

func ApplyRedactorChange

func ApplyRedactorChange(loggerInstance logger.Logger, callback func(*nucliozap.Redactor))

func ByteSliceToString

func ByteSliceToString(b []byte) string

func CatchAndLogPanicWithOptions

func CatchAndLogPanicWithOptions(ctx context.Context,
	loggerInstance logger.Logger,
	actionName string,
	options *CatchAndLogPanicOptions) error

func CompareTwoStrings

func CompareTwoStrings(stringOne, stringTwo string) float32

CompareTwoStrings uses Sørensen–Dice coefficient to check similarity

func CompileImageName

func CompileImageName(registryURL string, imageName string) string

func CompileListFunctionPodsLabelSelector

func CompileListFunctionPodsLabelSelector(functionName string) string

func CompileStalePodsFieldSelector

func CompileStalePodsFieldSelector() string

CompileStalePodsFieldSelector creates a field selector(string) for stale pods

func DownloadFile

func DownloadFile(url string, out *os.File, headers http.Header) error

func EnsureDirExists

func EnsureDirExists(ctx context.Context, loggerInstance logger.Logger, dir string) error

EnsureDirExists checks if the directory exists, creates it if it doesn't

func EnvInSlice

func EnvInSlice(env v1.EnvVar, slice []v1.EnvVar) bool

func ErrorFromRecoveredError

func ErrorFromRecoveredError(recoveredError interface{}) error

func FileExists

func FileExists(path string) bool

FileExists returns true if the file @ path exists

func FilterInvalidLabels

func FilterInvalidLabels(labels map[string]string) map[string]string

FilterInvalidLabels filters out invalid kubernetes labels from a map of labels

func FixEscapeChars

func FixEscapeChars(s string) string

func GenerateRandomString

func GenerateRandomString(length int, letters []rune) string

func GenerateStringMatchVerifier

func GenerateStringMatchVerifier(str string) func(string) bool

GenerateStringMatchVerifier generates a function that returns whether a given string matches the specified string

func GetAttributeRecursivelyFromMapStringInterface

func GetAttributeRecursivelyFromMapStringInterface(mapStringInterface map[string]interface{}, attributes []string) map[string]interface{}

GetAttributeRecursivelyFromMapStringInterface iterates over the attributes slice and recursively searches the map, returning the last attribute in the slice

func GetClientConfig

func GetClientConfig(kubeconfigPath string) (*rest.Config, error)

func GetDurationOrInfinite

func GetDurationOrInfinite(timeout *time.Duration) time.Duration

func GetEnvOrDefaultBool

func GetEnvOrDefaultBool(key string, defaultValue bool) bool

func GetEnvOrDefaultInt

func GetEnvOrDefaultInt(key string, defaultValue int) int

func GetEnvOrDefaultString

func GetEnvOrDefaultString(key string, defaultValue string) string

func GetKubeConfigClientCmdByKubeconfigPath

func GetKubeConfigClientCmdByKubeconfigPath(kubeconfigPath string) (*clientcmdapi.Config, error)

func GetKubeconfigPath

func GetKubeconfigPath(kubeconfigPath string) string

func GetLoggersFromInstance

func GetLoggersFromInstance(loggerInstance logger.Logger) []logger.Logger

func GetPathFromLocalFileURL

func GetPathFromLocalFileURL(s string) string

GetPathFromLocalFileURL extracts absolute path to file from local file URL example: "file://path/to/file" -> "/path/to/file"

func GetRedactorInstance

func GetRedactorInstance(output io.Writer) *nucliozap.Redactor

func GetRuntimeNameAndVersion

func GetRuntimeNameAndVersion(runtime string) (string, string)

GetRuntimeNameAndVersion return runtime name and version. e.g. go:1.8 -> go, 1.8

func GetSourceDir

func GetSourceDir() string

func GetStringToStringMapOrEmpty

func GetStringToStringMapOrEmpty(m map[string]string) map[string]string

GetStringToStringMapOrEmpty returns string to string map if it's not nil. otherwise, return an empty one

func GetZipFileContents

func GetZipFileContents(zf *zip.File) (string, error)

func IsDir

func IsDir(path string) bool

IsDir returns true if the object @ path is a dir

func IsFile

func IsFile(path string) bool

IsFile returns true if the object @ path is a file

func IsInKubernetesCluster

func IsInKubernetesCluster() bool

func IsJavaProjectDir

func IsJavaProjectDir(dirPath string) bool

IsJavaProjectDir Checks if the given @dirPath is in a java project structure for example if the following dir existed "/my-project/src/main/java" then IsJavaProjectDir("/my-project") -> true

func IsLocalFileURL

func IsLocalFileURL(s string) bool

func IsURL

func IsURL(s string) bool

func LabelsMapMatchByLabelSelector

func LabelsMapMatchByLabelSelector(labelSelector string, labelsMap map[string]string) (bool, error)

LabelsMapMatchByLabelSelector returns whether a labelsMap is matched by an encoded label selector corresponding to https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/

Example:

labelsMap:
  a: b
  c: d
encodedLabelSelector: a=b

returns true

func LogPanic

func LogPanic(ctx context.Context,
	loggerInstance logger.Logger,
	actionName string,
	args []interface{},
	callStack []byte,
	err interface{})

func MapInterfaceInterfaceToMapStringInterface

func MapInterfaceInterfaceToMapStringInterface(mapInterfaceInterface map[interface{}]interface{}) map[string]interface{}

MapInterfaceInterfaceToMapStringInterface recursively converts map[interface{}]interface{} to map[string]interface{}

func MapStringInterfaceGetOrDefault

func MapStringInterfaceGetOrDefault(mapStringInterface map[string]interface{},
	key string,
	defaultValue interface{}) interface{}

MapStringInterfaceGetOrDefault will return the key as an integer or return a default

func MapStringStringToMapStringBytesArray

func MapStringStringToMapStringBytesArray(m map[string]string) map[string][]byte

MapStringStringToMapStringBytesArray converts the string values of a map to byte arrays Example: {"a": "b"} -> {"a": []byte("b")}

func MapToSlice

func MapToSlice(m map[string]interface{}) []interface{}

MapToSlice converts {key1: val1, key2: val2 ...} to [key1, val1, key2, val2 ...]

func MatchStringPatterns

func MatchStringPatterns(patterns []string, s string) bool

func MergeEnvSlices

func MergeEnvSlices(primaryEnv []v1.EnvVar, secondaryEnv []v1.EnvVar) []v1.EnvVar

MergeEnvSlices merges two lists of environment variables, giving priority to variables from the primary list

func NormalizeURLPath

func NormalizeURLPath(p string) string

NormalizeURLPath normalizes URL Path examples: "" -> "/" "a" -> "/a/" "//a//b/c/" -> "/a/b/c/"

func ParseQuantityOrDefault

func ParseQuantityOrDefault(value string,
	defaultValue string,
	loggerInstance logger.Logger) apiresource.Quantity

func PopulateFieldsFromValues

func PopulateFieldsFromValues[T string | bool | int](fieldsToValues map[*T]T)

PopulateFieldsFromValues populates empty fields with values if the value is not zero. the fieldsToValues argument is a map of pointers to fields to populate and values.

func PrependStringToStringSlice

func PrependStringToStringSlice(slice []string, str string) []string

PrependStringToStringSlice prepends a string to a string slice

func PrependStringsToStringSlice

func PrependStringsToStringSlice(slice []string, strs ...string) []string

PrependStringsToStringSlice prepends multiple strings to a string slice

func Quote

func Quote(s string) string

Quote returns a shell-escaped version of the string s. The returned value is a string that can safely be used as one token in a shell command line.

func RemoveANSIColorsFromString

func RemoveANSIColorsFromString(s string) string

RemoveANSIColorsFromString strips out ANSI Colors chars from string example: "\u001b[31mHelloWorld" -> "HelloWorld"

func RemoveDuplicatesFromSliceString

func RemoveDuplicatesFromSliceString(slice []string) []string

func RemoveEmptyLines

func RemoveEmptyLines(input string) string

RemoveEmptyLines removes all empty lines from a string

func RemoveEnvFromSlice

func RemoveEnvFromSlice(env v1.EnvVar, slice []v1.EnvVar) []v1.EnvVar

func RemoveStringSliceItemsFromStringSlice

func RemoveStringSliceItemsFromStringSlice(slice []string, itemsToRemove []string) []string

func RemoveWindowsCarriage

func RemoveWindowsCarriage(b []byte) []byte

RemoveWindowsCarriage removes windows carriage character '\r' when it follows by '\n'

func RenderTemplate

func RenderTemplate(text string, data map[string]interface{}) (string, error)

func RenderTemplateWithCustomDelimiters

func RenderTemplateWithCustomDelimiters(text string,
	data map[string]interface{},
	leftDelimiter string,
	rightDelimiter string) (string, error)

func ResolveDefaultNamespace

func ResolveDefaultNamespace(namespace string) string

ResolveDefaultNamespace returns the proper default resource namespace, given the current default namespace

func ResolveErrorStatusCodeOrDefault

func ResolveErrorStatusCodeOrDefault(err error, defaultStatusCode int) int

func ResolveNamespace

func ResolveNamespace(namespaceArgument string, defaultEnvVarKey string) string

ResolveNamespace returns the namespace by the following order: 1. If namespace is passed as an argument, use that 2. If namespace is passed as an environment variable, use that 3. Alternatively, use "this" namespace (where the pod is running)

func RetryUntilSuccessful

func RetryUntilSuccessful(duration time.Duration, interval time.Duration, callback func() bool) error

RetryUntilSuccessful calls callback every interval for duration until it returns true

func RetryUntilSuccessfulOnErrorPatterns

func RetryUntilSuccessfulOnErrorPatterns(
	duration time.Duration,
	interval time.Duration,
	errorRegexPatterns []string,
	callback func(retryCounter int) (string, error)) error

RetryUntilSuccessfulOnErrorPatterns calls callback every interval for duration as long as error pattern is matched the callback argument retryCounter is the number of times the callback was called

func RunningContainerHostname

func RunningContainerHostname() (string, error)

RunningContainerHostname returns the hostname (aka container id) of the running container

func RunningInContainer

func RunningInContainer() bool

RunningInContainer returns true if currently running in a container, false otherwise

func SanitizeResponseData

func SanitizeResponseData(data []byte, headers http.Header) []byte

SanitizeResponseData tries to parse byte data as html. if it succeeds, it is returned sanitized, otherwise the original data is returned. func SanitizeResponseData(data []byte) []byte {

func SendHTTPRequest

func SendHTTPRequest(httpClient *http.Client,
	method string,
	requestURL string,
	body []byte,
	headers map[string]string,
	cookies []*http.Cookie,
	expectedStatusCode int) ([]byte, *http.Response, error)

SendHTTPRequest Sends an HTTP request using custom http client ignore expectedStatusCode by setting it to 0

func SendHTTPRequestWithContext

func SendHTTPRequestWithContext(ctx context.Context,
	httpClient *http.Client,
	method string,
	requestURL string,
	body []byte,
	headers map[string]string,
	cookies []*http.Cookie,
	expectedStatusCode int) ([]byte, *http.Response, error)

SendHTTPRequestWithContext is like SendHTTPRequest but with context

func SetLoggerRedactionMode

func SetLoggerRedactionMode(loggerInstance logger.Logger, enabled bool)

func SetVersionFromEnv

func SetVersionFromEnv()

SetVersionFromEnv is being used by tests to override linker injected values

func StringInSlice

func StringInSlice(a string, list []string) bool

func StringMapToString

func StringMapToString(source map[string]string) string

StringMapToString converts a map of a: x, b: y to a string in the form of "a=x,b=y"

func StringSliceContainsString

func StringSliceContainsString(slice []string, str string) bool

StringSliceContainsString returns whether the input str is in the slice

func StringSliceContainsStringCaseInsensitive

func StringSliceContainsStringCaseInsensitive(slice []string, str string) bool

StringSliceContainsStringCaseInsensitive returns whether the input str is in the slice case-insensitive

func StringSliceContainsStringPrefix

func StringSliceContainsStringPrefix(prefixes []string, str string) bool

StringSliceContainsStringPrefix returns whether the input str has prefix

func StringSliceToIntSlice

func StringSliceToIntSlice(stringSlice []string) ([]int, error)

StringSliceToIntSlice converts slices of strings to slices of int. e.g. ["1", "3"] -> [1, 3]

func StringToStringMap

func StringToStringMap(source string, separator string) map[string]string

StringToStringMap converts a string in the form of a{separator}x,b{separator}y to a map of a: x, b: y, inputs source-string & string-separator

func StripPrefixes

func StripPrefixes(input string, prefixes []string) string

func StripSuffixes

func StripSuffixes(input string, suffixes []string) string

func StructureToMap

func StructureToMap(input interface{}) map[string]interface{}

StructureToMap converts a strcuture to a map, flattening all members

func ValidateLabels

func ValidateLabels(labels map[string]string) error

ValidateLabels validates the given labels according to k8s label constraints

Types

type AbstractS3Client

type AbstractS3Client struct {
	S3Client
}

func (*AbstractS3Client) Download

func (asc *AbstractS3Client) Download(file *os.File,
	bucket string,
	itemKey string,
	region string,
	accessKeyID string,
	secretAccessKey string,
	sessionToken string) error

func (*AbstractS3Client) DownloadWithinEC2Instance

func (asc *AbstractS3Client) DownloadWithinEC2Instance(file *os.File, bucket, itemKey string) error

type AbstractScrubber

type AbstractScrubber struct {
	SensitiveFields []*regexp.Regexp
	KubeClientSet   kubernetes.Interface
	ReferencePrefix string
	Scrubber        Scrubber
}

func NewAbstractScrubber

func NewAbstractScrubber(sensitiveFields []*regexp.Regexp, kubeClientSet kubernetes.Interface) *AbstractScrubber

NewAbstractScrubber returns a new AbstractScrubber If the scrubber is only used for restoring, the arguments can be nil

func (*AbstractScrubber) ConvertMapToConfig

func (s *AbstractScrubber) ConvertMapToConfig(mapConfig interface{}) (interface{}, error)

func (*AbstractScrubber) DecodeSecretData

func (s *AbstractScrubber) DecodeSecretData(secretData map[string][]byte) (map[string]string, error)

DecodeSecretData decodes the keys of a secrets map

func (*AbstractScrubber) DecodeSecretKey

func (s *AbstractScrubber) DecodeSecretKey(secretKey string) (string, error)

DecodeSecretKey decodes a secret key and returns the original field

func (*AbstractScrubber) DecodeSecretsMapContent

func (s *AbstractScrubber) DecodeSecretsMapContent(secretsMapContent string) (map[string]string, error)

DecodeSecretsMapContent decodes the secrets map content

func (*AbstractScrubber) EncodeSecretKey

func (s *AbstractScrubber) EncodeSecretKey(fieldPath string) string

EncodeSecretKey encodes a secret key

func (*AbstractScrubber) EncodeSecretsMap

func (s *AbstractScrubber) EncodeSecretsMap(secretsMap map[string]string) (map[string]string, error)

EncodeSecretsMap encodes the keys of a secrets map

func (*AbstractScrubber) GenerateObjectSecretName

func (s *AbstractScrubber) GenerateObjectSecretName(objectName string) string

GenerateObjectSecretName e generates a secret name for a function, in the form of: `nuclio-secret-<project-name>-<object-name>-<unique-id>`

func (*AbstractScrubber) HasScrubbedConfig

func (s *AbstractScrubber) HasScrubbedConfig(object interface{}, sensitiveFields []*regexp.Regexp) (bool, error)

HasScrubbedConfig checks if a object has scrubbed data, using the Scrub function

func (*AbstractScrubber) Restore

func (s *AbstractScrubber) Restore(scrubbedObject interface{}, secretsMap map[string]string) (interface{}, error)

Restore restores sensitive data in an object from a secrets map

func (*AbstractScrubber) Scrub

func (s *AbstractScrubber) Scrub(objectToScrub interface{},
	existingSecretMap map[string]string,
	sensitiveFields []*regexp.Regexp) (interface{}, map[string]string, error)

Scrub scrubs sensitive data from an object

func (*AbstractScrubber) ValidateReference

func (s *AbstractScrubber) ValidateReference(objectToScrub interface{},
	existingSecretMap map[string]string,
	fieldPath,
	secretKey,
	stringValue string) error

type CatchAndLogPanicOptions

type CatchAndLogPanicOptions struct {
	Args          []interface{}
	CustomHandler func(error)
}

type ExportFunctionOptions

type ExportFunctionOptions struct {
	NoScrub     bool
	CleanupSpec bool
	PrevState   string
}

type FunctionStateMessage

type FunctionStateMessage string
const (
	FunctionStateMessageUnhealthy FunctionStateMessage = "Function is not healthy"
)

type KubernetesClientWarningHandler

type KubernetesClientWarningHandler struct {
	// contains filtered or unexported fields
}

func NewKubernetesClientWarningHandler

func NewKubernetesClientWarningHandler(logger logger.Logger) *KubernetesClientWarningHandler

func (*KubernetesClientWarningHandler) HandleWarningHeader

func (kcl *KubernetesClientWarningHandler) HandleWarningHeader(code int, agent string, message string)

HandleWarningHeader handles miscellaneous warning messages yielded by Kubernetes api server e.g.: "autoscaling/v2beta1 HorizontalPodAutoscaler is deprecated in v1.22+, unavailable in v1.25+; use autoscaling/v2beta2 HorizontalPodAutoscaler" Note: code is determined by the Kubernetes server

type MockS3Client

type MockS3Client struct {
	mock.Mock
	FilePath string
}

func (*MockS3Client) Download

func (msc *MockS3Client) Download(file *os.File, bucket, itemKey, region, accessKeyID, secretAccessKey, sessionToken string) error

func (*MockS3Client) DownloadWithinEC2Instance

func (msc *MockS3Client) DownloadWithinEC2Instance(file *os.File, bucket, itemKey string) error

type ReusedMessage

type ReusedMessage string
const (
	UnexpectedTerminationChildProcess ReusedMessage = "Unexpected termination of child process"
	WorkDirectoryDoesNotExist         ReusedMessage = "Work directory does not exist"
	WorkDirectoryExpectedBeString     ReusedMessage = "Work directory is expected to be string"
	FailedReadFromEventConnection     ReusedMessage = "Failed to read from event connection"
	FailedReadFromControlConnection   ReusedMessage = "Failed to read from control connection"
	FailedReadControlMessage          ReusedMessage = "Failed to read control message"
)

type S3Client

type S3Client interface {
	Download(file *os.File, bucket, itemKey, region, accessKeyID, secretAccessKey, sessionToken string) error
	DownloadWithinEC2Instance(file *os.File, bucket, itemKey string) error
}

type Scrubber

type Scrubber interface {
	// Scrub scrubs sensitive data from an object
	Scrub(objectToScrub interface{}, existingSecretMap map[string]string, sensitiveFields []*regexp.Regexp) (interface{}, map[string]string, error)

	// Restore restores sensitive data in an object from a secrets map
	Restore(scrubbedObject interface{}, secretsMap map[string]string) (interface{}, error)

	// ValidateReference validates references in a scrubbed object
	ValidateReference(objectToScrub interface{},
		existingSecretMap map[string]string,
		fieldPath,
		secretKey,
		stringValue string) error

	// ConvertMapToConfig converts map back to an object entity
	ConvertMapToConfig(mapConfig interface{}) (interface{}, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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