From 747064aa20706a929159d0e85a22241a87f3d6c2 Mon Sep 17 00:00:00 2001 From: MirkoZETA Date: Sun, 16 Feb 2025 22:03:41 -0300 Subject: [PATCH 1/3] More curl examples --- README.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1357e74..8a5b866 100644 --- a/README.md +++ b/README.md @@ -37,11 +37,27 @@ This is a Flask-based backend API that runs the Flex Net Sim C++ library. The backend will be accessible at `http://127.0.0.1:5000`. 5. **Send simulation requests using `curl` or a frontend application:** - Example `curl` request: + Example `curl` request with minimal parameters (defaults applied): ```bash 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) ``` + Example `curl` request with all parameters specified + ```bash + curl -X POST -H "Content-Type: application/json" \ + -d '{ + "algorithm": "BestFit", + "networkType": 2, + "goal_connections": 50000, + "confidence": 0.01, + "lambda": 1.5, + "mu": 8, + "network": "USNet", + "bitrate": "100Gbps" + }' \ + http://127.0.0.1:5000/run_simulation + ``` + ## Dockerization To build the Docker image: From 8d8ebf265f453b4d2677cf6a906471302bd1b96b Mon Sep 17 00:00:00 2001 From: MirkoZETA Date: Sun, 16 Feb 2025 22:48:55 -0300 Subject: [PATCH 2/3] Google docker build --- .github/workflows/gke-cd.yml | 69 ++++++-------------- .github/workflows/google.yml | 118 +++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 49 deletions(-) create mode 100644 .github/workflows/google.yml diff --git a/.github/workflows/gke-cd.yml b/.github/workflows/gke-cd.yml index 1ccb456..8eae0b1 100644 --- a/.github/workflows/gke-cd.yml +++ b/.github/workflows/gke-cd.yml @@ -1,62 +1,33 @@ -name: Backend CI/CD to GKE +name: Deploy FNS API on: push: - branches: [ "main" ] + branches: + - main pull_request: - branches: [ "main" ] + branches: + - main jobs: - build-and-push-docker: + deploy: name: Build and Push Docker Image runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v3 - - - name: Set up Google Cloud SDK - uses: google-github-actions/setup-gcloud@v1 - with: - service_account_key: ${{ secrets.GKE_SA_KEY }} # Configure this secret in GitHub Actions - project_id: ${{ secrets.GKE_PROJECT_ID }} # Configure this secret in GitHub Actions - - - name: Enable Docker Credential Helper - run: |- - gcloud auth configure-docker - - - name: Build Docker image - run: |- - docker build --tag gcr.io/${{ secrets.GKE_PROJECT_ID }}/mi-backend-simulacion:${GITHUB_SHA} . - - name: Push Docker image to GCR - run: |- - docker push gcr.io/${{ secrets.GKE_PROJECT_ID }}/mi-backend-simulacion:${GITHUB_SHA} - - deploy-to-gke: - name: Deploy to GKE - needs: build-and-push-docker - runs-on: ubuntu-latest - - steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v2 - - name: Set up Google Cloud SDK - uses: google-github-actions/setup-gcloud@v1 + - name: Set up Google CLoud CLI + uses: google-github-actions/setup-gcloud@v0 with: - service_account_key: ${{ secrets.GKE_SA_KEY }} # Configure this secret in GitHub Actions - project_id: ${{ secrets.GKE_PROJECT_ID }} # Configure this secret in GitHub Actions - cluster_name: tu-cluster-gke # Reemplaza with your GKE cluster name - cluster_zone: tu-zona-gke # Reemplaza with your GKE cluster zone - - - name: Get Kubernetes credentials - run: |- - gcloud container clusters get-credentials tu-cluster-gke --zone tu-zona-gke --project ${{ secrets.GKE_PROJECT_ID }} # Reemplaza with your cluster and zone - - - name: Deploy to GKE (Ejemplo Básico - Necesitas Adaptar!) - run: |- - # This is a VERY basic example. YOU MUST ADAPT THIS TO YOUR KUBERNETES DEPLOYMENT. - # Normally you would use a Kubernetes Deployment YAML file and kubectl apply -f deployment.yaml - # Here, we just update the image of an existing Deployment named 'mi-backend-deployment' (example). - kubectl set image deployment/mi-backend-deployment mi-backend-container=gcr.io/${{ secrets.GKE_PROJECT_ID }}/mi-backend-simulacion:${GITHUB_SHA} \ No newline at end of file + service_account_key: ${{ secrets.GKE_SA_KEY }} + project_id: ${{ secrets.GKE_PROJECT_ID }} + export_default_credentials: true + + - name: Build and push Docker image + env: + GOOGLE_PROJECT: ${{ secrets.GKE_PROJECT_ID }} + run: | + gcloud auth configure-docker us-central1-docker.pkg.dev + docker build -t us-central1-docker.pkg.dev/flex-net-sim/flex-net-sim-repo/fns-api:latest . + docker push us-central1-docker.pkg.dev/flex-net-sim/flex-net-sim-repo/fns-api:latest diff --git a/.github/workflows/google.yml b/.github/workflows/google.yml new file mode 100644 index 0000000..68a1d3b --- /dev/null +++ b/.github/workflows/google.yml @@ -0,0 +1,118 @@ +# This workflow will build a docker container, publish it to Google Container +# Registry, and deploy it to GKE when there is a push to the "main" +# branch. +# +# To configure this workflow: +# +# 1. Enable the following Google Cloud APIs: +# +# - Artifact Registry (artifactregistry.googleapis.com) +# - Google Kubernetes Engine (container.googleapis.com) +# - IAM Credentials API (iamcredentials.googleapis.com) +# +# You can learn more about enabling APIs at +# https://support.google.com/googleapi/answer/6158841. +# +# 2. Ensure that your repository contains the necessary configuration for your +# Google Kubernetes Engine cluster, including deployment.yml, +# kustomization.yml, service.yml, etc. +# +# 3. Create and configure a Workload Identity Provider for GitHub: +# https://github.com/google-github-actions/auth#preferred-direct-workload-identity-federation. +# +# Depending on how you authenticate, you will need to grant an IAM principal +# permissions on Google Cloud: +# +# - Artifact Registry Administrator (roles/artifactregistry.admin) +# - Kubernetes Engine Developer (roles/container.developer) +# +# You can learn more about setting IAM permissions at +# https://cloud.google.com/iam/docs/manage-access-other-resources +# +# 5. Change the values in the "env" block to match your values. + +name: 'Build and Deploy to GKE' + +on: + push: + branches: + - '"main"' + pull_request: + branches: none + +env: + PROJECT_ID: 'flexnetsim' # TODO: update to your Google Cloud project ID + GAR_LOCATION: 'us-central1' # TODO: update to your region + GKE_CLUSTER: 'flex-net-sim-cluster' # TODO: update to your cluster name + GKE_ZONE: 'us-central1-c' # TODO: update to your cluster zone + DEPLOYMENT_NAME: 'gke-test' # TODO: update to your deployment name + REPOSITORY: 'samples' # TODO: update to your Artifact Registry docker repository name + IMAGE: 'static-site' + WORKLOAD_IDENTITY_PROVIDER: 'projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider' # TODO: update to your workload identity provider + +jobs: + setup-build-publish-deploy: + name: 'Setup, Build, Publish, and Deploy' + runs-on: 'ubuntu-latest' + environment: 'production' + + permissions: + contents: 'read' + id-token: 'write' + + steps: + - name: 'Checkout' + uses: 'actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332' # actions/checkout@v4 + + # Configure Workload Identity Federation and generate an access token. + # + # See https://github.com/google-github-actions/auth for more options, + # including authenticating via a JSON credentials file. + - id: 'auth' + name: 'Authenticate to Google Cloud' + uses: 'google-github-actions/auth@f112390a2df9932162083945e46d439060d66ec2' # google-github-actions/auth@v2 + with: + workload_identity_provider: '${{ env.WORKLOAD_IDENTITY_PROVIDER }}' + + # Authenticate Docker to Google Cloud Artifact Registry + - name: 'Docker Auth' + uses: 'docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567' # docker/login-action@v3 + with: + username: 'oauth2accesstoken' + password: '${{ steps.auth.outputs.auth_token }}' + registry: '${{ env.GAR_LOCATION }}-docker.pkg.dev' + + # Get the GKE credentials so we can deploy to the cluster + - name: 'Set up GKE credentials' + uses: 'google-github-actions/get-gke-credentials@6051de21ad50fbb1767bc93c11357a49082ad116' # google-github-actions/get-gke-credentials@v2 + with: + cluster_name: '${{ env.GKE_CLUSTER }}' + location: '${{ env.GKE_ZONE }}' + + # Build the Docker image + - name: 'Build and push Docker container' + run: |- + DOCKER_TAG="${GAR_LOCATION}-docker.pkg.dev/${PROJECT_ID}/${REPOSITORY}/${IMAGE}:${GITHUB_SHA}" + + docker build \ + --tag "${DOCKER_TAG}" \ + --build-arg GITHUB_SHA="${GITHUB_SHA}" \ + --build-arg GITHUB_REF="${GITHUB_REF}" \ + . + + docker push "${DOCKER_TAG}" + + # Set up kustomize + - name: 'Set up Kustomize' + run: |- + curl -sfLo kustomize https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv5.4.3/kustomize_v5.4.3_linux_amd64.tar.gz + chmod u+x ./kustomize + + # Deploy the Docker image to the GKE cluster + - name: 'Deploy to GKE' + run: |- + # replacing the image name in the k8s template + ./kustomize edit set image LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE:TAG=$GAR_LOCATION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY/$IMAGE:$GITHUB_SHA + ./kustomize build . | kubectl apply -f - + kubectl rollout status deployment/$DEPLOYMENT_NAME + kubectl get services -o wide From 0f1c91c4551dcfb0453276877b4a47cfec366684 Mon Sep 17 00:00:00 2001 From: MirkoZETA Date: Sun, 16 Feb 2025 23:00:52 -0300 Subject: [PATCH 3/3] Added credentials --- .github/workflows/gke-cd.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/gke-cd.yml b/.github/workflows/gke-cd.yml index 8eae0b1..e9621c5 100644 --- a/.github/workflows/gke-cd.yml +++ b/.github/workflows/gke-cd.yml @@ -20,14 +20,14 @@ jobs: - name: Set up Google CLoud CLI uses: google-github-actions/setup-gcloud@v0 with: - service_account_key: ${{ secrets.GKE_SA_KEY }} - project_id: ${{ secrets.GKE_PROJECT_ID }} + service_account_key: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }} + project_id: ${{ secrets.GOOGLE_PROJECT }} export_default_credentials: true - name: Build and push Docker image env: - GOOGLE_PROJECT: ${{ secrets.GKE_PROJECT_ID }} + GOOGLE_PROJECT: ${{ secrets.GOOGLE_PROJECT }} run: | gcloud auth configure-docker us-central1-docker.pkg.dev - docker build -t us-central1-docker.pkg.dev/flex-net-sim/flex-net-sim-repo/fns-api:latest . - docker push us-central1-docker.pkg.dev/flex-net-sim/flex-net-sim-repo/fns-api:latest + docker build -t us-central1-docker.pkg.dev/$GOOGLE_PROJECT/flex-net-sim-repo/fns-api:latest . + docker push us-central1-docker.pkg.dev/$GOOGLE_PROJECT/flex-net-sim-repo/fns-api:latest