Static Context
About 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.
Certificate Functions
PrivateCACert
Introduced in KOTS v1.117.0
func PrivateCACert() string
PrivateCACert returns the name of a ConfigMap that contains private CA certificates provided by the end user. For Embedded Cluster installations, these certificates are provided with the --private-ca
flag for the install
command. For KOTS installations, the user provides the ConfigMap using the --private-ca-configmap
flag for the install
command.
You can use this template function to mount the specified ConfigMap so your containers can access the internet through enterprise proxies that issue their own TLS certificates in order to inspect traffic.
This function will return the name of the ConfigMap even if the ConfigMap has no entries. If no ConfigMap exists, this function returns the empty string.
Cluster Information Functions
Distribution
func Distribution() string
Distribution returns the Kubernetes distribution detected. The possible return values are:
- aks
- digitalOcean
- dockerDesktop
- eks
- embedded-cluster
- gke
- ibm
- k0s
- k3s
- kind
- kurl
- microk8s
- minikube
- oke
- openShift
- rke2
IsKurl can also be used to detect kURL instances.
Detect the Distribution
repl{{ Distribution }}
Equal To Comparison
repl{{ eq Distribution "gke" }}
Not Equal To Comparison
repl{{ ne Distribution "embedded-cluster" }}
See Functions in the Go documentation.
IsKurl
func IsKurl() bool
IsKurl returns true if running within a kurl-based installation.
Detect kURL Installations
repl{{ IsKurl }}
Detect Non-kURL Installations
repl{{ not IsKurl }}
See Functions in the Go documentation.
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.
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
.
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.
Namespace
func Namespace() string
Namespace returns the Kubernetes namespace that the application belongs to.
'{{repl Namespace}}'
NodeCount
func NodeCount() int
NodeCount returns the number of nodes detected within the Kubernetes cluster.
repl{{ NodeCount }}
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:
Behavior | Lookup function |
---|---|
kubectl get pod mypod -n mynamespace | repl{{ Lookup "v1" "Pod" "mynamespace" "mypod" }} |
kubectl get pods -n mynamespace | repl{{ Lookup "v1" "Pod" "mynamespace" "" }} |
kubectl get pods --all-namespaces | repl{{ Lookup "v1" "Pod" "" "" }} |
kubectl get namespace mynamespace | repl{{ Lookup "v1" "Namespace" "" "mynamespace" }} |
kubectl get namespaces | repl{{ 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.
Date Functions
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" }}'
Encoding Functions
Base64Decode
func Base64Decode(stringToDecode string) string
Returns decoded string from a Base64 stored value.
'{{repl ConfigOption "base_64_encoded_name" | Base64Decode }}'
Base64Encode
func Base64Encode(stringToEncode string) string
Returns a Base64 encoded string.
'{{repl ConfigOption "name" | Base64Encode }}'
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'
Encryption Functions
KubeSeal
func KubeSeal(certData string, namespace string, name string, value string) string
Integer and Float Functions
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 }}'