Skip to content

Commit e5f63f5

Browse files
committed
Reworked the example to support COS mounts
1 parent a4cc3c7 commit e5f63f5

File tree

7 files changed

+145
-352
lines changed

7 files changed

+145
-352
lines changed

gallery/README.md

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,19 @@ $ brew install jq
2929

3030
Login into IBM Cloud using the CLI
3131
```
32-
$ ibmcloud login
32+
> ibmcloud login
3333
```
3434

3535
Choose a proper region and resource group. In this script, we'll use the Frankfurt region and the default resource group.
3636
```
37-
$ ibmcloud target -r eu-de
37+
> REGION=eu-de
38+
39+
> ibmcloud target -r $REGION
3840
```
3941

4042
Create a new project
4143
```
42-
$ ibmcloud ce project create --name gallery
44+
> ibmcloud ce project create --name gallery
4345
4446
Creating project 'gallery'...
4547
ID for project 'gallery' is '91efff97-1001-4144-997a-744ec8009303'.
@@ -51,8 +53,10 @@ OK
5153
Read the project guid from the CLI output and store it in a local variable.
5254
We'll need it later on to configure the bucket.
5355
```
54-
$ export CE_PROJECT_GUID=$(ibmcloud ce project current --output json|jq -r '.guid')
55-
$ echo "CE_PROJECT_GUID: $CE_PROJECT_GUID"
56+
> export CE_PROJECT_GUID=$(ibmcloud ce project current --output json|jq -r '.guid')
57+
> echo "CE_PROJECT_GUID: $CE_PROJECT_GUID"
58+
59+
CE_PROJECT_GUID: 91efff97-1001-4144-997a-744ec8009303
5660
```
5761

5862
Once the project has become active, you are good to proceed with the next step.
@@ -61,7 +65,7 @@ Once the project has become active, you are good to proceed with the next step.
6165

6266
Create an application
6367
```
64-
$ ibmcloud ce app create --name gallery --image icr.io/codeengine/gallery
68+
> ibmcloud ce app create --name gallery --image icr.io/codeengine/gallery
6569
6670
Creating application 'gallery'...
6771
Configuration 'gallery' is waiting for a Revision to become ready.
@@ -78,7 +82,7 @@ to compose an image gallery by either dragging the sample images into
7882
the preview box and click `Add`. Furthermore, it is possible to upload images
7983
from the local workstation, by clicking `Upload`.
8084

81-
Note, that the image gallery will reset, once the browser reloads.
85+
Note, that the image gallery will reset, once the app instance tears down.
8286

8387
![Gallery application](./docs/application-in-memory.png)
8488

@@ -91,8 +95,8 @@ in this case IBM Cloud Object Storage.
9195

9296
First we'll create a new instance of that service:
9397
```
94-
$ ibmcloud resource service-instance-create gallery-cos \
95-
cloud-object-storage standard global
98+
> ibmcloud resource service-instance-create gallery-cos \
99+
cloud-object-storage standard global -d premium-global-deployment-iam
96100
97101
Creating service instance gallery-cos in resource group default of account John Doe's Account as [email protected]...
98102
OK
@@ -118,13 +122,13 @@ From this output you'll need to save the `ID` value for a future command.
118122
So to make life easier, let's save it as an environment variable:
119123

120124
```
121-
$ export COS_ID=$(ibmcloud resource service-instance gallery-cos --output json|jq -r '.[]|.crn')
122-
$ echo "COS_ID: $COS_ID"
125+
> export COS_INSTANCE_ID=$(ibmcloud resource service-instance gallery-cos --output json|jq -r '.[]|.id')
126+
> echo "COS_INSTANCE_ID: $COS_INSTANCE_ID"
123127
```
124128

125129
Let's direct all COS CLI uses to our COS instance:
126130
```
127-
$ ibmcloud cos config crn --crn $COS_ID --force
131+
> ibmcloud cos config crn --crn $COS_INSTANCE_ID --force
128132
129133
Saving new Service Instance ID...
130134
OK
@@ -135,63 +139,75 @@ Now, let's use the "IAM" authentication method which will use the same API Key
135139
that the rest of our CLI commands will use:
136140

137141
```
138-
$ ibmcloud cos config auth --method IAM
142+
> ibmcloud cos config auth --method IAM
139143
140144
OK
141145
Successfully switched to IAM-based authentication. The program will access your Cloud Object Storage account using your IAM Credentials.
142146
```
143147

148+
Last, let's set the region for the bucket
149+
```
150+
> ibmcloud cos config region --region ${REGION}
151+
152+
OK
153+
Successfully saved default region. The program will look for buckets in the region eu-de.
154+
```
155+
144156
Next, let's go ahead and create a new bucket into which our data will be stored.
145157
To do this you'll need to provide a unique name for your bucket. It needs to be
146158
globally unique across all buckets in the IBM Cloud. In the command below
147159
we'll use our project's ID appended with "-gallery", but you can technically use any value you want as long as it's unique. Let's save that name in an environment variable for
148160
easy use:
149161

150162
```
151-
$ export BUCKET="$CE_PROJECT_GUID-gallery"
152-
$ echo "BUCKET: $BUCKET"
163+
> export BUCKET="$CE_PROJECT_GUID-gallery"
164+
> echo "BUCKET: $BUCKET"
153165
```
154166

155-
Now let's ask COS to create our bucket:
167+
Now, let's ask COS to create our bucket:
156168

157169
```
158-
$ ibmcloud cos bucket-create --bucket $BUCKET
170+
> ibmcloud cos bucket-create --bucket $BUCKET
159171
160172
OK
161173
Details about bucket 91efff97-1001-4144-997a-744ec8009303-gallery:
162174
Region: eu-de
163175
Class: Standard
164176
```
165177

166-
To complete this setup, we'll need to adjust the application configuration and make it aware of the persistence store.
178+
In order to enable the Code Engine app to interact with the COS bucket, we'll create a service credential that contains HMAC credentials, store it in a Code Engine secret and create a persistent data store so that Code Engine components can mount the bucket.
167179
```
168-
$ ibmcloud ce app update --name gallery \
169-
--env BUCKET=$BUCKET \
170-
--scale-down-delay 3600
180+
> COS_HMAC_CREDENTIALS=$(ibmcloud resource service-key-create gallery-cos-credentials Writer --instance-id $COS_INSTANCE_ID --parameters '{"HMAC":true}' --output JSON)
171181
172-
Updating application 'gallery' to latest revision.
173-
Traffic is not yet migrated to the latest revision.
174-
Ingress has not yet been reconciled.
175-
Waiting for load balancer to be ready.
176-
Run 'ibmcloud ce application get -n gallery' to check the application status.
182+
> ibmcloud ce secret create --name gallery-cos-hmac-credentials \
183+
--format hmac \
184+
--access-key-id "$(echo "$COS_HMAC_CREDENTIALS"|jq -r '.credentials.cos_hmac_keys.access_key_id')" \
185+
--secret-access-key "$(echo "$COS_HMAC_CREDENTIALS"|jq -r '.credentials.cos_hmac_keys.secret_access_key')"
186+
187+
Creating hmac_auth secret 'gallery-cos-hmac-credentials'...
177188
OK
178189
179-
https://gallery.172utxcdky5l.eu-de.codeengine.appdomain.cloud
190+
> ibmcloud ce persistentdatastore create --name gallery-cos-pds --cos-bucket-name $BUCKET --cos-access-secret gallery-cos-hmac-credentials
191+
192+
Successfully created persistent data store named 'gallery-cos-pds'.
193+
OK
180194
```
181195

182-
Furthermore, we'll create a service binding between the application and the COS instance, which will expose
183-
credentials to the application allowing it to read and write to the COS instance.
196+
To complete this setup, we'll need to adjust the application configuration and make it aware of the persistence store.
184197
```
185-
$ ibmcloud ce app bind --name gallery --service-instance gallery-cos
198+
> ibmcloud ce app update --name gallery \
199+
--mount-data-store /mnt/bucket=gallery-cos-pds: \
200+
--env MOUNT_LOCATION=/mnt/bucket \
201+
--scale-down-delay 3600
186202
187-
Binding service instance...
188-
Status: Done
189-
Waiting for application revision to become ready...
190-
The Configuration is still working to reflect the latest desired specification.
203+
Updating application 'gallery' to latest revision.
191204
Traffic is not yet migrated to the latest revision.
192205
Ingress has not yet been reconciled.
193206
Waiting for load balancer to be ready.
207+
Run 'ibmcloud ce application get -n gallery' to check the application status.
194208
OK
209+
210+
https://gallery.172utxcdky5l.eu-de.codeengine.appdomain.cloud
195211
```
196212

197213
Open the gallery application in your browser. Notice the Gallery title on the right-hand side has slightly changed. It now says `My Gallery hosted on IBM Cloud Object Storage`. Play around with the gallery by adding a few images. Notice, that the gallery images re-appear after reloading the page.
@@ -243,7 +259,7 @@ In order to allow the function to read and write to the bucket, we'll need to cr
243259

244260
List all service credentials of the Object Storage instance:
245261
```
246-
$ ibmcloud resource service-keys --instance-id $COS_ID
262+
$ ibmcloud resource service-keys --instance-id $COS_INSTANCE_ID
247263
248264
Retrieving all service keys in resource group default under account John Does's Account as [email protected]...
249265
OK
@@ -253,7 +269,7 @@ gallery-ce-service-binding-prw1t active Fri Sep 8 07:56:19 UTC 2023
253269

254270
Extract the name of the service access secret, that has been created for the app
255271
```
256-
$ export COS_SERVICE_CREDENTIAL=$(ibmcloud resource service-keys --instance-id $COS_ID --output json|jq -r '.[0].name')
272+
$ export COS_SERVICE_CREDENTIAL=$(ibmcloud resource service-keys --instance-id $COS_INSTANCE_ID --output json|jq -r '.[0].name')
257273
$ echo "COS_SERVICE_CREDENTIAL: $COS_SERVICE_CREDENTIAL"
258274
```
259275

gallery/app/Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
FROM registry.access.redhat.com/ubi9/nodejs-20:latest AS build-env
1+
FROM registry.access.redhat.com/ubi10/nodejs-22-minimal:latest AS build-env
22
WORKDIR /app
33

44
# Define which files should be copied into the container image
55
COPY *.js .
66
COPY page.* .
77
COPY pictures/* pictures/
88
COPY package.json .
9+
RUN mkdir tmp/
910

1011
# Load all dependencies
1112
RUN npm install
1213

1314
# Use a small distroless image for as runtime image
14-
FROM gcr.io/distroless/nodejs20-debian12
15+
FROM gcr.io/distroless/nodejs22-debian12
1516
COPY --from=build-env /app /app
1617
WORKDIR /app
1718
EXPOSE 8080

0 commit comments

Comments
 (0)