util

package
v0.0.0-...-e4d61ee Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2019 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// PodNameEnvVar is the env variable for getting the pod name via downward api
	PodNameEnvVar = "POD_NAME"
	// PodNamespaceEnvVar is the env variable for getting the pod namespace via downward api
	PodNamespaceEnvVar = "POD_NAMESPACE"
)

Variables

View Source
var DefaultUpdateRetry = wait.Backoff{
	Steps:    10,
	Duration: 500 * time.Millisecond,
	Factor:   1.0,
	Jitter:   0.1,
}

DefaultUpdateRetry is the recommended retry for a failure to update a resource or ensuring that the changes from an update have been committed and have become consistent.

Functions

func Apply

func Apply(ctx context.Context, kube client.Client, o runtime.Object) error

Apply a helper function to save Kubernetes API object using controller-runtime client. This function will attempt to create a new object, or update existing object.

func ApplySecret

func ApplySecret(c kubernetes.Interface, s *corev1.Secret) (*corev1.Secret, error)

ApplySecret creates or updates if exist kubernetes secret

func ConditionalStringFormat

func ConditionalStringFormat(format string, value string) string

ConditionalStringFormat returns based on the format string and substitution value. If format is not provided, substitution value is returned If format is provided with '%s' substitution symbol, fmt.Sprintf(fmt, val) is returned.

NOTE: only single %s substitution is supported

If name format does not contain '%s' substitution, i.e. a constant string, the constant string value is returned back

Examples:

For all examples assume "value" = "test-value"
1. format = "", ContainerName = "test-value"
2. format = "foo", ContainerName = "foo"
3. format = "foo-%s", ContainerName = "foo-test-value"
4. format = "foo-%s-bar-%s", ContainerName = "foo-test-value-bar-%!s(MISSING)"

func CreateOrUpdate

func CreateOrUpdate(ctx context.Context, c client.Client, obj runtime.Object, f MutateFn) error

CreateOrUpdate creates or updates the given object in the Kubernetes cluster. The object's desired state must be reconciled with the existing state inside the passed in callback MutateFn. The MutateFn is called regardless of creating or updating an object.

func GenerateHex

func GenerateHex(dataLen int) (string, error)

GenerateHex generates a hex string using random data of the given length.

func GenerateName

func GenerateName(base string) string

GenerateName returns the given base name plus a random suffix of five alphanumerics and a hyphen separator. The string is guaranteed to not exceed the length of a standard Kubernetes name (253 characters). e.g., GenerateName("foo") would return a value similar to: "foo-a1b2c".

If base string length exceeds 247 (253 - 6) characters, it will be truncated to 247 characters before adding random suffix e.g., GenerateName("foo...ververylongstringof253chars") would return value similar to:

"foo...ververylongstringof25-x8y9z"

func GenerateNameMaxLength

func GenerateNameMaxLength(base string, maxLength int) string

GenerateNameMaxLength returns the given base name with a dash and random 5 character suffix. basename will be trimmed as necessary to meet the limits of maxLength.

func GeneratePassword

func GeneratePassword(dataLen int) (string, error)

GeneratePassword generates a password using random data of the given length, then encodes to a base64 string.

func GetContainerImage

func GetContainerImage(pod *v1.Pod, name string) (string, error)

GetContainerImage will get the container image for the container with the given name in the given pod.

func GetMatchingContainer

func GetMatchingContainer(containers []v1.Container, name string) (v1.Container, error)

GetMatchingContainer returns the container from the given set of containers that matches the given name. If the given container list has only 1 item then the name field is ignored and that container is returned.

func GetRunningPod

func GetRunningPod(ctx context.Context, kube client.Client) (*v1.Pod, error)

GetRunningPod will get the pod object for the currently running pod. This assumes that the downward API has been used to inject the pod name and namespace as env vars.

func GetSpecContainerImage

func GetSpecContainerImage(spec v1.PodSpec, name string, initContainer bool) (string, error)

GetSpecContainerImage will get the container image for the container with the given name in the given pod spec.

func IfEmptyString

func IfEmptyString(s, r string) string

IfEmptyString test input string and if empty, i.e = "", return a replacement string

func LatestDeploymentCondition

func LatestDeploymentCondition(conditions []appsv1.DeploymentCondition) appsv1.DeploymentCondition

LatestDeploymentCondition returns the supplied condition with the latest last update time.

func ParseBool

func ParseBool(s string) bool

ParseBool returns true IFF string value is "true" or "True"

func ParseMap

func ParseMap(s string) map[string]string

ParseMap string encoded map values example: "foo:bar,one:two" -> map[string]string{"foo":"bar","one":"two"}

func SecretData

func SecretData(client kubernetes.Interface, namespace string, ks corev1.SecretKeySelector) ([]byte, error)

SecretData returns secret data value for a given secret/key combination or error if secret or key is not found

func Split

func Split(s, sep string) []string

Split function helper will return an empty slice on empty string and removing empty entries and trimming leading and trailing spaces Example: Split("a ,, b") results in []string{"a","b"}

func String

func String(v string) *string

String returns a pointer to the string value passed in.

func StringValue

func StringValue(v *string) string

StringValue returns the value of the string pointer passed in or "" if the pointer is nil.

func ToLowerRemoveSpaces

func ToLowerRemoveSpaces(input string) string

ToLowerRemoveSpaces returns the supplied string in lowercase with all spaces (not all whitespace) removed.

Types

type MutateFn

type MutateFn func() error

MutateFn is a function which mutates the existing object into its desired state.

Jump to

Keyboard shortcuts

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