diff --git a/.github/workflows/ci-night.yml b/.github/workflows/ci-night.yml deleted file mode 100644 index 95f69d6..0000000 --- a/.github/workflows/ci-night.yml +++ /dev/null @@ -1,58 +0,0 @@ -name: CI NIGHT - -on: - schedule: - - cron: '0 2 * * *' # Se ejecuta todos los días a medianoche UTC - -jobs: - test: - name: python Test - runs-on: ubuntu-latest - - steps: - - name: Checkout code - uses: actions/checkout@v2 - - - name: Set up Python - uses: actions/setup-python@v2 - with: - python-version: 3.9 - - - name: Install dependencies - run: | - python3 -m pip install --upgrade pip - pip install -r apppython/app/requirements.txt - - - name: Run tests - run: | - python3 -m unittest apppython/test/test_main.py - - build_and_push_docker: - name: Build and Push Docker Image - runs-on: ubuntu-latest - needs: test - if: ${{ github.event_name == 'push' && success() }} - - steps: - - name: Checkout code - uses: actions/checkout@v2 - - - name: Build Docker image - run: docker build -t test_api_python -f apppython/Dockerfile apppython - - - name: Tag Docker image - run: | - docker tag test_api_python ${{ secrets.DOCKER_USERNAME }}/test_api_python:night - docker tag test_api_python ${{ secrets.DOCKER_USERNAME }}/test_api_python:latest-night - - - name: Log into Docker Hub - uses: docker/login-action@v2 - with: - username: ${{ secrets.DOCKER_USERNAME }} - password: ${{ secrets.DOCKER_ACCESS_TOKEN }} - - - name: Tag Docker image - run: docker tag test_api_python ${{ secrets.DOCKER_USERNAME }}/test_api_python:latest-night - - - name: Push Docker image - run: docker push ${{ secrets.DOCKER_USERNAME }}/test_api_python:latest-night \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8198171..7a10019 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,7 +3,7 @@ name: CI on: push: branches: - - main + - dev jobs: test: @@ -38,7 +38,7 @@ jobs: uses: actions/checkout@v2 - name: Build Docker image - run: docker build -t test_api_python -f apppython/Dockerfile apppython + run: docker build -t microserver_dev -f apppython/Dockerfile apppython - name: Log into Docker Hub uses: docker/login-action@v2 @@ -53,7 +53,7 @@ jobs: echo "COMMIT_SHORT_SHA=$calculatedSha" >> $GITHUB_ENV - name: Tag Docker image - run: docker tag test_api_python ${{ secrets.DOCKER_USERNAME }}/test_api_python:${{ env.COMMIT_SHORT_SHA }} + run: docker tag microserver_dev ${{ secrets.DOCKER_USERNAME }}/microserver_dev:${{ env.COMMIT_SHORT_SHA }} - name: Push Docker image - run: docker push ${{ secrets.DOCKER_USERNAME }}/test_api_python:${{ env.COMMIT_SHORT_SHA }} + run: docker push ${{ secrets.DOCKER_USERNAME }}/microserver_dev:${{ env.COMMIT_SHORT_SHA }} diff --git a/HELM/apppython/charts/apppython-0.1.0.tgz b/HELM/apppython/charts/apppython-0.1.0.tgz deleted file mode 100644 index a68270a..0000000 Binary files a/HELM/apppython/charts/apppython-0.1.0.tgz and /dev/null differ diff --git a/HELM/apppython/values.yaml b/HELM/apppython/values.yaml index 1ee2744..810d1e4 100644 --- a/HELM/apppython/values.yaml +++ b/HELM/apppython/values.yaml @@ -14,8 +14,8 @@ restartPolicy: Always # Container image configuration image: - repository: garrijuan/test_api_python - tag: latest + repository: garrijuan/microserver_dev + tag: 75222e9 # API port apiPort: 80 diff --git a/apppython/k8s-test/deploy.yml b/apppython/k8s-test/deploy.yml deleted file mode 100644 index 41cbcd9..0000000 --- a/apppython/k8s-test/deploy.yml +++ /dev/null @@ -1,49 +0,0 @@ -# Deployment ---- -apiVersion: apps/v1 -kind: Deployment -metadata: - name: apppython -spec: - strategy: - type: Recreate - selector: - matchLabels: - app: apppython - template: - metadata: - labels: - app: apppython - spec: - securityContext: - runAsUser: 1001 - runAsGroup: 3000 - fsGroup: 2000 - restartPolicy: Always - containers: - - name: apppython - image: garrijuan/test_api_python:latest - env: - - name: API_PORT - value: "80" - ports: - - containerPort: 80 - replicas: 3 - ---- -# Service -apiVersion: v1 -kind: Service -metadata: - name: apppython - labels: - app: apppython -spec: - selector: - app: apppython - ports: - - port: 8081 - targetPort: 80 #puerto del pod al que mando la peticion. - protocol: TCP - name: apppython-port - type: ClusterIP \ No newline at end of file diff --git a/apppython/k8s-test/ingress.yml b/apppython/k8s-test/ingress.yml deleted file mode 100644 index 7c676bc..0000000 --- a/apppython/k8s-test/ingress.yml +++ /dev/null @@ -1,18 +0,0 @@ -apiVersion: networking.k8s.io/v1 -kind: Ingress -metadata: - name: appphyton-ingress - annotations: - nginx.ingress.kubernetes.io/rewrite-target: /$1 -spec: - rules: - - host: apppython - http: - paths: - - path: / - pathType: Prefix - backend: - service: - name: apppython - port: - number: 8081 diff --git a/apppython/k8s/CD.yml b/apppython/k8s/CD.yml index 4850941..6da082b 100644 --- a/apppython/k8s/CD.yml +++ b/apppython/k8s/CD.yml @@ -3,7 +3,7 @@ kind: Application metadata: #muy importante para usar Image Updater annotations: - argocd-image-updater.argoproj.io/image-list: myalias=garrijuan/test_api_python + argocd-image-updater.argoproj.io/image-list: myalias=garrijuan/microserver_dev argocd-image-updater.argoproj.io/myalias.helm.image-tag: image.tag argocd-image-updater.argoproj.io/myalias.helm.image-name: image.repository argocd-image-updater.argoproj.io/myalias.update-strategy: latest @@ -15,9 +15,9 @@ spec: source: #desplegamos con HELM, hay que hacerlo obligatoriamente para usar Image Updater path: HELM/apppython #apppython/k8s-test # the k8s source files - repoURL: https://github.com/garrijuan/app-python-CICD.git - targetRevision: main + repoURL: https://github.com/garrijuan/GitOpsKubeCD.git + targetRevision: dev destination: server: 'https://kubernetes.default.svc' #Target cluster - namespace: staging #namespace where we want to deploy + namespace: dev #namespace where we want to deploy project: default \ No newline at end of file diff --git a/createitem_microservice/Dockerfile b/createitem_microservice/Dockerfile deleted file mode 100644 index 909d2f5..0000000 --- a/createitem_microservice/Dockerfile +++ /dev/null @@ -1,18 +0,0 @@ -# Usa la imagen base oficial de Python -FROM python:3.9-slim - -# Establece el directorio de trabajo en /app -WORKDIR /app - -# Copia los archivos necesarios al contenedor -COPY requirements.txt . -COPY main.py . - -# Instala las dependencias del proyecto -RUN pip install fastapi uvicorn - -# Expone el puerto 8000 para acceder al servicio -EXPOSE 8000 - -# Comando para ejecutar la aplicación FastAPI -CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] \ No newline at end of file diff --git a/createitem_microservice/README.md b/createitem_microservice/README.md deleted file mode 100644 index 2a3c38a..0000000 --- a/createitem_microservice/README.md +++ /dev/null @@ -1,25 +0,0 @@ -## Run app in local envoroment -```sh -uvicorn main:app --reload -``` - -## generate dependencies file -```sh -pip freeze > requirements.txt -``` - -## Build image calling Dockerfile -```sh -docker build -t createitem . -docker tag createitem ${{ secrets.DOCKER_USERNAME }}/createitem:latest -docker push ${{ secrets.DOCKER_USERNAME }}/createitem:latest - -docker run -d -p 8000:8000 createitem #levantar imagen docker - -curl http://localhost:8000 - -docker run -d -p 8000:8000 garrijuan/createitem:latest -``` - - -levantar mysql y asignar datos a las variables de entorno diff --git a/createitem_microservice/k8s/ConfigMaps.yml b/createitem_microservice/k8s/ConfigMaps.yml deleted file mode 100644 index fba3e11..0000000 --- a/createitem_microservice/k8s/ConfigMaps.yml +++ /dev/null @@ -1,8 +0,0 @@ -apiVersion: v1 -kind: ConfigMap -metadata: - name: mysql-config -data: - MYSQL_USER: admin - MYSQL_PASSWORD: admin - MYSQL_DATABASE: item diff --git a/createitem_microservice/k8s/Deployment.yml b/createitem_microservice/k8s/Deployment.yml deleted file mode 100644 index 9d21955..0000000 --- a/createitem_microservice/k8s/Deployment.yml +++ /dev/null @@ -1,45 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: mysql -spec: - replicas: 2 - selector: - matchLabels: - app: mysql - template: - metadata: - labels: - app: mysql - spec: - containers: - - name: mysql - image: mysql:latest - ports: - - containerPort: 3306 - env: - - name: MYSQL_USER - valueFrom: - configMapKeyRef: - name: mysql-config - key: MYSQL_USER - - name: MYSQL_PASSWORD - valueFrom: - secretKeyRef: - name: mysql-secret - key: MYSQL_ROOT_PASSWORD - - name: MYSQL_DATABASE - valueFrom: - configMapKeyRef: - name: mysql-config - key: MYSQL_DATABASE ---- -apiVersion: v1 -kind: Service -metadata: - name: mysql -spec: - ports: - - port: 3306 - selector: - app: mysql diff --git a/createitem_microservice/k8s/Secrets.yml b/createitem_microservice/k8s/Secrets.yml deleted file mode 100644 index bfdc7ba..0000000 --- a/createitem_microservice/k8s/Secrets.yml +++ /dev/null @@ -1,7 +0,0 @@ -apiVersion: v1 -kind: Secret -metadata: - name: mysql-secret -type: Opaque -data: - MYSQL_ROOT_PASSWORD: YWRtaW4= #pass Base64 encode ; echo -n "tu_contraseña" | base64 diff --git a/createitem_microservice/main.py b/createitem_microservice/main.py deleted file mode 100644 index 255dda4..0000000 --- a/createitem_microservice/main.py +++ /dev/null @@ -1,40 +0,0 @@ -import os -from fastapi import FastAPI, HTTPException -from pydantic import BaseModel -import mysql.connector - -# Modelo Pydantic para la entrada de datos -class Item(BaseModel): - name: str - description: str - -app = FastAPI() - -# Obtiene las credenciales de MySQL de las variables de entorno, definir estas variables de entorno en tu clúster de Kubernetes antes de desplegar el microservicio. -db_config = { - 'user': os.getenv('MYSQL_USER'), - 'password': os.getenv('MYSQL_PASSWORD'), - 'host': os.getenv('MYSQL_HOST'), # Nombre del servicio de MySQL en Kubernetes - 'database': os.getenv('MYSQL_DATABASE') -} - -# Función para guardar el ítem en la base de datos -def save_item_to_db(item: Item): - connection = mysql.connector.connect(**db_config) - cursor = connection.cursor() - query = "INSERT INTO items (name, description) VALUES (%s, %s)" - data = (item.name, item.description) - cursor.execute(query, data) - connection.commit() - cursor.close() - connection.close() - -# Endpoint para la creación de un nuevo ítem -@app.post("/itemcreate/") -async def create_item(item: Item): - save_item_to_db(item) - return {"message": "Item created successfully"} - -if __name__ == "__main__": - import uvicorn - uvicorn.run(app, host="0.0.0.0", port=8000) \ No newline at end of file diff --git a/createitem_microservice/requirements.txt b/createitem_microservice/requirements.txt deleted file mode 100644 index 36f4b61..0000000 --- a/createitem_microservice/requirements.txt +++ /dev/null @@ -1,97 +0,0 @@ -annotated-types==0.6.0 -anyio==3.7.1 -apturl==0.5.2 -asgiref==3.5.0 -attrs==21.2.0 -bcrypt==3.2.0 -blinker==1.4 -Brlapi==0.8.3 -certifi==2020.6.20 -chardet==4.0.0 -click==8.0.3 -colorama==0.4.4 -command-not-found==0.3 -cryptography==3.4.8 -cupshelpers==1.0 -dbus-python==1.2.18 -defer==1.0.6 -distro==1.7.0 -distro-info==1.1+ubuntu0.1 -docker==5.0.3 -docker-compose==1.29.2 -dockerpty==0.4.1 -docopt==0.6.2 -duplicity==0.8.21 -exceptiongroup==1.1.3 -fastapi==0.104.1 -fasteners==0.14.1 -future==0.18.2 -gyp==0.1 -h11==0.14.0 -httpcore==1.0.2 -httplib2==0.20.2 -httpx==0.25.1 -idna==3.3 -importlib-metadata==4.6.4 -jeepney==0.7.1 -jsonschema==3.2.0 -keyring==23.5.0 -language-selector==0.1 -launchpadlib==1.10.16 -lazr.restfulclient==0.14.4 -lazr.uri==1.0.6 -lockfile==0.12.2 -louis==3.20.0 -macaroonbakery==1.3.1 -Mako==1.1.3 -MarkupSafe==2.0.1 -monotonic==1.6 -more-itertools==8.10.0 -netifaces==0.11.0 -oauthlib==3.2.0 -olefile==0.46 -paramiko==2.9.3 -pexpect==4.8.0 -Pillow==9.0.1 -protobuf==3.12.4 -ptyprocess==0.7.0 -pycairo==1.20.1 -pycups==2.0.1 -pydantic==2.5.0 -pydantic_core==2.14.1 -PyGObject==3.42.1 -PyJWT==2.3.0 -pymacaroons==0.13.0 -PyNaCl==1.5.0 -pyparsing==2.4.7 -pyRFC3339==1.1 -pyrsistent==0.18.1 -python-apt==2.4.0+ubuntu2 -python-dateutil==2.8.1 -python-debian==0.1.43+ubuntu1.1 -python-dotenv==0.19.2 -pytz==2022.1 -pyxdg==0.27 -PyYAML==5.4.1 -reportlab==3.6.8 -requests==2.25.1 -SecretStorage==3.3.1 -six==1.16.0 -sniffio==1.3.0 -starlette==0.27.0 -systemd-python==234 -texttable==1.6.4 -typing_extensions==4.8.0 -ubuntu-advantage-tools==8001 -ubuntu-drivers-common==0.0.0 -ufw==0.36.1 -unattended-upgrades==0.1 -urllib3==1.26.5 -usb-creator==0.3.7 -uvicorn==0.24.0.post1 -wadllib==1.3.6 -websocket-client==1.2.3 -wsproto==1.0.0 -xdg==5 -xkit==0.0.0 -zipp==1.0.0 diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..2f65137 --- /dev/null +++ b/test.txt @@ -0,0 +1 @@ +Este es un archivo de prueba.