Skip to content

Commit

Permalink
App to test network connectivity to CE services (#133)
Browse files Browse the repository at this point in the history
* App to test network connectivity to cloud services

---------

Signed-off-by: Nasir Pauldon-Collins <[email protected]>
Co-authored-by: Enrico Regge <[email protected]>
Co-authored-by: Steven Whitehead <[email protected]>
  • Loading branch information
3 people authored Sep 23, 2024
1 parent fae4c27 commit 83d43ad
Show file tree
Hide file tree
Showing 6 changed files with 1,062 additions and 0 deletions.
10 changes: 10 additions & 0 deletions network-test-app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM icr.io/codeengine/node:22-alpine
RUN apk -U upgrade

WORKDIR /network-test-app

COPY *.js *.json /network-test-app/

RUN npm install

ENTRYPOINT [ "node", "app.js" ]
20 changes: 20 additions & 0 deletions network-test-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Network Connectivity Test App

This sample is intended to help users debug connectivity issues for IBM Cloud Services. You can use this app to help isolate network connection issues between your own code and a working app.

- - -

This sample includes a `build` script which will build the container image and push the image to `icr.io/codeengine/network-test-app`. The customer should:
- Pull the image `icr.io/codeengine/network-test-app`
- Deploy the image as an application
- Make an HTTP request to your application, and observe the response

## Configuring the Service Credentials for the App

This app works by attempting to connect your Code Engine project to another IBM Cloud service; in order to do this properly, it must consume service credentials that should be configured by creating a `service binding` between the customer's project and the service they wish to connect to.

For more information about how to create a service binding, see [Working with service bindings to integrate IBM Cloud services with Code Engine](https://cloud.ibm.com/docs/codeengine?topic=codeengine-service-binding).

### Example: Databases for PostgreSQL
If the app is attempting to connect to a postgres instance, then after creating a service binding for the instance the app will contain the credentials for the postgres instance in the form of an environment variable `DATABASES_FOR_POSTGRESQL_CONNECTION`.
- **Without this environment variable properly configured, the app will NOT be able to connect to postgres**
71 changes: 71 additions & 0 deletions network-test-app/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const { Client } = require("pg");
const express = require("express");
const app = express()
const timeoutMs = 15000 // timeout in 15 seconds
const port = process.env.PORT;

app.get("/", async (request, response) => {
pgServiceCredentials = process.env.DATABASES_FOR_POSTGRESQL_CONNECTION
if(!!pgServiceCredentials){
/*
Postgres service credentials have been configured properly,
continue with attempting to connect to service
*/
try {
// Use env variables loaded from service binding to connect to our postgres instance
console.log("Connecting to PostgreSQL instance...");

postgresSetup = JSON.parse(pgServiceCredentials);
cli = postgresSetup.cli;
postgres = postgresSetup.postgres;
cert = Buffer.from(postgres.certificate.certificate_base64, 'base64').toString('utf8');

const client = new Client({
user: postgres.authentication.username,
password: cli.environment.PGPASSWORD,
host: postgres.hosts[0].hostname,
database: postgres.database,
port: postgres.hosts[0].port,
statement_timeout: timeoutMs,
query_timeout: timeoutMs,
lock_timeout: timeoutMs,
application_name: "network-test-app",
connectionTimeoutMillis: timeoutMs,
ssl: {
ca: cert,
rejectUnauthorized: true,
},
});
await client.connect();

// Run a simple command to verify that we connected to the postgres instance
console.log("List tables");
result = await client.query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE';");
console.log(result)
await client.end()
response.status(200).send("Successfully connected to postgres instance");
} catch (err) {
console.error("Failed to connect to PostgreSQL instance", err);
response.status(500).send("Could not connect to postgres instance:", err);
}
} else {
response.status(500).send("Could not connect to postgres instance: no postgres instance configured");
}


})

const server = app.listen(port, async () => {
console.log('listening on localhost', port)
})

process.on('SIGTERM', () => {
console.info('SIGTERM signal received.');
server.close(() => {
console.log('Http server closed.');
});
});




17 changes: 17 additions & 0 deletions network-test-app/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

# Env Vars:
# REGISTRY: name of the image registry/namespace to store the images
# NOCACHE: set this to "--no-cache" to turn off the Docker build cache
#
# NOTE: to run this you MUST set the REGISTRY environment variable to
# your own image registry/namespace otherwise the `docker push` commands
# will fail due to an auth failure. Which means, you also need to be logged
# into that registry before you run it.

set -ex
export REGISTRY=${REGISTRY:-icr.io/codeengine}

# First build the app's image and push it
docker build ${NOCACHE} -t ${REGISTRY}/network-test-app -f Dockerfile . --platform linux/amd64
docker push ${REGISTRY}/network-test-app
Loading

0 comments on commit 83d43ad

Please sign in to comment.