Skip to content

Commit

Permalink
init: initial beta validate documentaiton
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr Zaniewski <[email protected]>
  • Loading branch information
Piotr Zaniewski committed Feb 14, 2024
1 parent 6e0320b commit cf17878
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions content/master/cli/command-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,113 @@ flags, options or outputs in future releases.
Crossplane maintainers may promote or remove commands under `beta` in future
releases.

### beta validate

The `crossplane beta validate` validates [compositions]({{<ref "../concepts/compositions>"}}) against provider or XRD schema using
Kubernetes API server's validation library.

The validate command enables various validation scenarios:

- Validate any Crossplane resource against any schema (provider, XRD).
- Use the output of the render command to validate function based compositions.
- When validating resources against provider schemas, the command downloads and
cache the providers' CRDs to make future runs much faster.
- Work with Kubernetes CEL validation rules to enable validation of complex business rules expressed in the composition resources definitions (XRDs).
- Pass the entire folder with schema files and validate all the Crossplane
resources against the schemas.

{{< hint "important" >}}
The `crossplane beta validate` command performs all validation offline without requiring a running cluster.
{{< /hint >}}

### Kubernetes custom definition rules

Validate XRD [CEL Rules](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation-rules)

{{< expand "An example validation rule" >}}

```yaml
x-kubernetes-validations:
- rule: "self.minReplicas <= self.replicas && self.replicas <= self.maxReplicas"
message: "replicas should be in between minReplicas and maxReplicas."
```
{{< /expand >}}
`crossplane beta validate xrd.yaml rong-xr.yaml`
{{< expand "CEL rule violation" >}}

```console
[x] CEL validation error example.crossplane.io/v1beta1, Kind=XR, example : spec: Invalid value: "object": replicas should be in between minReplicas and maxReplicas.
Total 1 resources: 0 missing schemas, 0 success cases, 1 failure cases
crossplane: error: cannot validate resources: could not validate all resources
```

{{< /expand >}}

### Validate resource against a schema

In the first scenario we can validate any resource (in this case XR and MR)
against any schema (in this case provider CRD and an XRD)

`crossplane beta validate schemas.yaml resources.yaml`

The command has two categories of output; _errors_ and _warnings_.
If there is a validation error or warning, the command will indicate it accordingly. Let's
try it out, here the command will output validation warning since were are
missing an XRD:

`crossplane beta validate missing-schemas.yaml resources.yaml`

### Validate render command output

This is a very useful feature when developing functions. Passing the output of
the _render_ command to the _validate_ command makes sure that our function does
not violate schema requirements.

`crossplane beta render xr.yaml composition.yaml func.yaml | crossplane beta validate schemas.yaml -`

You might have noticed that there is an error indicating the missing XR. This is
because functions do not have access to spec fields. The flag _--include-full-xr_ fixed
it by adding the XR to the _render_ command output and satisfying the validate
constrains.

`crossplane beta render xr.yaml composition.yaml func.yaml --include-full-xr | crossplane beta validate schemas.yaml -`

### Validate by folder

If you have multiple compositions and resources to validate, it is easy to use
the _validate_ command to travers all the Crossplane yaml files and validate in
batch mode across all the resources.

First let's render all our resources into a file (xr, compostion and function).
It will serve as a base for validation.

`crossplane beta render xr.yaml composition.yaml func.yaml --include-full-xr > rendered.yaml`

The _Extensions_ folder contains packages (provider and configuration) to validate against `tree Extensions`

`crossplane beta validate Extensions rendered.yaml`

After executing this command you will notice that the validate command downloads
missing CRDs into a _.crossplane_ folder. This enables the validation process
without the need to access external cluster!

Running the same command again will not trigger the download since we have
cached all the schemas.

`crossplane beta validate Extensions rendered.yaml`

Finally we can use a custom directory as a cache for downloading the schemas.
This enables having a centralized and shared cache directory between different
projects.

`crossplane beta validate Extensions rendered.yaml --cache-dir mycache`

Or clean the cache directory.

`crossplane beta validate Extensions rendered.yaml --cache-dir mycache --clean-cache`

### beta render

The `crossplane beta render` command previews the output of a
Expand Down

0 comments on commit cf17878

Please sign in to comment.