Skip to content

Commit

Permalink
Add a PostgreSQL Extender Recipe (#48)
Browse files Browse the repository at this point in the history
* Add extender Recipe

Signed-off-by: Aaron Crawfis <[email protected]>

* Update README

Signed-off-by: Aaron Crawfis <[email protected]>

---------

Signed-off-by: Aaron Crawfis <[email protected]>
  • Loading branch information
AaronCrawfis authored Dec 1, 2023
1 parent 2ba375e commit 70fc625
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 11 deletions.
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,36 @@ The [local-dev](/local-dev) directory contains lightweight Recipes for developme
| Recipe | Resource | Description | Template Path |
|--------|----------|-------------|---------------|
| [`local-dev/daprpubsubbrokers`](/local-dev/pubsubbrokers.bicep) | `Applications.Dapr/pubSubBrokers` | A lightweight container running the `redis` image and a Redis Dapr Pub/Sub component for development purposes. | `radius.azurecr.io/recipes/local-dev/daprpubsubbrokers:TAG` |
| [`local-dev/daprstatestores`](/local-dev/statestores.bicep) | `Applications.Dapr/stateStores` |A lightweight container running the `redis` image and a Redis Dapr state store component for development purposes. | `radius.azurecr.io/recipes/local-dev/daprstatestores:TAG` |
| [`local-dev/rabbitmqmessagequeues`](/local-dev/rabbitmqmessagequeues.bicep) | `Applications.Messaging/rabbitMQQueues` |A lightweight container running the `rabbitmq` image for development purposes. | `radius.azurecr.io/recipes/local-dev/rabbitmqmessagequeues:TAG` |
| [`local-dev/rediscaches`](/local-dev/rediscaches.bicep) | `Applications.Datastores/redisCaches` |A lightweight container running the `redis` image for development purposes. | `radius.azurecr.io/recipes/local-dev/rediscaches:TAG` |
| [`local-dev/mongodatabases`](/local-dev/mongodatabases.bicep) | `Applications.Datastores/mongoDatabases` |A lightweight container running the `mongo` image for development purposes. | `radius.azurecr.io/recipes/local-dev/mongodatabases:TAG` |
| [`local-dev/sqldatabases`](/local-dev/sqldatabases.bicep) | `Applications.Datastores/sqlDatabases` |A lightweight container running the `azure-sql-edge` image for development purposes. | `radius.azurecr.io/recipes/local-dev/sqldatabases:TAG` |
| [`local-dev/daprpubsubbrokers`](/local-dev/pubsubbrokers.bicep) | `Applications.Dapr/pubSubBrokers` | A lightweight container running the `redis` image and a Redis Dapr Pub/Sub component for development purposes. | `ghcr.io/radius-project/recipes/local-dev/daprpubsubbrokers:TAG` |
| [`local-dev/daprstatestores`](/local-dev/statestores.bicep) | `Applications.Dapr/stateStores` |A lightweight container running the `redis` image and a Redis Dapr state store component for development purposes. | `ghcr.io/radius-project/recipes/local-dev/daprstatestores:TAG` |
| [`local-dev/extender-postgresql`](/local-dev/extender-postgresql.bicep) | `Applications.Core/extenders` |A lightweight container running the `postgres` image for development purposes. Used with the Radius extender resource. | `ghcr.io/radius-project/recipes/local-dev/extender-postgresql:TAG` |
| [`local-dev/rabbitmqmessagequeues`](/local-dev/rabbitmqmessagequeues.bicep) | `Applications.Messaging/rabbitMQQueues` |A lightweight container running the `rabbitmq` image for development purposes. | `ghcr.io/radius-project/recipes/local-dev/rabbitmqmessagequeues:TAG` |
| [`local-dev/rediscaches`](/local-dev/rediscaches.bicep) | `Applications.Datastores/redisCaches` |A lightweight container running the `redis` image for development purposes. | `ghcr.io/radius-project/recipes/local-dev/rediscaches:TAG` |
| [`local-dev/mongodatabases`](/local-dev/mongodatabases.bicep) | `Applications.Datastores/mongoDatabases` |A lightweight container running the `mongo` image for development purposes. | `ghcr.io/radius-project/recipes/local-dev/mongodatabases:TAG` |
| [`local-dev/sqldatabases`](/local-dev/sqldatabases.bicep) | `Applications.Datastores/sqlDatabases` |A lightweight container running the `azure-sql-edge` image for development purposes. | `ghcr.io/radius-project/recipes/local-dev/sqldatabases:TAG` |

### azure

The [azure](/azure) directory contains Recipes for Azure resources. They are configurable via parameters, with the default values optimizing for cost and security.

| Recipe | Resource | Description | Template Path |
|--------|----------|-------------|---------------|
| [`azure/rediscaches`](/azure/rediscaches.bicep) | `Applications.Datastores/redisCaches` | An Azure Cache for Redis resource with a configurable size and SKU. | `radius.azurecr.io/recipes/azure/rediscaches:TAG` |
| [`azure/rediscaches`](/azure/rediscaches.bicep) | `Applications.Datastores/redisCaches` | An Azure Cache for Redis resource with a configurable size and SKU. | `ghcr.io/radius-project/recipes/azure/rediscaches:TAG` |

### aws

The [aws](/aws) directory contains Recipes for AWS resources. They are configurable via parameters, with the default values optimizing for cost and security.

| Recipe | Resource | Description | Template Path |
|--------|----------|-------------|---------------|
| [`aws/rediscaches`](/aws/rediscaches.bicep) | `Applications.Datastores/redisCaches` | An AWS MemoryDB resource with a configurable size and SKU. | `radius.azurecr.io/recipes/aws/rediscaches:TAG` |
| [`aws/rediscaches`](/aws/rediscaches.bicep) | `Applications.Datastores/redisCaches` | An AWS MemoryDB resource with a configurable size and SKU. | `ghcr.io/radius-project/recipes/aws/rediscaches:TAG` |

## Versioning and Tags

Recipes are versioned using [semantic versioning](https://semver.org/). Each Recipe is tagged with its version number, and the `latest` tag points to the latest version of each Recipe. For example, Radius v0.21 uses the `0.21` tag for each Recipe:

```
radius.azurecr.io/recipes/azure/rediscaches:0.21
ghcr.io/radius-project/recipes/azure/rediscaches:0.21
```

### Patching
Expand All @@ -61,7 +62,7 @@ To use a community recipe from this repo, simply use [`rad recipe register`](htt
rad recipe register azure \
--environment myenv \
--template-kind bicep \
--template-path "radius.azurecr.io/recipes/azure/rediscaches:TAG" \
--template-path "ghcr.io/radius-project/recipes/azure/rediscaches:TAG" \
--resource-type "Applications.Datastores/redisCaches"
```

Expand All @@ -78,7 +79,7 @@ resource myenv 'Applications.Core/environments' = {
'Applications.Core/redisCaches': {
'azure': {
template-kind: 'bicep'
template-path: 'radius.azurecr.io/recipes/azure/rediscaches:0.21'
template-path: 'ghcr.io/radius-project/recipes/azure/rediscaches:0.21'
}
}
}
Expand Down
149 changes: 149 additions & 0 deletions local-dev/extender-postgresql.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
Copyright 2023 The Radius Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

@description('Information about what resource is calling this Recipe. Generated by Radius. For more information visit https://docs.radapp.dev/operations/custom-recipes/')
param context object

@description('Name of the PostgreSQL database. Defaults to the name of the Radius resource.')
param database string = context.resource.name

@description('PostgreSQL username')
param user string = 'postgres'

@description('PostgreSQL password')
@secure()
#disable-next-line secure-parameter-default
param password string = 'P@ssword1234$$'

@description('Tag to pull for the postgres container image.')
param tag string = '16-alpine'

@description('Memory request for the postgres deployment.')
param memoryRequest string = '512Mi'

@description('Memory limit for the postgres deployment')
param memoryLimit string = '1024Mi'

import kubernetes as kubernetes {
kubeConfig: ''
namespace: context.runtime.kubernetes.namespace
}

var uniqueName = 'postgres-${uniqueString(context.resource.id)}'
var port = 5432

// Based on https://hub.docker.com/_/postgres/
resource postgresql 'apps/Deployment@v1' = {
metadata: {
name: uniqueName
}
spec: {
selector: {
matchLabels: {
app: 'postgresql'
resource: context.resource.name
}
}
template: {
metadata: {
labels: {
app: 'postgresql'
resource: context.resource.name

// Label pods with the application name so `rad run` can find the logs.
'radapp.io/application': context.application == null ? '' : context.application.name
}
}
spec: {
containers: [
{
// This container is the running postgresql instance.
name: 'postgres'
image: 'postgres:${tag}'
ports: [
{
containerPort: port
}
]
resources: {
requests: {
memory: memoryRequest
}
limits: {
memory: memoryLimit
}
}
env: [
{
name: 'POSTGRES_USER'
value: user
}
{
name: 'POSTGRES_PASSWORD'
value: password
}
{
name: 'POSTGRES_DB'
value: database
}
]
}
]
}
}
}
}

resource svc 'core/Service@v1' = {
metadata: {
name: uniqueName
labels: {
name: uniqueName
}
}
spec: {
type: 'ClusterIP'
selector: {
app: 'postgresql'
resource: context.resource.name
}
ports: [
{
port: port
}
]
}
}

output result object = {
// This workaround is needed because the deployment engine omits Kubernetes resources from its output.
// This allows Kubernetes resources to be cleaned up when the resource is deleted.
// Once this gap is addressed, users won't need to do this.
resources: [
'/planes/kubernetes/local/namespaces/${svc.metadata.namespace}/providers/core/Service/${svc.metadata.name}'
'/planes/kubernetes/local/namespaces/${postgresql.metadata.namespace}/providers/apps/Deployment/${postgresql.metadata.name}'
]
values: {
host: '${svc.metadata.name}.${svc.metadata.namespace}.svc.cluster.local'
port: port
database: database
username: user
}
secrets: {
#disable-next-line outputs-should-not-contain-secrets
password: password
}
}

0 comments on commit 70fc625

Please sign in to comment.