kubectlk8s.io/kubectl/pkg/cmd/set/env Index | Examples | Files

package env

import "k8s.io/kubectl/pkg/cmd/set/env"

Package env provides functions to incorporate environment variables into set env.

Index

Examples

Functions

func GetEnvVarRefString

func GetEnvVarRefString(from *corev1.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 *corev1.EnvVarSource, obj runtime.Object, c *corev1.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)

Code:

{
	test := "returnsfalse"
	fmt.Println(IsEnvironmentArgument(test))
	// Output: false
}

Output:

false
Example (True)

Code:

{
	test := "returns=true"
	fmt.Println(IsEnvironmentArgument(test))
	// Output: true
}

Output:

true

func IsHugePageResourceName

func IsHugePageResourceName(name corev1.ResourceName) bool

IsHugePageResourceName returns true if the resource name has the huge page resource prefix.

func ParseEnv

func ParseEnv(spec []string, defaultReader io.Reader) ([]v1.EnvVar, []string, bool, 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 and returns true for its third return value. The same environment variable cannot be both modified and removed in the same command.

Example (Bad_first)

Code:

{
	var r io.Reader
	bad := []string{"This not in the key=value format."}
	fmt.Println(ParseEnv(bad, r))
	// Output:
	// [] [] false "This not in the key" is not a valid key name: a valid environment variable name must consist of alphabetic characters, digits, '_', '-', or '.', and must not start with a digit (e.g. 'my.env-name',  or 'MY_ENV.NAME',  or 'MyEnvName1', regex used for validation is '[-._a-zA-Z][-._a-zA-Z0-9]*')
}

Output:

[] [] false "This not in the key" is not a valid key name: a valid environment variable name must consist of alphabetic characters, digits, '_', '-', or '.', and must not start with a digit (e.g. 'my.env-name',  or 'MY_ENV.NAME',  or 'MyEnvName1', regex used for validation is '[-._a-zA-Z][-._a-zA-Z0-9]*')
Example (Bad_fourth)

Code:

{
	var r io.Reader
	bad := []string{"..ENV=VARIABLE"}
	fmt.Println(ParseEnv(bad, r))
	// Output:
	// [] [] false "..ENV" is not a valid key name: must not start with '..'
}

Output:

[] [] false "..ENV" is not a valid key name: must not start with '..'
Example (Bad_second)

Code:

{
	var r io.Reader
	bad := []string{".=VARIABLE"}
	fmt.Println(ParseEnv(bad, r))
	// Output:
	// [] [] false "." is not a valid key name: must not be '.'
}

Output:

[] [] false "." is not a valid key name: must not be '.'
Example (Bad_third)

Code:

{
	var r io.Reader
	bad := []string{"..=VARIABLE"}
	fmt.Println(ParseEnv(bad, r))
	// Output:
	// [] [] false ".." is not a valid key name: must not be '..'
}

Output:

[] [] false ".." is not a valid key name: must not be '..'
Example (Good_with_stdin)

Code:

{
	r := strings.NewReader("FROM=READER")
	ss := []string{"ENV=VARIABLE", "ENV.TEST=VARIABLE", "AND=ANOTHER", "REMOVE-", "-"}
	fmt.Println(ParseEnv(ss, r))
	// Output:
	// [{ENV VARIABLE nil} {ENV.TEST VARIABLE nil} {AND ANOTHER nil} {FROM READER nil}] [REMOVE] true <nil>
}

Output:

[{ENV VARIABLE nil} {ENV.TEST VARIABLE nil} {AND ANOTHER nil} {FROM READER nil}] [REMOVE] true <nil>
Example (Good_with_stdin_and_error)

Code:

{
	r := strings.NewReader("FROM=READER")
	ss := []string{"-", "This not in the key=value format."}
	fmt.Println(ParseEnv(ss, r))
	// Output:
	// [] [] true "This not in the key" is not a valid key name: a valid environment variable name must consist of alphabetic characters, digits, '_', '-', or '.', and must not start with a digit (e.g. 'my.env-name',  or 'MY_ENV.NAME',  or 'MyEnvName1', regex used for validation is '[-._a-zA-Z][-._a-zA-Z0-9]*')
}

Output:

[] [] true "This not in the key" is not a valid key name: a valid environment variable name must consist of alphabetic characters, digits, '_', '-', or '.', and must not start with a digit (e.g. 'my.env-name',  or 'MY_ENV.NAME',  or 'MyEnvName1', regex used for validation is '[-._a-zA-Z][-._a-zA-Z0-9]*')
Example (Good_without_stdin)

Code:

{
	ss := []string{"ENV=VARIABLE", "ENV.TEST=VARIABLE", "AND=ANOTHER", "REMOVE-"}
	fmt.Println(ParseEnv(ss, nil))
	// Output:
	// [{ENV VARIABLE nil} {ENV.TEST VARIABLE nil} {AND ANOTHER nil}] [REMOVE] false <nil>
}

Output:

[{ENV VARIABLE nil} {ENV.TEST VARIABLE nil} {AND ANOTHER nil}] [REMOVE] false <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

Code:

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

Output:

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

Types

type ResourceStore

type ResourceStore struct {
	SecretStore    map[string]*corev1.Secret
	ConfigMapStore map[string]*corev1.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.

Source Files

doc.go env_parse.go env_resolve.go

Version
v0.32.3 (latest)
Published
Mar 12, 2025
Platform
linux/amd64
Imports
16 packages
Last checked
5 days ago

Tools for package owners.