Skip to content

Commit aadf08f

Browse files
committed
TELCODOCS#2148: Usability improvements in cluster-compare plugin
1 parent 653dbfa commit aadf08f

7 files changed

+189
-4
lines changed

modules/cluster-compare-configure-inline-diff.adoc

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ The `cluster-compare` plugin provides two functions for inline validation:
1414
* `regex`: Validates content in a field using a regular expression.
1515
* `capturegroups`: Enhances multi-line text comparisons by processing non-capture group text as exact matches, applying regular expression matching only within named capture groups, and ensuring consistency for repeated capture groups.
1616

17+
When you use either the `regex` or `capturegroups` function for inline validation, the `cluster-compare` plugin enforces that identically named capture groups have the same values across multiple fields within a template. This means that if a named capture group, such as `(?<username>[a-z0-9]+)`, appears in multiple fields, the values for that group must be consistent throughout the template.
18+
1719
[id="cluster-compare-configure-regex_{context}"]
1820
== Configuring inline validation with the regex function
1921

@@ -49,6 +51,7 @@ kind: ConfigMap
4951
metadata:
5052
namespace: dashboard
5153
data:
54+
username: "(?<username>[a-z0-9]+)"
5255
bigTextBlock: |-
5356
This is a big text block with some static content, like this line.
5457
It also has a place where (?<username>[a-z0-9]+) would put in their own name. (?<username>[a-z0-9]+) would put in their own name.
@@ -57,7 +60,7 @@ data:
5760
[id="cluster-compare-configure-capturegroups_{context}"]
5861
== Configuring inline validation with the capturegroups function
5962

60-
Use the `capturegroups` inline function for more precise validation of fields featuring multi-line strings.
63+
Use the `capturegroups` inline function for more precise validation of fields featuring multi-line strings. This function also ensures that identically named capture groups have the same values across multiple fields.
6164

6265
.Procedure
6366

@@ -74,11 +77,15 @@ parts:
7477
- path: example.yaml
7578
config:
7679
perField:
77-
- pathToKey: spec.bigTextBlock <1>
78-
inlineDiffFunc: capturegroups <2>
80+
- pathToKey: data.username <1>
81+
inlineDiffFunc: regex <2>
82+
- pathToKey: spec.bigTextBlock <3>
83+
inlineDiffFunc: capturegroups <4>
7984
----
8085
<1> Specifies the field for inline validation.
8186
<2> Enables inline validation using capture groups.
87+
<3> Specifies the multi-line field for capture-group validation.
88+
<4> Enables inline validation using capture groups.
8289

8390
. Use a regular expression to validate the field in the associated template:
8491
+
@@ -89,8 +96,16 @@ kind: ConfigMap
8996
metadata:
9097
namespace: dashboard
9198
data:
99+
username: "(?<username>[a-z0-9]+)" <1>
92100
bigTextBlock: |-
93101
This static content outside of a capture group should match exactly.
94102
Here is a username capture group: (?<username>[a-z0-9]+).
95103
It should match this capture group: (?<username>[a-z0-9]+).
104+
----
105+
<1> If the username value in the `data.username` field and the value captured in `bigTextBlock` do not match, the `cluster-compare` plugin warns you about the inconsistent matching.
106+
+
107+
.Example output with warning about the inconsistent matching:
108+
[source,terminal]
109+
----
110+
WARNING: Capturegroup (?<username>…) matched multiple values: « mismatchuser | exampleuser »
96111
----

modules/cluster-compare-reference-args.adoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ The following content describes the options for the `cluster-compare` plugin.
2929
| `--generate-override-for`
3030
| Specify the path for templates that requires a patch.
3131

32+
| `--show-template-functions`
33+
| Displays the available template functions.
34+
3235
[NOTE]
3336
====
3437
You must use a file path for the target template that is relative to the `metadata.yaml` file. For example, if the file path for the `metadata.yaml` file is `./compare/metadata.yaml`, a relative file path for the template might be `optional/my-template.yaml`.
@@ -41,7 +44,7 @@ You must use a file path for the target template that is relative to the `metada
4144
| Specify a path to process the `kustomization` directory. This flag cannot be used together with `-f` or `-R`.
4245

4346
| `-o`, `--output`
44-
| Specify the output format. Options include `json`, `yaml`, or `generate-patches`.
47+
| Specify the output format. Options include `json`, `yaml`, `junit`, or `generate-patches`.
4548

4649
| `--override-reason`
4750
| Specify a reason for generating the override.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+

modules/using-cluster-compare-live-cluster.adoc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,18 @@ No patched CRs <10>
8484
<8> The CRs that did not match to a corresponding template in the reference configuration.
8585
<9> The metadata hash identifies the reference configuration.
8686
<10> The list of patched CRs.
87+
88+
[NOTE]
89+
====
90+
Get the output in the `junit` format by adding `-o junit` to the command. For example:
91+
[source,terminal]
92+
----
93+
$ oc cluster-compare -r <path_to_reference_config>/metadata.yaml -o junit
94+
----
95+
96+
The `junit` output includes the following result types:
97+
98+
* Passed results for each fully matched template.
99+
* Failed results for differences found or missing required custom resources (CRs).
100+
* Skipped results for differences patched using the user override mechanism.
101+
====

modules/using-cluster-compare-must-gather.adoc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,18 @@ No patched CRs <10>
8282
<8> The CRs that did not match to a corresponding template in the reference configuration.
8383
<9> The metadata hash identifies the reference configuration.
8484
<10> The list of patched CRs.
85+
86+
[NOTE]
87+
====
88+
Get the output in the `junit` format by adding `-o junit` to the command. For example:
89+
[source,terminal]
90+
----
91+
$ oc cluster-compare -r <path_to_reference_config>/metadata.yaml -f "must-gather*/*/cluster-scoped-resources","must-gather*/*/namespaces" -R -o junit
92+
----
93+
94+
The `junit` output includes the following result types:
95+
96+
* Passed results for each fully matched template.
97+
* Failed results for differences found or missing required custom resources (CRs).
98+
* Skipped results for differences patched using the user override mechanism.
99+
====

modules/using-cluster-compare-telco-ref.adoc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,18 @@ No patched CRs <10>
158158
<8> The CRs that did not match to a corresponding template in the reference configuration.
159159
<9> The metadata hash identifies the reference configuration.
160160
<10> The list of patched CRs.
161+
162+
[NOTE]
163+
====
164+
Get the output in the `junit` format by adding `-o junit` to the command. For example:
165+
[source,terminal]
166+
----
167+
$ oc cluster-compare -r out/telco-core-rds/configuration/reference-crs-kube-compare/metadata.yaml -o junit
168+
----
169+
170+
The `junit` output includes the following result types:
171+
172+
* Passed results for each fully matched template.
173+
* Failed results for differences found or missing required custom resources (CRs).
174+
* Skipped results for differences patched using the user override mechanism.
175+
====

scalability_and_performance/cluster-compare/creating-a-reference-configuration.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ include::modules/cluster-compare-template-groupings.adoc[leveloffset=+1]
1414

1515
include::modules/cluster-compare-templating.adoc[leveloffset=+1]
1616

17+
include::modules/cluster-compare-templating-reference.adoc[leveloffset=+2]
18+
19+
[role="_additional-resources"]
20+
.Additional resources
21+
22+
* link:https://masterminds.github.io/sprig/[Sprig Function Documentation]
23+
1724
include::modules/cluster-compare-exclude-metadata.adoc[leveloffset=+1]
1825

1926
include::modules/cluster-compare-configure-inline-diff.adoc[leveloffset=+1]

0 commit comments

Comments
 (0)