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

Modify script to enable multiple resources in one file #8

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all 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
257 changes: 132 additions & 125 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -23,141 +23,148 @@ for FILEPATH in $FILES; do
exit 1
fi


if [[ ${MODE} == "IMAGE_TAG" ]]; then
SUPPORTED_OBJECT_KINDS=(Deployment StatefulSet CronJob Kustomization)
if [ -z "${NEW_IMAGE_TAG}" ]; then
echo " +++++++++ ERROR NEW_IMAGE_TAG \"${NEW_IMAGE_TAG}\" is not correct " >&2
exit 1
fi

objectKind=$(yq r ${FILEPATH} kind)
echo " +++ + Detected Object kind as \"${objectKind}\" "

if [[ ! " ${SUPPORTED_OBJECT_KINDS[@]} " =~ " ${objectKind} " ]]; then
echo " +++++++++ ERROR Object kind \"${objectKind}\" is not part of the supported values [ ${SUPPORTED_OBJECT_KINDS[@]} ] for file ${FILEPATH} " >&2
exit 1
fi

if [[ ${objectKind} == "Deployment" ]] || [[ ${objectKind} == "StatefulSet" ]] ; then
containerPosition=$(yq r ${FILEPATH} spec.template.spec.containers.*.name | grep -n ${CONTAINER_NAME}$ | cut -d: -f1)
containerIndex=$((${containerPosition/M/}-1))
if (( ${containerIndex} < 0 )) ; then
echo " +++++++++ ERROR container with name ${CONTAINER_NAME} could not be found in file ${FILEPATH}" >&2
NUM_DOCUMENTS=$(cat $FILEPATH | grep -E '^kind:' | wc -l)
echo " +++ Found ${NUM_DOCUMENTS} resources in ${FILEPATH}"
i=0
while [[ $i -lt $NUM_DOCUMENTS ]] ; do
echo " +++ Processing document ${i}"

if [[ ${MODE} == "IMAGE_TAG" ]]; then
SUPPORTED_OBJECT_KINDS=(Deployment StatefulSet CronJob Kustomization)
if [ -z "${NEW_IMAGE_TAG}" ]; then
echo " +++++++++ ERROR NEW_IMAGE_TAG \"${NEW_IMAGE_TAG}\" is not correct " >&2
exit 1
fi

echo " +++ + Container Index $containerIndex"
currentImageValue=$(yq r ${FILEPATH} spec.template.spec.containers[${containerIndex}].image)
if [[ ${currentImageValue} == "null" ]]; then
echo " +++++++++ ERROR Cannot find image field for container named ${CONTAINER_NAME} in file ${FILEPATH} " >&2

objectKind=$(yq -d${i} r ${FILEPATH} kind)
echo " +++ + Detected Object kind as \"${objectKind}\" "

if [[ ! " ${SUPPORTED_OBJECT_KINDS[@]} " =~ " ${objectKind} " ]]; then
echo " +++++++++ ERROR Object kind \"${objectKind}\" is not part of the supported values [ ${SUPPORTED_OBJECT_KINDS[@]} ] for file ${FILEPATH} " >&2
exit 1
fi

echo " +++ + + Processing image from $currentImageValue"

imageFullName=$(grep -Po '\K.*?(?=:)' <<< ${currentImageValue})
if [ -z "${imageFullName}" ]; then imageFullName=${currentImageValue}; fi
echo " +++ + + to new image tag ${imageFullName}:${NEW_IMAGE_TAG}"
sed -i "s+${currentImageValue}+${imageFullName}:${NEW_IMAGE_TAG}+g" ${FILEPATH}
fi

if [[ ${objectKind} == "CronJob" ]] ; then
containerPosition=$(yq r ${FILEPATH} spec.jobTemplate.spec.template.spec.containers.*.name | grep -n ${CONTAINER_NAME}$ | cut -d: -f1)
containerIndex=$((${containerPosition/M/}-1))
if (( ${containerIndex} < 0 )); then
echo " +++++++++ ERROR container with name ${CONTAINER_NAME} could not be found in file CronJob ${FILEPATH}" >&2
exit 1

if [[ ${objectKind} == "Deployment" ]] || [[ ${objectKind} == "StatefulSet" ]] ; then
containerPosition=$(yq -d${i} r ${FILEPATH} spec.template.spec.containers.*.name | grep -n ${CONTAINER_NAME}$ | cut -d: -f1)
containerIndex=$((${containerPosition/M/}-1))
if (( ${containerIndex} < 0 )) ; then
echo " +++++++++ ERROR container with name ${CONTAINER_NAME} could not be found in file ${FILEPATH}" >&2
exit 1
fi

echo " +++ + Container Index $containerIndex"
currentImageValue=$(yq -d${i} r ${FILEPATH} spec.template.spec.containers[${containerIndex}].image)
if [[ ${currentImageValue} == "null" ]]; then
echo " +++++++++ ERROR Cannot find image field for container named ${CONTAINER_NAME} in file ${FILEPATH} " >&2
exit 1
fi

echo " +++ + + Processing image from $currentImageValue"

imageFullName=$(grep -Po '\K.*?(?=:)' <<< ${currentImageValue})
if [ -z "${imageFullName}" ]; then imageFullName=${currentImageValue}; fi
echo " +++ + + to new image tag ${imageFullName}:${NEW_IMAGE_TAG}"
sed -i "s+${currentImageValue}+${imageFullName}:${NEW_IMAGE_TAG}+g" ${FILEPATH}
fi

echo " +++ + Container Index in CronJob $containerIndex"
currentImageValue=$(yq r ${FILEPATH} spec.jobTemplate.spec.template.spec.containers[${containerIndex}].image)
if [[ ${currentImageValue} == "null" ]]; then
echo " +++++++++ ERROR Cannot find image field for container named ${CONTAINER_NAME} in file ${FILEPATH} " >&2
exit 1

if [[ ${objectKind} == "CronJob" ]] ; then
containerPosition=$(yq -d${i} r ${FILEPATH} spec.jobTemplate.spec.template.spec.containers.*.name | grep -n ${CONTAINER_NAME}$ | cut -d: -f1)
containerIndex=$((${containerPosition/M/}-1))
if (( ${containerIndex} < 0 )); then
echo " +++++++++ ERROR container with name ${CONTAINER_NAME} could not be found in file CronJob ${FILEPATH}" >&2
exit 1
fi

echo " +++ + Container Index in CronJob $containerIndex"
currentImageValue=$(yq -d${i} r ${FILEPATH} spec.jobTemplate.spec.template.spec.containers[${containerIndex}].image)
if [[ ${currentImageValue} == "null" ]]; then
echo " +++++++++ ERROR Cannot find image field for container named ${CONTAINER_NAME} in file ${FILEPATH} " >&2
exit 1
fi

echo " +++ + + Processing image from $currentImageValue"

imageFullName=$(grep -Po '\K.*?(?=:)' <<< ${currentImageValue})
if [ -z "${imageFullName}" ]; then imageFullName=${currentImageValue}; fi
echo " +++ + + to new image tag ${imageFullName}:${NEW_IMAGE_TAG}"
sed -i "s+${currentImageValue}+${imageFullName}:${NEW_IMAGE_TAG}+g" ${FILEPATH}
fi

echo " +++ + + Processing image from $currentImageValue"

imageFullName=$(grep -Po '\K.*?(?=:)' <<< ${currentImageValue})
if [ -z "${imageFullName}" ]; then imageFullName=${currentImageValue}; fi
echo " +++ + + to new image tag ${imageFullName}:${NEW_IMAGE_TAG}"
sed -i "s+${currentImageValue}+${imageFullName}:${NEW_IMAGE_TAG}+g" ${FILEPATH}
fi


if [[ ${objectKind} == "Kustomization" ]] ; then
kustomizeBuildPath="${FILEPATH%/*}"
echo " +++ + Building kustomize in directory ${kustomizeBuildPath}"
fullKustomizeBuild=$(kustomize build ${kustomizeBuildPath})

delimiter="---"
s=$fullKustomizeBuild$delimiter
kustomizeImageNameToUpdate=""
while [[ $s ]]; do
object="${s%%"$delimiter"*}"
containerPosition=$(echo "$object" | yq r - spec.template.spec.containers.*.name | grep -n ${CONTAINER_NAME}$ | cut -d: -f1)
if [[ $containerPosition ]]; then
containerIndex=$((${containerPosition/M/}-1))
currentImageValue=$(echo "$object" | yq r - spec.template.spec.containers[${containerIndex}].image)
if [[ ! $currentImageValue ]]; then
currentImageValue=$(echo "$object" | yq r - spec.jobTemplate.spec.template.spec.containers[${containerIndex}].image)


if [[ ${objectKind} == "Kustomization" ]] ; then
kustomizeBuildPath="${FILEPATH%/*}"
echo " +++ + Building kustomize in directory ${kustomizeBuildPath}"
fullKustomizeBuild=$(kustomize build ${kustomizeBuildPath})

delimiter="---"
s=$fullKustomizeBuild$delimiter
kustomizeImageNameToUpdate=""
while [[ $s ]]; do
object="${s%%"$delimiter"*}"
containerPosition=$(echo "$object" | yq -d${i} r - spec.template.spec.containers.*.name | grep -n ${CONTAINER_NAME}$ | cut -d: -f1)
if [[ $containerPosition ]]; then
containerIndex=$((${containerPosition/M/}-1))
currentImageValue=$(echo "$object" | yq -d${i} r - spec.template.spec.containers[${containerIndex}].image)
if [[ ! $currentImageValue ]]; then
currentImageValue=$(echo "$object" | yq -d${i} r - spec.jobTemplate.spec.template.spec.containers[${containerIndex}].image)
fi
imageFullName=$(grep -Po '\K.*?(?=:)' <<< ${currentImageValue})
if [ -z "${imageFullName}" ]; then imageFullName=${currentImageValue}; fi
kustomizeImageNameToUpdate=${imageFullName}
fi
imageFullName=$(grep -Po '\K.*?(?=:)' <<< ${currentImageValue})
if [ -z "${imageFullName}" ]; then imageFullName=${currentImageValue}; fi
kustomizeImageNameToUpdate=${imageFullName}
fi
s=${s#*"$delimiter"};
done;

if [[ ! $kustomizeImageNameToUpdate ]]; then
echo " +++++++++ ERROR container with name ${CONTAINER_NAME} could not be found in any file build by kustomize from folder ${kustomizeBuildPath}" >&2
exit 1
s=${s#*"$delimiter"};
done;

if [[ ! $kustomizeImageNameToUpdate ]]; then
echo " +++++++++ ERROR container with name ${CONTAINER_NAME} could not be found in any file build by kustomize from folder ${kustomizeBuildPath}" >&2
exit 1
fi

kustomizeImageNamePosition=$(yq -d${i} r ${FILEPATH} images.*.name | grep -n ${kustomizeImageNameToUpdate} | cut -d: -f1)
kustomizeContainerIndex=$((${kustomizeImageNamePosition/M/}-1))
kustomizeCurrentNewTagValue=$(yq -d${i} r ${FILEPATH} images[${kustomizeContainerIndex}].newTag)

echo " +++ + + Processing newTag for image name: $kustomizeImageNameToUpdate"
echo " +++ + + + from newTag: ${kustomizeCurrentNewTagValue}"
echo " +++ + + + to newTag: ${NEW_IMAGE_TAG}"
sed -i "s+${kustomizeCurrentNewTagValue}+${NEW_IMAGE_TAG}+g" ${FILEPATH}
fi

kustomizeImageNamePosition=$(yq r ${FILEPATH} images.*.name | grep -n ${kustomizeImageNameToUpdate} | cut -d: -f1)
kustomizeContainerIndex=$((${kustomizeImageNamePosition/M/}-1))
kustomizeCurrentNewTagValue=$(yq r ${FILEPATH} images[${kustomizeContainerIndex}].newTag)

echo " +++ + + Processing newTag for image name: $kustomizeImageNameToUpdate"
echo " +++ + + + from newTag: ${kustomizeCurrentNewTagValue}"
echo " +++ + + + to newTag: ${NEW_IMAGE_TAG}"
sed -i "s+${kustomizeCurrentNewTagValue}+${NEW_IMAGE_TAG}+g" ${FILEPATH}
fi
fi

if [[ ${MODE} == "ENV_VAR" ]]; then
SUPPORTED_OBJECT_KINDS=(Deployment StatefulSet)
objectKind=$(yq r ${FILEPATH} kind)
echo " +++ + Detected Object kind as \"${objectKind}\" "

if [[ ! " ${SUPPORTED_OBJECT_KINDS[@]} " =~ " ${objectKind} " ]]; then
echo " +++++++++ ERROR Object kind \"${objectKind}\" is not part of the supported values [ ${SUPPORTED_OBJECT_KINDS[@]} ] for file ${FILEPATH} " >&2
exit 1
fi

if [[ ${objectKind} == "Deployment" ]] || [[ ${objectKind} == "StatefulSet" ]] ; then
containerPosition=$(yq r ${FILEPATH} spec.template.spec.containers.*.name | grep -n ${CONTAINER_NAME}$ | cut -d: -f1)
containerIndex=$((${containerPosition/M/}-1))
if (( ${containerIndex} < 0 )); then
echo " +++++++++ ERROR container with name ${CONTAINER_NAME} could not be found in file ${FILEPATH}" >&2

if [[ ${MODE} == "ENV_VAR" ]]; then
SUPPORTED_OBJECT_KINDS=(Deployment StatefulSet)
objectKind=$(yq -d${i} r ${FILEPATH} kind)
echo " +++ + Detected Object kind as \"${objectKind}\" "

if [[ ! " ${SUPPORTED_OBJECT_KINDS[@]} " =~ " ${objectKind} " ]]; then
echo " +++++++++ ERROR Object kind \"${objectKind}\" is not part of the supported values [ ${SUPPORTED_OBJECT_KINDS[@]} ] for file ${FILEPATH} " >&2
exit 1
fi

echo " +++ + Container Index $containerIndex"
envPosition=$(yq r ${FILEPATH} spec.template.spec.containers[${containerIndex}].env[*].name | grep -n ${ENV_NAME}$ | cut -d: -f1)
envIndex=$((${envPosition/M/}-1))
if (( ${envIndex} < 0 )); then
echo " +++++++++ ERROR Environment variable with name ${ENV_NAME} not found in ${CONTAINER_NAME}" >&2
exit 1

if [[ ${objectKind} == "Deployment" ]] || [[ ${objectKind} == "StatefulSet" ]] ; then
containerPosition=$(yq -d${i} r ${FILEPATH} spec.template.spec.containers.*.name | grep -n ${CONTAINER_NAME}$ | cut -d: -f1)
containerIndex=$((${containerPosition/M/}-1))
if (( ${containerIndex} < 0 )); then
echo " +++++++++ ERROR container with name ${CONTAINER_NAME} could not be found in file ${FILEPATH}" >&2
exit 1
fi

echo " +++ + Container Index $containerIndex"
envPosition=$(yq -d${i} r ${FILEPATH} spec.template.spec.containers[${containerIndex}].env[*].name | grep -n ${ENV_NAME}$ | cut -d: -f1)
envIndex=$((${envPosition/M/}-1))
if (( ${envIndex} < 0 )); then
echo " +++++++++ ERROR Environment variable with name ${ENV_NAME} not found in ${CONTAINER_NAME}" >&2
exit 1
fi
currentEnvValue=$(yq -d${i} r ${FILEPATH} spec.template.spec.containers[${containerIndex}].env[${envIndex}].value)

echo " +++ + + Updating ${ENV_NAME} in container ${CONTAINER_NAME} from ${currentEnvValue}"
echo " +++ + + To env ${ENV_NAME} in container ${CONTAINER_NAME} to ${NEW_ENV_VALUE}"
sanitizedOldString=$(echo $currentEnvValue | sed 's/[][`~!@#$%^&*()-+{}\|;:_=",<.>/?'"'"']/\\&/g')
sanitizedNewString=$(echo $NEW_ENV_VALUE | sed 's/[][`~!@#$%^&*()-+{}\|;:_=",<.>/?'"'"']/\\&/g')
sed -i "s+${sanitizedOldString}+${sanitizedNewString}+g" ${FILEPATH}
fi
currentEnvValue=$(yq r ${FILEPATH} spec.template.spec.containers[${containerIndex}].env[${envIndex}].value)

echo " +++ + + Updating ${ENV_NAME} in container ${CONTAINER_NAME} from ${currentEnvValue}"
echo " +++ + + To env ${ENV_NAME} in container ${CONTAINER_NAME} to ${NEW_ENV_VALUE}"
sanitizedOldString=$(echo $currentEnvValue | sed 's/[][`~!@#$%^&*()-+{}\|;:_=",<.>/?'"'"']/\\&/g')
sanitizedNewString=$(echo $NEW_ENV_VALUE | sed 's/[][`~!@#$%^&*()-+{}\|;:_=",<.>/?'"'"']/\\&/g')
sed -i "s+${sanitizedOldString}+${sanitizedNewString}+g" ${FILEPATH}
fi
fi;
fi;
(( i += 1 ))
done
done