Persistent volume claims
This feature is only available for the following:
- Host Nodes
By default, this is enabled.
Sync PersistentVolumeClaim resources from the virtual cluster to the host cluster.
Disable syncing PersistentVolumeClaims from the virtual to host clusterβ
sync:
toHost:
persistentVolumeClaims:
enabled: false
Patchesβ
This feature is an Enterprise feature. See our pricing plans or contact our sales team for more information.
Patches override the default resource syncing rules in your vCluster yaml configurations.
By default, vCluster syncs specific resources 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.
For example, vCluster can mirror resources from the virtual cluster to the host clusterβany changes made in the virtual cluster are automatically applied to the host cluster. The same applies in the other direction if syncing is set up from host to virtual cluster.
vCluster supports two patch types:
- 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.
- Reference patch: Modifies specific fields within a resource to point to different resources. If the referenced resource exists in the host cluster, vCluster automatically imports it into the virtual cluster. If the referenced resource exists in the virtual cluster and syncing is configured, vCluster can import it into the host cluster.
You can apply a wildcard, using an asterisk (*) in a specified path, to modify all elements of an array or object.
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.
When using the asterisk (*) notation, vCluster applies changes individually to every element that matches the path.
JavaScript expression patchβ
A JavaScript expression patch allows you to use JavaScript ES6 expressions to change specific fields when syncing between virtual and host clusters. This is useful when modifying resource configurations to align with differing environments or naming conventions between clusters. If your clusters use different container name prefixes, a JavaScript expression patch can automatically update them.
You can define a path for spec.storageClassName field in vcluster.yaml using the following configuration:
sync:
toHost:
persistentVolumeClaims:
enabled: true
patches:
- path: spec.storageClassName
expression: '"my-prefix-"+value'
# optional reverseExpression to reverse the change from the host cluster
# reverseExpression: 'value.slice("my-prefix".length)'
In the example:
- The path targets the
spec.storageClassNamefield to override when syncing to the host cluster. The*wildcard applies the patch to each container individually. "'my-prefix-' + value"defines a JavaScript expression that prepends"my-prefix-"to thespec.storageClassNamefield in the host cluster.
You can use the reverseExpression field to define how to revert changes when syncing from the host cluster back to the virtual cluster.
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.
To replace value with a hardcoded one, put the desired value in the quotation marks:
sync:
toHost:
persistentVolumeClaims:
enabled: true
patches:
- path: spec.storageClassName
expression: '"my-value"'
The JavaScript expression patch supports a limited set of JavaScript features. For example, it does not support import statements or other Node.js-specific features. You can use standard JavaScript expressions, functions, and operators.
The expression is evaluated in a sandboxed environment, so it cannot access external resources or the host system.
Also, the JavaScript expression must be a valid ES6 expression. This means it should not contain any statements, only expressions that return a value.
A common use is to use ternary operators to conditionally modify values based on their existence or other criteria.
Another common use is the optional chaining operator like in: value.myfield?.anotherfield || "default-value".
Additional functions available:
atob(encodedData): Decodes a base64-encoded string and returns the decoded string. Ex:atob("bXktdmFsdWU=")returnsmy-value.btoa(stringToEncode): Encodes a string in base64 and returns the encoded string. Ex:btoa("my-value")returnsbXktdmFsdWU=.virtualToHostDNS(virtualSvc): Translates a virtual service to its corresponding host FQDN. The domain must follow the format<name>.<namespace>.svc[.<cluster-domain>]. Ex:virtualToHostDNS(nginx.default.svc.cluster.local)returnsnginx-x-default-x-vcluster.vcluster-ns.svc.cluster.local.
These functions are useful for encoding/decoding values or translating services when syncing between clusters, but be sure a value exists before operating on it:
sync:
toHost:
persistentVolumeClaims:
enabled: true
patches:
- path: metadata.annotations["encoded-value"]
expression: 'btoa(value)'
- path: metadata.annotations["decoded-value"]
expression: 'atob(value)'
- path: metadata.annotations["referenced-service"]
expression: 'virtualToHostDNS(value)'
Context variableβ
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:
context.vcluster.name: Name of the virtual cluster.context.vcluster.namespace: Namespace of the virtual cluster.context.vcluster.config: Configuration of the virtual cluster, basicallyvcluster.yamlmerged with the defaults.context.hostObject: Host object (nullif not available).context.virtualObject: Virtual object (nullif not available).context.path: Matched path on the object, useful when using wildcard path selectors (*).
Reference patchβ
A reference patch links a field in one resource to another resource. During syncing, vCluster updates the reference and imports the linked resource from the virtual cluster to the host cluster or from the host cluster to the virtual cluster, depending on the sync direction and whether the resource exists.
You can use reference patches to share resources, such as Secrets or ConfigMaps, between clusters without manually recreating or duplicating them.
For example, if the host cluster contains a secret named "my-example-secret", vCluster automatically imports it into the virtual cluster.
Workloads in the virtual cluster can then use the secret without manual syncing.
You can sync between the virtual cluster and the host cluster by mapping spec.secretName to a secret in the host cluster:
sync:
toHost:
persistentVolumeClaims:
enabled: true
patches:
- path: metadata.annotations["my-secret-ref"]
reference:
apiVersion: v1
kind: Secret
In the example:
- The code uses a patch to add
metadata.annotations["my-secret-ref"] - It references a
Secretin the host cluster using the patch and ensurespersistentVolumeClaimsin the host cluster links to the correctSecret.
::: Reference Patches with Namespace Syncing
If you have enabled syncing namespaces, reference patches are only required if the namespace is part of the patch. You can use the namespacePath option to specify the path of the namespace of the reference.
:::
Config referenceβ
persistentVolumeClaims required object β
PersistentVolumeClaims defines if persistent volume claims created within the virtual cluster should get synced to the host cluster.
persistentVolumeClaims required object βenabled required boolean true β
Enabled defines if this option should be enabled.
enabled required boolean true βpatches required object[] β
Patches patch the resource according to the provided specification.
patches required object[] βpath required string β
Path is the path within the patch to target. If the path is not found within the patch, the patch is not applied.
path required string βexpression required string β
Expression transforms the value according to the given JavaScript expression.
expression required string βreverseExpression required string β
ReverseExpression transforms the value according to the given JavaScript expression.
reverseExpression required string βreference required object β
Reference treats the path value as a reference to another object and will rewrite it based on the chosen mode
automatically. In single-namespace mode this will translate the name to "vxxxxxxxxx" to avoid conflicts with
other names, in multi-namespace mode this will not translate the name.
reference required object βapiVersion required string β
APIVersion is the apiVersion of the referenced object.
apiVersion required string βapiVersionPath required string β
APIVersionPath is optional relative path to use to determine the kind. If APIVersionPath is not found, will fallback to apiVersion.
apiVersionPath required string βkind required string β
Kind is the kind of the referenced object.
kind required string βkindPath required string β
KindPath is the optional relative path to use to determine the kind. If KindPath is not found, will fallback to kind.
kindPath required string βnamePath required string β
NamePath is the optional relative path to the reference name within the object.
namePath required string βnamespacePath required string β
NamespacePath is the optional relative path to the reference namespace within the object. If omitted or not found, namespacePath equals to the
metadata.namespace path of the object.
namespacePath required string βlabels required object β
Labels treats the path value as a labels selector.
labels required object β