Override Policy
The OverridePolicy and ClusterOverridePolicy are used to declare override rules for resources when they are propagating to different clusters.
Difference between OverridePolicy and ClusterOverridePolicy
ClusterOverridePolicy represents the cluster-wide policy that overrides a group of resources to one or more clusters while OverridePolicy will apply to resources in the same namespace as the namespace-wide policy. For cluster scoped resources, apply ClusterOverridePolicy by policies name in ascending. For namespaced scoped resources, first apply ClusterOverridePolicy, then apply OverridePolicy.
Resource Selector
ResourceSelectors restricts resource types that this override policy applies to. If you ignore this field it means matching all resources.
Resource Selector required apiVersion field which represents the API version of the target resources and kind which represents the Kind of the target resources.
The allowed selectors are as follows:
namespace: namespace of the target resource. Default is empty, which means inherit from the parent object scope.name: name of the target resource. Default is empty, which means selecting all resources.labelSelector: a label query over a set of resources. If name is not empty, labelSelector will be ignored.
Examples
Example 1: select one deployment kind resources
apiVersion: policy.karmada.io/v1alpha1
kind: OverridePolicy
metadata:
name: example
namespace: test
spec:
resourceSelectors:
- apiVersion: apps/v1
kind: Deployment
namespace: test
labelSelector:
matchLabels:
app: nginx
overrideRules:
#...
It means override rules above will only be applied to Deployment which is named nginx in test namespace and has labels with app: nginx.
Example 2: select more than one kind resource
apiVersion: policy.karmada.io/v1alpha1
kind: OverridePolicy
metadata:
name: example
namespace: test
spec:
resourceSelectors:
- apiVersion: apps/v1
kind: Deployment
name: nginx
- apiVersion: v1
kind: Service
name: nginx-svc
overrideRules:
#...
Override rules above will be applied to the Deployment named nginx and the Service named nginx-svc in test namespace. This indicates that all resources hit by resourceSelectors will be applied.
Target Cluster
Target Cluster defines restrictions on the override policy that only applies to resources propagated to the matching clusters. If you ignore this field it means matching all clusters.
The allowed selectors are as follows:
labelSelector: a filter to select member clusters by labels.fieldSelector: a filter to select member clusters by fields. Currently only three fields of provider(cluster.spec.provider), zone(cluster.spec.zone), and region(cluster.spec.region) are supported.clusterNames: the list of clusters to be selected.exclude: the list of clusters to be ignored.
labelSelector
Examples
apiVersion: policy.karmada.io/v1alpha1
kind: OverridePolicy
metadata:
name: example
spec:
#...
overrideRules:
- targetCluster:
labelSelector:
matchLabels:
cluster: member1
overriders:
#...
It means override rules above will only be applied to those resources propagated to clusters which has cluster: member1 label.