Skip to content

add placement cel examples #474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions content/en/docs/concepts/content-placement/placement.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ sets in the hub clusters. Then we can move on and create a placement in the

### Predicates

#### Label/Claim selection
In the `predicates` section, you can select clusters by labels, [clusterClaims](../cluster-inventory/clusterclaim), or CEL expressions.

#### Label or ClusterClaim Selection

In the `predicates` section, you can select clusters by labels or [clusterClaims](../cluster-inventory/clusterclaim).
For instance, you can select 3 clusters with label `purpose=test` and
clusterClaim `platform.open-cluster-management.io=aws` as seen in the following
examples:
Expand All @@ -72,7 +73,7 @@ apiVersion: cluster.open-cluster-management.io/v1beta1
kind: Placement
metadata:
name: placement1
namespace: default
namespace: ns1
spec:
numberOfClusters: 3
clusterSets:
Expand All @@ -94,6 +95,39 @@ Note that the distinction between label-selecting and claim-selecting is
elaborated in [this page]({{< ref "docs/scenarios/extending-managed-clusters" >}})
about how to extend attributes for the managed clusters.

#### CEL Expression Selection

```yaml
apiVersion: cluster.open-cluster-management.io/v1beta1
kind: Placement
metadata:
name: placement1
namespace: ns1
spec:
numberOfClusters: 3
clusterSets:
- prod
predicates:
- requiredClusterSelector:
celSelector:
celExpressions:
# Select clusters by Kubernetes version listed in managedCluster.Status.version.kubernetes.
- managedCluster.status.version.kubernetes == "v1.31.0"
# Select clusters by info stored in clusterClaims.
- managedCluster.status.clusterClaims.exists(c, c.name == "kubeversion.open-cluster-management.io" && c.value == "v1.31.0")
# Use CEL Standard macros and Standard functions on the managedCluster fields.
- managedCluster.metadata.labels["version"].matches('^1\\.(30|31)\\.\\d+$')
# Use Kubernetes semver library functions isLessThan and isGreaterThan to select clusters by version comparison.
- semver(managedCluster.metadata.labels["version"]).isGreaterThan(semver("1.30.0"))
# Use OCM customized function scores to select clusters by AddonPlacementScore.
- managedCluster.scores("resource-usage-score").filter(s, s.name == 'memNodeAvailable').all(e, e.value > 0)
```

The CEL expressions provide more flexible and powerful selection capabilities with [built-in libraries](https://github.com/open-cluster-management-io/sdk-go/blob/main/pkg/cel/common/values.go). For more detailed usage of CEL expressions, refer to:

- [Kubernetes CEL Documentation](https://kubernetes.io/docs/reference/using-api/cel/)
- [OCM CEL Library](https://github.com/open-cluster-management-io/sdk-go/tree/main/pkg/cel/library)

#### Taints/Tolerations

To support filtering unhealthy/not-reporting clusters and keep workloads from
Expand Down Expand Up @@ -133,7 +167,7 @@ The following example shows how to tolerate clusters with taints.
timeAdded: '2022-02-21T08:11:06Z'
```

By default, the placement wont select this cluster unless you define tolerations.
By default, the placement won't select this cluster unless you define tolerations.

```yaml
apiVersion: cluster.open-cluster-management.io/v1beta1
Expand Down