Skip to content

Commit

Permalink
refactor: move cleanup into configuration step to ensure removed rout…
Browse files Browse the repository at this point in the history
…es are removed from envvars
  • Loading branch information
shreddedbacon committed Oct 19, 2023
1 parent 7356943 commit d32ac9c
Showing 1 changed file with 69 additions and 60 deletions.
129 changes: 69 additions & 60 deletions legacy/build-deploy-docker-compose.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,6 @@ currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "serviceConfiguration2Complete" "Service Configuration Phase 2"
previousStepEnd=${currentStepEnd}
beginBuildStep "Route/Ingress Configuration" "configuringRoutes"
set -x

TEMPLATE_PARAMETERS=()

Expand Down Expand Up @@ -1181,10 +1180,71 @@ fi

# apply the currently templated components out so that the route and lagoon-env configmaps gets what they need
if [ "$(ls -A $YAML_FOLDER/)" ]; then
find $YAML_FOLDER -type f -exec cat {} \;
if [ "$(featureFlag LOG_DEBUGGING)" = enabled ]; then
find $YAML_FOLDER -type f -exec cat {} \;
fi
kubectl apply -n ${NAMESPACE} -f $YAML_FOLDER/
fi

currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "configuringRoutesComplete" "Route/Ingress Configuration"
previousStepEnd=${currentStepEnd}
beginBuildStep "Route/Ingress Cleanup" "cleanupRoutes"

##############################################
### CLEANUP Ingress/routes which have been removed from .lagoon.yml
##############################################

set +x
# collect the current routes, its possible to exclude ingress by adding a label 'lagoon.sh/noclean=true' and it won't get deleted
CURRENT_ROUTES=$(kubectl -n ${NAMESPACE} get ingress -l "lagoon.sh/noclean!=true" -l "lagoon.sh/autogenerated!=true" --no-headers | cut -d " " -f 1 | xargs)
# collect the routes that Lagoon thinks it should have based on the .lagoon.yml and any routes that have come from the api
# using the build-deploy-tool generator
YAML_ROUTES_TO_JSON=$(build-deploy-tool identify created-ingress | jq -r '.secondary[]')

MATCHED_INGRESS=false
DELETE_INGRESS=()
# loop over the routes from kubernetes
for SINGLE_ROUTE in ${CURRENT_ROUTES}; do
# loop over the routes that Lagoon thinks it should have
for YAML_ROUTE in ${YAML_ROUTES_TO_JSON}; do
if [ "${SINGLE_ROUTE}" == "${YAML_ROUTE}" ]; then
MATCHED_INGRESS=true
continue
fi
done
if [ "${MATCHED_INGRESS}" != "true" ]; then
DELETE_INGRESS+=($SINGLE_ROUTE)
fi
MATCHED_INGRESS=false
done

if [ ${#DELETE_INGRESS[@]} -ne 0 ]; then
echo ">> Lagoon detected routes that have been removed from the .lagoon.yml or Lagoon API"
if [ "$(featureFlag CLEANUP_REMOVED_LAGOON_ROUTES)" != enabled ]; then
echo "> You can remove these in the next build by setting the flag 'LAGOON_FEATURE_FLAG_CLEANUP_REMOVED_LAGOON_ROUTES=enabled' as a GLOBAL scoped variable to this environment or project"
fi
for DI in ${DELETE_INGRESS[@]}
do
if [ "$(featureFlag CLEANUP_REMOVED_LAGOON_ROUTES)" = enabled ]; then
if kubectl -n ${NAMESPACE} get ingress ${DI} &> /dev/null; then
echo ">> Removing ingress ${DI}"
kubectl -n ${NAMESPACE} delete ingress ${DI}
#delete anything else?
fi
else
echo "> The route '${DI}' would be removed, and any other associated "
fi
done
else
echo "No route cleanup required"
fi

currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "routeCleanupComplete" "Route/Ingress Cleanup"
previousStepEnd=${currentStepEnd}
beginBuildStep "Update Configmap" "updateConfigmap"

##############################################
### PROJECT WIDE ENV VARIABLES
##############################################
Expand Down Expand Up @@ -1252,7 +1312,6 @@ if [ ! -z "$LAGOON_ENVIRONMENT_VARIABLES" ]; then
-p "{\"data\":$(echo $LAGOON_ENVIRONMENT_VARIABLES | jq -r 'map( select(.scope == "runtime" or .scope == "global") ) | map( { (.name) : .value } ) | add | tostring')}"
fi
fi
set -x

if [ "$BUILD_TYPE" == "pullrequest" ]; then
kubectl patch \
Expand Down Expand Up @@ -1291,9 +1350,8 @@ do
esac
done

set +x
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "routeConfigurationComplete" "Route/Ingress Configuration"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "updateConfigmapComplete" "Update Configmap"
previousStepEnd=${currentStepEnd}
beginBuildStep "Image Push to Registry" "pushingImages"
set -x
Expand Down Expand Up @@ -1456,7 +1514,9 @@ fi
set -x

if [ "$(ls -A $YAML_FOLDER/)" ]; then
find $YAML_FOLDER -type f -exec cat {} \;
if [ "$(featureFlag LOG_DEBUGGING)" = enabled ]; then
find $YAML_FOLDER -type f -exec cat {} \;
fi
kubectl apply -n ${NAMESPACE} -f $YAML_FOLDER/
fi

Expand Down Expand Up @@ -1636,7 +1696,9 @@ if [ "$(ls -A $YAML_FOLDER/)" ]; then
find $YAML_FOLDER -type f -print0 | xargs -0 sed -i s/ReadWriteMany/ReadWriteOnce/g
fi

find $YAML_FOLDER -type f -exec cat {} \;
if [ "$(featureFlag LOG_DEBUGGING)" = enabled ]; then
find $YAML_FOLDER -type f -exec cat {} \;
fi
kubectl apply -n ${NAMESPACE} -f $YAML_FOLDER/
fi
set -x
Expand Down Expand Up @@ -1717,59 +1779,6 @@ set +x
currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "cronjobCleanupComplete" "Cronjob Cleanup"
previousStepEnd=${currentStepEnd}
beginBuildStep "Route/Ingress Cleanup" "cleanupRemovedRoutes"

##############################################
### CLEANUP Ingress/routes which have been removed from .lagoon.yml
##############################################

# collect the current routes, its possible to exclude ingress by adding a label 'lagoon.sh/noclean=true' and it won't get deleted
CURRENT_ROUTES=$(kubectl -n ${NAMESPACE} get ingress -l "lagoon.sh/noclean!=true" -l "lagoon.sh/autogenerated!=true" --no-headers | cut -d " " -f 1 | xargs)
# collect the routes that Lagoon thinks it should have based on the .lagoon.yml and any routes that have come from the api
# using the build-deploy-tool generator
YAML_ROUTES_TO_JSON=$(build-deploy-tool identify created-ingress | jq -r '.secondary[]')

MATCHED_INGRESS=false
DELETE_INGRESS=()
# loop over the routes from kubernetes
for SINGLE_ROUTE in ${CURRENT_ROUTES}; do
# loop over the routes that Lagoon thinks it should have
for YAML_ROUTE in ${YAML_ROUTES_TO_JSON}; do
if [ "${SINGLE_ROUTE}" == "${YAML_ROUTE}" ]; then
MATCHED_INGRESS=true
continue
fi
done
if [ "${MATCHED_INGRESS}" != "true" ]; then
DELETE_INGRESS+=($SINGLE_ROUTE)
fi
MATCHED_INGRESS=false
done

if [ ${#DELETE_INGRESS[@]} -ne 0 ]; then
echo ">> Lagoon detected routes that have been removed from the .lagoon.yml or Lagoon API"
if [ "$(featureFlag CLEANUP_REMOVED_LAGOON_ROUTES)" != enabled ]; then
echo "> You can remove these in the next build by setting the flag 'LAGOON_FEATURE_FLAG_CLEANUP_REMOVED_LAGOON_ROUTES=enabled' as a GLOBAL scoped variable to this environment or project"
fi
for DI in ${DELETE_INGRESS[@]}
do
if [ "$(featureFlag CLEANUP_REMOVED_LAGOON_ROUTES)" = enabled ]; then
if kubectl -n ${NAMESPACE} get ingress ${DI} &> /dev/null; then
echo ">> Removing ingress ${DI} because it was removed"
kubectl -n ${NAMESPACE} delete ingress ${DI}
#delete anything else?
fi
else
echo "> The route '${DI}' would be removed, and any other associated "
fi
done
else
echo "No route cleanup required"
fi

currentStepEnd="$(date +"%Y-%m-%d %H:%M:%S")"
patchBuildStep "${buildStartTime}" "${previousStepEnd}" "${currentStepEnd}" "${NAMESPACE}" "cleanupRemovedRoutesComplete" "Route/Ingress Cleanup"
previousStepEnd=${currentStepEnd}
beginBuildStep "Post-Rollout Tasks" "runningPostRolloutTasks"
set -x

Expand Down

0 comments on commit d32ac9c

Please sign in to comment.