DEV Community

Cover image for Crossplane and operators interactions
Maxime Guilbert
Maxime Guilbert

Posted on

Crossplane and operators interactions

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.

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
Enter fullscreen mode Exit fullscreen mode

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:


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.

go install github.com/crossplane-contrib/provider-aws 
go install github.com/crossplane/crossplane-runtime
Enter fullscreen mode Exit fullscreen mode

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.

func init() {  
   utilruntime.Must(clientgoscheme.AddToScheme(scheme))  
}
Enter fullscreen mode Exit fullscreen mode

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.

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

func init() {  
    utilruntime.Must(clientgoscheme.AddToScheme(scheme))    
    utilruntime.Must(crossplane.AddToScheme(scheme))
}
Enter fullscreen mode Exit fullscreen mode

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

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)
...
Enter fullscreen mode Exit fullscreen mode

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, Grafana, Kong or Litmus Chaos for example.

Check 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.


You want to support me?

Buy Me A Coffee

Top comments (0)