Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPSEXP-1776: refactor dbp verify helm action to allow for keeping logs #173

Merged
merged 22 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/actions/dbp-charts/kubernetes-valid-ns/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: kubernetes valid namespace
description: |
Generate a sanitized namespace name for kubernetes to use based on the release
name, ap version and some other parameters
inputs:
branch_name:
description: name of the git branch we're executing from
required: true
release_prefix:
description: a release prefix to differentiate from other releases
required: true
outputs:
namespace:
description: generated namespace
value: ${{ steps.genns.outputs.namespace }}
runs:
using: composite
steps:
- name: generate namespace
id: genns
shell: bash
env:
BRANCH_NAME: ${{ inputs.branch_name }}
RELEASE_PREFIX: ${{ inputs.release_prefix }}
run: |
BRANCH_PREFIX="${BRANCH_NAME:0:28}"
BRANCH_NS="${BRANCH_PREFIX//[^[:alnum:]]/-}"
RELEASE_NS=$"${RELEASE_PREFIX//[^[:alnum:]]/-}"
NS_SUFFIX=${GITHUB_RUN_NUMBER:-0}
echo namespace="${BRANCH_NS,,}"-"${RELEASE_NS,,}"-"${NS_SUFFIX}" >> $GITHUB_OUTPUT
25 changes: 23 additions & 2 deletions .github/actions/dbp-charts/verify-helm/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ runs:
uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: aws-actions/configure-aws-credentials@v1
- uses: aws-actions/configure-aws-credentials@v1-node16
with:
aws-region: ${{ inputs.aws_region }}
aws-access-key-id: ${{ inputs.aws_access_key_id }}
Expand All @@ -89,14 +89,35 @@ runs:
- name: Get commit msg
uses: >-
Alfresco/alfresco-build-tools/.github/actions/[email protected]
- name: Get a namespace to deploy on
id: k8sns
uses: >-
Alfresco/alfresco-build-tools/.github/actions/dbp-charts/kubernetes-valid-ns@OPSEXP-1776
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remember that you probably want to immediately do a new release changing those references in the release PR after merging this PR

with:
branch_name: ${{ env.BRANCH_NAME }}
release_prefix: ${{ inputs.release_prefix }}
- name: Run helm chart test script
run: ${{ github.action_path }}/helm_install.sh
shell: bash
env:
K8SNS: ${{ steps.k8sns.outputs.namespace }}
ACM_CERTIFICATE: ${{ inputs.acm_certificate }}
AWS_SG: ${{ inputs.aws_sg }}
DOMAIN: ${{ inputs.domain }}
ACS_VERSION: ${{ inputs.acs_version }}
RELEASE_PREFIX: ${{ inputs.release_prefix }}
PROJECT_NAME: ${{ inputs.chart_name }}
RELEASE_PREFIX: ${{ inputs.release_prefix }}
TEST_NEWMAN: ${{ inputs.test_newman }}
- name: Upload helm deployments logs as artifacts
if: always()
uses: >-
Alfresco/alfresco-build-tools/.github/actions/kubectl-keep-nslogs@OPSEXP-1776
with:
namespace: ${{ steps.k8sns.outputs.namespace }}
- name: Uninstall Helm releases
if: always() && ! contains(env.COMMIT_MESSAGE, '[keep env]')
run: ${{ github.action_path }}/helm_delete.sh
shell: bash
env:
RELEASE_PREFIX: ${{ inputs.release_prefix }}
K8SNS: ${{ steps.k8sns.outputs.namespace }}
14 changes: 14 additions & 0 deletions .github/actions/dbp-charts/verify-helm/helm_delete.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash +e
#
gionn marked this conversation as resolved.
Show resolved Hide resolved

out_of_ns_ingress_cleanup() {
echo "Something went wrong uninstalling ingress release... Need to do some cleanup"
for i in clusterrole clusterrolebindings; do kubectl delete "$i" "ing-${RELEASE_PREFIX}-${GITHUB_RUN_NUMBER}-ingress-nginx"
done
}

echo "cleaning up releases"
helm delete "ing-${RELEASE_PREFIX}-${GITHUB_RUN_NUMBER}" -n "${K8SNS}" || out_of_ns_ingress_cleanup
helm delete "${RELEASE_PREFIX}-${GITHUB_RUN_NUMBER}" -n "${K8SNS}"
kubectl delete secret quay-registry-secret -n "${K8SNS}"
kubectl delete namespace "${K8SNS}" --grace-period=1
16 changes: 5 additions & 11 deletions .github/actions/dbp-charts/verify-helm/helm_install.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
#!/bin/bash -e

clean_up () {
if [[ "${COMMIT_MESSAGE}" != *"[keep env]"* ]]; then
echo "cleaning up..."
set +e
helm delete "${release_name_ingress}" "${release_name}" -n "${namespace}"
kubectl delete secret quay-registry-secret -n "${namespace}"
kubectl delete namespace "${namespace}" --grace-period=1
fi
}
trap clean_up EXIT
if [ -n "$K8SNS" ]; then echo "Deploying in namespace $K8SNS"
namespace=$K8SNS
else echo "Could not find a namespace set in the NS env variable"
exit 3
fi

namespace=$(echo "${BRANCH_NAME}" | cut -c1-28 | tr /_ - | tr -d '[:punct:]' | awk '{print tolower($0)}')"-${RELEASE_PREFIX}-${GITHUB_RUN_NUMBER}"
release_name_ingress="ing-${RELEASE_PREFIX}-${GITHUB_RUN_NUMBER}"
release_name="${RELEASE_PREFIX}-${GITHUB_RUN_NUMBER}"
HOST=${namespace}.${DOMAIN}
Expand Down
44 changes: 44 additions & 0 deletions .github/actions/kubectl-keep-nslogs/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
name: upload k8s logs as artifacts
description: >
Upload Kubernetes logs (jobs and pods) as a github artifact.
Special attention must be payed to repo's storage limits.
inputs:
namespace:
description: The kubernetes namespace to get logs from
required: false
default: default
log_retention:
description: Number of days to keep the logs for
required: false
default: "10"
runs:
using: composite
steps:
- id: artifactdir
name: Create artifact directory
shell: bash
run: |
ARTIFACTS_DIR=$(mktemp -d)
echo "ARTIFACTS_DIR=${ARTIFACTS_DIR}" >> $GITHUB_OUTPUT
- name: Fetch logs
shell: bash
working-directory: ${{ steps.artifactdir.outputs.ARTIFACTS_DIR }}
run: |
echo "checking pwd (should be ${{ steps.artifactdir.outputs.ARTIFACTS_DIR }})"
pwd
gionn marked this conversation as resolved.
Show resolved Hide resolved
kubectl config set-context --current --namespace=${{ inputs.namespace }}
for type in deployment.apps statefulset.apps jobs; do
[ -d "${type}" ] || mkdir "${type}"
for i in $(kubectl get "$type" -o name); do \
kubectl logs "$i" --all-containers=true | \
gzip > "${i}.log.gz"
done
done
- name: upload kubernetes logs as artifact
uses: actions/[email protected]
with:
name: >-
${{ inputs.namespace }}_logs.${{ github.run_number }}.${{ github.run_attempt }}
alxgomz marked this conversation as resolved.
Show resolved Hide resolved
retention-days: ${{ inputs.log_retention }}
path: ${{ steps.artifactdir.outputs.ARTIFACTS_DIR }}