Skip to main content

Static Context

Mastermind Sprig

Many of the utility functions provided come from sprig, a third-party library of Go template functions. For more information, see Sprig Function Documentation on the sprig website.

Namespace

func Namespace() string

Namespace returns the Kubernetes namespace that the application belongs to.

'{{repl Namespace}}'

KubeSeal

func KubeSeal(certData string, namespace string, name string, value string) string

HumanSize

func HumanSize(size interface{}) string

HumanSize returns a human-readable approximation of a size in bytes capped at 4 valid numbers (eg. "2.746 MB", "796 KB"). The size must be a integer or floating point number.

'{{repl ConfigOption "min_size_bytes" | HumanSize }}'

Now

func Now() string

Returns the current timestamp as an RFC3339 formatted string.

'{{repl Now }}'

NowFmt

func NowFmt(format string) string

Returns the current timestamp as a formatted string. For information about Go time formatting guidelines, see Constants in the Go documentation.

'{{repl NowFmt "20060102" }}'

ToLower

func ToLower(stringToAlter string) string

Returns the string, in lowercase.

'{{repl ConfigOption "company_name" | ToLower }}'

ToUpper

func ToUpper(stringToAlter string) string

Returns the string, in uppercase.

'{{repl ConfigOption "company_name" | ToUpper }}'

TrimSpace

func TrimSpace(s string) string

Trim returns a string with all leading and trailing spaces removed.

'{{repl ConfigOption "str_value" | TrimSpace }}'

Trim

func Trim(s string, args ...string) string

Trim returns a string with all leading and trailing strings contained in the optional args removed (default space).

'{{repl Trim (ConfigOption "str_value") "." }}'

UrlEncode

func UrlEncode(stringToEncode string) string

Returns the string, url encoded. Equivalent to the QueryEscape function within the golang net/url library. For more information, see func QueryEscape in the Go documentation.

'{{repl ConfigOption "smtp_email" | UrlEncode }}:{{repl ConfigOption "smtp_password" | UrlEncode }}@smtp.example.com:587'

UrlPathEscape

func UrlPathEscape(stringToEncode string) string

Returns the string, url path encoded. Equivalent to the PathEscape function within the golang net/url library. For more information, see func PathEscape in the Go documentation.

'{{repl ConfigOption "smtp_email" | UrlPathEscape }}:{{repl ConfigOption "smtp_password" | UrlPathEscape }}@smtp.example.com:587'

Base64Encode

func Base64Encode(stringToEncode string) string

Returns a Base64 encoded string.

'{{repl ConfigOption "name" | Base64Encode }}'

Base64Decode

func Base64Decode(stringToDecode string) string

Returns decoded string from a Base64 stored value.

'{{repl ConfigOption "base_64_encoded_name" | Base64Decode }}'

Split

func Split(s string, sep string) []string

Split slices s into all substrings separated by sep and returns an array of the substrings between those separators.

'{{repl Split "A,B,C" "," }}'

Combining Split and index: Assuming the github_url param is set to https://github.mycorp.internal:3131, the following would set GITHUB_HOSTNAME to github.mycorp.internal.

'{{repl index (Split (index (Split (ConfigOption "github_url") "/") 2) ":") 0}}'

RandomString

func RandomString(length uint64, providedCharset ...string) string

Returns a random string with the desired length and charset. Provided charsets must be Perl formatted and match individual characters. If no charset is provided, [_A-Za-z0-9] will be used.

Examples

The following example generates a 64-character random string:

'{{repl RandomString 64}}'

The following example generates a 64-character random string that contains as and bs:

'{{repl RandomString 64 "[ab]" }}'

Generating Persistent and Ephemeral Strings

When you assign the RandomString template function to a value key in the Config custom resource, you can use the hidden and readonly properties to control the behavior of the RandomString function each time it is called. The RandomString template function is called each time the user deploys a change to the configuration settings for the application.

Depending on if the hidden and readonly properties are true or false, the random string generated by a RandomString template function in a value key is either ephemeral or persistent between configuration changes:

  • Ephemeral: The value of the random string changes when the user deploys a change to the configuration settings for the application.
  • Persistent: The value of the random string does not change when the user deploys a change to the configuration settings for the application.

For more information about these properties, see hidden and readonly in Config.

note

If you assign the RandomString template function to a default key in the Config custom resource rather than a value key, then the hidden and readonly properties do not affect the behavior of the RandomString template function. For more information about the behavior of the default key in the Config custom resource, see default in Config.

The following table describes the behavior of the RandomString template function when it is assigned to a value key in the Config custom resource and the hidden and readonly properties are true or false:

readonlyhiddenOutcomeUse Case
falsetruePersistent

Set readonly to false and hidden to true if:

  • The random string must not change each time the user deploys a change to the application's configuration settings.
  • The user does not need to see or change, or must be prevented from seeing or changing, the value of the random string.
truefalseEphemeral

Set readonly to true and hidden to false if:

  • The random string must change each time the user deploys a change to the application's configuration settings.
  • The user does not need to change, or must be prevented from changing, the value of the random string.
  • The user must be able to see the value of the random string.
truetrueEphemeral

Set readonly to true and hidden to true if:

  • The random string must change each time the user deploys a change to the application's configuration settings.
  • The user does not need to see or change, or must be preventing from seeing or changing, the value of the random string.
falsefalsePersistent

Set readonly to false and hidden to false if:

  • The random string must not change each time the user deploys a change to the application's configuration settings.
  • The user must be able to see and change the value of the random string.

For example, set both readonly and hidden to false to generate a random password that users must be able to see and then change to a different value that they choose.

Add

func Add(x interface{}, y interface{}) interface{}

Adds x and y.

If at least one of the operands is a floating point number, the result will be a floating point number.

If both operands are integers, the result will be an integer.

'{{repl Add (ConfigOption "maximum_users") 1}}'

Sub

func Sub(x interface{}, y interface{}) interface{}

Subtracts y from x.

If at least one of the operands is a floating point number, the result will be a floating point number.

If both operands are integers, the result will be an integer.

'{{repl Sub (ConfigOption "maximum_users") 1}}'

Mult

func Mult(x interface{}, y interface{}) interface{}

Multiplies x and y.

Both operands must be either an integer or a floating point number.

If at least one of the operands is a floating point number, the result will be a floating point number.

If both operands are integers, the result will be an integer.

'{{repl Mult (NodePrivateIPAddressAll "DB" "redis" | len) 2}}'

If a template function returns a string, the value must be converted to an integer or a floating point number first:

'{{repl Mult (ConfigOption "session_cookie_age" | ParseInt) 86400}}'

Div

func Div(x interface{}, y interface{}) interface{}

Divides x by y.

If at least one of the operands is a floating point number, the result will be a floating point number.

If both operands are integers, the result will be an integer and will be rounded down.

'{{repl Div (ConfigOption "maximum_users") 2.0}}'

ParseBool

func ParseBool(str string) bool

ParseBool returns the boolean value represented by the string.

'{{repl ConfigOption "str_value" | ParseBool }}'

ParseFloat

func ParseFloat(str string) float64

ParseFloat returns the float value represented by the string.

'{{repl ConfigOption "str_value" | ParseFloat }}'

ParseInt

func ParseInt(str string, args ...int) int64

ParseInt returns the integer value represented by the string with optional base (default 10).

'{{repl ConfigOption "str_value" | ParseInt }}'

ParseUint

func ParseUint(str string, args ...int) uint64

ParseUint returns the unsigned integer value represented by the string with optional base (default 10).

'{{repl ConfigOption "str_value" | ParseUint }}'

TLSCert

Deprecation Notice: This function has been superseded in Replicated KOTS v1.26.0 by the sprig crypto functions. For more information, see Example: Using Variables to Generate TLS Certificates and Keys in Config Context. For more information about the sprig crypto function, see Cryptographic and Security Functions in the sprig documentation.

func TLSCert(certName string, cn string, ips []interface{}, alternateDNS []interface{}, daysValid int) string

TLSCert generates and returns a self-signed certificate identified by certName.
The first parameter can be used in the TLSKey function to retrieve the matching key.

TLSCert takes the following parameters

  • Unique name that identifies the certificate.
    This is a not a part of the returned certificate.
  • Subject’s common name (cn)
  • Optional list of IPs; may be ()
  • Optional list of alternate DNS names; may be ()
  • Cert validity duration in days
repl{{ TLSCert "my_custom_cert" "foo.com" (list "10.0.0.1" "10.0.0.2") (list "bar.com" "bat.com") 365 }}

TLSKey

Deprecation Notice: This function has been superseded in KOTS v1.26.0 by the sprig crypto functions. For more information, see Example: Using Variables to Generate TLS Certificates and Keys in Config Context. For more information about the sprig crypto function, see Cryptographic and Security Functions in the sprig documentation.

func TLSKey(certName string, cn string, ips []interface{}, alternateDNS []interface{}, daysValid int) string

TLSKey returns the key that matches the certificate identified by certName.
The rest of the arguments are the same as in TLSCert and, if specified, must have the same values.
If they are omitted and the certificate with this name does not exist, the function will return an empty string.

repl{{ TLSKey "my_custom_cert" "foo.com" (list "10.0.0.1" "10.0.0.2") (list "bar.com" "bat.com") 365 }}

TLSCACert

Deprecation Notice: This function has been superseded in KOTS v1.26.0 by the sprig crypto functions. For more information, see Example: Using Variables to Generate TLS Certificates and Keys in Config Context. For more information about the sprig crypto function, see Cryptographic and Security Functions in the sprig documentation.

func TLSCACert(caName string, daysValid int) string

TLSCACert generates and returns a CA certificate that can be used as a CA to sign other certificates.

TLSCACert takes the following parameters

  • Subject’s common name (cn)
  • Cert validity duration in days
repl{{ TLSCACert "foo.com" 365 }}

TLSCertFromCA

Deprecation Notice: This function has been superseded in KOTS v1.26.0 by the sprig crypto functions. For more information, see Example: Using Variables to Generate TLS Certificates and Keys in Config Context. For more information about the sprig crypto function, see Cryptographic and Security Functions in the sprig documentation.

func TLSCertFromCA(caName string, certName string, cn string, ips []interface{}, alternateDNS []interface{}, daysValid int) string

TLSCertFromCA generates and returns a certificate signed by the CA identified by caName.
The rest of the arguments are the same as in TLSCert.

repl{{ TLSCertFromCA "foo.com" "my_custom_cert" "bar.com" (list "10.0.0.1" "10.0.0.2") (list "bar.com" "bat.com") 365 }}

TLSKeyFromCA

Deprecation Notice: This function has been superseded in KOTS v1.26.0 by the sprig crypto functions. For more information, see Example: Using Variables to Generate TLS Certificates and Keys in Config Context. For more information about the sprig crypto function, see Cryptographic and Security Functions in the sprig documentation.

func TLSKeyFromCA(caName string, certName string, cn string, ips []interface{}, alternateDNS []interface{}, daysValid int) string

TLSKeyFromCA generates and returns a key that matches the certificate returned by TLSCertFromCA.
The arguments are the same as in TLSCertFromCA and their values must match.

repl{{ TLSKeyFromCA "foo.com" "my_custom_cert" "bar.com" (list "10.0.0.1" "10.0.0.2") (list "bar.com" "bat.com") 365 }}

IsKurl

func IsKurl() bool

IsKurl returns true if running within a kurl-based installation.

repl{{ IsKurl }}

Distribution

func Distribution() string

Distribution returns the Kubernetes distribution detected. The possible return values are:

  • microk8s
  • dockerDesktop
  • eks
  • gke
  • digitalOcean
  • openShift
  • kurl
  • aks
  • ibm
  • minikube
  • rke2
  • k3s
  • oke
repl{{ Distribution }}

NodeCount

func NodeCount() int

NodeCount returns the number of nodes detected within the Kubernetes cluster.

repl{{ NodeCount }}

HTTPSProxy

func HTTPSProxy() string

HTTPSProxy returns the address of the proxy that the Replicated admin console is configured to use.

repl{{ HTTPSProxy }}

HTTPProxy

func HTTPProxy() string

HTTPProxy returns the address of the proxy that the admin console is configured to use.

repl{{ HTTPProxy }}

NoProxy

func NoProxy() string

NoProxy returns the comma-separated list of no-proxy addresses that the admin console is configured to use.

repl{{ NoProxy }}

KotsVersion

func KotsVersion() string

KotsVersion returns the current version of KOTS.

repl{{ KotsVersion }}

You can compare the KOTS version as follows:

repl{{KotsVersion | semverCompare ">= 1.19"}}

This returns true if the KOTS version is greater than or equal to 1.19.

For more complex comparisons, see Semantic Version Functions in the sprig documentation.

YamlEscape

func YamlEscape(input string) string

YamlEscape returns an escaped and quoted version of the input string, suitable for use within a YAML document. This can be useful when dealing with user-uploaded files that may include null bytes and other nonprintable characters. For more information about printable characters, see Character Set in the YAML documentation.

repl{{ ConfigOptionData "my_file_upload" | YamlEscape }}

KubernetesVersion

Introduced in KOTS v1.92.0

func KubernetesVersion() string

KubernetesVersion returns the Kubernetes server version.

repl{{ KubernetesVersion }}

You can compare the Kubernetes version as follows:

repl{{KubernetesVersion | semverCompare ">= 1.19"}}

This returns true if the Kubernetes version is greater than or equal to 1.19.

For more complex comparisons, see Semantic Version Functions in the sprig documentation.

KubernetesMajorVersion

Introduced in KOTS v1.92.0

func KubernetesMajorVersion() string

KubernetesMajorVersion returns the Kubernetes server major version.

repl{{ KubernetesMajorVersion }}

You can compare the Kubernetes major version as follows:

repl{{lt (KubernetesMajorVersion | ParseInt) 2 }}

This returns true if the Kubernetes major version is less than 2.

KubernetesMinorVersion

Introduced in KOTS v1.92.0

func KubernetesMinorVersion() string

KubernetesMinorVersion returns the Kubernetes server minor version.

repl{{ KubernetesMinorVersion }}

You can compare the Kubernetes minor version as follows:

repl{{gt (KubernetesMinorVersion | ParseInt) 19 }}

This returns true if the Kubernetes minor version is greater than 19.

Lookup

Introduced in KOTS v1.103.0

func Lookup(apiversion string, resource string, namespace string, name string) map[string]interface{}

Lookup searches resources in a running cluster and returns a resource or resource list.

Lookup uses the Helm lookup function to search resources and has the same functionality as the Helm lookup function. For more information, see lookup in the Helm documentation.

repl{{ Lookup "API_VERSION" "KIND" "NAMESPACE" "NAME" }}

Both NAME and NAMESPACE are optional and can be passed as an empty string ("").

The following combination of parameters are possible:

BehaviorLookup function
kubectl get pod mypod -n mynamespacerepl{{ Lookup "v1" "Pod" "mynamespace" "mypod" }}
kubectl get pods -n mynamespacerepl{{ Lookup "v1" "Pod" "mynamespace" "" }}
kubectl get pods --all-namespacesrepl{{ Lookup "v1" "Pod" "" "" }}
kubectl get namespace mynamespacerepl{{ Lookup "v1" "Namespace" "" "mynamespace" }}
kubectl get namespacesrepl{{ Lookup "v1" "Namespace" "" "" }}

The following describes working with values returned by the Lookup function:

  • When Lookup finds an object, it returns a dictionary with the key value pairs from the object. This dictionary can be navigated to extract specific values. For example, the following returns the annotations for the mynamespace object:

    repl{{ (Lookup "v1" "Namespace" "" "mynamespace").metadata.annotations }}
  • When Lookup returns a list of objects, it is possible to access the object list through the items field. For example:

    services: |
    repl{{- range $index, $service := (Lookup "v1" "Service" "mynamespace" "").items }}
    - repl{{ $service.metadata.name }}
    repl{{- end }}

    For an array value type, omit the |. For example:

    services:
    repl{{- range $index, $service := (Lookup "v1" "Service" "mynamespace" "").items }}
    - repl{{ $service.metadata.name }}
    repl{{- end }}
  • When no object is found, Lookup returns an empty value. This can be used to check for the existence of an object.