From 189fb5ae0cf904436820b9849b2a103258259a1c Mon Sep 17 00:00:00 2001 From: andhreljaKern Date: Wed, 2 Oct 2024 12:35:26 +0200 Subject: [PATCH] feat: introduce K8 delete workflow --- .github/workflows/k8s_delete.yml | 68 ++++++++++++++++++++++++++++++++ k8s/delete.sh | 47 ++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 .github/workflows/k8s_delete.yml create mode 100644 k8s/delete.sh diff --git a/.github/workflows/k8s_delete.yml b/.github/workflows/k8s_delete.yml new file mode 100644 index 0000000..7022dc8 --- /dev/null +++ b/.github/workflows/k8s_delete.yml @@ -0,0 +1,68 @@ +name: 'K8: Delete' + +on: + workflow_call: + inputs: + deployment_name: + description: 'Deployment Name' + required: true + type: string + +# Special permissions required for OIDC authentication +permissions: + id-token: write + contents: read + actions: read + +jobs: + k8-cluster-delete: + name: 'K8: Delete Cluster Resources' + runs-on: [self-hosted, "${{ github.ref_name }}"] + environment: ${{ github.ref_name }} + env: + KUBERNETES_NAMESPACE: "${{ vars.KUBERNETES_NAMESPACE }}" + KUBERNETES_CLUSTER_NAME: "${{ vars.KUBERNETES_CLUSTER_NAME }}" + KUBERNETES_MANIFEST_PATH: "${{ vars.KUBERNETES_MANIFEST_PATH }}" + AZURE_RESOURCE_GROUP: "${{ vars.AZURE_RESOURCE_GROUP }}" + AZURE_STORAGE_ACCOUNT: ${{ vars.AZURE_STORAGE_ACCOUNT }} + AZURE_STORAGE_FILE_SHARE: ${{ vars.AZURE_STORAGE_FILE_SHARE }} + AZCOPY_VERSION: "v10" + steps: + # Checkout the repository to the GitHub Actions runner + - name: Checkout + uses: actions/checkout@v4 + + - name: GitHub Configuration + run: git config --global url."https://oauth2:${{ secrets.GH_TOKEN }}@github.com".insteadOf https://github.com + + - name: Clone cicd-deployment-scripts + run: git clone https://github.com/code-kern-ai/cicd-deployment-scripts.git + + # Install the latest version of Kubernetes CLI and configure the Kubernetes CLI configuration file with a Kubernetes Cloud user API token + - name: Azure Login + uses: azure/login@v2 + with: + client-id: ${{ secrets.AZURE_CLIENT_ID }} + tenant-id: ${{ secrets.AZURE_TENANT_ID }} + subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} + + # Use kubelogin to configure your kubeconfig for Azure auth + - name: Set up kubelogin for non-interactive login + uses: azure/use-kubelogin@v1 + with: + kubelogin-version: 'v0.0.25' + + - uses: azure/aks-set-context@v3 + with: + resource-group: ${{ env.AZURE_RESOURCE_GROUP }} + cluster-name: ${{ env.KUBERNETES_CLUSTER_NAME }} + admin: 'false' + use-kubelogin: 'true' + + - name: Delete Cluster Deployment + shell: bash + run: | + bash cicd-deployment-scripts/k8s/delete.sh \ + -n ${{ env.KUBERNETES_NAMESPACE }} \ + -d ${{ inputs.deployment_name }} + \ No newline at end of file diff --git a/k8s/delete.sh b/k8s/delete.sh new file mode 100644 index 0000000..0b77879 --- /dev/null +++ b/k8s/delete.sh @@ -0,0 +1,47 @@ +# !/bin/bash +set -e + +KUBERNETES_NAMESPACE="" +KUBERNETES_DEPLOYMENT_NAME="" + +while getopts n:d: flag +do + case "${flag}" in + n) KUBERNETES_NAMESPACE=${OPTARG};; + d) KUBERNETES_DEPLOYMENT_NAME=${OPTARG};; + esac +done + +declare -A secret_rename_mapping=( \ + ["cognition-gateway"]="cg-gateway" \ + ["cognition-pdf2md"]="cg-gateway" \ + ["cognition-task-master"]="cg-task-master" \ + ["gates-gateway"]="gt-gateway" \ + ["kratos"]="kratos" \ + ["oathkeeper"]="oathkeeper" \ + ["object-storage"]="obj-storage" \ + ["platform-monitoring"]="plfm-monitor" \ + ["refinery-commercial-proxy"]="rf-comm-proxy" \ + ["refinery-config"]="rf-config" \ + ["refinery-doc-ock"]="rf-doc-ock" \ + ["refinery-embedder"]="rf-embedder" \ + ["refinery-gateway"]="rf-gateway" \ + ["refinery-gateway-proxy"]="rf-gw-proxy" \ + ["refinery-model-provider"]="rf-mdl-prvd" \ + ["refinery-neural-search"]="rf-nrl-search" \ + ["refinery-tokenizer"]="rf-tokenizer" \ + ["refinery-updater"]="rf-updater" \ + ["refinery-weak-supervisor"]="rf-weak-supvsr" \ + ["refinery-websocket"]="rf-websocket" \ + ["refinery-zero-shot"]="rf-zero-shot" \ +) + +kubectl config set-context --current --namespace=$KUBERNETES_NAMESPACE +echo "Context set to namespace: \"$KUBERNETES_NAMESPACE\"" + +KUBERNETES_SERVICE_NAME=$KUBERNETES_DEPLOYMENT_NAME +KUBERNETES_SECRET_NAME=${secret_rename_mapping[$KUBERNETES_DEPLOYMENT_NAME]} + +kubectl delete deployment $KUBERNETES_DEPLOYMENT_NAME +kubectl delete service $KUBERNETES_SERVICE_NAME +kubectl delete secret $KUBERNETES_SECRET_NAME \ No newline at end of file