-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathadd-namespace-to-env.sh
executable file
·52 lines (43 loc) · 1.41 KB
/
add-namespace-to-env.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env bash
set -ex
NAMESPACE="$1"
ENVIRONMENT="$2"
APPS_DIR="../../apps/"
CLUSTERS_DIR="../../clusters"
cd "$(dirname "$0")"
function usage() {
echo 'usage: ./add-namespace-to-env.sh <namespace> <env>'
}
if [ -z "${NAMESPACE}" ] || [ -z "${ENVIRONMENT}" ]
then
usage
exit 1
fi
BASE_PATH=../../base
if [ "${ENVIRONMENT}" == "preview" ]
then
BASE_PATH=../../../base
fi
if [ ! -d "${APPS_DIR}/${NAMESPACE}/${ENVIRONMENT}" ]; then
echo "Creating ${ENVIRONMENT} for ${NAMESPACE}"
mkdir ${APPS_DIR}/${NAMESPACE}/${ENVIRONMENT}/
mkdir ${APPS_DIR}/$NAMESPACE/${ENVIRONMENT}/base
(
cat <<EOF
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- $BASE_PATH
namespace: $NAMESPACE
EOF
) > "${APPS_DIR}/${NAMESPACE}/${ENVIRONMENT}/base/kustomization.yaml"
fi
if [[ "${ENVIRONMENT}" != "preview" ]] && [[ "${ENVIRONMENT}" != "prod" ]]; then
yq eval -i '.resources += "../../../rbac/nonprod-role.yaml"' ${APPS_DIR}/${NAMESPACE}/${ENVIRONMENT}/base/kustomization.yaml
fi
export NAMESPACE_PATH="../../../apps/$NAMESPACE/base/kustomize.yaml"
if [[ $(yq eval '.resources[] | ( . == env(NAMESPACE_PATH))' ${CLUSTERS_DIR}/${ENVIRONMENT}/base/kustomization.yaml) =~ "true" ]]; then
echo "Reference to ${CLUSTERS_DIR}/${ENVIRONMENT}/base/kustomization.yaml already exists ignoring.."
else
yq eval -i '.resources += [env(NAMESPACE_PATH)]' ${CLUSTERS_DIR}/${ENVIRONMENT}/base/kustomization.yaml
fi