Skip to content

Commit

Permalink
Update website/docs/examples
Browse files Browse the repository at this point in the history
Add a script to auto-generate GCP example docs from the examples folder
Move existing examples under folders
  • Loading branch information
barney-s committed Jan 31, 2025
1 parent f40bd51 commit cba914e
Show file tree
Hide file tree
Showing 22 changed files with 688 additions and 109 deletions.
71 changes: 30 additions & 41 deletions examples/gcp/cloud-sql/README.md
Original file line number Diff line number Diff line change
@@ -1,64 +1,53 @@
# CloudSQL example
# CloudSQL

This example creates a ResourceGraphDefinition called `CloudSQL` to deploy Cloud SQL instance in 2 regions as a primary replica pair.
This example show how you can use KRO to deploy GCP Cloud SQL instance in 2 regions as a primary and replica instances.

## Create ResourceGraphDefinitions

Apply the RGD to your cluster:
## End User: CloudSQL
The administrator needs to install the RGD first.
The end user creates a `CloudSQL` resource that looks like this:

```yaml
apiVersion: kro.run/v1alpha1
kind: CloudSQL
metadata:
name: demo
namespace: config-connector
spec:
name: demo
project: my-gcp-project
primaryRegion: us-central1
replicaRegion: us-west1
```
kubectl apply -f rgd.yaml
```

Validate the RGD:
```
> kubectl get rgd cloudsql.kro.run
NAME APIVERSION KIND STATE AGE
cloudsql.kro.run v1alpha1 CloudSQL Active 44m
```
The status of the applied resource can be checked using:
## Create an Instance of CloudSQL
Set the env variables used in the instance template:
```
export CLOUDSQL_NAME=demo
export GCP_PROJECT=myproject
export PRIMARY_REGION=us-central1
export REPLICA_REGION=us-west1
kubectl get cloudsqls
kubectl get cloudsql demo -n config-connector -o yaml
```

Run the following command to replace the env variables in `instance-template.yaml` file and create
a new file called instance.yaml.
```shell
envsubst < "instance-template.yaml" > "instance.yaml"
```
Navigate to CloudSQL page in the GCP Console and verify the creation of primary and replica instances.

Apply the `instance.yaml`
Once done, the user can delete the `CloudSQL` instance:

```
kubectl apply -f instance.yaml
kubectl delete cloudsql demo -n config-connector
```

Validate instance status:
## Administrator: ResourceGraphDefinition
The administrator needs to install the RGD in the cluster first before the user can consume it:

```
kubectl get cloudsqls
kubectl apply -f rgd.yaml
```

## Validate

Navigate to CloudSQL page in the GCP Console and verify the creation of primary and replica instances.

## Clean up

Remove the instance:
Validate the RGD is installed correctly:

```
kubectl delete cloudsql $CLOUDSQL_NAME
> kubectl get rgd cloudsql.kro.run
NAME APIVERSION KIND STATE AGE
cloudsql.kro.run v1alpha1 CloudSQL Active 44m
```

Remove the ResourceGraphDefinitions:

```
kubectl delete rgd cloudsql.kro.run
```
Once all user created instances are deleted, the administrator can choose to deleted the RGD.
11 changes: 0 additions & 11 deletions examples/gcp/cloud-sql/instance-template.yaml

This file was deleted.

73 changes: 26 additions & 47 deletions examples/gcp/gke-cluster/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# GKE cluster definition
# GKECluster

A **Platform Administrator** wants to give end users in their organization self-service access to create GKE clusters. The platform administrator creates a kro ResourceGraphDefinition called *gkecluster.kro.run* that defines the required Kubernetes resources and a CRD called *GKEcluster* that exposes only the options they want to be configurable by end users. The ResourceGraphDefinition would define the following resources (using [KCC](https://github.com/GoogleCloudPlatform/k8s-config-connector) to provide the mappings from K8s CRDs to Google Cloud APIs):

Expand All @@ -9,74 +9,53 @@ A **Platform Administrator** wants to give end users in their organization self-
* KMSKeyRing - Encrypt BootDisk
* KMSCryptoKey - Encrypt BootDisk

The platform administrator would define the schema for `GKECluster` CRD which the end user creates:
* name: same for all GCP resources created
* location: region to be used for the GCP resources
* maxnodes: Max scaling limit for the nodes in the node pool

Everything related to these resources would be hidden from the end user, simplifying their experience.

![GKE Cluster Stack](gke-cluster.png)

## End User: GKECluster

# GKECluster example

This example creates a ResourceGraphDefinition called `GKECluster` to deploy a GKE cluster.

## Create ResourceGraphDefinitions
The administrator needs to install the RGD first.
The end user creates a `GKECluster` resource something like this:

Apply the RGD to your cluster:

```
kubectl apply -f rgd.yaml
```yaml
apiVersion: kro.run/v1alpha1
kind: GKECluster
metadata:
name: krodemo
namespace: config-connector
spec:
name: krodemo # Name used for all resources created as part of this RGD
location: us-central1 # Region where the GCP resources are created
maxnodes: 4 # Max scaling limit for the nodes in the new nodepool
```
Validate the RGD:
They can then check the status of the applied resource:
```
kubectl get rgd
kubectl get gkeclusters
kubectl get gkeclusters krodemo -n config-connector -o yaml
```

## Create an Instance of CloudSQL
Set the env variables used in the instance template:
```
export CLUSTER_NAME=krodemo
export GCP_REGION=us-central1
export MAX_NODES=4
```
Navigate to GKE Cluster page in the GCP Console and verify the cluster creation.

Run the following command to replace the env variables in `instance-template.yaml` file and create
a new file called instance.yaml.
```shell
envsubst < "instance-template.yaml" > "instance.yaml"
```

Apply the `instance.yaml`
Once done, the user can delete the `GKECluster` instance:

```
kubectl apply -f instance.yaml
kubectl delete gkecluster krodemo -n config-connector
```

Validate instance status:
## Administrator: ResourceGraphDefinition
The administrator needs to install the RGD in the cluster first before the user can consume it:

```
kubectl get gkeclusters
kubectl apply -f rgd.yaml
```

## Validate

Navigate to GKE Cluster page in the GCP Console and verify the cluster.

## Clean up

Remove the instance:
Validate the RGD is installed correctly:

```
kubectl delete gkecluster $CLUSTER_NAME
kubectl get rgd gkecluster.kro.run
```

Remove the ResourceGraphDefinitions:

```
kubectl delete rgd gkecluster.kro.run
```
Once all user created instances are deleted, the administrator can choose to deleted the RGD.
10 changes: 0 additions & 10 deletions examples/gcp/gke-cluster/instance-template.yaml

This file was deleted.

75 changes: 75 additions & 0 deletions scripts/update-doc-examples.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/bin/bash

create_example() {
out_file=$1
position=$2
readme_file=$3
rgd_file=$4

# Verify all parameters are valid
if [ -z "$out_file" ] || [ -z "$position" ] || [ -z "$readme_file" ] || [ -z "$rgd_file" ]; then
echo "Error: Missing required parameters"
echo "Usage: create_example <out_file> <position> <readme_file> <rgd_file>"
return 1
fi


cat > "$out_file" << EOF
---
sidebar_position: $position
---
$(cat $readme_file)
<details>
<summary>ResourceGraphDefinition</summary>
\`\`\`yaml title="rgd.yaml"
$(cat "$yaml_file")
\`\`\`
</details>
EOF

}

update_gcp_example_docs() {
# Create the GCP examples directory if it doesn't exist
mkdir -p website/docs/examples/gcp
# Initialize position counter
position=405
# Find all rgd.yaml files in examples/gcp directory and its subdirectories
find examples/gcp -name "rgd.yaml" | while read -r yaml_file; do
# Extract the directory name as the example name
example_path=$(dirname "$yaml_file")
dir_name=$(basename $example_path)
readme_file=$example_path/README.md
out_file="website/docs/examples/gcp/${dir_name}.md"

# Convert directory name to title case (e.g., gke-cluster -> GKE Cluster)
# title=$(echo "$dir_name" | sed -E 's/-/ /g' | awk '{for(i=1;i<=NF;i++)sub(/./,toupper(substr($i,1,1)),$i)}1')

# copy all images
cp $example_path/*.png website/docs/examples/gcp/ 2>/dev/null
# Generate the markdown content
create_example "$out_file" "$position" "$readme_file" "$yaml_file"

# Increment position for next file
((position+=1))

echo "Generated documentation for ${dir_name} at ${out_file}"
done
}

update_aws_example_docs() {
echo "TODO: implement aws examples"
}
update_azure_example_docs() {
echo "TODO: implement azure examples"
}
update_kubernetes_example_docs() {
echo "TODO: implement kubernetes examples"
}

update_gcp_example_docs
update_aws_example_docs
update_azure_example_docs
update_kubernetes_example_docs
File renamed without changes.
10 changes: 10 additions & 0 deletions website/docs/examples/aws/category.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"label": "AWS Examples",
"position": 300,
"link": {
"type": "generated-index",
"description": "Examples of using Kro with AWS"
},
"collapsible": true,
"collapsed": true
}
File renamed without changes.
10 changes: 10 additions & 0 deletions website/docs/examples/azure/category.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"label": "Azure Examples",
"position": 500,
"link": {
"type": "generated-index",
"description": "Examples of using Kro with Microsoft Azure"
},
"collapsible": true,
"collapsed": true
}
10 changes: 10 additions & 0 deletions website/docs/examples/basic/category.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"label": "Basic Examples",
"position": 100,
"link": {
"type": "generated-index",
"description": "Basic examples to get started with Kro"
},
"collapsible": true,
"collapsed": true
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions website/docs/examples/gcp/category.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"label": "GCP Examples",
"position": 400,
"link": {
"type": "generated-index",
"description": "Examples of using Kro with Google Cloud Platform"
},
"collapsible": true,
"collapsed": true
}
Loading

0 comments on commit cba914e

Please sign in to comment.