env

package
v1.10.1-beta.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2018 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package env provides functions to incorporate environment variables into kubectl commands.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Env

func Env(key string, defaultValue string) string

Env returns an environment variable if not nil, or a default value.

Example (DefaultValue)
fmt.Println(Env("TESTENVVAR", "default"))
Output:

default
Example (VariableExists)
os.Setenv("TESTENVVAR", "test value")
defer os.Unsetenv("TESTENVVAR")
fmt.Println(Env("TESTENVVAR", "default"))
Output:

test value

func GetEnv

func GetEnv(key string) (string, bool)

GetEnv returns an environment value if not nil, and an ok boolean.

Example (VariableDoesNotExist)
fmt.Println(GetEnv("THISVARDOESNOTEXIST"))
Output:

 false
Example (VariableExists)
os.Setenv("THISVAREXISTS", "value")
defer os.Unsetenv("THISVAREXISTS")
fmt.Println(GetEnv("THISVAREXISTS"))
Output:

value true

func GetEnvVarRefString

func GetEnvVarRefString(from *v1.EnvVarSource) string

GetEnvVarRefString returns a text description of whichever field is set within the supplied EnvVarSource argument.

func GetEnvVarRefValue

func GetEnvVarRefValue(kc kubernetes.Interface, ns string, store *ResourceStore, from *v1.EnvVarSource, obj runtime.Object, c *v1.Container) (string, error)

GetEnvVarRefValue returns the value referenced by the supplied EnvVarSource given the other supplied information.

func IsEnvironmentArgument

func IsEnvironmentArgument(s string) bool

IsEnvironmentArgument checks whether a string is an environment argument, that is, whether it matches the "anycharacters=anycharacters" pattern.

Example (False)
test := "returnsfalse"
fmt.Println(IsEnvironmentArgument(test))
Output:

false
Example (True)
test := "returns=true"
fmt.Println(IsEnvironmentArgument(test))
Output:

true

func IsValidEnvironmentArgument

func IsValidEnvironmentArgument(s string) bool

IsValidEnvironmentArgument checks whether a string is a valid environment argument, that is, whether it matches the "wordcharacters=anycharacters" pattern. Word characters can be letters, numbers, and underscores.

Example (False)
test := "not$word^characters=test"
fmt.Println(IsValidEnvironmentArgument(test))
Output:

false
Example (True)
test := "wordcharacters=true"
fmt.Println(IsValidEnvironmentArgument(test))
Output:

true

func ParseEnv

func ParseEnv(spec []string, defaultReader io.Reader) ([]v1.EnvVar, []string, error)

ParseEnv parses the elements of the first argument looking for environment variables in key=value form and, if one of those values is "-", it also scans the reader. The same environment variable cannot be both modified and removed in the same command.

Example (Bad)
var r io.Reader
bad := []string{"This not in the key=value format."}
fmt.Println(ParseEnv(bad, r))
Output:

[] [] environment variables must be of the form key=value and can only contain letters, numbers, and underscores
Example (Good)
r := strings.NewReader("FROM=READER")
ss := []string{"ENV=VARIABLE", "AND=ANOTHER", "REMOVE-", "-"}
fmt.Println(ParseEnv(ss, r))
Output:

[{ENV VARIABLE nil} {AND ANOTHER nil} {FROM READER nil}] [REMOVE] <nil>

func SplitEnvironmentFromResources

func SplitEnvironmentFromResources(args []string) (resources, envArgs []string, ok bool)

SplitEnvironmentFromResources separates resources from environment arguments. Resources must come first. Arguments may have the "DASH-" syntax.

Example
args := []string{`resource`, "ENV\\=ARG", `ONE\=MORE`, `DASH-`}
fmt.Println(SplitEnvironmentFromResources(args))
Output:

[resource] [ENV\=ARG ONE\=MORE DASH-] true

Types

type ResourceStore

type ResourceStore struct {
	SecretStore    map[string]*v1.Secret
	ConfigMapStore map[string]*v1.ConfigMap
}

ResourceStore defines a new resource store data structure.

func NewResourceStore

func NewResourceStore() *ResourceStore

NewResourceStore returns a pointer to a new resource store data structure.

Jump to

Keyboard shortcuts

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