Documentation
¶
Index ¶
- func DeduplicateEnvVars(env []corev1.EnvVar) []corev1.EnvVar
- func EnvVarsToMap(envs []corev1.EnvVar) map[string]string
- func GetAppEnvVars(app *v1alpha1.App) []corev1.EnvVar
- func MapToEnvVars(envMap map[string]string) []corev1.EnvVar
- func NewJSONEnvVar(key string, value interface{}) (corev1.EnvVar, error)
- func ParseCLIEnvVars(cliEnv []string) ([]corev1.EnvVar, error)
- func RemoveEnvVars(varsToRemove []string, envs []corev1.EnvVar) []corev1.EnvVar
- func SetAppEnvVars(app *v1alpha1.App, env []corev1.EnvVar)
- func SortEnvVars(toSort []corev1.EnvVar)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeduplicateEnvVars ¶
DeduplicateEnvVars deduplicates environment variables and returns the canonical version of them (last environment variable takes preccidence).
Example ¶
package main import ( "fmt" "github.com/google/kf/pkg/internal/envutil" corev1 "k8s.io/api/core/v1" ) func main() { envs := []corev1.EnvVar{ {Name: "FOO", Value: "2"}, {Name: "BAR", Value: "0"}, {Name: "BAZZ", Value: "1"}, {Name: "BAZZ", Value: "1.5"}, } out := envutil.DeduplicateEnvVars(envs) for _, e := range out { fmt.Println("Key", e.Name, "Value", e.Value) } }
Output: Key BAR Value 0 Key BAZZ Value 1.5 Key FOO Value 2
func EnvVarsToMap ¶
EnvVarsToMap constructs a map of environment name to value from a slice of env vars. Vars with duplicate names will be resolved to the latest one in the list.
func GetAppEnvVars ¶
GetAppEnvVars reads the environment variables off a app. Prefer using this function directly rather than accessing nested objects on app so kf can adapt to future changes.
Example ¶
package main import ( "fmt" v1alpha1 "github.com/google/kf/pkg/apis/kf/v1alpha1" "github.com/google/kf/pkg/internal/envutil" corev1 "k8s.io/api/core/v1" ) func main() { var app v1alpha1.App envutil.SetAppEnvVars(&app, []corev1.EnvVar{ {Name: "FOO", Value: "2"}, {Name: "BAR", Value: "0"}, }) env := envutil.GetAppEnvVars(&app) for _, e := range env { fmt.Println("Key", e.Name, "Value", e.Value) } }
Output: Key FOO Value 2 Key BAR Value 0
Example (EmptyApp) ¶
package main import ( "fmt" "github.com/google/kf/pkg/internal/envutil" ) func main() { env := envutil.GetAppEnvVars(nil) fmt.Println(env) }
Output: []
func MapToEnvVars ¶
MapToEnvVars converts a map of name/value pairs into environment variables. The list will be sorted lexicographically based on name.
func NewJSONEnvVar ¶
NewJSONEnvVar converts a value to a JSON string and sets it on the environment variable.
Example ¶
package main import ( "fmt" "github.com/google/kf/pkg/internal/envutil" ) func main() { env, err := envutil.NewJSONEnvVar("INVENTORY", map[string]bool{ "Apples": true, "Bread": false, }) if err != nil { panic(err) } fmt.Println(env.Name, env.Value) }
Output: INVENTORY {"Apples":true,"Bread":false}
func ParseCLIEnvVars ¶
ParseCLIEnvVars turns a slice of strings formatted as NAME=VALUE into a map. The logic is taken from os/exec.dedupEnvCase with a few differences: malformed strings create an error, and case insensitivity is always assumed false.
func RemoveEnvVars ¶
RemoveEnvVars removes the environment variables with the given names from the list.
Example ¶
package main import ( "fmt" "github.com/google/kf/pkg/internal/envutil" corev1 "k8s.io/api/core/v1" ) func main() { envs := []corev1.EnvVar{ {Name: "FOO", Value: "2"}, {Name: "BAR", Value: "0"}, {Name: "BAZZ", Value: "1"}, {Name: "BAZZ", Value: "1.5"}, } out := envutil.RemoveEnvVars([]string{"MISSING", "BAZZ"}, envs) for _, e := range out { fmt.Println("Key", e.Name, "Value", e.Value) } }
Output: Key BAR Value 0 Key FOO Value 2
func SetAppEnvVars ¶
SetAppEnvVars sets environment variables on a app. Prefer using this function directly rather than accessing nested objects on app so kf can adapt to future changes.
func SortEnvVars ¶
SortEnvVars sorts the environment variable list in order by name.
Types ¶
This section is empty.