<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sahil Lakhwani</title>
    <description>The latest articles on DEV Community by Sahil Lakhwani (@sahillakhwani).</description>
    <link>https://dev.to/sahillakhwani</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F582314%2F66979a8a-2272-4db3-8f10-fc47ac76ae83.png</url>
      <title>DEV Community: Sahil Lakhwani</title>
      <link>https://dev.to/sahillakhwani</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sahillakhwani"/>
    <language>en</language>
    <item>
      <title>Building Custom Control Planes using Crossplane</title>
      <dc:creator>Sahil Lakhwani</dc:creator>
      <pubDate>Fri, 19 Feb 2021 13:31:05 +0000</pubDate>
      <link>https://dev.to/sahillakhwani/building-custom-control-planes-using-crossplane-404f</link>
      <guid>https://dev.to/sahillakhwani/building-custom-control-planes-using-crossplane-404f</guid>
      <description>&lt;p&gt;It's been about a year when we &lt;a href="https://www.infracloud.io/blogs/cluster-provisioning-using-crossplane/" rel="noopener noreferrer"&gt;wrote about Crossplane&lt;/a&gt;. The previous post explains how Crossplane helps you provision and manage infrastructure using the Kubernetes API. Crossplane has since evolved and reached v1.0! Big congratulations to the Crossplane community!&lt;br&gt;
Besides being an interface to cloud providers, Crossplane now has mechanisms to create and publish your own custom resources, without writing code. It offers ways to define your own infrastructure resources declaratively, in form of CRDs on top of cloud providers APIs. In this post, we'll see how you can use Crossplane to create your own control plane on top of cloud providers.&lt;/p&gt;
&lt;h2&gt;
  
  
  Infrastructure Provisioning
&lt;/h2&gt;

&lt;p&gt;Let's take a look at how Crossplane allows us to provision cloud resources.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Installation&lt;/strong&gt;&lt;br&gt;
The below commands will install Crossplane in a Kubernetes cluster using Helm 3&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kubectl create namespace crossplane-system

$ helm repo add crossplane-stable https://charts.crossplane.io/stable
$ helm repo update

$ helm install crossplane --namespace crossplane-system crossplane-stable/crossplane
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Crossplane CLI is kubectl plugin to manage Crossplane packages. To install, run:&lt;br&gt;
&lt;code&gt;curl -sL https://raw.githubusercontent.com/crossplane/crossplane/release-1.0/install.sh | sh&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Working with a Cloud Provider&lt;/strong&gt;&lt;br&gt;
A "provider" in Crossplane is a bunch of CRDs and their controllers, creating an interface to a cloud provider API (in fact, any API). We'll use AWS for demonstration here.&lt;/p&gt;

&lt;p&gt;Install the provider&lt;br&gt;
&lt;code&gt;kubectl crossplane install provider crossplane/provider-aws:v0.16.0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Create a provider secret from the AWS credentials by running the following. This uses the default profile from AWS CLI credentials.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ AWS_PROFILE=default &amp;amp;&amp;amp; echo -e "[default]\naws_access_key_id = $(aws configure get aws_access_key_id --profile $AWS_PROFILE)\naws_secret_access_key = $(aws configure get aws_secret_access_key --profile $AWS_PROFILE)" &amp;gt; creds.conf

$ kubectl create secret generic aws-creds -n crossplane-system --from-file=key=./creds.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And a ProviderConfig to configure credentials for the AWS provider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cat &amp;gt; providerconfig.yaml &amp;lt;&amp;lt;EOF
apiVersion: aws.crossplane.io/v1beta1
kind: ProviderConfig
metadata:
  name: default
spec:
  credentials:
    source: Secret
    secretRef:
      namespace: crossplane-system
      name: aws-creds
      key: key
EOF

$ kubectl apply -f providerconfig.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below is a Crossplane representation of a AWS RDS instance, called a "managed resource". A Crossplane managed resource is a representation of a cloud provider resource and in fact, any API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cat &amp;gt; rds.yaml &amp;lt;&amp;lt;EOF
apiVersion: database.aws.crossplane.io/v1beta1
kind: RDSInstance
metadata:
  name: rdspostgresql
spec:
  forProvider:
    region: us-east-1
    dbInstanceClass: db.t2.small
    masterUsername: masteruser
    allocatedStorage: 20
    engine: postgres
    engineVersion: "9.6"
    skipFinalSnapshotBeforeDeletion: true
  writeConnectionSecretToRef:
    namespace: crossplane-system
    name: aws-rdspostgresql-conn
EOF

$ kubectl apply -f rds.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create an RDS instance and a Kubernetes Secret with the credentials for it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Infrastructure Composition
&lt;/h2&gt;

&lt;p&gt;While managed resources are great for managing cloud resources, it can soon become overwhelming to manage lots of them each time. For instance, an application developer might not be interested in the details of how a RDS instance is being created and managed with firewall rules, database configuration, etc. The only thing of interest might be a database and credentials to access it. On the contrary, an infrastructure operator may not want to expose all of the RDS configurations to a developer, but instead restrict only to some necessary parameters.&lt;/p&gt;

&lt;p&gt;Crossplane offers mechanisms to compose managed resources where users can create their own abstractions in a declarative way.&lt;br&gt;
&lt;strong&gt;Composite Resources&lt;/strong&gt;: A &lt;em&gt;Composite Resource (XR)&lt;/em&gt; is a custom resource that is composed of managed resources allowing you to abstract the infrastructure details.&lt;br&gt;
A &lt;em&gt;CompositeResourceDefinition (XRD)&lt;/em&gt; defines a new kind of composite resource. XRDs are cluster scoped. To create a namespaced XR, the respective XRD may optionally offer a &lt;em&gt;Composite Resource Claim (XRC)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Composition&lt;/strong&gt;: A Composition specifies which resources a XR will be composed of i.e what happens when you  create XR. There can be multiple compositions for one XR. For example, for a &lt;code&gt;CompositeDatabase&lt;/code&gt; XR, you may have a composition which will create a AWS RDS instance, a security group and a MySQL database. Another composition can define GCP CloudSQL instance and a PostgreSQL database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Configuration&lt;/strong&gt;: A configuration is a package of XRDs and Compositions that can be then published using the Crossplane CLI to Crossplane by creating a declarative Configuration resource&lt;/p&gt;

&lt;p&gt;The below figure shows how the Crossplane resources might look like in a Kubernetes cluster. The XRDs are cluster scoped with compositions satisfying them. These XRDs offer a XRC each, which are namespace scoped. Offering a XRC is optional and is done to allow users to create XRs in their own namespace.&lt;br&gt;
The XRDs also are referring to a managed resource in the cluster scope, so that the XRC can use it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fep1cnej8wao1aclhh8mo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fep1cnej8wao1aclhh8mo.png" alt="Control Plane using Crossplane"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's see a quick example to understand these concepts better. We'll create a &lt;code&gt;CompositePostgreSQLInstance&lt;/code&gt; XRD for PostgreSQL database , a composition for satisfying this XRD, and see how we can use the XRC that gets created.&lt;/p&gt;

&lt;p&gt;You can leverage the Crossplane CLI to install a configuration package:&lt;br&gt;
&lt;code&gt;kubectl crossplane install configuration registry.upbound.io/xp/getting-started-with-aws:v1.0.0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Otherwise, you can define the XRD and composition yourself as explained below.&lt;br&gt;
(&lt;em&gt;The examples are taken from &lt;a href="https://crossplane.io/docs/v1.0/" rel="noopener noreferrer"&gt;official Crossplane docs&lt;/a&gt;&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;The XRD is defined below. Note how a claim is offered under &lt;code&gt;claimNames&lt;/code&gt;. The only configurable field in the spec is &lt;code&gt;storageGB&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// xrd.yaml
apiVersion: apiextensions.crossplane.io/v1
kind: CompositeResourceDefinition
metadata:
  name: compositepostgresqlinstances.database.example.org
spec:
  group: database.example.org
  names:
    kind: CompositePostgreSQLInstance
    plural: compositepostgresqlinstances
  claimNames:
    kind: PostgreSQLInstance
    plural: postgresqlinstances
  connectionSecretKeys:
    - username
    - password
    - endpoint
    - port
  versions:
  - name: v1alpha1
    served: true
    referenceable: true
    schema:
      openAPIV3Schema:
        type: object
        properties:
          spec:
            type: object
            properties:
              parameters:
                type: object
                properties:
                  storageGB:
                    type: integer
                required:
                  - storageGB
            required:
              - parameters
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The composition defined below defines creation of a AWS RDS instance. Notice how it references the XRD using the &lt;code&gt;compositeTypeRef&lt;/code&gt; field. The &lt;code&gt;resources&lt;/code&gt; define a set of &lt;code&gt;base&lt;/code&gt; resources that get created when this composition is used. And under that, the &lt;code&gt;patches&lt;/code&gt; field is used to fetch the property values from the XRD.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// composition.yaml
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
  name: compositepostgresqlinstances.aws.database.example.org
  labels:
    provider: aws
    guide: quickstart
    vpc: default
spec:
  writeConnectionSecretsToNamespace: crossplane-system
  compositeTypeRef:
    apiVersion: database.example.org/v1alpha1
    kind: CompositePostgreSQLInstance
  resources:
    - base:
        apiVersion: database.aws.crossplane.io/v1beta1
        kind: RDSInstance
        spec:
          forProvider:
            region: us-east-1
            dbInstanceClass: db.t2.small
            masterUsername: masteruser
            engine: postgres
            engineVersion: "9.6"
            skipFinalSnapshotBeforeDeletion: true
            publiclyAccessible: true
          writeConnectionSecretToRef:
            namespace: crossplane-system
      patches:
        - fromFieldPath: "metadata.uid"
          toFieldPath: "spec.writeConnectionSecretToRef.name"
          transforms:
            - type: string
              string:
                fmt: "%s-postgresql"
        - fromFieldPath: "spec.parameters.storageGB"
          toFieldPath: "spec.forProvider.allocatedStorage"
      connectionDetails:
        - fromConnectionSecretKey: username
        - fromConnectionSecretKey: password
        - fromConnectionSecretKey: endpoint
        - fromConnectionSecretKey: port
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install these two manifests&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ kubectl apply -f xrd.yaml &amp;amp;&amp;amp; kubectl apply -f composition.yaml&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can now see a new XRC created, &lt;code&gt;postgresqlinstances.database.example.org&lt;/code&gt;. Use this XRC to create a AWS RDS instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// xr.yaml
apiVersion: database.example.org/v1alpha1
kind: PostgreSQLInstance
metadata:
  name: my-db
  namespace: default
spec:
  parameters:
    storageGB: 20
  compositionSelector:
    matchLabels:
      provider: aws
      vpc: default
  writeConnectionSecretToRef:
    name: db-conn
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;$ kubectl apply -f xr.yaml&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will result in creation of RDS PostgreSQL instance. Similarly, you can have another Composition for GCP CloudSQL and create a XR by modifying the &lt;code&gt;compositionSelector&lt;/code&gt; field in the above manifest.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Control Planes
&lt;/h2&gt;

&lt;p&gt;As we saw, Crossplane helps to build custom resources on top of cloud resources and in fact on top of any API. This is great to build a control plane with required abstractions and restrictions using XRD, composition and XRC. &lt;/p&gt;

&lt;p&gt;The below depiction shows an example of how an infrastructure operator might allow other people to create their database instances which are attached to VPCs which they don't have control of.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk4070eqe1nbz2hgd2rgx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk4070eqe1nbz2hgd2rgx.png" alt="Control Plane using Crossplane"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The image shows a VPC at cluster scope. Also at the cluster level are two database XRDs offering a claim each. Other users can then create their own database XRs using the namespace scoped XRC and still refer to the VPC. There can be multiple XRDs with different configurations on the cluster scope, allowing users to choose from. Also, different compositions for different cloud providers to satisfy those XRDs.&lt;br&gt;&lt;br&gt;
The defined resource definitions can then be packaged and distributed as a configuration. You can &lt;a href="https://crossplane.io/docs/v1.0/getting-started/package-infrastructure.html#build-and-push-the-configuration" rel="noopener noreferrer"&gt;read more&lt;/a&gt; how the Crossplane CLI helps you do this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Crossplane helps to provision infrastructure using the Kubernetes API. Moreover, it helps to build custom abstractions and restrictions over any API. This makes it a great tool to build a single control plane to manage multiple cloud providers.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;We hope this post gives you a good start for using Crossplane. We're always thrilled to connect to people working with cloud native technologies. You can reach out to us via &lt;a href="https://twitter.com/infracloudio" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; and &lt;a href="https://www.linkedin.com/company/infracloudio" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>cloudnative</category>
      <category>crossplane</category>
      <category>infrastructure</category>
    </item>
  </channel>
</rss>
