|
| 1 | +/* |
| 2 | +Copyright 2023 The Radius Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +@description('Information about what resource is calling this Recipe. Generated by Radius. For more information visit https://docs.radapp.dev/operations/custom-recipes/') |
| 18 | +param context object |
| 19 | + |
| 20 | +@description('Tag to pull for the redis container image.') |
| 21 | +param tag string = '7' |
| 22 | + |
| 23 | +@description('Memory request for the redis deployment.') |
| 24 | +param memoryRequest string = '128Mi' |
| 25 | + |
| 26 | +@description('Memory limit for the redis deployment') |
| 27 | +param memoryLimit string = '1024Mi' |
| 28 | + |
| 29 | +extension kubernetes with { |
| 30 | + kubeConfig: '' |
| 31 | + namespace: context.runtime.kubernetes.namespace |
| 32 | +} as kubernetes |
| 33 | + |
| 34 | +var uniqueName = 'daprconfig-${uniqueString(context.resource.id)}' |
| 35 | +var port = 6379 |
| 36 | + |
| 37 | +resource redis 'apps/Deployment@v1' = { |
| 38 | + metadata: { |
| 39 | + name: uniqueName |
| 40 | + } |
| 41 | + spec: { |
| 42 | + selector: { |
| 43 | + matchLabels: { |
| 44 | + app: 'dapr-config-redis' |
| 45 | + resource: context.resource.name |
| 46 | + } |
| 47 | + } |
| 48 | + template: { |
| 49 | + metadata: { |
| 50 | + labels: { |
| 51 | + app: 'dapr-config-redis' |
| 52 | + resource: context.resource.name |
| 53 | + |
| 54 | + // Label pods with the application name so `rad run` can find the logs. |
| 55 | + 'radapp.io/application': context.application == null ? '' : context.application.name |
| 56 | + } |
| 57 | + } |
| 58 | + spec: { |
| 59 | + containers: [ |
| 60 | + { |
| 61 | + // This container is the running redis instance. |
| 62 | + name: 'redis' |
| 63 | + image: 'redis:${tag}' |
| 64 | + ports: [ |
| 65 | + { |
| 66 | + containerPort: port |
| 67 | + } |
| 68 | + ] |
| 69 | + resources: { |
| 70 | + requests: { |
| 71 | + memory: memoryRequest |
| 72 | + } |
| 73 | + limits: { |
| 74 | + memory: memoryLimit |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + ] |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +resource svc 'core/Service@v1' = { |
| 85 | + metadata: { |
| 86 | + name: uniqueName |
| 87 | + } |
| 88 | + spec: { |
| 89 | + type: 'ClusterIP' |
| 90 | + selector: { |
| 91 | + app: 'dapr-config-redis' |
| 92 | + resource: context.resource.name |
| 93 | + } |
| 94 | + ports: [ |
| 95 | + { |
| 96 | + port: port |
| 97 | + } |
| 98 | + ] |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +var daprType = 'configuration.redis' |
| 103 | +var daprVersion = 'v1' |
| 104 | + |
| 105 | +resource daprComponent 'dapr.io/Component@v1alpha1' = { |
| 106 | + metadata: { |
| 107 | + name: context.resource.name |
| 108 | + } |
| 109 | + spec: { |
| 110 | + type: daprType |
| 111 | + version: daprVersion |
| 112 | + metadata: [ |
| 113 | + { |
| 114 | + name: 'redisHost' |
| 115 | + value: '${svc.metadata.name}.${svc.metadata.namespace}.svc.cluster.local:${port}' |
| 116 | + } |
| 117 | + { |
| 118 | + name: 'redisPassword' |
| 119 | + value: '' |
| 120 | + } |
| 121 | + ] |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +output result object = { |
| 126 | + // This workaround is needed because the deployment engine omits Kubernetes resources from its output. |
| 127 | + // This allows Kubernetes resources to be cleaned up when the resource is deleted. |
| 128 | + // Once this gap is addressed, users won't need to do this. |
| 129 | + resources: [ |
| 130 | + '/planes/kubernetes/local/namespaces/${svc.metadata.namespace}/providers/core/Service/${svc.metadata.name}' |
| 131 | + '/planes/kubernetes/local/namespaces/${redis.metadata.namespace}/providers/apps/Deployment/${redis.metadata.name}' |
| 132 | + '/planes/kubernetes/local/namespaces/${daprComponent.metadata.namespace}/providers/dapr.io/Component/${daprComponent.metadata.name}' |
| 133 | + ] |
| 134 | + values: { |
| 135 | + type: daprType |
| 136 | + version: daprVersion |
| 137 | + metadata: daprComponent.spec.metadata |
| 138 | + } |
| 139 | +} |
0 commit comments