Skip to content

Commit ed0dea2

Browse files
authored
Merge pull request #2 from MirkoZETA/feature/cloud-run-deployment
Implemented Cloud Run instead of Kubernetes plus new functionalities: - New endpoint `/help`. - New networks and bitrates alternatives. - New algorithms alternative ExactFit. - Implemented K route parameter. - Separated Jobs in YML file. - Updated README.
2 parents 3f8dd16 + cb4e75e commit ed0dea2

File tree

16 files changed

+53569
-2273
lines changed

16 files changed

+53569
-2273
lines changed

.github/workflows/gke-cd.yml

Lines changed: 24 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
name: Deploy FNS API
1+
name: Deploy FNS API to Cloud Run (Separated Jobs)
22

33
on:
44
push:
55
branches:
6-
- develop
6+
- master
77
pull_request:
88
branches:
9-
- develop
9+
- master
1010

1111
jobs:
12-
deploy:
13-
name: Build, Push Docker Image & Deploy to Kubernetes
12+
build-and-push-image:
13+
name: Build and Push Docker Image
1414
runs-on: ubuntu-latest
1515
steps:
1616
- name: Checkout code
@@ -26,15 +26,6 @@ jobs:
2626
with:
2727
version: latest
2828
project_id: ${{ secrets.GOOGLE_PROJECT }}
29-
install_components: kubectl # Install kubectl component
30-
31-
- name: Install gke-gcloud-auth-plugin # Install the auth plugin
32-
run: |
33-
CLOUD_SDK_URL="https://packages.cloud.google.com/apt"
34-
echo "deb $CLOUD_SDK_URL cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
35-
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
36-
sudo apt-get update -y
37-
sudo apt-get install google-cloud-sdk-gke-gcloud-auth-plugin -y
3829

3930
- name: Configure Docker to push to Artifact Registry # Configure docker auth
4031
run: |
@@ -49,54 +40,23 @@ jobs:
4940
docker build -t $IMAGE_NAME:$IMAGE_TAG .
5041
docker push $IMAGE_NAME:$IMAGE_TAG
5142
52-
- name: Get Kubernetes cluster credentials # Get cluster credentials
53-
env:
54-
GOOGLE_PROJECT: ${{ secrets.GOOGLE_PROJECT }}
55-
CLUSTER_NAME: flex-net-sim-cluster
56-
CLUSTER_REGION: us-central1
57-
run: |
58-
gcloud container clusters get-credentials $CLUSTER_NAME --region $CLUSTER_REGION --project $GOOGLE_PROJECT
43+
deploy-to-cloud-run:
44+
name: Deploy to Cloud Run
45+
runs-on: ubuntu-latest
46+
needs: build-and-push-image # Ensure this job runs after build-and-push-image
47+
steps:
48+
- name: Checkout code (again, if needed for deploy steps - optional)
49+
uses: actions/checkout@v2
5950

60-
- name: Deploy to Kubernetes cluster # Deploy to cluster
61-
env:
62-
DEPLOYMENT_NAME: fns-api-deployment
63-
NAMESPACE: default # Replace with your namespace if needed
64-
IMAGE_NAME: us-central1-docker.pkg.dev/${{ secrets.GOOGLE_PROJECT }}/flex-net-sim-repo/fns-api
65-
IMAGE_TAG: latest
66-
run: |
67-
kubectl apply -f - <<EOF
68-
apiVersion: apps/v1
69-
kind: Deployment
70-
metadata:
71-
name: $DEPLOYMENT_NAME
72-
namespace: $NAMESPACE
73-
spec:
74-
replicas: 2 # Adjust as needed
75-
selector:
76-
matchLabels:
77-
app: fns-api
78-
template:
79-
metadata:
80-
labels:
81-
app: fns-api
82-
spec:
83-
containers:
84-
- name: fns-api-container
85-
image: $IMAGE_NAME:$IMAGE_TAG
86-
ports:
87-
- containerPort: 8080 # Replace with your application port
88-
---
89-
apiVersion: v1
90-
kind: Service
91-
metadata:
92-
name: fns-api-service
93-
namespace: $NAMESPACE
94-
spec:
95-
selector:
96-
app: fns-api
97-
ports:
98-
- protocol: TCP
99-
port: 80
100-
targetPort: 8080 # Replace with your application port
101-
type: LoadBalancer
102-
EOF
51+
- name: Google Cloud Auth # Authenticate gcloud CLI
52+
uses: google-github-actions/auth@v2
53+
with:
54+
credentials_json: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
55+
56+
- name: Deploy to Cloud Run
57+
uses: google-github-actions/deploy-cloudrun@v1
58+
with:
59+
image: us-central1-docker.pkg.dev/${{ secrets.GOOGLE_PROJECT }}/flex-net-sim-repo/fns-api:latest # Use the same image as built
60+
service: fns-api-cloud-run
61+
region: us-central1
62+
project_id: ${{ secrets.GOOGLE_PROJECT }}

README.md

Lines changed: 77 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
# Flex Net Sim Backend API
22

3-
Flask-based backend API for integrating the FlexNetSim C++ library, powering the web application deployed at [www.in-progress.com](www.in-progress.com). While unofficial, it serves as a bridge between the simulation engine and the web interface.
3+
Flask-based backend API for integrating the FlexNetSim C++ library, powering the web application.
44

55
## Prerequisites
66

77
* Python 3.9 or higher
88
* g++ (GNU C++ Compiler)
99
* Docker (for containerization)
10-
* Google Cloud SDK (for deployment to GKE) -> In progress.
11-
* A Google Cloud Project with Google Kubernetes Engine (GKE) and Google Container Registry (GCR) enabled -> In progress.
10+
* Google Cloud SDK (for deployment to Cloud Run)
11+
* A Google Cloud Project with Cloud Run API enabled.
1212

1313
## Getting Started (Local Development)
1414

1515
1. **Clone the repository:**
1616
```bash
17-
git clone [repository-url]
17+
git clone <repository-url> # Replace <repository-url> with your repository URL
1818
cd flask-simulation-backend
1919
```
2020

@@ -36,14 +36,34 @@ Flask-based backend API for integrating the FlexNetSim C++ library, powering the
3636
```
3737
The backend will be accessible at `http://127.0.0.1:5000`.
3838

39-
5. **Send simulation requests using `curl` or a frontend application:**
40-
Example `curl` request with minimal parameters (defaults applied):
41-
```bash
42-
curl -X POST -H "Content-Type: application/json" -d '{"algorithm": "FirstFit", "networkType": 1, "bitrate": "bitrate"}' [http://127.0.0.1:5000/run_simulation](http://127.0.0.1:5000/run_simulation)
43-
```
39+
## API Endpoints
4440

45-
Example `curl` request with all parameters specified
46-
```bash
41+
### `/run_simulation` (POST)
42+
43+
This endpoint runs a FlexNetSim simulation based on the parameters provided in the JSON request body.
44+
45+
**Request Body Parameters:**
46+
47+
| Parameter | Type | Description | Allowed Values | Default Value | Constraints |
48+
| :---------------- | :--------------- | :----------------------------------------------------------------------------- | :---------------------------- | :------------ | :-------------------- |
49+
| `algorithm` | `string` | Routing and spectrum assignment algorithm to use. | `FirstFit`, `ExactFit` | `FirstFit` | |
50+
| `networkType` | `integer` | Type of optical network. | `1` | `1` | Only `1` (EON) available |
51+
| `goal_connections`| `integer` | Target number of connection requests for the simulation. | | `100000` | Must be integer > 0 |
52+
| `confidence` | `number (float)` | Confidence level for the simulation results. | | `0.05` | Must be > 0 |
53+
| `lambda_param` | `number (float)` | Arrival rate (lambda) of connection requests. | | `1.0` | Must be > 0 |
54+
| `mu` | `number (float)` | Service rate (mu) of connection requests. | | `10.0` | Must be > 0 |
55+
| `network` | `string` | Network topology to simulate. | `NSFNet`, `Cost239`, `EuroCore`, `GermanNet`, `UKNet` | `NSFNet` | |
56+
| `bitrate` | `string` | Type of bitrate allocation. | `fixed-rate`, `flex-rate` | `bitrate` | |
57+
| `K` | `integer` | Number of paths to compute. | | `3` | |
58+
59+
**Example `curl` request with minimal parameters (defaults applied):**
60+
61+
```bash
62+
curl -X POST -H "Content-Type: application/json" -d '{"algorithm": "FirstFit", "networkType": 1, "bitrate": "bitrate"}' http://127.0.0.1:5000/run_simulation
63+
```
64+
65+
**Example `curl`request with all parameters specified:**
66+
```bash
4767
curl -X POST -H "Content-Type: application/json" \
4868
-d '{
4969
"algorithm": "FirstFit",
@@ -56,7 +76,27 @@ Flask-based backend API for integrating the FlexNetSim C++ library, powering the
5676
"bitrate": "bitrate" -> (filename in the ./bitrates folder)
5777
}' \
5878
http://127.0.0.1:5000/run_simulation
79+
```
80+
81+
**Response**:
82+
- 200 OK: Simulation executed successfully. The response body will be a JSON object with the following structure:
83+
```JSON
84+
{
85+
"output": "string", // Simulation output results
86+
"error": "string" // Empty string if no errors
87+
}
5988
```
89+
- 400 Bad Request: Indicates an error in the request, such as missing or invalid parameters. The response body will be a JSON object with an `"error"` field describing the issue.
90+
- 500 Internal Server Error: Indicates a server-side error, either during compilation or simulation execution. The response body will be a JSON object with `"error"` and "details" fields providing more information about the error.
91+
92+
### `/help` (GET)
93+
94+
This endpoint provides detailed information about the `/run_simulation` endpoint, including the expected request structure, parameters, allowed values, and response formats.
95+
96+
**Request**:
97+
```bash
98+
curl http://127.0.0.1:5000/help
99+
```
60100
61101
## Dockerization
62102
@@ -74,77 +114,54 @@ To stop:
74114
docker stop fns-api
75115
```
76116
77-
## Docs: GCloud Deployment Configuration
117+
## GCloud Deployment Configuration
78118
79-
For detailed step-by-step instructions on configuring Google Cloud (GCloud) aspects such as Kubernetes Cluster creation, Artifact Registry, Service Account creation, and IAM policy binding, please refer to the following video tutorial:
119+
As a prerequisite is mandatory to apply the following steps to the GCloud project for the docker image build and upload to artifacts, and also service account creation and IAM policy binding:
80120
81121
[GCloud Configuration Video Tutorial](https://www.youtube.com/watch?v=KQUKDiBz3IA)
82122
83-
This video will guide you through the necessary configurations in the Google Cloud Console to prepare your project for Kubernetes deployments using GitHub Actions.
123+
This video will guide you through the necessary configurations in the Google Cloud Console to prepare your project for Cloud Run deployments using GitHub Actions.
84124
85125
**Key Reminders from the Video & for Successful Deployment:**
86126
87-
* **Keep Track of Docker Image Name, Project ID, and Kubernetes Cluster Name:** Note these down during the video configuration, as you will need them in subsequent steps and for your GitHub Actions workflow.
127+
* **Keep Track of Docker Image Name, Project ID:** Note these down during the video configuration, as you will need them in subsequent steps and for your GitHub Actions workflow.
88128
* **Service Account Email:** Ensure you create a Service Account as shown in the video and securely download and store the JSON key file. You'll also need to note the Service Account's email address.
89129
90-
**Post-Configuration Steps (using `gcloud` and `kubectl`):**
130+
**Post-Configuration Steps (using `gcloud` and `cloud-run`):**
91131
92-
1. **Create a Kubernetes Cluster in GCloud:**
132+
1. Activate necessary apis:
93133
94-
* Navigate to the Kubernetes Engine section in your Google Cloud Console.
95-
* Click **Create Cluster**.
96-
* Choose a cluster name (e.g., `flex-net-sim-cluster`). **Remember this name.**
97-
* Select a region for your cluster (e.g., `us-central1`).
98-
* For the purpose of this guide, you can use the default settings for node pools, networking, and other configurations, or adjust them based on your specific needs.
99-
* Click **Create** to create the cluster. It will take a few minutes for the cluster to be provisioned.
134+
* `gcloud services enable run.googleapis.com`
100135
101-
2. **Set IAM Policy Binding (using `gcloud`):**
136+
2. Create cloud run service:
137+
* Navigate to the Cloud Run section in your Google Cloud Console.
138+
* Create a **Service**.
139+
* Select *Use an inline editor to create a function*.
140+
* Set a name, in this case *fns-api-cloud-run* will be used.
141+
* Note down the Endpoint URL, because it will the defaul url for the API.
142+
* Select the authetification preferences.
143+
* Create.
102144
103-
Replace `<YOUR-GOOGLE-PROJECT-ID>` and `<SERVICE_ACCOUNT_EMAIL>` with your actual Google Cloud Project ID and the Service Account Email you noted down.
145+
3. Update access of service accounts to cloud run resources:
104146
105147
```bash
106-
gcloud projects add-iam-policy-binding <YOUR-GOOGLE-PROJECT-ID> --member="serviceAccount:<SERVICE_ACCOUNT_EMAIL>" --role="roles/container.admin"
148+
gcloud projects add-iam-policy-binding "<YOUR-GOOGLE-PROJECT-ID>" --member="serviceAccount:<SERVICE_ACCOUNT_EMAIL>" --role="roles/run.admin"
107149
```
108150
109-
3. **Get Kubernetes Cluster Credentials (using `gcloud`):**
110-
111-
Replace `<CLUSTER-NAME>` and `<YOUR-GOOGLE-PROJECT-ID>` with your Kubernetes Cluster Name and Google Cloud Project ID. Ensure the region is set to `us-central1`.
151+
and
112152
113153
```bash
114-
gcloud container clusters get-credentials <CLUSTER-NAME> --region us-central1 --project <YOUR-GOOGLE-PROJECT-ID>
115-
```
116-
117-
The command will fetch the cluster credentials and configure `kubectl` to use them. You should see output similar to:
118-
119-
```
120-
Fetching cluster endpoint and auth data.
121-
kubeconfig entry generated for <CLUSTER-NAME>.
122-
```
154+
gcloud iam service-accounts add-iam-policy-binding "<YOUR_PROJECT_NUMBER>-compute@developer.gserviceaccount.com" --member="serviceAccount:<SERVICE_ACCOUNT_EMAIL>" --role="roles/iam.serviceAccountActor"
155+
```
123156
124-
4. **Verify `kubectl` Configuration and Service Deployment (using `kubectl`):**
157+
4. **Test the Deployed API (using `curl`):**
125158
126-
After the command is successful, verify your `kubectl` configuration and check for the `fns-api-service`:
159+
Use the `curl` command with the `ENDPOINT-URL` you obtained from the previous steps to test your deployed API. Replace `YOUR-ENDPOINT-URL` with the actual `ENDPOINT-URL`.
127160
128161
```bash
129-
kubectl get service fns-api-service
130-
```
131-
132-
If the service is correctly deployed (after your GitHub Actions workflow runs), it should display information about your service, including the `EXTERNAL-IP`.
162+
curl -X POST -H "Content-Type: application/json" -d '{"algorithm": "FirstFit", "networkType": 1, "bitrate": "bitrate"}' <YOUR-ENDPOINT-URL>/run_simulation
163+
```
133164
134-
You should see output similar to this (the `EXTERNAL-IP` will likely be different):
135-
136-
```
137-
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
138-
fns-api-service LoadBalancer 10.3.xxx.xxx 34.56.1.247 80:8080/TCP 20h
139-
kubernetes ClusterIP 10.3.xxx.xxx <none> 443/TCP 21h
140-
```
141-
142-
5. **Test the Deployed API (using `curl`):**
143-
144-
Use the `curl` command with the `EXTERNAL-IP` you obtained from the previous step to test your deployed API. Replace `<YOUR-EXTERNAL-IP>` with the actual `EXTERNAL-IP`.
145-
146-
```bash
147-
curl -X POST -H "Content-Type: application/json" -d '{"algorithm": "FirstFit", "networkType": 1, "bitrate": "bitrate"}' http://<YOUR-EXTERNAL-IP>/run_simulation
148-
```
165+
Remember that depending on the authetification preferences you might need to authetificate to send request to the Endpoint just created.
149166
150-
**Remember**: These GCloud configurations, along with the repository's `gke-cd.yml` GitHub Actions workflow and correctly configured GitHub secrets, are essential for successful automated deployment of your FlexNetSim-API application to Google Cloud Kubernetes Engine.
167+
**Remember**: These GCloud configurations, along with the repository's `gke-cd.yml` GitHub Actions workflow and correctly configured GitHub secrets, are essential for successful automated deployment of your FlexNetSim-API application to Google Cloud Run.

0 commit comments

Comments
 (0)