|
| 1 | +--- |
| 2 | +title: "Customize cluster scoped resources in Azure Kubernetes Fleet Manager with cluster resource overrides" |
| 3 | +description: This article provides an overview of how to use the Fleet ClusterResourceOverride API to override cluster scoped resources in Azure Kubernetes Fleet Manager. |
| 4 | +ms.topic: how-to |
| 5 | +ms.date: 05/10/2024 |
| 6 | +author: schaffererin |
| 7 | +ms.author: schaffererin |
| 8 | +ms.service: kubernetes-fleet |
| 9 | +ms.custom: |
| 10 | + - build-2024 |
| 11 | +--- |
| 12 | + |
| 13 | +# Customize cluster scoped resources in Azure Kubernetes Fleet Manager with cluster resource overrides (preview) |
| 14 | + |
| 15 | +This article provides an overview of how to use the Fleet `ClusterResourceOverride` API to customize cluster scoped resources in Azure Kubernetes Fleet Manager. |
| 16 | + |
| 17 | +[!INCLUDE [preview features callout](./includes/preview/preview-callout.md)] |
| 18 | + |
| 19 | +## Cluster resource override overview |
| 20 | + |
| 21 | +The cluster resource override feature allows you to modify or override specific attributes across cluster-wide resources. With `ClusterResourceOverride`, you can define rules based on cluster labels, specifying changes to be applied to various cluster-wide resources such as namespaces, cluster roles, cluster role bindings, or custom resource definitions (CRDs). These modifications might include updates to permissions, configurations, or other parameters, ensuring consistent management and enforcement of configurations across your Fleet-managed Kubernetes clusters. |
| 22 | + |
| 23 | +## API components |
| 24 | + |
| 25 | +The `ClusterResourceOverride` API consists of the following components: |
| 26 | + |
| 27 | +* **`clusterResourceSelectors`**: Specifies the set of cluster resources selected for overriding. |
| 28 | +* **`policy`**: Specifies the set of rules to apply to the selected cluster resources. |
| 29 | + |
| 30 | +### Cluster resource selectors |
| 31 | + |
| 32 | +A `ClusterResourceOverride` object can include one or more cluster resource selectors to specify which resources to override. The `ClusterResourceSelector` object supports the following fields: |
| 33 | + |
| 34 | +> [!NOTE] |
| 35 | +> If you select a namespace in the `ClusterResourceSelector`, the override will apply to all resources in the namespace. |
| 36 | +
|
| 37 | +* `group`: The API group of the resource. |
| 38 | +* `version`: The API version of the resource. |
| 39 | +* `kind`: The kind of the resource. |
| 40 | +* `name`: The name of the resource. |
| 41 | + |
| 42 | +To add a cluster resource selector to a `ClusterResourceOverride` object, use the `clusterResourceSelectors` field with the following YAML format: |
| 43 | + |
| 44 | +```yaml |
| 45 | +apiVersion: placement.kubernetes-fleet.io/v1alpha1 |
| 46 | +kind: ClusterResourceOverride |
| 47 | +metadata: |
| 48 | + name: example-cro |
| 49 | +spec: |
| 50 | + clusterResourceSelectors: |
| 51 | + - group: rbac.authorization.k8s.io |
| 52 | + kind: ClusterRole |
| 53 | + version: v1 |
| 54 | + name: secret-reader |
| 55 | +``` |
| 56 | +
|
| 57 | +This example selects a `ClusterRole` named `secret-reader` from the `rbac.authorization.k8s.io/v1` API group for overriding. |
| 58 | + |
| 59 | +## Policy |
| 60 | + |
| 61 | +A `Policy` object consists of a set of rules, `overrideRules`, that specify the changes to apply to the selected cluster resources. Each `overrideRule` object supports the following fields: |
| 62 | + |
| 63 | +* `clusterSelector`: Specifies the set of clusters to which the override rule applies. |
| 64 | +* `jsonPatchOverrides`: Specifies the changes to apply to the selected resources. |
| 65 | + |
| 66 | +To add an override rule to a `ClusterResourceOverride` object, use the `policy` field with the following YAML format: |
| 67 | + |
| 68 | +```yaml |
| 69 | +apiVersion: placement.kubernetes-fleet.io/v1alpha1 |
| 70 | +kind: ClusterResourceOverride |
| 71 | +metadata: |
| 72 | + name: example-cro |
| 73 | +spec: |
| 74 | + clusterResourceSelectors: |
| 75 | + - group: rbac.authorization.k8s.io |
| 76 | + kind: ClusterRole |
| 77 | + version: v1 |
| 78 | + name: secret-reader |
| 79 | + policy: |
| 80 | + overrideRules: |
| 81 | + - clusterSelector: |
| 82 | + clusterSelectorTerms: |
| 83 | + - labelSelector: |
| 84 | + matchLabels: |
| 85 | + env: prod |
| 86 | + jsonPatchOverrides: |
| 87 | + - op: remove |
| 88 | + path: /rules/0/verbs/2 |
| 89 | +``` |
| 90 | + |
| 91 | +This example removes the verb "list" in the `ClusterRole` named `secret-reader` on clusters with the label `env: prod`. |
| 92 | + |
| 93 | +### Cluster selector |
| 94 | + |
| 95 | +You can use the `clusterSelector` field in the `overrideRule` object to specify the clusters to which the override rule applies. The `ClusterSelector` object supports the following field: |
| 96 | + |
| 97 | +* `clusterSelectorTerms`: A list of terms that specify the criteria for selecting clusters. Each term includes a `labelSelector` field that defines a set of labels to match. |
| 98 | + |
| 99 | +> [!IMPORTANT] |
| 100 | +> Only `labelSelector` is supported in the `clusterSelectorTerms` field. |
| 101 | + |
| 102 | +### JSON patch overrides |
| 103 | + |
| 104 | +You can use `jsonPatchOverrides` in the `overrideRule` object to specify the changes to apply to the selected resources. The `JsonPatch` object supports the following fields: |
| 105 | + |
| 106 | +* `op`: The operation to perform. |
| 107 | + * Supported operations include `add`, `remove`, and `replace`. |
| 108 | + * `add`: Adds a new value to the specified path. |
| 109 | + * `remove`: Removes the value at the specified path. |
| 110 | + * `replace`: Replaces the value at the specified path. |
| 111 | +* `path`: The path to the field to modify. |
| 112 | + * Guidance on specifying paths includes: |
| 113 | + * Must start with a `/` character. |
| 114 | + * Can't be empty or contain an empty string. |
| 115 | + * Can't be a `TypeMeta` field ("/kind" or "/apiVersion"). |
| 116 | + * Can't be a `Metadata` field ("/metadata/name" or "/metadata/namespace") except the fields "/metadata/labels" and "/metadata/annotations". |
| 117 | + * Can't be any field in the status of the resource. |
| 118 | + * Examples of valid paths include: |
| 119 | + * `/metadata/labels/new-label` |
| 120 | + * `/metadata/annotations/new-annotation` |
| 121 | + * `/spec/template/spec/containers/0/resources/limits/cpu` |
| 122 | + * `/spec/template/spec/containers/0/resources/requests/memory` |
| 123 | +* `value`: The value to add, remove, or replace. |
| 124 | + * If the `op` is `remove`, you can't specify a `value`. |
| 125 | + |
| 126 | +### Use multiple override patches |
| 127 | + |
| 128 | +You can add multiple `jsonPatchOverrides` to an `overrideRule` to apply multiple changes to the select cluster resources, as shown in the following example: |
| 129 | + |
| 130 | +```yaml |
| 131 | +apiVersion: placement.kubernetes-fleet.io/v1alpha1 |
| 132 | +kind: ClusterResourceOverride |
| 133 | +metadata: |
| 134 | + name: cro-1 |
| 135 | +spec: |
| 136 | + clusterResourceSelectors: |
| 137 | + - group: rbac.authorization.k8s.io |
| 138 | + kind: ClusterRole |
| 139 | + version: v1 |
| 140 | + name: secret-reader |
| 141 | + policy: |
| 142 | + overrideRules: |
| 143 | + - clusterSelector: |
| 144 | + clusterSelectorTerms: |
| 145 | + - labelSelector: |
| 146 | + matchLabels: |
| 147 | + env: prod |
| 148 | + jsonPatchOverrides: |
| 149 | + - op: remove |
| 150 | + path: /rules/0/verbs/2 |
| 151 | + - op: remove |
| 152 | + path: /rules/0/verbs/1 |
| 153 | +``` |
| 154 | + |
| 155 | +This example removes the verbs "list" and "watch" in the `ClusterRole` named `secret-reader` on clusters with the label `env: prod`. |
| 156 | + |
| 157 | +`jsonPatchOverrides` apply a JSON patch on the selected resources following [RFC 6902](https://datatracker.ietf.org/doc/html/rfc6902). |
| 158 | + |
| 159 | +## Apply the cluster resource override |
| 160 | + |
| 161 | +1. Create a `ClusterResourcePlacement` resource to specify the placement rules for distributing the cluster resource overrides across the cluster infrastructure, as shown in the following example. Make sure you select the appropriate resource. |
| 162 | + |
| 163 | + ```yaml |
| 164 | + apiVersion: placement.kubernetes-fleet.io/v1beta1 |
| 165 | + kind: ClusterResourcePlacement |
| 166 | + metadata: |
| 167 | + name: crp |
| 168 | + spec: |
| 169 | + resourceSelectors: |
| 170 | + - group: rbac.authorization.k8s.io |
| 171 | + kind: ClusterRole |
| 172 | + version: v1 |
| 173 | + name: secret-reader |
| 174 | + policy: |
| 175 | + placementType: PickAll |
| 176 | + affinity: |
| 177 | + clusterAffinity: |
| 178 | + requiredDuringSchedulingIgnoredDuringExecution: |
| 179 | + clusterSelectorTerms: |
| 180 | + - labelSelector: |
| 181 | + matchLabels: |
| 182 | + env: prod |
| 183 | + ``` |
| 184 | + |
| 185 | + This example distributes resources across all clusters labeled with `env: prod`. As the changes are implemented, the corresponding `ClusterResourceOverride` configurations will be applied to the designated clusters, triggered by the selection of matching cluster role resource, `secret-reader`. |
| 186 | + |
| 187 | +2. Apply the `ClusterResourcePlacement` using the `kubectl apply` command. |
| 188 | + |
| 189 | + ```bash |
| 190 | + kubectl apply -f cluster-resource-placement.yaml |
| 191 | + ``` |
| 192 | + |
| 193 | +3. Verify the `ClusterResourceOverride` object applied to the selected resources by checking the status of the `ClusterResourcePlacement` resource using the `kubectl describe` command. |
| 194 | + |
| 195 | + ```bash |
| 196 | + kubectl describe clusterresourceplacement crp |
| 197 | + ``` |
| 198 | + |
| 199 | + Your output should resemble the following example output: |
| 200 | + |
| 201 | + ```output |
| 202 | + Status: |
| 203 | + Conditions: |
| 204 | + ... |
| 205 | + Last Transition Time: 2024-04-27T04:18:00Z |
| 206 | + Message: The selected resources are successfully overridden in the 10 clusters |
| 207 | + Observed Generation: 1 |
| 208 | + Reason: OverriddenSucceeded |
| 209 | + Status: True |
| 210 | + Type: ClusterResourcePlacementOverridden |
| 211 | + ... |
| 212 | + Observed Resource Index: 0 |
| 213 | + Placement Statuses: |
| 214 | + Applicable Cluster Resource Overrides: |
| 215 | + example-cro-0 |
| 216 | + Cluster Name: member-50 |
| 217 | + Conditions: |
| 218 | + ... |
| 219 | + Message: Successfully applied the override rules on the resources |
| 220 | + Observed Generation: 1 |
| 221 | + Reason: OverriddenSucceeded |
| 222 | + Status: True |
| 223 | + Type: Overridden |
| 224 | + ... |
| 225 | + ``` |
| 226 | + |
| 227 | + The `ClusterResourcePlacementOverridden` condition indicates whether the resource override was successfully applied to the selected resources in the clusters. Each cluster maintains its own `Applicable Cluster Resource Overrides` list, which contains the cluster resource override snapshot if relevant. Individual status messages for each cluster indicate whether the override rules were successfully applied. |
| 228 | + |
| 229 | +## Next steps |
| 230 | + |
| 231 | +To learn more about Fleet, see the following resources: |
| 232 | + |
| 233 | +* [Upstream Fleet documentation](https://github.com/Azure/fleet/tree/main/docs) |
| 234 | +* [Azure Kubernetes Fleet Manager overview](./overview.md) |
0 commit comments