[go: nahoru, domu]

Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #110 from turkenh/script-only-component
Browse files Browse the repository at this point in the history
Local dev: add script only component support
  • Loading branch information
turkenh committed Sep 2, 2020
2 parents d6994cc + c18aad5 commit 8da3758
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 22 deletions.
4 changes: 3 additions & 1 deletion makelib/local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export KUBECTL
export HELM
export HELM3
export GOMPLATE
export ISTIO
export ISTIO_VERSION
export BUILD_REGISTRY
export ROOT_DIR
export SCRIPTS_DIR
Expand Down Expand Up @@ -121,7 +123,7 @@ local.up: local.prepare kind.up local.helminit

local.down: kind.down local.clean

local.deploy.%: local.prepare $(KUBECTL) $(HELM) $(HELM3) $(HELM_HOME) $(GOMPLATE) kind.setcontext
local.deploy.%: local.prepare $(KUBECTL) $(HELM) $(HELM3) $(HELM_HOME) $(GOMPLATE) $(ISTIO) kind.setcontext
@$(INFO) localdev deploy component: $*
@$(eval PLATFORMS=$(BUILD_PLATFORMS))
@$(SCRIPTS_DIR)/localdev-deploy-component.sh $* || $(FAIL)
Expand Down
50 changes: 35 additions & 15 deletions scripts/localdev-deploy-component.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,30 @@ source "${SCRIPTS_DIR}/load-configs.sh" "${COMPONENT}"

# Skip deployment of this component if COMPONENT_SKIP_DEPLOY is set to true
if [ "${COMPONENT_SKIP_DEPLOY}" == "true" ]; then
echo "COMPONENT_SKIP_DEPLOY set to true, skipping deployment of ${COMPONENT}"
echo_info "COMPONENT_SKIP_DEPLOY set to true, skipping deployment of ${COMPONENT}"
exit 0
fi

DEPLOY_SCRIPT="${DEPLOY_LOCAL_CONFIG_DIR}/${COMPONENT}/deploy.sh"

# Run deploy script, if exists.
# If there is a deploy.sh script, which indicates this is a "script-only" component, only it will be run for this
# component and no helm deployments will be made.
if [ -f "${DEPLOY_SCRIPT}" ]; then
echo_info "Loading required images..."
# shellcheck disable=SC2068
for i in ${REQUIRED_IMAGES[@]+"${REQUIRED_IMAGES[@]}"}; do
pullAndLoadImage "${i}"
done
echo_info "Loading required images...OK"

echo_info "Running deploy script..."
source "${DEPLOY_SCRIPT}"
echo_info "Running deploy script...OK"
exit 0
fi


if [ "${USE_HELM3}" == "true" ]; then
HELM="${HELM3}"
XDG_DATA_HOME="${HELM_HOME}"
Expand All @@ -36,7 +56,7 @@ charts_arr=($BUILD_HELM_CHARTS_LIST)

if [ "${LOCAL_BUILD}" == "true" ] && containsElement "${HELM_CHART_NAME}" ${charts_arr[@]+"${charts_arr[@]}"}; then
# If local build is set and helm chart is from this repository, use locally build helm chart tgz file.
echo "Deploying locally built artifacts..."
echo_info "Deploying locally built artifacts..."
HELM_CHART_VERSION=${BUILD_HELM_CHART_VERSION}
HELM_CHART_REF="${HELM_OUTPUT_DIR}/${COMPONENT}-${HELM_CHART_VERSION}.tgz"
[ -f "${HELM_CHART_REF}" ] || echo_error "Local chart ${HELM_CHART_REF} not found. Did you run \"make build\" ? "
Expand All @@ -46,15 +66,15 @@ if [ "${LOCAL_BUILD}" == "true" ] && containsElement "${HELM_CHART_NAME}" ${char
for i in "${images_arr[@]}"; do
for a in "${image_archs_arr[@]}"; do
if containsElement "${r}/${i}" "${REQUIRED_IMAGES[@]}"; then
echo "Tagging locally built image as ${r}/${i}:${VERSION}"
echo_info "Tagging locally built image as ${r}/${i}:${VERSION}"
docker tag "${BUILD_REGISTRY}/${i}-${a}" "${r}/${i}:${VERSION}"
fi
done
done
done
else
# If local build is NOT set or helm chart is NOT from this repository, deploy chart from a remote repository.
echo "Deploying latest artifacts in chart repo \"${HELM_REPOSITORY_NAME}\"..."
echo_info "Deploying artifacts in chart repo \"${HELM_REPOSITORY_NAME}\"..."
if [ -z ${HELM_REPOSITORY_NAME} ] || [ -z ${HELM_CHART_NAME} ]; then
echo_error "HELM_REPOSITORY_NAME and/or HELM_CHART_NAME is not set for component ${COMPONENT}!"
fi
Expand All @@ -67,13 +87,14 @@ else
if [ -z "${HELM_CHART_VERSION}" ]; then
# if no HELM_CHART_VERSION provided, then get the latest version from repo which will be used to load required images for chart.
HELM_CHART_VERSION=$("${HELM}" search -l ${HELM_CHART_REF} --devel |awk 'NR==2{print $2}')
echo "Latest version found in repo: ${HELM_CHART_VERSION}"
echo_info "Latest version found in repo: ${HELM_CHART_VERSION}"
fi
if [ -z "${HELM_CHART_VERSION}" ]; then
echo_error "No version found in repo for chart ${HELM_CHART_REF}"
fi
fi

echo_info "Loading required images..."
# shellcheck disable=SC2068
for i in ${REQUIRED_IMAGES[@]+"${REQUIRED_IMAGES[@]}"}; do
# check if image has a tag, if not, append tag for the chart
Expand All @@ -83,12 +104,9 @@ for i in ${REQUIRED_IMAGES[@]+"${REQUIRED_IMAGES[@]}"}; do
# Pull the image:
# - if has a tag "master" or "latest"
# - or does not exist already.
if echo "${i}" | grep ":master\s*$" >/dev/null || echo "${i}" | grep ":latest\s*$" >/dev/null || ! docker inspect --type=image "${i}" >/dev/null 2>&1; then
docker pull "${i}"
fi
"${KIND}" load docker-image "${i}" --name="${KIND_CLUSTER_NAME}"
pullAndLoadImage "${i}"
done

echo_info "Loading required images...OK"

PREDEPLOY_SCRIPT="${DEPLOY_LOCAL_CONFIG_DIR}/${COMPONENT}/pre-deploy.sh"
POSTDEPLOY_SCRIPT="${DEPLOY_LOCAL_CONFIG_DIR}/${COMPONENT}/post-deploy.sh"
Expand All @@ -97,13 +115,13 @@ POSTDEPLOY_SCRIPT="${DEPLOY_LOCAL_CONFIG_DIR}/${COMPONENT}/post-deploy.sh"
test -f "${DEPLOY_LOCAL_CONFIG_DIR}/config.validate.sh" && source "${DEPLOY_LOCAL_CONFIG_DIR}/config.validate.sh"

# Create the HELM_RELEASE_NAMESPACE if not exist already.
"${KUBECTL}" --kubeconfig "${KUBECONFIG}" get ns "${HELM_RELEASE_NAMESPACE}" >/dev/null 2>&1 || ${KUBECTL} \
--kubeconfig "${KUBECONFIG}" create ns "${HELM_RELEASE_NAMESPACE}"
createNamespace "${HELM_RELEASE_NAMESPACE}"

# Run pre-deploy script, if exists.
if [ -f "${PREDEPLOY_SCRIPT}" ]; then
echo "Running pre-deploy script..."
echo_info "Running pre-deploy script..."
source "${PREDEPLOY_SCRIPT}"
echo_info "Running pre-deploy script...OK"
fi

# With all configuration sourced as environment variables, render value-overrides.yaml file with gomplate.
Expand All @@ -125,16 +143,18 @@ if [ -z "${HELM_RELEASE_NAME}" ]; then
HELM_RELEASE_NAME=${COMPONENT}
fi

# Run helm upgrade --install with computed parameters.
echo_info "Running helm upgrade --install with computed parameters..."
# shellcheck disable=SC2086
set -x
"${HELM}" upgrade --install "${HELM_RELEASE_NAME}" --namespace "${HELM_RELEASE_NAMESPACE}" --kubeconfig "${KUBECONFIG}" \
"${HELM_CHART_REF}" ${helm_chart_version_flag:-} -f "${DEPLOY_LOCAL_CONFIG_DIR}/${COMPONENT}/value-overrides.yaml" \
${helm_wait_atomic_flag:-}
{ set +x; } 2>/dev/null
echo_info "Running helm upgrade --install with computed parameters...OK"

# Run post-deploy script, if exists.
if [ -f "${POSTDEPLOY_SCRIPT}" ]; then
echo "Running post-deploy script..."
echo_info "Running post-deploy script..."
source "${POSTDEPLOY_SCRIPT}"
echo_info "Running post-deploy script...OK"
fi
4 changes: 2 additions & 2 deletions scripts/localdev-prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if [ -n "${LOCAL_DEV_REPOS}" ]; then
if [ "${LOCAL_BUILD}" == "true" ] && [ "${repo}" == "${local_repo}" ]; then
# if it is a local build and repo is the local one, just use local config

echo "Using local config for repo \"${repo}\""
echo_info "Using local config for repo \"${repo}\""
repo_dir="${ROOT_DIR}"
else
# otherwise, shallow clone the repo
Expand All @@ -33,7 +33,7 @@ if [ -n "${LOCAL_DEV_REPOS}" ]; then
repo_url="https://github.com/${i}.git"
fi
# only clone if not cloned already.
test -d "${repo_dir}" || { echo "Cloning repo ${repo} to get local dev config"; git clone --depth 1 "${repo_url}" "${repo_dir}"; }
test -d "${repo_dir}" || { echo_info "Cloning repo ${repo} to get local dev config..."; git clone --depth 1 "${repo_url}" "${repo_dir}"; echo_info "Cloning repo ${repo} to get local dev config...OK"; }
fi

# copy local dev config under workdir
Expand Down
14 changes: 13 additions & 1 deletion scripts/localdev-remove-component.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ COMPONENT=$1
source "${SCRIPTS_DIR}/utils.sh"
source "${SCRIPTS_DIR}/load-configs.sh" "${COMPONENT}"

DEPLOY_SCRIPT="${DEPLOY_LOCAL_CONFIG_DIR}/${COMPONENT}/deploy.sh"

# Run deploy script, if exists.
# If there is a deploy.sh script, which indicates this is a "script-only" component, removing not supported.
if [ -f "${DEPLOY_SCRIPT}" ]; then
echo_warn "${COMPONENT} is a \"script-only\" component, local.remove not supported!"
exit 0
fi


if [ -z "${HELM_RELEASE_NAME}" ]; then
HELM_RELEASE_NAME=${COMPONENT}
fi
Expand All @@ -19,6 +29,8 @@ if [ "${USE_HELM3}" == "true" ]; then
helm_purge_flag=""
fi

echo_info "Running helm delete..."
set -x
"${HELM}" delete "${HELM_RELEASE_NAME}" --kubeconfig "${KUBECONFIG}" ${helm_purge_flag}
{ set +x; } 2>/dev/null
{ set +x; } 2>/dev/null
echo_info "Running helm delete...OK!"
28 changes: 25 additions & 3 deletions scripts/utils.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
################################# setup colors
# setting up colors
BLU='\033[0;34m'
BOLD='\033[10;1m'
YLW='\033[0;33m'
GRN='\033[0;32m'
RED='\033[0;31m'
NOC='\033[0m' # No Color

echo_info(){
printf "${BLU}%s${NOC}\n" "$1"
printf "${BOLD}%s${NOC}\n" "$1"
}

echo_success(){
printf "\n${GRN}%s${NOC}\n" "$1"
printf "${GRN}%s${NOC}\n" "$1"
}

echo_warn(){
printf "${YLW}%s${NOC}\n" "$1"
}

echo_error(){
printf "\n${RED}%s${NOC}\n" "$1"
return 1
Expand All @@ -27,4 +29,24 @@ containsElement () {
shift
for e; do [[ "$e" == "$match" ]] && return 0; done
return 1
}

pullAndLoadImage () {
i=$1
# Pull the image:
# - if has a tag "master" or "latest"
# - or does not exist already.
if echo "${i}" | grep ":master\s*$" >/dev/null || echo "${i}" | grep ":latest\s*$" >/dev/null || \
! docker inspect --type=image "${i}" >/dev/null 2>&1; then
docker pull "${i}"
fi
"${KIND}" load docker-image "${i}" --name="${KIND_CLUSTER_NAME}"
return 0
}

createNamespace () {
n=$1
# Create namespace if not exists
"${KUBECTL}" --kubeconfig "${KUBECONFIG}" get ns "${n}" >/dev/null 2>&1 || \
${KUBECTL} --kubeconfig "${KUBECONFIG}" create ns "${n}"
}

0 comments on commit 8da3758

Please sign in to comment.