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 string contained in the optional args removed (default space).

'{{repl ConfigOption "str_value" | Trim " " "." }}'

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

You can use the hidden and readonly properties in the Config custom resource 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 the RandomString template function 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.

The following table describes the behavior of the RandomString template function when the hidden and readonly properties are true or false, including whether the random string is ephemeral or persistent between configuration changes and the relevant use cases:

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 the Replicated app manager 1.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 nil
  • Optional list of alternate DNS names; may be nil
  • 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 the app manager 1.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 the app manager 1.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 the app manager 1.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 the app manager 1.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
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 Replicated 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 KOTS tag/version the admin console is using. Note: The app manager is based on the KOTS open source project. The current KOTS version is the same as the current app manager version. The app manager installs the admin console.

repl{{ KotsVersion }}

KotsVersion can be compared to Semantic Versions (SemVers) like follows:

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

The above template function will return true if KotsVersion is greater than 1.19. Note: The app manager is based on the KOTS open source project. The current KOTS version is the same as the current app manager version.

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 }}