--- title: GCP Quickstart Part 2 weight: 120 tocHidden: true aliases: - /master/getting-started/provider-azure-part-3 --- {{< hint "important" >}} This guide is part 2 of a series. [**Part 1**]({{}}) covers to installing Crossplane and connect your Kubernetes cluster to GCP. {{< /hint >}} This guide walks you through building and accessing a custom API with Crossplane. ## Prerequisites * Complete [quickstart part 1]({{}}) connecting Kubernetes to GCP. * a GCP account with permissions to create a GCP [storage bucket](https://cloud.google.com/storage) and a [Pub/Sub topic](https://cloud.google.com/pubsub). {{}} 1. Add the Crossplane Helm repository and install Crossplane. ```shell helm repo add \ crossplane-stable https://charts.crossplane.io/stable helm repo update && helm install crossplane \ crossplane-stable/crossplane \ --namespace crossplane-system \ --create-namespace ``` 2. When the Crossplane pods finish installing and are ready, apply the GCP Provider. ```yaml {label="provider",copy-lines="all"} cat <}} The [GCP documentation](https://cloud.google.com/iam/docs/creating-managing-service-account-keys) provides information on how to generate a service account JSON file. {{< /hint >}} 4. Create a Kubernetes secret from the GCP JSON file ```shell {label="kube-create-secret",copy-lines="all"} kubectl create secret \ generic gcp-secret \ -n crossplane-system \ --from-file=creds=./gcp-credentials.json ``` 5. Create a _ProviderConfig_ Include your {{< hover label="providerconfig" line="7" >}}GCP project ID{{< /hover >}} in the _ProviderConfig_ settings. {{< hint type="tip" >}} Find your GCP project ID from the `project_id` field of the `gcp-credentials.json` file. {{< /hint >}} {{< editCode >}} ```yaml {label="providerconfig",copy-lines="all"} cat <$@ credentials: source: Secret secretRef: namespace: crossplane-system name: gcp-secret key: creds EOF ``` {{< /editCode >}} {{}} ## Install the PubSub Provider Part 1 only installed the GCP Storage Provider. This section deploys a PubSub Topic along with a GCP storage bucket. First install the GCP PubSub Provider. Add the new Provider to the cluster. ```yaml cat < Crossplane allows you to build your own custom APIs for your users, abstracting away details about the cloud provider and their resources. You can make your API as complex or simple as you wish. The custom API is a Kubernetes object. Here is an example custom API. ```yaml {label="exAPI"} apiVersion: database.example.com/v1alpha1 kind: NoSQL metadata: name: my-nosql-database spec: location: "US" ``` Like any Kubernetes object the API has a {{}}version{{}}, {{}}kind{{}} and {{}}spec{{}}. ### Define a group and version To create your own API start by defining an [API group](https://kubernetes.io/docs/reference/using-api/#api-groups) and [version](https://kubernetes.io/docs/reference/using-api/#api-versioning). The _group_ can be any value, but common convention is to map to a fully qualified domain name. The version shows how mature or stable the API is and increments when changing, adding or removing fields in the API. Crossplane doesn't require specific versions or a specific version naming convention, but following [Kubernetes API versioning guidelines](https://kubernetes.io/docs/reference/using-api/#api-versioning) is strongly recommended. * `v1alpha1` - A new API that may change at any time. * `v1beta1` - An existing API that's considered stable. Breaking changes are strongly discouraged. * `v1` - A stable API that doesn't have breaking changes. This guide uses the group {{}}database.example.com{{}}. Because this is the first version of the API, this guide uses the version {{}}v1alpha1{{}}. ```yaml {label="version",copy-lines="none"} apiVersion: database.example.com/v1alpha1 ``` ### Define a kind The API group is a logical collection of related APIs. In a group are individual kinds representing different resources. For example a `queue` group may have a `PubSub` and `CloudTask` kinds. The `kind` can be anything, but it must be [UpperCamelCased](https://kubernetes.io/docs/contribute/style/style-guide/#use-upper-camel-case-for-api-objects). This API's kind is {{}}PubSub{{}} ```yaml {label="kind",copy-lines="none"} apiVersion: queue.example.com/v1alpha1 kind: PubSub ``` ### Define a spec The most important part of an API is the schema. The schema defines the inputs accepted from users. This API allows users to provide a {{}}location{{}} of where to run their cloud resources. All other resource settings can't be configurable by the users. This allows Crossplane to enforce any policies and standards without worrying about user errors. ```yaml {label="spec",copy-lines="none"} apiVersion: queue.example.com/v1alpha1 kind: PubSub spec: location: "US" ``` ### Apply the API Crossplane uses {{}}Composite Resource Definitions{{}} (also called an `XRD`) to install your custom API in Kubernetes. The XRD {{}}spec{{}} contains all the information about the API including the {{}}group{{}}, {{}}version{{}}, {{}}kind{{}} and {{}}schema{{}}. The XRD's {{}}name{{}} must be the combination of the {{}}plural{{}} and {{}}group{{}}. The {{}}schema{{}} uses the {{}}OpenAPIv3{{}} specification to define the API {{}}spec{{}}. The API defines a {{}}location{{}} that must be {{}}oneOf{{}} either {{}}EU{{}} or {{}}US{{}}. Apply this XRD to create the custom API in your Kubernetes cluster. ```yaml {label="xrd",copy-lines="all"} cat <}}claimNames{{}} allows users to access this API either at the cluster level with the {{}}pubsub{{}} endpoint or in a namespace with the {{}}pubsubclaim{{}} endpoint. The namespace scoped API is a Crossplane _Claim_. {{}} For more details on the fields and options of Composite Resource Definitions read the [XRD documentation]({{}}). {{< /hint >}} View the installed XRD with `kubectl get xrd`. ```shell {copy-lines="1"} kubectl get xrd NAME ESTABLISHED OFFERED AGE pubsubs.queue.example.com True True 7s ``` View the new custom API endpoints with `kubectl api-resources | grep pubsub` ```shell {copy-lines="1",label="apiRes"} kubectl api-resources | grep queue.example pubsubclaims queue.example.com/v1alpha1 true PubSubClaim pubsubs queue.example.com/v1alpha1 false PubSub ``` ## Create a deployment template When users access the custom API Crossplane takes their inputs and combines them with a template describing what infrastructure to deploy. Crossplane calls this template a _Composition_. The {{}}Composition{{}} defines all the cloud resources to deploy. Each entry in the template is a full resource definitions, defining all the resource settings and metadata like labels and annotations. This template creates a GCP {{}}Storage{{}} {{}}Bucket{{}} and a {{}}PubSub{{}} {{}}Topic{{}}. This Composition takes the user's {{}}location{{}} input and uses it as the {{}}location{{}} used in the individual resource. {{}} This Composition uses an array of resource templates. You can patch each template with data copied from the custom API. Crossplane calls this a _Patch and Transform_ Composition. You don't have to use Patch and Transform. Crossplane supports a variety of alternatives, including Go Templating and CUE. You can also write a function in Go or Python to template your resources. Read the [Composition documentation]({{}}) for more information on configuring Compositions and all the available options. {{< /hint >}} Apply this Composition to your cluster. ```yaml {label="comp",copy-lines="all"} cat <}}compositeTypeRef{{}} defines which custom APIs can use this template to create resources. A Composition uses a pipeline of _composition functions_ to define the cloud resources to deploy. This template uses {{}}function-patch-and-transform{{}}. You must install the function before you can use it in a Composition. Apply this Function to install `function-patch-and-transform`: ```yaml {label="install"} cat <}} Read the [Composition documentation]({{}}) for more information on configuring Compositions and all the available options. Read the [Patch and Transform function documentation]({{}}) for more information on how it uses patches to map user inputs to Composition resource templates. {{< /hint >}} View the Composition with `kubectl get composition` ```shell {copy-lines="1"} kubectl get composition NAME XR-KIND XR-APIVERSION AGE topic-with-bucket PubSub queue.example.com 3s ``` ## Access the custom API With the custom API (XRD) installed and associated to a resource template (Composition) users can access the API to create resources. Create a {{}}PubSub{{}} object to create the cloud resources. ```yaml {copy-lines="all",label="xr"} cat <}} It may take up to 5 minutes to delete the resources. {{< /hint >}} ```shell {copy-lines="1"} kubectl get managed No resources found ``` ## Using the API with namespaces Accessing the API `pubsub` happens at the cluster scope. Most organizations isolate their users into namespaces. A Crossplane _Claim_ is the custom API in a namespace. Creating a _Claim_ is just like accessing the custom API endpoint, but with the {{}}kind{{}} from the custom API's `claimNames`. Create a new namespace to test create a Claim in. ```shell kubectl create namespace crossplane-test ``` Then create a Claim in the `crossplane-test` namespace. ```yaml {label="claim",copy-lines="all"} cat <}} It may take up to 5 minutes to delete the resources. {{< /hint >}} Verify Crossplane deleted the composite resource with `kubectl get composite`. ```shell {copy-lines="1"} kubectl get composite No resources found ``` Verify Crossplane deleted the managed resources with `kubectl get managed`. ```shell {copy-lines="1"} kubectl get managed No resources found ``` ## Next steps * Explore AWS resources that Crossplane can configure in the [provider CRD reference](https://github.com/crossplane-contrib/provider-upjet-aws/blob/main/package/crds). * Join the [Crossplane Slack](https://slack.crossplane.io/) and connect with Crossplane users and contributors. * Read more about the [Crossplane concepts]({{}}) to find out what else you can do with Crossplane.