|
| 1 | +// Module included in the following assemblies: |
| 2 | + |
| 3 | +// *scalability_and_performance/cluster-compare/creating-a-reference-configuration.adoc |
| 4 | + |
| 5 | +:_mod-docs-content-type: REFERENCE |
| 6 | + |
| 7 | +[id="cluster-compare-templating-reference_{context}"] |
| 8 | += Reference template functions |
| 9 | + |
| 10 | +The `cluster-compare` plugin supports all `sprig` library functions, except for the `env` and `expandenv` functions. For the full list of `sprig` library functions, see "Sprig Function Documentation". |
| 11 | + |
| 12 | +The following table describes the additional template functions for the `cluster-compare` plugin: |
| 13 | + |
| 14 | +.Additional cluster-compare template functions |
| 15 | +[cols=".^2,.^4,.^6a",options="header"] |
| 16 | +|==== |
| 17 | + |
| 18 | +|Function |Description |Example |
| 19 | + |
| 20 | +|`fromJson` |
| 21 | +|Parses the incoming string as a structured JSON object. |
| 22 | +|`value: {{ obj := spec.jsontext \| fromJson }}{{ obj.field }}` |
| 23 | + |
| 24 | +|`fromJsonArray` |
| 25 | +|Parses the incoming string as a structured JSON array. |
| 26 | +| `value: {{ obj := spec.jsontext \| fromJson}}{{ index $obj 0 }}` |
| 27 | + |
| 28 | +|`fromYaml` |
| 29 | +|Parses the incoming string as a structured YAML object. |
| 30 | +|`value: {{ obj := spec.yamltext \| fromYaml }}{{ obj.field }}` |
| 31 | + |
| 32 | +|`fromYamlArray` |
| 33 | +|Parses the incoming string as a structured YAML array. |
| 34 | +|`value: {{ obj := spec.yamltext \| fromYaml}}{{ index $obj 0 }` |
| 35 | + |
| 36 | +|`toJson` |
| 37 | +|Renders incoming data as JSON while preserving object types. |
| 38 | +|`jsonstring: {{ $variable \| toJson }}` |
| 39 | + |
| 40 | +|`toToml` |
| 41 | +|Renders the incoming string as structured TOML data. |
| 42 | +|`tomlstring: {{ $variable \| toToml }}` |
| 43 | + |
| 44 | +|`toYaml` |
| 45 | +|Renders incoming data as YAML while preserving object types. |
| 46 | +|For simple scalar values: `value: {{ $data \| toYaml }}` |
| 47 | + |
| 48 | +For lists or dictionaries: `value: {{ $dict \| toYaml \| nindent 2 }}` |
| 49 | + |
| 50 | +|`doNotMatch` |
| 51 | +|Prevents a template from matching a cluster resource, even if it would normally match. You can use this function inside a template to conditionally exclude certain resources from correlation. The specified reason is logged when running with the `--verbose` flag. Templates excluded due to `doNotMatch` are not considered comparison failures. |
| 52 | + |
| 53 | +This function is especially useful when your template does not specify a fixed name or namespace. In these cases, you can use the `doNotMatch` function to exclude specific resources based on other fields, such as `labels` or `annotations`. |
| 54 | +|`{{ if $condition }}{{ doNotMatch $reason }}{{ end }}` |
| 55 | + |
| 56 | +|`lookupCRs` |
| 57 | +|Returns an array of objects that match the specified parameters. For example: `lookupCRs $apiVersion $kind $namespace $name`. |
| 58 | + |
| 59 | +If the `$namespace` parameter is an empty string (`""`) or `\*`, the function matches all namespaces. For cluster-scoped objects, the function matches objects with no namespace. |
| 60 | + |
| 61 | +If the `$name` is an empty string or `*`, the function matches any named object. |
| 62 | +|- |
| 63 | + |
| 64 | +|`lookupCR` |
| 65 | +|Returns a single object that matches the parameters. If multiple objects match, the function returns nothing. This function takes the same arguments as the `lookupCRs` function. |
| 66 | +|- |
| 67 | + |
| 68 | +|==== |
| 69 | + |
| 70 | +The following example shows how to use the `lookupCRs` function to retrieve and render values from multiple matching resources: |
| 71 | + |
| 72 | +.Config map example using `lookupCRs` |
| 73 | +[source,yaml] |
| 74 | +---- |
| 75 | +kind: ConfigMap |
| 76 | +apiVersion: v1 |
| 77 | +metadata: |
| 78 | + labels: |
| 79 | + k8s-app: kubernetes-dashboard |
| 80 | + name: kubernetes-dashboard-settings |
| 81 | + namespace: kubernetes-dashboard |
| 82 | +data: |
| 83 | + dashboard: {{ index (lookupCR "apps/v1" "Deployment" "kubernetes-dashboard" "kubernetes-dashboard") "metadata" "name" \| toYaml }} |
| 84 | + metrics: {{ (lookupCR "apps/v1" "Deployment" "kubernetes-dashboard" "dashboard-metrics-scraper").metadata.name \| toYaml }} |
| 85 | +---- |
| 86 | + |
| 87 | +The following example shows how to use the `lookupCR` function to retrieve and use specific values from a single matching resource: |
| 88 | + |
| 89 | +.Config map example using `lookupCR` |
| 90 | +[source,yaml] |
| 91 | +---- |
| 92 | +kind: ConfigMap |
| 93 | +apiVersion: v1 |
| 94 | +metadata: |
| 95 | + labels: |
| 96 | + k8s-app: kubernetes-dashboard |
| 97 | + name: kubernetes-dashboard-settings |
| 98 | + namespace: kubernetes-dashboard |
| 99 | +data: |
| 100 | + {{- $objlist := lookupCRs "apps/v1" "Deployment" "kubernetes-dashboard" "*" }} |
| 101 | + {{- $dashboardName := "unknown" }} |
| 102 | + {{- $metricsName := "unknown" }} |
| 103 | + {{- range $obj := $objlist }} |
| 104 | + {{- $appname := index $obj "metadata" "labels" "k8s-app" }} |
| 105 | + {{- if contains "metrics" $appname }} |
| 106 | + {{- $metricsName = $obj.metadata.name }} |
| 107 | + {{- end }} |
| 108 | + {{- if eq "kubernetes-dashboard" $appname }} |
| 109 | + {{- $dashboardName = $obj.metadata.name }} |
| 110 | + {{- end }} |
| 111 | + {{- end }} |
| 112 | + dashboard: {{ $dashboardName }} |
| 113 | + metrics: {{ $metricsName }} |
| 114 | +---- |
| 115 | + |
0 commit comments