Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App to test network connectivity to CE services #133

Merged
merged 17 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
stevenwhitehead marked this conversation as resolved.
Show resolved Hide resolved
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);
stevenwhitehead marked this conversation as resolved.
Show resolved Hide resolved
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
Loading