Skip to content

Commit b3d635c

Browse files
authored
blog: building a compound promise (#193)
Signed-off-by: Cat Morris <[email protected]>
1 parent 7c2df2b commit b3d635c

File tree

17 files changed

+1024
-1
lines changed

17 files changed

+1024
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: internal.platform.io/v1
2+
kind: RubyApp
3+
metadata:
4+
name: myapp
5+
spec:
6+
image: syntasso/example-rails-app:v1.0.0
7+
database:
8+
driver: postgresql
9+
cache:
10+
driver: redis

assets/rubyapp-promise/promise.yaml

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
apiVersion: platform.kratix.io/v1alpha1
2+
kind: Promise
3+
metadata:
4+
creationTimestamp: null
5+
name: rubyapp
6+
spec:
7+
requiredPromises:
8+
- name: postgresql
9+
version: v1.0.0-beta.2
10+
- name: redis
11+
version: v0.1.0
12+
- name: runtime
13+
version: v1.0.0
14+
destinationSelectors:
15+
- matchLabels:
16+
environment: platform
17+
api:
18+
apiVersion: apiextensions.k8s.io/v1
19+
kind: CustomResourceDefinition
20+
metadata:
21+
creationTimestamp: null
22+
name: rubyapps.internal.platform.io
23+
spec:
24+
group: internal.platform.io
25+
names:
26+
kind: RubyApp
27+
plural: rubyapps
28+
singular: rubyapp
29+
scope: Namespaced
30+
versions:
31+
- name: v1
32+
schema:
33+
openAPIV3Schema:
34+
properties:
35+
spec:
36+
properties:
37+
cache:
38+
properties:
39+
driver:
40+
type: string
41+
enum: [ redis ]
42+
type: object
43+
database:
44+
properties:
45+
driver:
46+
type: string
47+
enum: [ postgresql ]
48+
type: object
49+
image:
50+
type: string
51+
type: object
52+
type: object
53+
served: true
54+
storage: true
55+
status:
56+
acceptedNames:
57+
kind: ""
58+
plural: ""
59+
conditions: null
60+
storedVersions: null
61+
workflows:
62+
promise: {}
63+
resource:
64+
configure:
65+
- apiVersion: platform.kratix.io/v1alpha1
66+
kind: Pipeline
67+
metadata:
68+
name: instance
69+
spec:
70+
containers:
71+
- image: ghcr.io/syntasso/kratix-docs/rubyapp-promise:v1.0.0
72+
name: deploy-resources
73+
status: {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM "alpine"
2+
3+
RUN apk update && apk add --no-cache yq ruby
4+
5+
ADD scripts/pipeline.rb /usr/bin/pipeline.rb
6+
ADD resources resources
7+
8+
RUN chmod +x /usr/bin/pipeline.rb
9+
10+
CMD [ "sh", "-c", "pipeline.rb" ]
11+
ENTRYPOINT []
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: marketplace.kratix.io/v1alpha1
2+
kind: postgresql
3+
metadata:
4+
name: example
5+
namespace: default
6+
spec:
7+
env: dev
8+
teamId: acid
9+
dbName: bestdb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: marketplace.kratix.io/v1alpha1
2+
kind: redis
3+
metadata:
4+
name: example
5+
namespace: default
6+
spec:
7+
size: small
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: marketplace.kratix.io/v1alpha1
2+
kind: Runtime
3+
metadata:
4+
name: example-runtime
5+
namespace: default
6+
spec:
7+
lifecycle: dev
8+
image: syntasso/website
9+
servicePort: 8000
10+
replicas: 1
11+
applicationEnv:
12+
- name: hello
13+
value: from-env
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'yaml'
4+
5+
# Read the input YAML file
6+
input_yaml = YAML.load_file('/kratix/input/object.yaml')
7+
8+
# Extract values from input
9+
app_name = input_yaml['metadata']['name']
10+
namespace = input_yaml['metadata']['namespace']
11+
app_image = input_yaml['spec']['image']
12+
13+
# Create the Runtime request
14+
runtime_request = {
15+
'apiVersion' => 'marketplace.kratix.io/v1alpha1',
16+
'kind' => 'Runtime',
17+
'metadata' => {
18+
'name' => app_name,
19+
'namespace' => namespace
20+
},
21+
'spec' => {
22+
'image' => app_image,
23+
'replicas' => 1,
24+
'servicePort' => 80,
25+
'applicationEnv' => [
26+
{ 'name' => 'PORT', 'value' => '80' }
27+
]
28+
}
29+
}
30+
31+
database_driver = input_yaml.dig('spec', 'database', 'driver')
32+
33+
if database_driver == "postgresql" then
34+
# The PostgreSQL Request
35+
database_request = {
36+
'apiVersion' => 'marketplace.kratix.io/v1alpha1',
37+
'kind' => 'postgresql',
38+
'metadata' => {
39+
'name' => app_name + '-db',
40+
'namespace' => namespace
41+
},
42+
'spec' => {
43+
'env' => 'dev',
44+
'teamId' => app_name,
45+
'dbName' => app_name + '-db'
46+
}
47+
}
48+
49+
# This is the secret name the PostgreSQL promise will generate
50+
secret_name="#{app_name}.#{app_name}-#{app_name}-db-postgresql.credentials.postgresql.acid.zalan.do"
51+
52+
## Injecting the secrets into the application env
53+
runtime_request['spec']['applicationEnv'].push({
54+
'name' => 'PGHOST',
55+
'value' => '${app_name}-${app_name}-db-postgresql.default.svc.cluster.local'
56+
}, {
57+
'name' => 'DBNAME',
58+
'value' => '${app_name}-db'
59+
}, {
60+
'name' => 'PGUSER',
61+
'valueFrom' => {
62+
'secretKeyRef' => { 'name' => secret_name, 'key' => 'username' }
63+
}
64+
}, {
65+
'name' => 'PGPASSWORD',
66+
'valueFrom' => {
67+
'secretKeyRef' => { 'name' => secret_name, 'key' => 'password' }
68+
}
69+
}
70+
)
71+
72+
File.write('/kratix/output/postgresql-request.yaml', database_request.to_yaml)
73+
end
74+
75+
cache_driver = input_yaml.dig('spec', 'cache', 'driver')
76+
77+
if cache_driver == "redis" then
78+
redis_request = {
79+
'apiVersion' => 'marketplace.kratix.io/v1alpha1',
80+
'kind' => 'redis',
81+
'metadata' => {
82+
'name' => app_name + '-cache',
83+
'namespace' => namespace
84+
},
85+
'spec' => {
86+
'size' => 'small'
87+
}
88+
}
89+
90+
runtime_request['spec']['applicationEnv'].push({
91+
'name' => 'REDIS_URL',
92+
'value' => "redis://rfs-#{app_name}-cache:26379/1"
93+
}, {
94+
'name' => 'REDIS_POOL_SIZE',
95+
'value' => '5'
96+
})
97+
98+
File.write('/kratix/output/redis-request.yaml', redis_request.to_yaml)
99+
end
100+
101+
# Write to Runtime request to the output file
102+
File.write('/kratix/output/runtime-request.yaml', runtime_request.to_yaml)
556 KB
Loading
979 KB
Loading
890 KB
Loading
778 KB
Loading
Loading

0 commit comments

Comments
 (0)