Skip to content

Commit 082d3ea

Browse files
authored
Merge pull request #29 from matrober-uk/openshift-app2
Sample template for running an application in OpenShift
2 parents 6bacebf + 14661b8 commit 082d3ea

File tree

10 files changed

+269
-0
lines changed

10 files changed

+269
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
mq-golang-jms20.test
22
vendor
3+
.DS_Store

openshift-app-sample/Dockerfile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
FROM golang:1.15 as builder
2+
ENV APP_HOME /go/src/openshift-app-sample
3+
RUN mkdir -p /opt/mqm \
4+
&& chmod a+rx /opt/mqm
5+
# Location of the downloadable MQ client package \
6+
ENV RDURL="https://public.dhe.ibm.com/ibmdl/export/pub/software/websphere/messaging/mqdev/redist" \
7+
RDTAR="IBM-MQC-Redist-LinuxX64.tar.gz" \
8+
VRMF=9.2.0.1
9+
# Install the MQ client from the Redistributable package. This also contains the
10+
# header files we need to compile against. Setup the subset of the package
11+
# we are going to keep - the genmqpkg.sh script removes unneeded parts
12+
ENV genmqpkg_incnls=1 \
13+
genmqpkg_incsdk=1 \
14+
genmqpkg_inctls=1
15+
RUN cd /opt/mqm \
16+
&& curl -LO "$RDURL/$VRMF-$RDTAR" \
17+
&& tar -zxf ./*.tar.gz \
18+
&& rm -f ./*.tar.gz \
19+
&& bin/genmqpkg.sh -b /opt/mqm
20+
RUN mkdir -p $APP_HOME
21+
WORKDIR $APP_HOME
22+
COPY src/ .
23+
RUN go build -o openshift-app-sample
24+
25+
FROM golang:1.15
26+
ENV APP_HOME /go/src/openshift-app-sample
27+
# Create the directories the client expects to be present
28+
RUN mkdir -p $APP_HOME \
29+
&& mkdir -p /IBM/MQ/data/errors \
30+
&& mkdir -p /.mqm \
31+
&& chmod -R 777 /IBM \
32+
&& chmod -R 777 /.mqm
33+
WORKDIR $APP_HOME
34+
COPY --chown=0:0 --from=builder $APP_HOME/openshift-app-sample $APP_HOME
35+
COPY --chown=0:0 --from=builder /opt/mqm /opt/mqm
36+
CMD ["./openshift-app-sample"]

openshift-app-sample/README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# IBM MQ Golang application sample for OpenShift
2+
This sample provides a working template that you can use to build your own Golang
3+
application container image that runs in Red Hat OpenShift under the most secure
4+
"Restricted SCC" and uses either the
5+
[IBM MQ Golang](https://github.com/ibm-messaging/mq-golang) or
6+
[IBM MQ Golang JMS](https://github.com/ibm-messaging/mq-golang-jms20) libraries to connect
7+
to an IBM MQ queue manager.
8+
9+
You can build and run your application using the following simple steps;
10+
1. Modify the sample application code to add your business logic
11+
2. Build the application into a Docker container image locally
12+
3. Push the container image to your OpenShift cluster
13+
4. Configure the variables and run the application!
14+
15+
16+
Let's get started!
17+
18+
19+
## Step 1: Modify the application file to add your code
20+
Add your application logic into the [openshift-app-sample/src/main.go](./src/main.go) file using either the
21+
[IBM MQ Golang](https://github.com/ibm-messaging/mq-golang) or
22+
[IBM MQ Golang JMS](https://github.com/ibm-messaging/mq-golang-jms20) interfaces depending
23+
on your preference.
24+
25+
The file contains all the basic details necessary to create a
26+
connection to a queue manager, so you can simply add your logic to send and/or receive
27+
messages at the bottom of the function.
28+
29+
30+
## Step 2: Build the application into a Docker container image locally
31+
Open a command shell to the same directory as the Dockerfile and execute the following command;
32+
```bash
33+
# Make sure you are in the directory where the Dockerfile is
34+
cd $YOUR_GITHUB_PATH/ibm-messaging/mq-golang-jms20/openshift-app-sample
35+
36+
# Run the dockerfile build to create the container image
37+
docker build -t golang-app -f Dockerfile .
38+
```
39+
40+
41+
## Step 3: Push the container image to your OpenShift cluster
42+
This step will vary depending on what sort of OpenShift cluster you are going to deploy to -
43+
for example a registry that is part of the cluster or an external registry that can be
44+
accessed by the cluster.
45+
46+
In this example we are using the IBM Cloud Container Registry (in London) that is hosted
47+
at `uk.icr.io`.
48+
49+
```bash
50+
# Tag the new image against the target registry
51+
docker tag golang-app uk.icr.io/golang-sample/golang-app:1.0
52+
53+
# Push the updated image to your registry
54+
docker push uk.icr.io/golang-sample/golang-app:1.0
55+
```
56+
57+
**Note:** If you are iterating over this step multiple times while testing your application then
58+
don't forget to increase the tag version each time as image caching on cluster may mean
59+
that your updates don't actually get used when you expect them to!
60+
61+
62+
## Step 4: Configure the variables and run the application!
63+
Next we will do a one-time set up of some basic objects on the cluster in order to
64+
supply configuration settings to the application.
65+
66+
```bash
67+
# Create a service account that we can use to deploy using the Restricted SCC
68+
oc apply -f ./yaml/sa-pod-deployer.yaml
69+
70+
# Create a config map containing the details of your queue manager.
71+
#
72+
# If your queue manager is in the same OpenShift cluster then the hostname will be the
73+
# name of the "service".
74+
oc create configmap qmgr-details \
75+
--from-literal=HOSTNAME=mydynamichostname \
76+
--from-literal=PORT=34567 \
77+
--from-literal=QMNAME=QM100 \
78+
--from-literal=CHANNELNAME=SYSTEM.DEF.SVRCONN
79+
80+
# If necessary, create a secret to hold the username and password your application should
81+
# use to authenticate to the queue manager.
82+
oc create secret generic qmgr-credentials \
83+
--from-literal=USERNAME=appuser100 \
84+
--from-literal=PASSWORD='password100'
85+
```
86+
87+
Now run the application!
88+
```bash
89+
# Before you run this command, be sure to update the "image" attribute on
90+
# line 8 of pod-sample.yaml so that it matches your image tag version from step 3.
91+
oc apply -f ./yaml/pod-sample.yaml --as=my-service-account
92+
```
93+
94+
Congratulations on running your MQ Golang application in OpenShift - and happy messaging!

openshift-app-sample/src/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
src
2+

openshift-app-sample/src/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/ibm-messaging/mq-golang-jms20/openshift-app-sample/src
2+
3+
go 1.13
4+
5+
require github.com/ibm-messaging/mq-golang-jms20 v1.3.0

openshift-app-sample/src/go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/ibm-messaging/mq-golang-jms20 v1.3.0 h1:tcO7+kdJ1UAkll8gMLwV+bOS8iiapYVaMvdtN4hUB0s=
3+
github.com/ibm-messaging/mq-golang-jms20 v1.3.0/go.mod h1:/n2YVhPy7J4o5zoPSqEGnCv9yPYi6Od51/f8gAxkHRc=
4+
github.com/ibm-messaging/mq-golang/v5 v5.1.3 h1:B1Xfk1jMulmDfPf3jH2Dh9nnyEkaqfI1FvTHt4pFza4=
5+
github.com/ibm-messaging/mq-golang/v5 v5.1.3/go.mod h1:ywCwmYbJOU/E0rl+z4GiNoxVMty68O+LVO39a1VMXrE=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
8+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

openshift-app-sample/src/main.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) IBM Corporation 2021.
2+
//
3+
// This program and the accompanying materials are made available under the
4+
// terms of the Eclipse Public License 2.0, which is available at
5+
// http://www.eclipse.org/legal/epl-2.0.
6+
//
7+
// SPDX-License-Identifier: EPL-2.0
8+
9+
// Package main provides the entry point for a executable application.
10+
package main
11+
12+
import (
13+
"fmt"
14+
"log"
15+
"os"
16+
"strconv"
17+
18+
"github.com/ibm-messaging/mq-golang-jms20/mqjms"
19+
)
20+
21+
func main() {
22+
fmt.Println("Beginning world!!!")
23+
24+
portNum, _ := strconv.Atoi(os.Getenv("PORT"))
25+
26+
// Initialise the attributes of the CF in whatever way you like
27+
cf := mqjms.ConnectionFactoryImpl{
28+
QMName: os.Getenv("QMNAME"),
29+
Hostname: os.Getenv("HOSTNAME"),
30+
PortNumber: portNum,
31+
ChannelName: os.Getenv("CHANNELNAME"),
32+
UserName: os.Getenv("USERNAME"),
33+
Password: os.Getenv("PASSWORD"),
34+
}
35+
36+
// Creates a connection to the queue manager, using defer to close it automatically
37+
// at the end of the function (if it was created successfully)
38+
context, errCtx := cf.CreateContext()
39+
if context != nil {
40+
defer context.Close()
41+
}
42+
43+
if errCtx != nil {
44+
log.Fatal(errCtx)
45+
} else {
46+
fmt.Println(" -- Connection successful")
47+
48+
// TODO - Add your application code here!
49+
50+
}
51+
52+
fmt.Println("Ending world!!!")
53+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: golang-app
5+
spec:
6+
containers:
7+
- name: golang-app
8+
image: uk.icr.io/golang-sample/golang-app:1.0
9+
envFrom:
10+
- configMapRef:
11+
name: qmgr-details
12+
- secretRef:
13+
name: qmgr-credentials
14+
restartPolicy: OnFailure
15+
imagePullSecrets:
16+
- name: all-icr-io
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
apiVersion: mq.ibm.com/v1beta1
2+
kind: QueueManager
3+
metadata:
4+
name: sample-mq
5+
namespace: cp4i
6+
spec:
7+
license:
8+
accept: true
9+
license: L-RJON-BQPGWD
10+
use: NonProduction
11+
queueManager:
12+
name: MYQM
13+
storage:
14+
queueManager:
15+
type: ephemeral
16+
availability:
17+
type: SingleInstance
18+
template:
19+
pod:
20+
containers:
21+
- env:
22+
- name: MQSNOAUT
23+
value: 'yes'
24+
name: qmgr
25+
version: 9.1.5.0-r2
26+
web:
27+
enabled: true
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
apiVersion: v1
2+
kind: ServiceAccount
3+
metadata:
4+
name: my-service-account
5+
---
6+
kind: Role
7+
apiVersion: rbac.authorization.k8s.io/v1
8+
metadata:
9+
name: pod-interactions
10+
rules:
11+
- apiGroups: [""]
12+
resources: ["pods", "pods/exec"]
13+
verbs: ["get", "list", "delete", "patch", "create"]
14+
---
15+
apiVersion: rbac.authorization.k8s.io/v1
16+
kind: RoleBinding
17+
metadata:
18+
name: pod-interactions
19+
subjects:
20+
- kind: User
21+
name: my-service-account
22+
roleRef:
23+
kind: Role
24+
name: pod-interactions
25+
apiGroup: rbac.authorization.k8s.io

0 commit comments

Comments
 (0)