|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2025 The Kubernetes Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +set -euo pipefail |
| 18 | + |
| 19 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 20 | +CONFIGMAP_FILE="${SCRIPT_DIR}/../apps/k8s-io/configmap-nginx.yaml" |
| 21 | +TEMP_DIR=$(mktemp -d) |
| 22 | +NGINX_CONF="${TEMP_DIR}/nginx.conf" |
| 23 | + |
| 24 | +cleanup() { |
| 25 | + rm -rf "${TEMP_DIR}" |
| 26 | +} |
| 27 | +trap cleanup EXIT |
| 28 | + |
| 29 | +echo "Extracting nginx.conf from ConfigMap..." |
| 30 | + |
| 31 | +# Extract the nginx.conf section from the ConfigMap YAML |
| 32 | +awk ' |
| 33 | +/^ nginx\.conf: \|/ { |
| 34 | + # Found the start, skip this line and start collecting |
| 35 | + found = 1 |
| 36 | + next |
| 37 | +} |
| 38 | +found && /^ / { |
| 39 | + # Line starts with 4 spaces (indented content), remove the leading 4 spaces and print |
| 40 | + print substr($0, 5) |
| 41 | + next |
| 42 | +} |
| 43 | +found && !/^ / && !/^[[:space:]]*$/ { |
| 44 | + # Hit a non-indented line that is not empty, stop collecting |
| 45 | + exit |
| 46 | +} |
| 47 | +' "${CONFIGMAP_FILE}" > "${NGINX_CONF}" |
| 48 | + |
| 49 | +echo "Extracted nginx.conf to: ${NGINX_CONF}" |
| 50 | +echo "Config file size: $(wc -l < "${NGINX_CONF}") lines" |
| 51 | + |
| 52 | +echo "Validating nginx configuration..." |
| 53 | + |
| 54 | +# Check if we can use Docker for validation |
| 55 | +if command -v docker >/dev/null 2>&1; then |
| 56 | + # Use the same nginx version as specified in the deployment |
| 57 | + NGINX_IMAGE="nginx:1.26-alpine@sha256:5b44a5ab8ab467854f2bf7b835a32f850f32eb414b749fbf7ed506b139cd8d6b" |
| 58 | + docker run --rm \ |
| 59 | + -v "${TEMP_DIR}:/etc/nginx:ro" \ |
| 60 | + "${NGINX_IMAGE}" \ |
| 61 | + nginx -c /etc/nginx/nginx.conf -t |
| 62 | +else |
| 63 | + # Check if nginx is already installed |
| 64 | + if ! command -v nginx >/dev/null 2>&1; then |
| 65 | + if apt-get update -qq && apt-get install -y -qq nginx; then |
| 66 | + echo "Installed nginx via apt" |
| 67 | + fi |
| 68 | + fi |
| 69 | + nginx -c "${NGINX_CONF}" -t |
| 70 | +fi |
| 71 | + |
| 72 | +echo "✅ Nginx configuration validation completed successfully!" |
0 commit comments