Dns routing inside Kubernetes with Edge tunneler daemonset

On the SDK side, the easiest way to get started is with the python-sdk and monkeypatch, you just install the openziti:

pip install openziti

And you can use something like this:

def get_db_connection_pg8000():
    try:
        with openziti.monkeypatch():
            conn = pg8000.connect(
                user=config['database']['user'],
                password=config['database']['password'],
                host=config['database']['host'],
                port=config['database']['port'],
                database=config['database']['dbname']
            )
            return conn
    except Exception as e:
        logging.error(f"Error getting the connection: {e}")
        logging.error("Error connecting to the database", exc_info=True)
        raise

@app.route('/database/character/<string:name>', methods=['GET'])
def get_character(name):
    global config

    try:
        # Get OpenZiti Data
        with openziti.monkeypatch():
            # Connect to a Zitified Database
            conn = get_db_connection_pg8000()
            cursor = conn.cursor()
            cursor.execute("SELECT * FROM characters WHERE name ILIKE %s", (name,))
            character = cursor.fetchone()
            conn.close()

            if character:
                return jsonify({
                    "id": character[0],
                    "name": character[1],
                    "age": character[2],
                    "anime": character[3]
                })
            else:
                return jsonify({"error": "Character not found"}), 404

    except Exception as e:
        logging.error('General error: %s', str(e))
        return jsonify({'error': 'An unexpected error occurred'}), 500

That's actually part of my code (A flask server connecting to a DB to get data). I'm using the PG8000 driver on python.

3 Likes