Skip to content

Commit 4c6139c

Browse files
committed
edit patches text
change file location change file location change file location change file location change file location change file location change file location change file location change file location change file location change file location remove admonitions change file location change file location change file location update update link update link update link update link update link update link update link update link update link update first line updates for linter update text for clarity update text for consistency slight rewrite
1 parent 54ba9ec commit 4c6139c

File tree

1 file changed

+60
-25
lines changed

1 file changed

+60
-25
lines changed

vcluster/_fragments/patches.mdx

+60-25
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,71 @@ import CodeBlock from '@theme/CodeBlock';
33

44
<ProAdmonition/>
55

6-
You can modify the sync behaviour with patches that target specific paths. Currently there is 2 different kinds of patches supported.
6+
Patches override the default resource syncing rules in your vCluster YAML configurations.
77

8-
:::info Wildcard patches
9-
You can use `*` in paths to select all entries of an array or object, e.g. `spec.containers[*].name` or `spec.containers[*].volumeMounts[*]`. vCluster calls the patch multiple times when using the wildcard reference.
8+
By default, vCluster [syncs specific resources](https://www.vcluster.com/docs/vcluster/next/configure/vcluster-yaml/sync/) between virtual and host clusters. To modify the sync behavior, you can use patches to specify which fields to edit, exclude, or override during syncing.
9+
10+
Patches can provide greater control over syncing by preventing unintended changes, preserving critical settings, and ensuring only necessary updates are applied.
11+
12+
vCluster supports two patch types:
13+
14+
- [JavaScript expression patch](#javascript-expression-patch): Uses JavaScript ES6 expressions to dynamically modify fields during syncing. You can define how a field changes when syncing from a virtual cluster to a host cluster, or from a host cluster to a virtual cluster.
15+
- [Reference patch](#reference-patch): Modifies specific fields within a resource to point to different resources. If the specified resource exists in a host cluster, vCluster automatically imports it into the corresponding virtual cluster.
16+
17+
:::info Using wildcards in patches
18+
You can apply a wildcard, using an asterisk (`*`) in a specified path, to modify all elements of an array or object.
19+
For instance, `spec.containers[*].name` selects the `name` field of every container in the `spec.containers` array, and `spec.containers[*].volumeMounts[*]` selects all `volumeMounts` for each container.
20+
When using the asterisk (`*`) notation, vCluster applies changes individually to every element that matches the path.
1021
:::
1122

12-
### JavaScript Expression Patches
23+
### JavaScript expression patch
24+
25+
A JavaScript expression patch allows you to use JavaScript ES6 expressions to change specific fields when syncing between virtual and host clusters.
26+
This is useful when modifying resource configurations to align with differing environments or naming conventions between clusters.
27+
28+
For example, if your virtual cluster follows a specific naming convention for container names, but the host cluster requires a different prefix, you can use a JavaScript expression patch to automatically update the relevant field for each container.
29+
30+
You can define a path for `spec.containers[*].name` in `vcluster.yaml` using the following configuration:
1331

14-
These are powerful JavaScript ES6 compatible expression patches that can be used to change a field while syncing. You define how it changes when syncing from the virtual cluster into the host cluster or when syncing from the host cluster into the virtual cluster. To change the path <span>{props.path}</span> you can do:
1532
<CodeBlock language="yaml">
16-
{`sync:
33+
{`
34+
sync:
1735
toHost:
18-
${props.resource}:
19-
enabled: true
20-
patches:
21-
- path: ${props.path}
22-
expression: '"my-prefix-"+value'
23-
# optional reverseExpression to reverse the change from the host cluster
24-
# reverseExpression: 'value.slice("my-prefix".length)'`}
36+
enabled: true
37+
patches:
38+
- path: spec.containers[*].name
39+
expression: "'my-prefix-' + value"
40+
`}
2541
</CodeBlock>
2642

27-
There is also a variable called `context` besides `value` that can be used to access specific data of the virtual cluster:
28-
* `context.vcluster.name`: Name of the virtual cluster
29-
* `context.vcluster.namespace`: Namespace of the virtual cluster
30-
* `context.vcluster.config`: Config of the virtual cluster, basically `vcluster.yaml` merged with the defaults
31-
* `context.hostObject`: Host object (can be null if not available)
32-
* `context.virtualObject`: Virtual object (can be null if not available)
33-
* `context.path`: The matched path on the object, useful when using wildcard path selectors (*)
43+
In the example:
44+
45+
- `spec.containers[*].name` targets the `name` field of all containers within a Pod. The `*` wildcard applies the patch to each container individually.
46+
- `"'my-prefix-' + value"` defines a JavaScript expression that prepends `"my-prefix-"` to each container name during syncing.
47+
48+
:::note Reverse sync
49+
You can use the `reverseExpression` field to define how to revert changes when syncing from the host cluster back to the virtual cluster.
50+
For example, add `reverseExpression: {"value.slice('my-prefix'.length)"}` to `vcluster.yaml` to remove the `"my-prefix-"` prefix when syncing back from the host cluster to the virtual cluster in the previous example.
51+
:::
52+
53+
#### Context variable
3454

35-
### Reference patches
55+
The context variable is an object supported in JavaScript expression patches, that provides access to virtual cluster data during syncing. The context object includes the following properties:
56+
57+
* `context.vcluster.name`: Name of the virtual cluster.
58+
* `context.vcluster.namespace`: Namespace of the virtual cluster.
59+
* `context.vcluster.config`: Configuration of the virtual cluster, basically `vcluster.yaml` merged with the defaults.
60+
* `context.hostObject`: Host object (`null` if not available).
61+
* `context.virtualObject`: Virtual object (`null` if not available).
62+
* `context.path`: Matched path on the object, useful when using wildcard path selectors (`*`).
63+
64+
### Reference patch
65+
66+
A reference patch allows you to link a field in one resource to another resource. During syncing, vCluster rewrites the reference and imports the linked resource from a host cluster into its corresponding virtual cluster, if the resource exists.
67+
You can use reference patches to share resources, such as `Secrets` or `ConfigMaps`, between clusters without manually recreating or duplicating them.
68+
69+
For example, if you have a custom resource in your virtual cluster that references a `Secret` named `my-secret-ref`, you can define a reference patch in your `vcluster.yaml` using the following configuration:
3670

37-
A reference patch can be used to have a specific field of one resource point to a different resource that should get rewritten. vCluster automatically imports the referenced resource to the virtual cluster if it can find it in the host cluster. For example:
3871
<CodeBlock language="yaml">
3972
{`sync:
4073
toHost:
@@ -47,8 +80,10 @@ A reference patch can be used to have a specific field of one resource point to
4780
kind: Secret`}
4881
</CodeBlock>
4982

50-
With this yaml, vCluster translates the path `metadata.annotations["my-secret-ref"]` as it points to a secret. If the secret is created in the host cluster, vCluster automatically imports it into the virtual cluster.
83+
In the example:
84+
85+
- `metadata.annotations["my-secret-ref"]` points to a `Secret`. The `Secret` is created in the host cluster and vCluster automatically imports it into the virtual cluster.
5186

52-
:::info Multi-Namespace-Mode
53-
With multi-namespace-mode you only need to rewrite references that include a namespace. You can use the `namespacePath` option to specify the path of the namespace of the reference.
87+
:::info Multi namespace mode
88+
With multi-namespace mode you only need to rewrite references that include a namespace. You can use the `namespacePath` option to specify the path of the namespace of the reference.
5489
:::

0 commit comments

Comments
 (0)