diff --git a/.github/workflows/mainClient.yml b/.github/workflows/mainClient.yml index b81a194e..dc9df59a 100644 --- a/.github/workflows/mainClient.yml +++ b/.github/workflows/mainClient.yml @@ -63,6 +63,30 @@ jobs: run: | docker push $GCLOUD_REGION-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhubnext-client/klowhub-client:${{ github.sha }} + # 6.5. Limpiar imágenes antiguas + - name: Cleanup Old Images + working-directory: ./client + run: | + TAGS=$(gcloud container images list-tags \ + ${{ secrets.GCLOUD_REGION }}-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhubnext-client/klowhub-client \ + --format='get(tags)' \ + --sort-by=~TIMESTAMP) + + TOTAL_IMAGES=$(echo "$TAGS" | wc -l) + if [ $TOTAL_IMAGES -gt 2 ]; then + TO_DELETE=$((TOTAL_IMAGES - 2)) + + echo "Found $TOTAL_IMAGES images, keeping 2, deleting $TO_DELETE" + echo "$TAGS" | tail -n $TO_DELETE | while read TAG; do + if [ ! -z "$TAG" ]; then + echo "Deleting image with tag: $TAG" + gcloud container images delete \ + "${{ secrets.GCLOUD_REGION }}-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhubnext-client/klowhub-client:$TAG" \ + --quiet + fi + done + fi + # 7. Desplegar a Cloud Run - name: Deploy to Cloud Run working-directory: ./client diff --git a/.github/workflows/mainServer.yml b/.github/workflows/mainServer.yml index bc5f8ceb..0b085815 100644 --- a/.github/workflows/mainServer.yml +++ b/.github/workflows/mainServer.yml @@ -30,7 +30,7 @@ jobs: GATEWAY_SERVICE_PORT: 3000 UPLOAD_SERVICE_NAME: klowhub-upload-api UPLOAD_SERVICE_PORT: 3003 - MAX_IMAGES_TO_KEEP: 2 + MAX_IMAGES_TO_KEEP: 1 steps: - uses: actions/checkout@v4 @@ -293,7 +293,4 @@ jobs: DEPLOY_NAME="${DEPLOY_INFO#*/}" echo "Setting revision history limit for $DEPLOY_NAME" kubectl patch deployment $DEPLOY_NAME -n $NAMESPACE -p '{"spec": {"revisionHistoryLimit": 2}}' - kubectl rollout status deployment/$DEPLOY_NAME -n $NAMESPACE - echo "Current revision history for $DEPLOY_NAME:" - kubectl rollout history deployment/$DEPLOY_NAME -n $NAMESPACE done diff --git a/server/api/check-deploy.rest b/server/api/check-deploy.rest deleted file mode 100644 index bf23fa77..00000000 --- a/server/api/check-deploy.rest +++ /dev/null @@ -1 +0,0 @@ -GET http://34.111.85.215/gateway \ No newline at end of file diff --git a/server/api/check-gateway-deploy.rest b/server/api/check-gateway-deploy.rest new file mode 100644 index 00000000..7608154d --- /dev/null +++ b/server/api/check-gateway-deploy.rest @@ -0,0 +1,3 @@ +### Check Gateway Deploy +GET http://34.111.85.215/gateway/hello +Content-Type: application/json \ No newline at end of file diff --git a/server/api/check-gateway-local.rest b/server/api/check-gateway-local.rest new file mode 100644 index 00000000..0052a031 --- /dev/null +++ b/server/api/check-gateway-local.rest @@ -0,0 +1,3 @@ +### Check Gateway Local +GET http://localhost:3000/gateway/hello +Content-Type: application/json \ No newline at end of file diff --git a/server/apps/gateway/protos/upload.proto b/server/apps/gateway/protos/upload.proto index 2c6d61ba..a4748f17 100644 --- a/server/apps/gateway/protos/upload.proto +++ b/server/apps/gateway/protos/upload.proto @@ -1,7 +1,19 @@ syntax = "proto3"; -package googlecloudstorage; +package googlecloudstorage; // Este será el paquete común para ambos servicios +// Servicio PubSub +service PubSubService { + rpc Test (TestRequest) returns (TestResponse); +} + +message TestRequest {} + +message TestResponse { + string message = 1; +} + +// Servicio GoogleCloudStorage service GoogleCloudStorageService { rpc UploadFile (UploadFileRequest) returns (UploadFileResponse); } diff --git a/server/apps/gateway/src/app.module.ts b/server/apps/gateway/src/app.module.ts index bf0d70f6..0c0d6f5d 100644 --- a/server/apps/gateway/src/app.module.ts +++ b/server/apps/gateway/src/app.module.ts @@ -3,7 +3,9 @@ import { AuthModule } from './auth/auth.module'; import { UploadModule } from './upload/upload.module'; import { CoursesModule } from './courses/courses.module'; import { PubSubModule } from './pubsub/pubsub.module'; +import { GatewayController } from './gateway.controller'; @Module({ imports: [AuthModule, UploadModule, CoursesModule,PubSubModule], + controllers: [GatewayController], }) export class AppModule {} diff --git a/server/apps/gateway/src/gateway.controller.ts b/server/apps/gateway/src/gateway.controller.ts index 00893c2a..17c41206 100644 --- a/server/apps/gateway/src/gateway.controller.ts +++ b/server/apps/gateway/src/gateway.controller.ts @@ -2,7 +2,7 @@ import { Controller, Get } from '@nestjs/common'; @Controller('gateway') export class GatewayController { - @Get() + @Get("/hello") getHello(): string { return 'Hello from Gateway!'; } diff --git a/server/apps/gateway/src/gateway.module.ts b/server/apps/gateway/src/gateway.module.ts index 5dca70ca..89be967c 100644 --- a/server/apps/gateway/src/gateway.module.ts +++ b/server/apps/gateway/src/gateway.module.ts @@ -2,6 +2,7 @@ import { Module } from '@nestjs/common'; import { ClientsModule, Transport } from '@nestjs/microservices'; import { AuthController } from './auth/auth.controllers'; import { UploadController } from './upload/upload.controllers'; +import { join } from "path"; import * as dotenv from 'dotenv'; dotenv.config(); @@ -29,7 +30,7 @@ dotenv.config(); transport: Transport.GRPC, options: { package: 'googlecloudstorage', - protoPath: '../protos/upload.proto', + protoPath: join(__dirname,'../protos/upload.proto'), url: `${process.env.UPLOAD_MICROSERVICE_HOST || "0.0.0.0"}:${process.env.UPLOAD_SERVICE_PORT || 3003}`, }, }, diff --git a/server/apps/gateway/src/middleware/routersPublics.ts b/server/apps/gateway/src/middleware/routersPublics.ts index 629f6373..5745cae5 100644 --- a/server/apps/gateway/src/middleware/routersPublics.ts +++ b/server/apps/gateway/src/middleware/routersPublics.ts @@ -3,4 +3,5 @@ export const publicRoutes: string[] = [ '/auth/login', '/auth/verifyEmail', '/auth/resetToken', + '/gateway/hello' ]; diff --git a/server/apps/gateway/src/proto/upload.proto b/server/apps/gateway/src/proto/upload.proto deleted file mode 100644 index a4748f17..00000000 --- a/server/apps/gateway/src/proto/upload.proto +++ /dev/null @@ -1,30 +0,0 @@ -syntax = "proto3"; - -package googlecloudstorage; // Este será el paquete común para ambos servicios - -// Servicio PubSub -service PubSubService { - rpc Test (TestRequest) returns (TestResponse); -} - -message TestRequest {} - -message TestResponse { - string message = 1; -} - -// Servicio GoogleCloudStorage -service GoogleCloudStorageService { - rpc UploadFile (UploadFileRequest) returns (UploadFileResponse); -} - -message UploadFileRequest { - string filename = 1; - bytes file = 2; - string userId = 3; -} - -message UploadFileResponse { - string url = 1; - string message = 2; -} \ No newline at end of file diff --git a/server/apps/gateway/src/pubsub/pubsub.module.ts b/server/apps/gateway/src/pubsub/pubsub.module.ts index 58984dad..b1ef8279 100644 --- a/server/apps/gateway/src/pubsub/pubsub.module.ts +++ b/server/apps/gateway/src/pubsub/pubsub.module.ts @@ -2,6 +2,7 @@ import { Module } from '@nestjs/common'; import { ClientsModule, Transport } from '@nestjs/microservices'; import { PubSubGatewayController } from './pubsub.gateway.controller'; import { PubSubGatewayService } from './pubsub.gateway.service'; +import { join } from "path"; @Module({ imports: [ @@ -11,8 +12,8 @@ import { PubSubGatewayService } from './pubsub.gateway.service'; transport: Transport.GRPC, options: { package: 'googlecloudstorage', - protoPath: 'src/proto/upload.proto', - url: '0.0.0.0:50051', + protoPath: join(__dirname,'../../protos/upload.proto'), + url: `${process.env.UPLOAD_MICROSERVICE_HOST || "0.0.0.0"}:${process.env.UPLOAD_SERVICE_PORT || 3003}`, }, }, ]), diff --git a/server/apps/gateway/src/upload/upload.module.ts b/server/apps/gateway/src/upload/upload.module.ts index 2b18b0da..9d464ecb 100644 --- a/server/apps/gateway/src/upload/upload.module.ts +++ b/server/apps/gateway/src/upload/upload.module.ts @@ -1,6 +1,7 @@ import { Module } from '@nestjs/common'; import { ClientsModule, Transport } from '@nestjs/microservices'; import { UploadController } from './upload.controllers'; +import { join } from "path"; @Module({ imports: [ @@ -10,7 +11,7 @@ import { UploadController } from './upload.controllers'; transport: Transport.GRPC, options: { package: 'googlecloudstorage', - protoPath: 'src/proto/upload.proto', + protoPath: join(__dirname,'../../protos/upload.proto'), url: `${process.env.UPLOAD_MICROSERVICE_HOST || "0.0.0.0"}:${process.env.UPLOAD_SERVICE_PORT || 3003}`, }, }, diff --git a/server/k8s/courses-deployment.yml b/server/k8s/courses-deployment.yml index a002dfc8..b3fa4b37 100644 --- a/server/k8s/courses-deployment.yml +++ b/server/k8s/courses-deployment.yml @@ -21,6 +21,13 @@ spec: image: ${GCLOUD_REGION}-docker.pkg.dev/${GCLOUD_PROJECT_ID}/klowhub-server/${COURSE_SERVICE_NAME}:${IMAGE_TAG} ports: - containerPort: 3002 + resources: + requests: + cpu: "50m" + memory: "64Mi" + limits: + cpu: "100m" + memory: "128Mi" env: - name: NODE_ENV value: "production" diff --git a/server/k8s/courses-hpa.yml b/server/k8s/courses-hpa.yml new file mode 100644 index 00000000..9705446f --- /dev/null +++ b/server/k8s/courses-hpa.yml @@ -0,0 +1,19 @@ +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: courses-hpa + namespace: courseservice +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: courses-deployment + minReplicas: 1 + maxReplicas: 2 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 80 \ No newline at end of file diff --git a/server/k8s/gateway-deployment.yml b/server/k8s/gateway-deployment.yml index 891d9635..5fa40381 100644 --- a/server/k8s/gateway-deployment.yml +++ b/server/k8s/gateway-deployment.yml @@ -21,6 +21,13 @@ spec: image: ${GCLOUD_REGION}-docker.pkg.dev/${GCLOUD_PROJECT_ID}/klowhub-server/${GATEWAY_SERVICE_NAME}:${IMAGE_TAG} ports: - containerPort: 3000 + resources: + requests: + cpu: "50m" + memory: "64Mi" + limits: + cpu: "100m" + memory: "128Mi" env: - name: NODE_ENV value: "production" diff --git a/server/k8s/gateway-hpa.yml b/server/k8s/gateway-hpa.yml new file mode 100644 index 00000000..4f2bf90c --- /dev/null +++ b/server/k8s/gateway-hpa.yml @@ -0,0 +1,19 @@ +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: gateway-hpa + namespace: gateway +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: gateway-deployment + minReplicas: 1 + maxReplicas: 2 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 80 \ No newline at end of file diff --git a/server/k8s/upload-deployment.yml b/server/k8s/upload-deployment.yml index 9527832a..1928b167 100644 --- a/server/k8s/upload-deployment.yml +++ b/server/k8s/upload-deployment.yml @@ -21,6 +21,13 @@ spec: image: ${GCLOUD_REGION}-docker.pkg.dev/${GCLOUD_PROJECT_ID}/klowhub-server/${UPLOAD_SERVICE_NAME}:${IMAGE_TAG} ports: - containerPort: 3003 + resources: + requests: + cpu: "50m" + memory: "64Mi" + limits: + cpu: "100m" + memory: "128Mi" env: - name: NODE_ENV value: "production" diff --git a/server/k8s/upload-hpa.yml b/server/k8s/upload-hpa.yml new file mode 100644 index 00000000..f832d764 --- /dev/null +++ b/server/k8s/upload-hpa.yml @@ -0,0 +1,19 @@ +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: upload-hpa + namespace: gcc-upload +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: klowhub-upload-api-deployment + minReplicas: 1 + maxReplicas: 2 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 80 \ No newline at end of file diff --git a/server/k8s/users-deployment.yml b/server/k8s/users-deployment.yml index ea43f0a5..bf1d662d 100644 --- a/server/k8s/users-deployment.yml +++ b/server/k8s/users-deployment.yml @@ -21,6 +21,13 @@ spec: image: ${GCLOUD_REGION}-docker.pkg.dev/${GCLOUD_PROJECT_ID}/klowhub-server/${USERS_SERVICE_NAME}:${IMAGE_TAG} ports: - containerPort: 3001 + resources: + requests: + cpu: "50m" + memory: "64Mi" + limits: + cpu: "100m" + memory: "128Mi" env: - name: NODE_ENV value: "production" diff --git a/server/k8s/users-hpa.yml b/server/k8s/users-hpa.yml new file mode 100644 index 00000000..234cadb8 --- /dev/null +++ b/server/k8s/users-hpa.yml @@ -0,0 +1,19 @@ +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: users-hpa + namespace: userservice +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: users-deployment + minReplicas: 1 + maxReplicas: 2 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 80 \ No newline at end of file