Here is my unedited bash solution for this.
If there is any interest, I can rewrite it to make it more portable. I will post comments in a script to help you.
#!/bin/bash
kubectl get ingress laravel-ingress -n=gitlab-ci -o json > original_json
existing_hosts=()
exisiting_host_names=()
API_HOST=$1
DASHBOARD_HOST=$2
NAME=$3
name="$NAME"
potential_new_hosts=(
"$API_HOST"
"$DASHBOARD_HOST"
)
new_hosts=()
while read -r value
do
existing_host_names+=("$value")
done < <(cat original_json | jq ['.spec.rules[] | .host'])
for potential_new_host in "${potential_new_hosts[@]}"; do
i=0
for existing_host in "${existing_host_names[@]}"; do
if [[ "$existing_host" == "\"$potential_new_host\"," ]] ; then
i=1
fi
done
if [[ "$i" == 0 ]] ; then
new_hosts+=("$potential_new_host")
fi
done
while read -r value2
do
existing_hosts+=("$value2")
done < <(cat original_json | jq ['.spec.rules[] | .'])
output_json=""
for existing_host in "${existing_hosts[@]}"; do
output_json=("$output_json$existing_host")
done
output_json=${output_json::-1}
i=0
for new_host in "${new_hosts[@]}"; do
output_json=("$output_json,{\"host\":\"$new_host\",\"http\":{ \"paths\": [{ \"backend\":{ \"serviceName\":\"$name-laravel-web\",\"servicePort\":80} } ]} }")
i=1
done
printf '%s]\n' "$output_json" > new_json
if [[ "$i" == 1 ]] ; then
echo "Ingress json has changed and should be updated."
echo "OLD:"
cat original_json
echo "PATCH: \"spec\": {\"rules\": $output_json]}"
kubectl patch ingress laravel-ingress -n=gitlab-ci -p="\"spec\": {\"rules\": $output_json]}"
else
echo "Ingress json has not changed and will not be updated."
fi
Edit (fixed bugs and converted to a single host when running the script)
#!/bin/bash
NEW_HOST=$1
INGRESS_NAME=$2
SERVICE_NAME=$3
echo "Running update-ingress for New host: $NEW_HOST, ingress: $INGRESS_NAME, service: $SERVICE_NAME"
echo " Downloading existing ingress config"
kubectl get ingress "$INGRESS_NAME" -n=gitlab-ci -o json > original_json
existing_hosts=()
exisiting_host_names=()
echo " $ kubectl get ingress $INGRESS_NAME -n=gitlab-ci -o json > original json && cat original_json"
cat original_json
name="$SERVICE_NAME"
potential_new_host="$NEW_HOST"
new_hosts=()
echo "Looping through json values1"
cat original_json | jq ['.spec.rules[] | .host']
for value in $(cat original_json | jq ['.spec.rules[] | .host']);
do
value=${value/,/""}
existing_host_names+=("$value")
echo "Looping v1: value:$value"
done
echo "Right after values1"
i=0
for existing_host in "${existing_host_names[@]}"; do
echo "Looping through existing host: $existing_host == $potential_new_host"
if [[ "$existing_host" == "\"$potential_new_host\"" ]] ; then
i=1
fi
done
if [[ "$i" == 0 ]] ; then
new_hosts+=("$potential_new_host")
fi
echo "Looping through json values2"
for value2 in $(cat original_json | jq ['.spec.rules[] | .']);
do
existing_hosts+=("$value2")
done
echo "Original json"
echo "Existing host names"
printf '%s\n' "${existing_host_names[@]}"
echo "New hosts"
printf '%s\n' "${new_hosts[@]}"
echo "Existing hosts before adding"
printf '%s\n' "${existing_hosts[@]}"
echo "Hosts after adding new ones"
output_json=""
for existing_host in "${existing_hosts[@]}"; do
output_json=("$output_json$existing_host")
done
output_json=${output_json::-1}
i=0
for new_host in "${new_hosts[@]}"; do
output_json=("$output_json,{\"host\":\"$new_host\",\"http\":{ \"paths\": [{ \"backend\":{ \"serviceName\":\"$SERVICE_NAME-laravel-web\",\"servicePort\":80} }, { \"backend\":{ \"serviceName\":\"$SERVICE_NAME-laravel-web\",\"servicePort\":443} } ]} }")
i=1
done
printf '%s]\n' "$output_json" > new_json
if [[ "$i" == 1 ]] ; then
echo "Ingress json has changed and should be updated."
echo "OLD:"
cat original_json
echo "PATCH: \"spec\": {\"rules\": $output_json]}"
kubectl patch ingress "$INGRESS_NAME" -n=gitlab-ci -p="\"spec\": {\"rules\": $output_json]}"
else
echo "Ingress json has not changed and will not be updated."
fi