InnerLoop Development using Cloud Workstations with Python

1. Overview

This lab demonstrates features and capabilities designed to streamline the development workflow for software engineers tasked with developing Python applications in a containerized environment. Typical container development requires the user to understand details of containers and the container build process. Additionally, developers typically have to break their flow, moving out of their IDE to test and debug their applications in remote environments. With the tools and technologies mentioned in this tutorial, developers can work effectively with containerized applications without leaving their IDE.

What you will learn

In this lab you will learn methods for developing with containers in GCP including:

  • Creating a new Python starter application
  • Walk through the development process
  • Develop a simple CRUD rest service
  • Deploying to GKE
  • Debugging an error state
  • Utilizing breakpoint / logs
  • Hot deploying changes back to GKE

58a4cdd3ed7a123a.png

2. Setup and Requirements

Self-paced environment setup

  1. Sign-in to the Google Cloud Console and create a new project or reuse an existing one. If you don't already have a Gmail or Google Workspace account, you must create one.

b35bf95b8bf3d5d8.png

a99b7ace416376c4.png

bd84a6d3004737c5.png

  • The Project name is the display name for this project's participants. It is a character string not used by Google APIs. You can update it at any time.
  • The Project ID is unique across all Google Cloud projects and is immutable (cannot be changed after it has been set). The Cloud Console auto-generates a unique string; usually you don't care what it is. In most codelabs, you'll need to reference the Project ID (it is typically identified as PROJECT_ID). If you don't like the generated ID, you may generate another random one. Alternatively, you can try your own and see if it's available. It cannot be changed after this step and will remain for the duration of the project.
  • For your information, there is a third value, a Project Number which some APIs use. Learn more about all three of these values in the documentation.
  1. Next, you'll need to enable billing in the Cloud Console to use Cloud resources/APIs. Running through this codelab shouldn't cost much, if anything at all. To shut down resources so you don't incur billing beyond this tutorial, you can delete the resources you created or delete the whole project. New users of Google Cloud are eligible for the $300 USD Free Trial program.

Start Cloudshell Editor

This lab was designed and tested for use with Google Cloud Shell Editor. To access the editor,

  1. access your google project at https://console.cloud.google.com.
  2. In the top right corner click on the cloud shell editor icon

8560cc8d45e8c112.png

  1. A new pane will open in the bottom of your window
  2. Click on the Open Editor button

9e504cb98a6a8005.png

  1. The editor will open with an explorer on the right and editor in the central area
  2. A terminal pane should also be available in the bottom of the screen
  3. If the terminal is NOT open use the key combination of `ctrl+`` to open a new terminal window

Environment Setup

In Cloud Shell, set your project ID and the project number for your project. Save them as PROJECT_ID and PROJECT_ID variables.

export PROJECT_ID=$(gcloud config get-value project)
export PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID \
    --format='value(projectNumber)')

Provision the infrastructure used in this lab

In this lab you will deploy code to GKE and access data stored in a Spanner database. You will also use Cloud workstations as the IDE. The setup script below prepares this infrastructure for you.

  1. Download setup script and make it executable.
wget https://raw.githubusercontent.com/GoogleCloudPlatform/container-developer-workshop/main/labs/python/setup_with_cw.sh
chmod +x setup_with_cw.sh
  1. Open setup_with_cw.sh file and edit the values of passwords that are currently set to CHANGEME
  2. Run the setup script to stand up a GKE cluster and a Spanner database that you will use in this lab
./setup_with_cw.sh &

Cloud Workstations Cluster

  1. Open Cloud Workstations in the Cloud Console. Wait for the cluster to be in READY status.

305e1a3d63ac7ff6.png

Create Workstations Configuration

  1. If your Cloud Shell session was disconnected, click "Reconnect" and then run the gcloud cli command to set the project ID. Replace sample project id below with your qwiklabs project ID before running the command.
gcloud config set project qwiklabs-gcp-project-id
  1. Download and run the script below in the terminal to create Cloud Workstations configuration.
wget https://raw.githubusercontent.com/GoogleCloudPlatform/container-developer-workshop/main/labs/python/workstation_config_setup.sh
chmod +x workstation_config_setup.sh
./workstation_config_setup.sh
  1. Verify results under the Configurations section. It will take 2 minutes to transition to READY status.

2e23c2e9983d1ccf.png

  1. Open Cloud Workstations in the Console and create new instance.

a53adeeac81a78c8.png

  1. Change name to my-workstation and select existing configuration: codeoss-python.

f052cd47701ec774.png

  1. Verify results under the Workstations section.

Launch Workstation

  1. Start and launch the workstation. It will take a couple of minutes to start the Workstation.

682f8a307032cba3.png

  1. Allow 3rd party cookies by clicking on the icon in the address bar. 1b8923e2943f9bc4.png

fcf9405b6957b7d7.png

  1. Click "Site not working?".

36a84c0e2e3b85b.png

  1. Click "Allow cookies".

2259694328628fba.png

  1. Once the workstation launches you will see Code OSS IDE come up. Click on "Mark Done" on the Getting Started page one the workstation IDE

94874fba9b74cc22.png

3. Create a new Python starter application

In this section you'll create a new Python application.

  1. Open a new Terminal.

c31d48f2e4938c38.png

  1. Make a new directory and open it as a workspace
mkdir music-service && cd music-service

code-oss-cloud-workstations -r --folder-uri="$PWD"

Click on the Allow button if you see this message, so that you can copy paste into the workstation.

58149777e5cc350a.png

  1. Create a file called requirements.txt and copy the following contents into it

789e8389170bd900.png

Flask
gunicorn
google-cloud-spanner
ptvsd==4.3.2
  1. Create a file named app.py and paste the following code into it
import os
from flask import Flask, request, jsonify
from google.cloud import spanner

app = Flask(__name__)

@app.route("/")
def hello_world():
    message="Hello, World!"
    return message

if __name__ == '__main__':
    server_port = os.environ.get('PORT', '8080')
    app.run(debug=False, port=server_port, host='0.0.0.0')

  1. Create a file named Dockerfile and paste the following into it
FROM python:3.8
ARG FLASK_DEBUG=0
ENV FLASK_DEBUG=$FLASK_DEBUG
ENV FLASK_APP=app.py
WORKDIR /app
COPY requirements.txt .
RUN pip install --trusted-host pypi.python.org -r requirements.txt
COPY . .
ENTRYPOINT ["python3", "-m", "flask", "run", "--port=8080", "--host=0.0.0.0"]

Note: FLASK_DEBUG=1 allows you to auto reload code changes to a Python flask app. This Dockerfile allows you to pass this value as a build argument.

Generate Manifests

In your terminal execute the following command to generate a default skaffold.yaml and deployment.yaml

  1. Initialize Skaffold with the following command
skaffold init --generate-manifests

When prompted use the arrows to move your cursor and the spacebar to select the options.

Choose:

  • 8080 for the port
  • y to save the configuration

Update Skaffold Configurations

  • Change default application name
  • Open skaffold.yaml
  • Select the image name currently set as dockerfile-image
  • Right click and choose Change All Occurrences
  • Type in the new name as python-app
  • Further edit the build section to
  • add docker.buildArgs to pass FLASK_DEBUG=1
  • Sync settings to load any changes to *.py files from IDE to running container

After the edits, the build section in the skaffold.yaml file would be as under:

build:
 artifacts:
 - image: python-app
   docker:
     buildArgs:
       FLASK_DEBUG: "1"
     dockerfile: Dockerfile
   sync:
     infer:
     - '**/*.py'

Modify Kubernetes Configuration File

  1. Change the default Name
  • Open deployment.yaml file
  • Select the image name currently set as dockerfile-image
  • Right click and choose Change All Occurrences
  • Type in the new name as python-app

4. Walking through the development process

With the business logic added you can now deploy and test your application. The following section will showcase the use of the Cloud Code plugin. Among other things, this plugin integrates with skaffold to streamline your development process. When you deploy to GKE in the following steps, Cloud Code and Skaffold will automatically build your container image, push it to a Container Registry, and then deploy your application to GKE. This happens behind the scenes abstracting the details away from the developer flow.

Sign in to Google Cloud

  1. Click on Cloud Code icon and Select "Sign in to Google Cloud":

1769afd39be372ff.png

  1. Click "Proceed to sign in".

923bb1c8f63160f9.png

  1. Check the output in the Terminal and open the link:

517fdd579c34aa21.png

  1. Login with your Qwiklabs students credentials.

db99b345f7a8e72c.png

  1. Select "Allow":

a5376553c430ac84.png

  1. Copy verification code and return to the Workstation tab.

6719421277b92eac.png

  1. Paste the verification code and hit Enter.

e9847cfe3fa8a2ce.png

Add Kubernetes Cluster

  1. Add a Cluster

62a3b97bdbb427e5.png

  1. Select Google Kubernetes Engine:

9577de423568bbaa.png

  1. Select project.

c5202fcbeebcd41c.png

  1. Select "python-cluster" that was created in the initial setup.

719c2fc0a7f9e84f.png

  1. The cluster now shows up in the Kubernetes clusters list under Cloud Code. Navigate and explore the cluster from here.

7e5f50662d4eea3c.png

Set current project id using gcloud cli

  1. Copy project ID for this lab from qwiklabs page.

fcff2d10007ec5bc.png

  1. From the terminal, run the gcloud cli command to set the project ID. Replace sample project id before running the command. SUBSTITUTE the project id before running the command below.
gcloud config set project qwiklabs-gcp-project-id

Deploy to Kubernetes

  1. In the pane at the bottom of Cloud Shell Editor, select Cloud Code 

d99a88992e15fea9.png

  1. In the panel that appears at the top, select Run on Kubernetes.. If prompted, select Yes to use the current Kubernetes context.

bfd65e9df6d4a6cb.png

This command starts a build of the source code and then runs the tests. The build and tests will take a few minutes to run. These tests include unit tests and a validation step that checks the rules that are set for the deployment environment. This validation step is already configured, and it ensures that you get warning of deployment issues even while you're still working in your development environment.

  1. The first time you run the command a prompt will appear at the top of the screen asking if you want the current kubernetes context, select "Yes" to accept and use the current context.
  2. Next a prompt will be displayed asking which container registry to use. Press enter to accept the default value provided
  3. Select the "Output" tab in the lower pane to view progress and notifications. Using dropdown select "Kubernetes: Run/Debug"

9c87ccbf5d06f50a.png

  1. Select "Kubernetes: Run/Debug - Detailed" in the channel drop down to the right to view additional details and logs streaming live from the containers

804abc8833ffd571.png

When the build and tests are done, the Output tab logs will have the URL http://localhost:8080 is listed in the "Kubernetes: Run/Debug" view.

  1. In the Cloud Code terminal, hover over the first URL in the output (http://localhost:8080), and then in the tool tip that appears select Open Web Preview.
  2. A new browser tab will open and display the message Hello, World!

Hot Reload

  1. Open the app.py file
  2. Change the greeting message to Hello from Python

Notice immediately that in the Output window, Kubernetes: Run/Debug view, the watcher syncs the updated files with the container in Kubernetes

Update initiated
Build started for artifact python-app
Build completed for artifact python-app

Deploy started
Deploy completed

Status check started
Resource pod/python-app-6f646ffcbb-tn7qd status updated to In Progress
Resource deployment/python-app status updated to In Progress
Resource deployment/python-app status completed successfully
Status check succeeded
...
  1. If you switch to Kubernetes: Run/Debug - Detailed view, you will notice it recognizes file changes then builds and redeploys the app
files modified: [app.py]
Syncing 1 files for gcr.io/veer-pylab-01/python-app:3c04f58-dirty@sha256:a42ca7250851c2f2570ff05209f108c5491d13d2b453bb9608c7b4af511109bd
Copying files:map[app.py:[/app/app.py]]togcr.io/veer-pylab-01/python-app:3c04f58-dirty@sha256:a42ca7250851c2f2570ff05209f108c5491d13d2b453bb9608c7b4af511109bd
Watching for changes...
[python-app] * Detected change in '/app/app.py', reloading
[python-app] * Restarting with stat
[python-app] * Debugger is active!
[python-app] * Debugger PIN: 744-729-662
  1. Refresh your browser tab where you saw previous results to see the updated results.

Debugging

  1. Go to the Debug view and stop the current thread 647213126d7a4c7b.png. If it asks, you can choose to clean up after each run.
  2. 70d6bd947d04d1e6.png
  3. Click on Cloud Code in the bottom menu and select Debug on Kubernetes to run the application in debug mode.
  • In the Kubernetes Run/Debug - Detailed view of Output window, notice that skaffold will deploy this application in debug mode.
  1. When the process completes. You'll notice a debugger attached and the Output tab says: Attached debugger to container "python-app-8476f4bbc-h6dsl" successfully., and the URL http://localhost:8080 is listed.
Port forwarding pod/python-app-8bd64cf8b-cskfl in namespace default, remote port 5678 -> http://127.0.0.1:5678
  1. The bottom status bar changes its color from blue to orange indicating that it is in Debug mode.
  2. In the Kubernetes Run/Debug view, notice that a Debuggable container is started
**************URLs*****************
Forwarded URL from service python-app: http://localhost:8080
Debuggable container started pod/python-app-8bd64cf8b-cskfl:python-app (default)
Update succeeded
***********************************

Utilize Breakpoints

  1. Open the app.py file
  2. Locate the statement which reads return message
  3. Add a breakpoint to that line by clicking the blank space to the left of the line number. A red indicator will show to note the breakpoint is set
  4. The first time this is run a prompt will ask where the source is inside the container. This value is related to the directories in the Dockerfile.

Press Enter to accept the default

fccc866f32b5ed86.png

It will take a couple of minutes for the application to build and deploy.

  1. Reload your browser and note the debugger stops the process at the breakpoint and allows you to investigate the variables and state of the application which is running remotely in GKE
  2. Click down into the VARIABLES section
  3. Click Locals there you'll find the "message" variable.
  4. Double click on the variable name "message" and in the popup, change the value to something different like "Greetings from Python"
  5. Click the Continue button in the debug control panel 607c33934f8d6b39.png
  6. Review the response in your browser which now shows the updated value you just entered.
  7. Stop the "Debug" mode by pressing the stop button 647213126d7a4c7b.png and remove the breakpoint by clicking on the breakpoint again.

5. Developing a Simple CRUD Rest Service

At this point your application is fully configured for containerized development and you've walked through the basic development workflow with Cloud Code. In the following sections you practice what you've learned by adding rest service endpoints connecting to a managed database in Google Cloud.

Code the rest service

The code below creates a simple rest service that uses Spanner as the database backing the application. Create the application by copying the following code into your application.

  1. Create the main application by replacing app.py with the following contents
import os
from flask import Flask, request, jsonify
from google.cloud import spanner


app = Flask(__name__)


instance_id = "music-catalog"

database_id = "musicians"

spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)


@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

@app.route('/singer', methods=['POST'])
def create():
    try:
        request_json = request.get_json()
        singer_id = request_json['singer_id']
        first_name = request_json['first_name']
        last_name = request_json['last_name']
        def insert_singers(transaction):
            row_ct = transaction.execute_update(
                f"INSERT Singers (SingerId, FirstName, LastName) VALUES" \
                f"({singer_id}, '{first_name}', '{last_name}')"
            )
            print("{} record(s) inserted.".format(row_ct))

        database.run_in_transaction(insert_singers)

        return {"Success": True}, 200
    except Exception as e:
        return e



@app.route('/singer', methods=['GET'])
def get_singer():

    try:
        singer_id = request.args.get('singer_id')
        def get_singer():
            first_name = ''
            last_name = ''
            with database.snapshot() as snapshot:
                results = snapshot.execute_sql(
                    f"SELECT SingerId, FirstName, LastName FROM Singers " \
                    f"where SingerId = {singer_id}",
                    )
                for row in results:
                    first_name = row[1]
                    last_name = row[2]
                return (first_name,last_name )
        first_name, last_name = get_singer()  
        return {"first_name": first_name, "last_name": last_name }, 200
    except Exception as e:
        return e


@app.route('/singer', methods=['PUT'])
def update_singer_first_name():
    try:
        singer_id = request.args.get('singer_id')
        request_json = request.get_json()
        first_name = request_json['first_name']
        
        def update_singer(transaction):
            row_ct = transaction.execute_update(
                f"UPDATE Singers SET FirstName = '{first_name}' WHERE SingerId = {singer_id}"
            )

            print("{} record(s) updated.".format(row_ct))

        database.run_in_transaction(update_singer)
        return {"Success": True}, 200
    except Exception as e:
        return e


@app.route('/singer', methods=['DELETE'])
def delete_singer():
    try:
        singer_id = request.args.get('singer')
    
        def delete_singer(transaction):
            row_ct = transaction.execute_update(
                f"DELETE FROM Singers WHERE SingerId = {singer_id}"
            )
            print("{} record(s) deleted.".format(row_ct))

        database.run_in_transaction(delete_singer)
        return {"Success": True}, 200
    except Exception as e:
        return e

port = int(os.environ.get('PORT', 8080))
if __name__ == '__main__':
    app.run(threaded=True, host='0.0.0.0', port=port)

Add Database Configurations

To connect to Spanner securely, set the application up to use Workload Identities. This enables your application to act as its own service account and have individual permissions when accessing the database.

  1. Update deployment.yaml. Add the following code at the end of the file (ensure you keep the tab indents in the example below)
      serviceAccountName: python-ksa
      nodeSelector:
        iam.gke.io/gke-metadata-server-enabled: "true" 

After the changes the spec section should look like this

   spec:
     containers:
     - name: python-app
       image: python-app
     serviceAccountName: python-ksa
     nodeSelector:
       iam.gke.io/gke-metadata-server-enabled: "true"

Deploy and Validate Application

  1. In the pane at the bottom of Cloud Shell Editor, select Cloud Code then select Debug on Kubernetes at the top of the screen.
  2. When the build and tests are done, the Output tab says: Resource deployment/python-app status completed successfully, and a url is listed: "Forwarded URL from service python-app: http://localhost:8080"
  3. Add a couple of entries.

From cloudshell Terminal, run the command below

curl -X POST http://localhost:8080/singer -H 'Content-Type: application/json' -d '{"first_name":"Cat","last_name":"Meow", "singer_id": 6}'
  1. Test the GET by running the command below in the terminal
curl -X GET http://localhost:8080/singer?singer_id=6
  1. Test Delete: Now try to delete an entry by running the following command. Change the value of item-id if required.
curl -X DELETE http://localhost:8080/singer?singer_id=6
    This throws an error message
500 Internal Server Error

Identify and fix the issue

  1. Debug mode and find the issue. Here are some tips:
  • We know something is wrong with the DELETE as it is not returning the desired result. So you would set the breakpoint in app.py in the delete_singer method.
  • Run step by step execution and watch the variables at each step to observe the values of local variables in the left window.
  • To observe specific values such as singer_id and request.args in the add these variables to the Watch window.
  1. Notice that the value assigned to singer_id is None. Change the code to fix the issue.

The fixed code snippet would look like this.

@app.route('/delete-singer', methods=['DELETE', 'GET'])
def delete_singer():
    try:
        singer_id = request.args.get('singer_id')
  1. Once the application is restarted, test again by trying to delete.
  2. Stop the debugging session by clicking on the red square in the debug toolbar 647213126d7a4c7b.png

6. Cleanup

Congratulations! In this lab you've created a new Python application from scratch and configured it to work effectively with containers. You then deployed and debugged your application to a remote GKE cluster following the same developer flow found in traditional application stacks.

To clean up after completing the lab:

  1. Delete the files used in the lab
cd ~ && rm -rf ~/music-service
  1. Delete the project to remove all related infrastructure and resources