-
Notifications
You must be signed in to change notification settings - Fork 14
[On hold] Refactored postgreSqlDatabases to handle secrets properly #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
03332d5
ae59b86
b2b59c0
c2abc53
5afd18f
745211a
13a3ab0
2344302
ab4d35a
383447f
b80c3e7
2c61ba4
a454b81
70145b0
d6311ce
ce88b7a
5a4cdcb
fed1664
9ea10f2
7b974f3
dd546ef
46749af
6933cbf
ac56adc
daa624a
037abb8
8f0bb70
0e5e202
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,44 @@ | ||
| @description('Information about what resource is calling this Recipe. Generated by Radius.') | ||
| extension kubernetes with { | ||
| namespace: namespace | ||
| kubeConfig: '' | ||
| } as kubernetes | ||
|
|
||
| ////////////////////////////////////////// | ||
| // Common Radius variables | ||
| ////////////////////////////////////////// | ||
|
|
||
| param context object | ||
|
|
||
| @description('Name of the PostgreSQL database. Defaults to the name of the Radius resource.') | ||
| param database string = context.resource.name | ||
| var resourceName = context.resource.name | ||
| var namespace = context.runtime.kubernetes.namespace | ||
| var resourceProperties = context.resource.properties ?? {} | ||
|
|
||
| // Extract last segment from environment path for labels | ||
| var environmentId = resourceProperties.?environment ?? '' | ||
| var environmentParts = environmentId != '' ? split(environmentId, '/') : [] | ||
| var environmentLabel = length(environmentParts) > 0 | ||
| ? environmentParts[length(environmentParts) - 1] | ||
| : '' | ||
|
|
||
| @description('PostgreSQL username') | ||
| param user string = 'postgres' | ||
| // Extract resource group name | ||
| // Index 4 is the resource group name | ||
| var resourceGroupName = split(context.resource.id, '/')[4] | ||
|
|
||
| @description('PostgreSQL password') | ||
| @secure() | ||
| #disable-next-line secure-parameter-default | ||
| param password string = uniqueString(context.resource.id) | ||
| // Application name (safe) | ||
| var applicationName = context.application != null ? context.application.name : '' | ||
|
|
||
| // Common labels | ||
| var labels = { | ||
| 'radapp.io/resource': resourceName | ||
| 'radapp.io/application': applicationName | ||
| 'radapp.io/environment': environmentLabel | ||
| 'radapp.io/resource-type': replace(context.resource.type, '/', '-') | ||
| 'radapp.io/resource-group': resourceGroupName | ||
| } | ||
|
|
||
| @description('Tag to pull for the postgres container image.') | ||
| param tag string = '16-alpine' | ||
| ////////////////////////////////////////// | ||
| // PostgreSQL variables | ||
| ////////////////////////////////////////// | ||
|
|
||
| @description('Memory limits for the PostgreSQL container') | ||
| var memory ={ | ||
|
|
@@ -28,63 +53,75 @@ var memory ={ | |
| } | ||
| } | ||
|
|
||
| extension kubernetes with { | ||
| kubeConfig: '' | ||
| namespace: context.runtime.kubernetes.namespace | ||
| } as kubernetes | ||
|
|
||
| var uniqueName = 'postgres-${uniqueString(context.resource.id)}' | ||
| var port = 5432 | ||
|
|
||
| // Based on https://hub.docker.com/_/postgres/ | ||
| // Get the secret reference. Should be only a single connected resource. | ||
| var radiusConnectionsMap = context.resource.?connections ?? {} | ||
| var radiusConnectionList = items(radiusConnectionsMap) | ||
| var radiusFirstConnection = length(radiusConnectionList) > 0 ? radiusConnectionList[0].value : null | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would the secret be the only connection? Or if there are multiple, why would it be the first connection? Maybe a comment here would help explain.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the challenge with modeling the secret as a connection. @Reshrahim highlighted an inconsistency in how we are handling secrets. @lakshmimsft's approach was to use connections from a database resource to a secret. We got some things for free, but I'll have to let her comment on what exactly the benefits of using connections was. However, in the Containers Resource Type, @sk593 used the secretName and key which is more Kubernetes-like. This appears more straightforward since there can only be one secretName and key. Unlike connections which could be anything. We should be consistent. |
||
| var radiusSecretName = radiusFirstConnection != null ? (radiusFirstConnection.?name ?? null) : null | ||
|
|
||
|
|
||
| ////////////////////////////////////////// | ||
| // PostgreSQL variables | ||
| ////////////////////////////////////////// | ||
|
|
||
| resource postgresql 'apps/Deployment@v1' = { | ||
| metadata: { | ||
| name: uniqueName | ||
| name: resourceName | ||
| namespace: namespace | ||
| labels: labels | ||
| } | ||
| spec: { | ||
| selector: { | ||
| matchLabels: { | ||
| app: 'postgresql' | ||
| resource: context.resource.name | ||
| app: 'postgres' | ||
| } | ||
| } | ||
| 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 | ||
| labels: union(labels, { | ||
| app: 'postgres' | ||
| }) | ||
| } | ||
| } | ||
| spec: { | ||
| containers: [ | ||
| { | ||
| // This container is the running postgresql instance. | ||
| name: 'postgres' | ||
| image: 'postgres:${tag}' | ||
| ports: [ | ||
| { | ||
| containerPort: port | ||
| } | ||
| ] | ||
| image: 'postgres:16-alpine' | ||
| resources: { | ||
| requests: { | ||
| memory: memory[context.resource.properties.size].memoryRequest | ||
| } | ||
| } | ||
| ports: [ | ||
| { | ||
| containerPort: port | ||
| } | ||
| ] | ||
| env: [ | ||
| { | ||
| name: 'POSTGRES_USER' | ||
| value: user | ||
| valueFrom: { | ||
| secretKeyRef: { | ||
| name: radiusSecretName | ||
| key: 'username' | ||
| } | ||
| } | ||
| } | ||
| { | ||
| name: 'POSTGRES_PASSWORD' | ||
| value: password | ||
| valueFrom: { | ||
| secretKeyRef: { | ||
| name: radiusSecretName | ||
| key: 'password' | ||
| } | ||
| } | ||
| } | ||
| { | ||
| name: 'POSTGRES_DB' | ||
| value: database | ||
| value: 'postgres_db' | ||
| } | ||
| ] | ||
| } | ||
|
|
@@ -96,16 +133,14 @@ resource postgresql 'apps/Deployment@v1' = { | |
|
|
||
| resource svc 'core/Service@v1' = { | ||
| metadata: { | ||
| name: uniqueName | ||
| labels: { | ||
| name: uniqueName | ||
| } | ||
| name: resourceName | ||
| namespace: namespace | ||
| labels: labels | ||
| } | ||
| spec: { | ||
| type: 'ClusterIP' | ||
| selector: { | ||
| app: 'postgresql' | ||
| resource: context.resource.name | ||
| app: 'postgres' | ||
| } | ||
| ports: [ | ||
| { | ||
|
|
@@ -115,6 +150,10 @@ resource svc 'core/Service@v1' = { | |
| } | ||
| } | ||
|
|
||
| ////////////////////////////////////////// | ||
| // Output Radius result | ||
| ////////////////////////////////////////// | ||
|
|
||
| output result object = { | ||
| resources: [ | ||
| '/planes/kubernetes/local/namespaces/${svc.metadata.namespace}/providers/core/Service/${svc.metadata.name}' | ||
|
|
@@ -123,11 +162,6 @@ output result object = { | |
| values: { | ||
| host: '${svc.metadata.name}.${svc.metadata.namespace}.svc.cluster.local' | ||
| port: port | ||
| database: database | ||
| username: user | ||
| database: 'postgres_db' | ||
| } | ||
| secrets: { | ||
| #disable-next-line outputs-should-not-contain-secrets | ||
| password: password | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add an example for the
credentialssecrets resource declaration here?