Crossplane and operators interactions
Tags: kubernetes, devops, sre, tutorial
Published: 2023-07-10 13:00:00

In this new post about Kubernetes operators, we will talk about an  operator which already exist : **Crossplane**.

## What is Crossplane?
> Crossplane is a Kubernetes operator which allows you to manage cloud resources from Kubernetes. These cloud resources can come from differents cloud providers like AWS, GCP...

It means that **from your Kubernetes cluster, you can dynamically create resources in a cloud**.

The following example will create a **S3 bucket in AWS**.
```yaml
apiVersion: s3.aws.upbound.io/v1beta1 
kind: Bucket 
metadata: 
	generateName: crossplane-bucket- 
	labels:
		docs.crossplane.io/example: provider-aws 
spec: 
	forProvider:
		region: us-east-2 
	providerConfigRef: 
		name: default
```

This is crazy, isn't it?

---

## How to install Crossplane?

If you are interested by using Crossplane on your cluster and using it, you can check the following links where everything is well explained for each cloud provider:
- [**Setup for AWS**](https://docs.crossplane.io/v1.11/getting-started/provider-aws/)
- [**Setup for Azure**](https://docs.crossplane.io/v1.11/getting-started/provider-azure/)
- [**Setup  for GCP**](https://docs.crossplane.io/v1.11/getting-started/provider-gcp/)

---

## Using Crossplane from another operator!?

As we installed Crossplane in our Kubernetes cluster, all its custom resources are now available in the cluster. So **our operator can use them to manage new resource instances**.

In previous posts of this serie,  we  mentionned that we can use an operator to manage Grafana, Prometheus, Postgres and OpenTelemetry deployments. With our new context, as the 3 first are available in AWS,  it means that **our operator can dynamically create these resources in AWS!**

> *Note:  For all the following examples, I will only talk about AWS but it will be similar for other cloud providers. 

### Download libraries

The first thing to do in our operator is to **download crossplane librairies**.

```bash
go install github.com/crossplane-contrib/provider-aws 
go install github.com/crossplane/crossplane-runtime
```

They will allow us to access to **all the common Crossplane resources** and **all the specific resources for AWS**.

### Manage schemas

In the `main.go` file, there is a `init` function that we  ignore until now.

```go
func init() {  
   utilruntime.Must(clientgoscheme.AddToScheme(scheme))  
}
```

Declared like this, **it will give access to the operator to all the basic Kubernetes resources** with the method `AddToScheme`.

So if we want to be able to use custom resources from Crossplane, we **must add another declaration**.

```go
import (
	crossplane "github.com/crossplane-contrib/provider-aws/apis"
)

func init() {  
    utilruntime.Must(clientgoscheme.AddToScheme(scheme))    
	utilruntime.Must(crossplane.AddToScheme(scheme))
}
```

Always working with the same pattern, you need to **import the `AddToScheme` function of the librairie containing all the resources you want to work with, and all it in the `init` function.**

### RBAC
It's something really important to not forget! **Go add all the annotations you need to give the correct rights to your operator on the new resources.**

### Using new resources

Now that we have everything setup to be able to use the Crossplane resources from our operator, **we can use them in our Reconcile method!**

*Example*
```go
import (
	"github.com/crossplane-contrib/provider-aws/apis/s3/v1beta1"
)

...
    myBucket := &v1beta1.Bucket{} // Refers to an AWS S3 Bucket
    err := r.Get(ctx, req.NamespacedName, myBucket)
...
```

---

## Summary

We discovered Crossplane in this post and saw that it's possible to make interactions between operators.
And there are a LOT of operators! **[Datadog](https://operatorhub.io/operator/datadog-operator)**, **[Grafana](https://operatorhub.io/operator/grafana-operator)**, **[Kong](https://operatorhub.io/operator/kong)** or [**Litmus Chaos**](https://operatorhub.io/operator/litmuschaos) for example.

Check **[Operatorhub.io](https://operatorhub.io/)** if you want to find a list of operators.

Also, with what we saw today, we are now able to create operators like microservices, with each of them have a focused scope and make things easier to develop and scale. So think about all the things which can be done in full automation!

Talking about automation, in the next post of this serie, we will talk about **Crossplane vs Terraform**.

I hope it will help you and if you have any questions *(there are not dumb questions)* or some points are not clear for you, don't hesitate to add your question in the comments or to contact me directly on  [LinkedIn](https://www.linkedin.com/in/maxime-guilbert-14b6a5128/).

---

<center><h3>You want to support me?</h3></center>
<a href="https://www.buymeacoffee.com/mxguilbert7" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="41" width="174"></a>